From: Pierre-Emmanuel Patry <[email protected]>
Lang item segment name were made optional because we thought we wouldn't
need them once identified. But the lang item name are not fixed, they're
declared within the core crate. This means we couldn't accurately dump
the AST anymore.
gcc/rust/ChangeLog:
* ast/rust-ast-builder.cc (Builder::type_path_segment): Add segment
name argument.
(Builder::type_path_segment_generic): Likewise.
(Builder::single_generic_type_path): Likewise.
(Builder::type_path): Likewise.
* ast/rust-ast-builder.h: Add segment name string to ctor.
* ast/rust-path.h: Make segment name mandatory.
* expand/rust-derive-clone.cc (DeriveClone::visit_union): Forward
segment name.
* expand/rust-derive-copy.cc: Likewise.
* expand/rust-derive-eq.cc (DeriveEq::assert_param_is_eq): Likewise.
* expand/rust-derive-partial-eq.cc: Likewise.
Signed-off-by: Pierre-Emmanuel Patry <[email protected]>
---
This change was merged into the gccrs repository and is posted here for
upstream visibility and potential drive-by review, as requested by GCC
release managers.
Each commit email contains a link to its details on github from where you can
find the Pull-Request and associated discussions.
Commit on github:
https://github.com/Rust-GCC/gccrs/commit/b765e3e0343050d8299bf8a9f3d4981432d50adf
The commit has NOT been mentioned in any issue.
The commit has been mentioned in the following pull-request(s):
- https://github.com/Rust-GCC/gccrs/pull/4588
gcc/rust/ast/rust-ast-builder.cc | 17 ++++++-----
gcc/rust/ast/rust-ast-builder.h | 10 ++++---
gcc/rust/ast/rust-path.h | 36 ++++++++---------------
gcc/rust/expand/rust-derive-clone.cc | 14 +++++----
gcc/rust/expand/rust-derive-copy.cc | 5 ++--
gcc/rust/expand/rust-derive-eq.cc | 9 ++++--
gcc/rust/expand/rust-derive-partial-eq.cc | 5 ++--
7 files changed, 49 insertions(+), 47 deletions(-)
diff --git a/gcc/rust/ast/rust-ast-builder.cc b/gcc/rust/ast/rust-ast-builder.cc
index bce5bb85a..9802f2a56 100644
--- a/gcc/rust/ast/rust-ast-builder.cc
+++ b/gcc/rust/ast/rust-ast-builder.cc
@@ -182,10 +182,10 @@ Builder::type_path_segment (std::string seg) const
}
std::unique_ptr<TypePathSegment>
-Builder::type_path_segment (LangItem::Kind lang_item) const
+Builder::type_path_segment (LangItem::Kind lang_item, std::string &name) const
{
return std::unique_ptr<TypePathSegment> (
- new TypePathSegment (lang_item, loc));
+ new TypePathSegment (lang_item, PathIdentSegment (name, loc), loc));
}
std::unique_ptr<TypePathSegment>
@@ -196,11 +196,12 @@ Builder::type_path_segment_generic (std::string seg,
GenericArgs args) const
}
std::unique_ptr<TypePathSegment>
-Builder::type_path_segment_generic (LangItem::Kind lang_item,
+Builder::type_path_segment_generic (LangItem::Kind lang_item, std::string
&name,
GenericArgs args) const
{
return std::unique_ptr<TypePathSegment> (
- new TypePathSegmentGeneric (lang_item, args, loc));
+ new TypePathSegmentGeneric (lang_item, PathIdentSegment (name, loc), args,
+ loc));
}
std::unique_ptr<Type>
@@ -228,11 +229,11 @@ Builder::single_generic_type_path (std::string type,
GenericArgs args) const
}
std::unique_ptr<Type>
-Builder::single_generic_type_path (LangItem::Kind lang_item,
+Builder::single_generic_type_path (LangItem::Kind lang_item, std::string &name,
GenericArgs args) const
{
auto segments = std::vector<std::unique_ptr<TypePathSegment>> ();
- segments.emplace_back (type_path_segment_generic (lang_item, args));
+ segments.emplace_back (type_path_segment_generic (lang_item, name, args));
return std::unique_ptr<Type> (new TypePath (std::move (segments), loc));
}
@@ -272,9 +273,9 @@ Builder::type_path (std::string type) const
}
TypePath
-Builder::type_path (LangItem::Kind lang_item) const
+Builder::type_path (LangItem::Kind lang_item, std::string &name) const
{
- return type_path (type_path_segment (lang_item));
+ return type_path (type_path_segment (lang_item, name));
}
std::unique_ptr<Type>
diff --git a/gcc/rust/ast/rust-ast-builder.h b/gcc/rust/ast/rust-ast-builder.h
index 2b34c2ab1..143616816 100644
--- a/gcc/rust/ast/rust-ast-builder.h
+++ b/gcc/rust/ast/rust-ast-builder.h
@@ -198,13 +198,14 @@ public:
/* And similarly for type path segments */
std::unique_ptr<TypePathSegment> type_path_segment (std::string seg) const;
- std::unique_ptr<TypePathSegment>
- type_path_segment (LangItem::Kind lang_item) const;
+ std::unique_ptr<TypePathSegment> type_path_segment (LangItem::Kind lang_item,
+ std::string &name) const;
std::unique_ptr<TypePathSegment>
type_path_segment_generic (std::string seg, GenericArgs args) const;
std::unique_ptr<TypePathSegment>
- type_path_segment_generic (LangItem::Kind lang_item, GenericArgs args) const;
+ type_path_segment_generic (LangItem::Kind lang_item, std::string &name,
+ GenericArgs args) const;
/* Create a Type from a single string - the most basic kind of type in our
AST
*/
@@ -214,6 +215,7 @@ public:
std::unique_ptr<Type> single_generic_type_path (std::string type,
GenericArgs args) const;
std::unique_ptr<Type> single_generic_type_path (LangItem::Kind lang_item,
+ std::string &name,
GenericArgs args) const;
TypePath type_path (std::vector<std::unique_ptr<TypePathSegment>> &&segment,
@@ -222,7 +224,7 @@ public:
bool opening_scope = false) const;
TypePath type_path (std::unique_ptr<TypePathSegment> &&segment) const;
TypePath type_path (std::string type) const;
- TypePath type_path (LangItem::Kind lang_item) const;
+ TypePath type_path (LangItem::Kind lang_item, std::string &name) const;
std::unique_ptr<Type>
reference_type (std::unique_ptr<TypeNoBounds> &&inner_type,
diff --git a/gcc/rust/ast/rust-path.h b/gcc/rust/ast/rust-path.h
index 09baba1d8..3ce7ed7ef 100644
--- a/gcc/rust/ast/rust-path.h
+++ b/gcc/rust/ast/rust-path.h
@@ -834,7 +834,7 @@ public:
private:
tl::optional<LangItem::Kind> lang_item;
- tl::optional<PathIdentSegment> ident_segment;
+ PathIdentSegment ident_segment;
location_t locus;
protected:
@@ -880,8 +880,9 @@ public:
node_id (Analysis::Mappings::get ().get_next_node_id ())
{}
- TypePathSegment (LangItem::Kind lang_item, location_t locus)
- : lang_item (lang_item), ident_segment (tl::nullopt), locus (locus),
+ TypePathSegment (LangItem::Kind lang_item, PathIdentSegment ident_segment,
+ location_t locus)
+ : lang_item (lang_item), ident_segment (ident_segment), locus (locus),
has_separating_scope_resolution (false),
node_id (Analysis::Mappings::get ().get_next_node_id ())
{}
@@ -897,7 +898,7 @@ public:
// General constructor
TypePathSegment (tl::optional<LangItem::Kind> lang_item,
- tl::optional<PathIdentSegment> ident_segment,
+ PathIdentSegment ident_segment,
bool has_separating_scope_resolution, location_t locus)
: lang_item (lang_item), ident_segment (ident_segment), locus (locus),
has_separating_scope_resolution (has_separating_scope_resolution),
@@ -930,16 +931,12 @@ public:
if (lang_item.has_value ())
return LangItem::PrettyString (*lang_item);
- return ident_segment->as_string ();
+ return ident_segment.as_string ();
}
/* Returns whether the type path segment is in an error state. May be
* virtual in future. */
- bool is_error () const
- {
- rust_assert (ident_segment);
- return ident_segment->is_error ();
- }
+ bool is_error () const { return ident_segment.is_error (); }
/* Returns whether segment is identifier only (as opposed to generic args or
* function). Overridden in derived classes with other segments. */
@@ -957,17 +954,9 @@ public:
return has_separating_scope_resolution;
}
- PathIdentSegment &get_ident_segment ()
- {
- rust_assert (!is_lang_item ());
- return *ident_segment;
- };
+ PathIdentSegment &get_ident_segment () { return ident_segment; };
- const PathIdentSegment &get_ident_segment () const
- {
- rust_assert (!is_lang_item ());
- return *ident_segment;
- };
+ const PathIdentSegment &get_ident_segment () const { return ident_segment; };
LangItem::Kind get_lang_item () const
{
@@ -1016,9 +1005,10 @@ public:
generic_args (std::move (generic_args))
{}
- TypePathSegmentGeneric (LangItem::Kind lang_item, GenericArgs generic_args,
- location_t locus)
- : TypePathSegment (lang_item, locus),
+ TypePathSegmentGeneric (LangItem::Kind lang_item,
+ PathIdentSegment ident_segment,
+ GenericArgs generic_args, location_t locus)
+ : TypePathSegment (lang_item, ident_segment, locus),
generic_args (std::move (generic_args))
{}
diff --git a/gcc/rust/expand/rust-derive-clone.cc
b/gcc/rust/expand/rust-derive-clone.cc
index d83743786..bef5fdae4 100644
--- a/gcc/rust/expand/rust-derive-clone.cc
+++ b/gcc/rust/expand/rust-derive-clone.cc
@@ -86,8 +86,9 @@ DeriveClone::clone_impl (
std::unique_ptr<AssociatedItem> &&clone_fn, std::string name,
const std::vector<std::unique_ptr<GenericParam>> &type_generics)
{
- auto clone_trait_path
- = [this] () { return builder.type_path (LangItem::Kind::CLONE); };
+ auto clone_trait_path = [this, &name] () {
+ return builder.type_path (LangItem::Kind::CLONE, name);
+ };
auto trait_items = vec (std::move (clone_fn));
@@ -349,8 +350,10 @@ DeriveClone::visit_union (Union &item)
// FIXME: Should be $crate::core::clone::AssertParamIsCopy (or similar)
// (Rust-GCC#3329)
- auto copy_path = builder.type_path (LangItem::Kind::COPY);
- auto sized_path = builder.type_path (LangItem::Kind::SIZED);
+ std::string copy_name = "Copy";
+ std::string sized_name = "Sized";
+ auto copy_path = builder.type_path (LangItem::Kind::COPY, copy_name);
+ auto sized_path = builder.type_path (LangItem::Kind::SIZED, sized_name);
auto copy_bound = std::unique_ptr<TypeParamBound> (
new TraitBound (copy_path, item.get_locus ()));
@@ -364,12 +367,13 @@ DeriveClone::visit_union (Union &item)
auto assert_param_is_copy = "AssertParamIsCopy";
auto t = std::unique_ptr<GenericParam> (
new TypeParam (Identifier ("T"), item.get_locus (), std::move (bounds)));
+ std::string phantom_data_name = "PhantomData";
auto assert_param_is_copy_struct = builder.struct_struct (
assert_param_is_copy, vec (std::move (t)),
{StructField (
Identifier ("_t"),
builder.single_generic_type_path (
- LangItem::Kind::PHANTOM_DATA,
+ LangItem::Kind::PHANTOM_DATA, phantom_data_name,
GenericArgs (
{}, {GenericArg::create_type (builder.single_type_path ("T"))}, {})),
Visibility::create_private (), item.get_locus ())});
diff --git a/gcc/rust/expand/rust-derive-copy.cc
b/gcc/rust/expand/rust-derive-copy.cc
index 305100768..330ec2d7f 100644
--- a/gcc/rust/expand/rust-derive-copy.cc
+++ b/gcc/rust/expand/rust-derive-copy.cc
@@ -42,8 +42,9 @@ DeriveCopy::copy_impl (
std::string name,
const std::vector<std::unique_ptr<GenericParam>> &type_generics)
{
- auto copy_trait_path
- = [this] () { return builder.type_path (LangItem::Kind::COPY); };
+ auto copy_trait_path = [this, &name] () {
+ return builder.type_path (LangItem::Kind::COPY, name);
+ };
auto generics = setup_impl_generics (name, type_generics, [&, this] () {
return builder.trait_bound (copy_trait_path ());
diff --git a/gcc/rust/expand/rust-derive-eq.cc
b/gcc/rust/expand/rust-derive-eq.cc
index c388c379e..2ace157a6 100644
--- a/gcc/rust/expand/rust-derive-eq.cc
+++ b/gcc/rust/expand/rust-derive-eq.cc
@@ -69,8 +69,9 @@ DeriveEq::assert_param_is_eq ()
auto eq_bound = std::unique_ptr<TypeParamBound> (
new TraitBound (get_eq_trait_path (builder), loc));
+ auto name = std::string{"Sized"};
auto sized_bound = std::unique_ptr<TypeParamBound> (
- new TraitBound (builder.type_path (LangItem::Kind::SIZED), loc, false,
+ new TraitBound (builder.type_path (LangItem::Kind::SIZED, name), loc,
false,
true /* opening_question_mark */));
auto bounds = vec (std::move (eq_bound), std::move (sized_bound));
@@ -80,12 +81,14 @@ DeriveEq::assert_param_is_eq ()
auto t = std::unique_ptr<GenericParam> (
new TypeParam (Identifier ("T"), loc, std::move (bounds)));
+ std::string phantom_data = "PhantomData";
+
return builder.struct_struct (
assert_param_is_eq, vec (std::move (t)),
{StructField (
Identifier ("_t"),
builder.single_generic_type_path (
- LangItem::Kind::PHANTOM_DATA,
+ LangItem::Kind::PHANTOM_DATA, phantom_data,
GenericArgs (
{}, {GenericArg::create_type (builder.single_type_path ("T"))}, {})),
Visibility::create_private (), loc)});
@@ -121,7 +124,7 @@ DeriveEq::eq_impls (
auto eq = [this] () { return get_eq_trait_path (builder); };
auto eq_bound = [&, this] () { return builder.trait_bound (eq ()); };
- auto steq = builder.type_path (LangItem::Kind::STRUCTURAL_TEQ);
+ auto steq = builder.type_path (LangItem::Kind::STRUCTURAL_TEQ, name);
auto trait_items = vec (std::move (fn));
diff --git a/gcc/rust/expand/rust-derive-partial-eq.cc
b/gcc/rust/expand/rust-derive-partial-eq.cc
index 03879fad9..10212c2ab 100644
--- a/gcc/rust/expand/rust-derive-partial-eq.cc
+++ b/gcc/rust/expand/rust-derive-partial-eq.cc
@@ -42,8 +42,9 @@ DerivePartialEq::partialeq_impls (
std::unique_ptr<AssociatedItem> &&eq_fn, std::string name,
const std::vector<std::unique_ptr<GenericParam>> &type_generics)
{
- auto eq = [this] () { return builder.type_path (LangItem::Kind::EQ); };
- auto speq = builder.type_path (LangItem::Kind::STRUCTURAL_PEQ);
+ auto eq
+ = [this, &name] () { return builder.type_path (LangItem::Kind::EQ, name);
};
+ auto speq = builder.type_path (LangItem::Kind::STRUCTURAL_PEQ, name);
auto trait_items = vec (std::move (eq_fn));
--
2.54.0