Skip to content

Commit 335f6c2

Browse files
powerboat9philberty
authored andcommitted
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 <[email protected]>
1 parent e82d79c commit 335f6c2

9 files changed

+21
-57
lines changed

gcc/rust/ast/rust-item.h

+7-16
Original file line numberDiff line numberDiff line change
@@ -2831,6 +2831,7 @@ class Trait : public VisItem
28312831
bool has_auto;
28322832
Identifier name;
28332833
std::vector<std::unique_ptr<GenericParam>> generic_params;
2834+
TypeParam self_param;
28342835
std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds;
28352836
WhereClause where_clause;
28362837
std::vector<Attribute> inner_attrs;
@@ -2870,7 +2871,7 @@ class Trait : public VisItem
28702871
std::vector<Attribute> inner_attrs, location_t locus)
28712872
: VisItem (std::move (vis), std::move (outer_attrs)),
28722873
has_unsafe (is_unsafe), has_auto (is_auto), name (std::move (name)),
2873-
generic_params (std::move (generic_params)),
2874+
generic_params (std::move (generic_params)), self_param ({"Self"}, locus),
28742875
type_param_bounds (std::move (type_param_bounds)),
28752876
where_clause (std::move (where_clause)),
28762877
inner_attrs (std::move (inner_attrs)),
@@ -2880,8 +2881,9 @@ class Trait : public VisItem
28802881
// Copy constructor with vector clone
28812882
Trait (Trait const &other)
28822883
: VisItem (other), has_unsafe (other.has_unsafe), has_auto (other.has_auto),
2883-
name (other.name), where_clause (other.where_clause),
2884-
inner_attrs (other.inner_attrs), locus (other.locus)
2884+
name (other.name), self_param (other.self_param),
2885+
where_clause (other.where_clause), inner_attrs (other.inner_attrs),
2886+
locus (other.locus)
28852887
{
28862888
generic_params.reserve (other.generic_params.size ());
28872889
for (const auto &e : other.generic_params)
@@ -2901,6 +2903,7 @@ class Trait : public VisItem
29012903
{
29022904
VisItem::operator= (other);
29032905
name = other.name;
2906+
self_param = other.self_param;
29042907
has_unsafe = other.has_unsafe;
29052908
has_auto = other.has_auto;
29062909
where_clause = other.where_clause;
@@ -2968,19 +2971,7 @@ class Trait : public VisItem
29682971

29692972
WhereClause &get_where_clause () { return where_clause; }
29702973

2971-
void insert_implict_self (std::unique_ptr<AST::GenericParam> &&param)
2972-
{
2973-
std::vector<std::unique_ptr<GenericParam>> new_list;
2974-
new_list.reserve (generic_params.size () + 1);
2975-
2976-
new_list.push_back (std::move (param));
2977-
for (auto &p : generic_params)
2978-
{
2979-
new_list.push_back (std::move (p));
2980-
}
2981-
2982-
generic_params = std::move (new_list);
2983-
}
2974+
AST::TypeParam &get_implicit_self () { return self_param; }
29842975

29852976
protected:
29862977
/* Use covariance to implement clone function as returning this object

gcc/rust/hir/rust-ast-lower-item.cc

+6
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,12 @@ ASTLoweringItem::visit (AST::Trait &trait)
572572
generic_params = lower_generic_params (trait.get_generic_params ());
573573
}
574574

575+
// TODO: separate "Self" from normal generic parameters
576+
// in HIR as well as in AST?
577+
HIR::GenericParam *self_param
578+
= ASTLowerGenericParam::translate (trait.get_implicit_self ());
579+
generic_params.emplace (generic_params.begin (), self_param);
580+
575581
std::vector<std::unique_ptr<HIR::TypeParamBound>> type_param_bounds;
576582
if (trait.has_type_param_bounds ())
577583
{

gcc/rust/resolve/rust-ast-resolve-item.cc

+3-9
Original file line numberDiff line numberDiff line change
@@ -755,20 +755,14 @@ ResolveItem::visit (AST::Trait &trait)
755755
resolver->push_new_name_rib (resolver->get_name_scope ().peek ());
756756
resolver->push_new_type_rib (resolver->get_type_scope ().peek ());
757757

758-
// we need to inject an implicit self TypeParam here
759-
// FIXME: which location should be used for Rust::Identifier `Self`?
760-
AST::TypeParam *implicit_self
761-
= new AST::TypeParam ({"Self"}, trait.get_locus ());
762-
trait.insert_implict_self (
763-
std::unique_ptr<AST::GenericParam> (implicit_self));
764-
CanonicalPath Self = CanonicalPath::get_big_self (trait.get_node_id ());
765-
758+
ResolveGenericParam::go (trait.get_implicit_self (), prefix,
759+
canonical_prefix);
766760
for (auto &generic : trait.get_generic_params ())
767761
ResolveGenericParam::go (*generic, prefix, canonical_prefix);
768762

769763
// Self is an implicit TypeParam so lets mark it as such
770764
resolver->get_type_scope ().append_reference_for_def (
771-
Self.get_node_id (), implicit_self->get_node_id ());
765+
trait.get_node_id (), trait.get_implicit_self ().get_node_id ());
772766

773767
if (trait.has_type_param_bounds ())
774768
{

gcc/rust/resolve/rust-early-name-resolver.cc

+2
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,8 @@ EarlyNameResolver::visit (AST::TraitItemType &)
353353
void
354354
EarlyNameResolver::visit (AST::Trait &trait)
355355
{
356+
// shouldn't need to visit trait.get_implicit_self ()
357+
356358
for (auto &generic : trait.get_generic_params ())
357359
generic->accept_vis (*this);
358360

gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc

-17
Original file line numberDiff line numberDiff line change
@@ -103,23 +103,6 @@ TopLevel::visit (AST::Module &module)
103103
void
104104
TopLevel::visit (AST::Trait &trait)
105105
{
106-
// FIXME: This Self injection is dodgy. It even lead to issues with metadata
107-
// export in the past (#2349). We cannot tell appart injected parameters from
108-
// regular ones. Dumping generic parameters highlights this Self in metadata,
109-
// during debug or proc macro collection. This is clearly a hack.
110-
//
111-
// For now I'll keep it here in the new name resolver even if it should
112-
// probably not be there. We need to find another way to solve this.
113-
// Maybe an additional attribute to Trait ?
114-
//
115-
// From old resolver:
116-
//// we need to inject an implicit self TypeParam here
117-
//// FIXME: which location should be used for Rust::Identifier `Self`?
118-
AST::TypeParam *implicit_self
119-
= new AST::TypeParam ({"Self"}, trait.get_locus ());
120-
trait.insert_implict_self (
121-
std::unique_ptr<AST::GenericParam> (implicit_self));
122-
123106
insert_or_error_out (trait.get_identifier ().as_string (), trait,
124107
Namespace::Types);
125108

gcc/testsuite/rust/compile/nr2/exclude

-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
attr-mismatch-crate-name.rs
22
attr_deprecated.rs
33
attr_deprecated_2.rs
4-
auto_trait_super_trait.rs
5-
auto_trait_valid.rs
6-
auto_trait_invalid.rs
74
bad=file-name.rs
85
bounds1.rs
96
break-rust2.rs
@@ -152,7 +149,6 @@ privacy1.rs
152149
privacy3.rs
153150
privacy4.rs
154151
privacy5.rs
155-
privacy6.rs
156152
privacy8.rs
157153
macros/proc/attribute_non_function.rs
158154
macros/proc/derive_non_function.rs
@@ -171,8 +167,6 @@ specify-crate-name.rs
171167
stmt_with_block_dot.rs
172168
struct-expr-parse.rs
173169
traits1.rs
174-
traits10.rs
175-
traits11.rs
176170
traits12.rs
177171
traits2.rs
178172
traits3.rs
@@ -181,7 +175,6 @@ traits5.rs
181175
traits6.rs
182176
traits7.rs
183177
traits8.rs
184-
traits9.rs
185178
type-bindings1.rs
186179
unconstrained_type_param.rs
187180
undeclared_label.rs
@@ -191,15 +184,13 @@ v0-mangle1.rs
191184
v0-mangle2.rs
192185
while_break_expr.rs
193186
negative_impls.rs
194-
auto_trait.rs
195187
exhaustiveness1.rs
196188
exhaustiveness2.rs
197189
exhaustiveness3.rs
198190
trait13.rs
199191
trait14.rs
200192
issue-2324-1.rs
201193
issue-2324-2.rs
202-
issue-2725.rs
203194
issue-2987.rs
204195
issue-3045-1.rs
205196
issue-3045-2.rs

gcc/testsuite/rust/link/generic_function_0.rs

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
// { dg-xfail-if "https://github.com/Rust-GCC/gccrs/issues/2349" { *-*-* } }
2-
// { dg-excess-errors "" { xfail *-*-* } }
3-
41
extern crate generic_function_1;
52
use generic_function_1::generic_function;
63

gcc/testsuite/rust/link/trait_import_0.rs

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
// { dg-xfail-if "https://github.com/Rust-GCC/gccrs/issues/2349" { *-*-* } }
2-
// { dg-excess-errors "" { xfail *-*-* } }
3-
41
extern crate trait_import_1;
52
use trait_import_1::Add;
63

gcc/testsuite/rust/link/trait_import_1.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#[lang = "sized"]
2+
pub trait Sized {}
3+
14
#[lang = "add"]
25
pub trait Add<Rhs = Self> {
36
type Output;

0 commit comments

Comments
 (0)