aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust
diff options
context:
space:
mode:
authorOwen Avery <powerboat9.gamer@gmail.com>2024-11-04 15:05:03 -0500
committerPhilip Herron <philip.herron@embecosm.com>2024-11-06 11:13:22 +0000
commit335f6c2b0918c27eecd93a114668841b74ee06c3 (patch)
tree5dce66527422bcd2d7eaf2e732e3e02881b8ac6d /gcc/rust
parente82d79c4f39b62550769525b9dec4ce446c2c96f (diff)
downloadgcc-335f6c2b0918c27eecd93a114668841b74ee06c3.zip
gcc-335f6c2b0918c27eecd93a114668841b74ee06c3.tar.gz
gcc-335f6c2b0918c27eecd93a114668841b74ee06c3.tar.bz2
Improve handling of implicit Self parameter in AST
gcc/rust/ChangeLog: * ast/rust-item.h (Trait::self_param): Add. (Trait::Trait): Initialize self_param. (Trait::operator=): Copy self_param. (Trait::insert_implicit_self): Remove. (Trait::get_implicit_self): Add. * hir/rust-ast-lower-item.cc (ASTLoweringItem::visit): Make sure implicit self is still lowered to HIR. * resolve/rust-ast-resolve-item.cc (ResolveItem::visit): Adjust handling of implicit self. * resolve/rust-early-name-resolver.cc (EarlyNameResolver::visit): Add commit to Trait visitor mentioning that implicit self is not visited. * resolve/rust-toplevel-name-resolver-2.0.cc (TopLevel::visit): Remove call to Trait::insert_implicit_self. gcc/testsuite/ChangeLog: * rust/compile/nr2/exclude: Remove entries. * rust/link/generic_function_0.rs: No longer expect failure. * rust/link/trait_import_0.rs: Likewise. * rust/link/trait_import_1.rs (trait Sized): Add. Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
Diffstat (limited to 'gcc/rust')
-rw-r--r--gcc/rust/ast/rust-item.h23
-rw-r--r--gcc/rust/hir/rust-ast-lower-item.cc6
-rw-r--r--gcc/rust/resolve/rust-ast-resolve-item.cc12
-rw-r--r--gcc/rust/resolve/rust-early-name-resolver.cc2
-rw-r--r--gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc17
5 files changed, 18 insertions, 42 deletions
diff --git a/gcc/rust/ast/rust-item.h b/gcc/rust/ast/rust-item.h
index f6b2913..3969398 100644
--- a/gcc/rust/ast/rust-item.h
+++ b/gcc/rust/ast/rust-item.h
@@ -2831,6 +2831,7 @@ class Trait : public VisItem
bool has_auto;
Identifier name;
std::vector<std::unique_ptr<GenericParam>> generic_params;
+ TypeParam self_param;
std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds;
WhereClause where_clause;
std::vector<Attribute> inner_attrs;
@@ -2870,7 +2871,7 @@ public:
std::vector<Attribute> inner_attrs, location_t locus)
: VisItem (std::move (vis), std::move (outer_attrs)),
has_unsafe (is_unsafe), has_auto (is_auto), name (std::move (name)),
- generic_params (std::move (generic_params)),
+ generic_params (std::move (generic_params)), self_param ({"Self"}, locus),
type_param_bounds (std::move (type_param_bounds)),
where_clause (std::move (where_clause)),
inner_attrs (std::move (inner_attrs)),
@@ -2880,8 +2881,9 @@ public:
// Copy constructor with vector clone
Trait (Trait const &other)
: VisItem (other), has_unsafe (other.has_unsafe), has_auto (other.has_auto),
- name (other.name), where_clause (other.where_clause),
- inner_attrs (other.inner_attrs), locus (other.locus)
+ name (other.name), self_param (other.self_param),
+ where_clause (other.where_clause), inner_attrs (other.inner_attrs),
+ locus (other.locus)
{
generic_params.reserve (other.generic_params.size ());
for (const auto &e : other.generic_params)
@@ -2901,6 +2903,7 @@ public:
{
VisItem::operator= (other);
name = other.name;
+ self_param = other.self_param;
has_unsafe = other.has_unsafe;
has_auto = other.has_auto;
where_clause = other.where_clause;
@@ -2968,19 +2971,7 @@ public:
WhereClause &get_where_clause () { return where_clause; }
- void insert_implict_self (std::unique_ptr<AST::GenericParam> &&param)
- {
- std::vector<std::unique_ptr<GenericParam>> new_list;
- new_list.reserve (generic_params.size () + 1);
-
- new_list.push_back (std::move (param));
- for (auto &p : generic_params)
- {
- new_list.push_back (std::move (p));
- }
-
- generic_params = std::move (new_list);
- }
+ AST::TypeParam &get_implicit_self () { return self_param; }
protected:
/* Use covariance to implement clone function as returning this object
diff --git a/gcc/rust/hir/rust-ast-lower-item.cc b/gcc/rust/hir/rust-ast-lower-item.cc
index cb07b26..2457e91 100644
--- a/gcc/rust/hir/rust-ast-lower-item.cc
+++ b/gcc/rust/hir/rust-ast-lower-item.cc
@@ -572,6 +572,12 @@ ASTLoweringItem::visit (AST::Trait &trait)
generic_params = lower_generic_params (trait.get_generic_params ());
}
+ // TODO: separate "Self" from normal generic parameters
+ // in HIR as well as in AST?
+ HIR::GenericParam *self_param
+ = ASTLowerGenericParam::translate (trait.get_implicit_self ());
+ generic_params.emplace (generic_params.begin (), self_param);
+
std::vector<std::unique_ptr<HIR::TypeParamBound>> type_param_bounds;
if (trait.has_type_param_bounds ())
{
diff --git a/gcc/rust/resolve/rust-ast-resolve-item.cc b/gcc/rust/resolve/rust-ast-resolve-item.cc
index bf47c73..97faeab 100644
--- a/gcc/rust/resolve/rust-ast-resolve-item.cc
+++ b/gcc/rust/resolve/rust-ast-resolve-item.cc
@@ -755,20 +755,14 @@ ResolveItem::visit (AST::Trait &trait)
resolver->push_new_name_rib (resolver->get_name_scope ().peek ());
resolver->push_new_type_rib (resolver->get_type_scope ().peek ());
- // we need to inject an implicit self TypeParam here
- // FIXME: which location should be used for Rust::Identifier `Self`?
- AST::TypeParam *implicit_self
- = new AST::TypeParam ({"Self"}, trait.get_locus ());
- trait.insert_implict_self (
- std::unique_ptr<AST::GenericParam> (implicit_self));
- CanonicalPath Self = CanonicalPath::get_big_self (trait.get_node_id ());
-
+ ResolveGenericParam::go (trait.get_implicit_self (), prefix,
+ canonical_prefix);
for (auto &generic : trait.get_generic_params ())
ResolveGenericParam::go (*generic, prefix, canonical_prefix);
// Self is an implicit TypeParam so lets mark it as such
resolver->get_type_scope ().append_reference_for_def (
- Self.get_node_id (), implicit_self->get_node_id ());
+ trait.get_node_id (), trait.get_implicit_self ().get_node_id ());
if (trait.has_type_param_bounds ())
{
diff --git a/gcc/rust/resolve/rust-early-name-resolver.cc b/gcc/rust/resolve/rust-early-name-resolver.cc
index 14d2041..6345231 100644
--- a/gcc/rust/resolve/rust-early-name-resolver.cc
+++ b/gcc/rust/resolve/rust-early-name-resolver.cc
@@ -353,6 +353,8 @@ EarlyNameResolver::visit (AST::TraitItemType &)
void
EarlyNameResolver::visit (AST::Trait &trait)
{
+ // shouldn't need to visit trait.get_implicit_self ()
+
for (auto &generic : trait.get_generic_params ())
generic->accept_vis (*this);
diff --git a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc
index a5e6c83..badaaed 100644
--- a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc
+++ b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc
@@ -103,23 +103,6 @@ TopLevel::visit (AST::Module &module)
void
TopLevel::visit (AST::Trait &trait)
{
- // FIXME: This Self injection is dodgy. It even lead to issues with metadata
- // export in the past (#2349). We cannot tell appart injected parameters from
- // regular ones. Dumping generic parameters highlights this Self in metadata,
- // during debug or proc macro collection. This is clearly a hack.
- //
- // For now I'll keep it here in the new name resolver even if it should
- // probably not be there. We need to find another way to solve this.
- // Maybe an additional attribute to Trait ?
- //
- // From old resolver:
- //// we need to inject an implicit self TypeParam here
- //// FIXME: which location should be used for Rust::Identifier `Self`?
- AST::TypeParam *implicit_self
- = new AST::TypeParam ({"Self"}, trait.get_locus ());
- trait.insert_implict_self (
- std::unique_ptr<AST::GenericParam> (implicit_self));
-
insert_or_error_out (trait.get_identifier ().as_string (), trait,
Namespace::Types);