Skip to content

Commit d90770b

Browse files
powerboat9philberty
authored andcommitted
nr2.0: Adjust resolution of impl items
gcc/rust/ChangeLog: * ast/rust-path.cc (TypePath::make_debug_string): Add definition. * ast/rust-path.h (TypePath::make_debug_string): Add declaration. * resolve/rust-default-resolver.cc (DefaultResolver::visit): Adjust InherentImpl and TraitImpl visitors to better handle associated item scope. * resolve/rust-default-resolver.h (DefaultResolver::maybe_insert_big_self): Add. * resolve/rust-late-name-resolver-2.0.cc (Late::visit): Adjust type path resolution errors. * resolve/rust-rib.h (Rib::Kind): Add Generics kind. * resolve/rust-toplevel-name-resolver-2.0.cc (TopLevel::visit): Remove InherentImpl and TraitImpl visitor overrides. (TopLevel::maybe_insert_big_self): Add override in order to add a definition of 'Self'. * resolve/rust-toplevel-name-resolver-2.0.h (TopLevel::visit): Remove InherentImpl and TraitImpl visitor overrides. (TopLevel::maybe_insert_big_self): Add override. gcc/testsuite/ChangeLog: * rust/compile/nr2/exclude: Remove entries. Signed-off-by: Owen Avery <[email protected]>
1 parent dd3de3f commit d90770b

9 files changed

+76
-37
lines changed

gcc/rust/ast/rust-path.cc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,27 @@ TypePath::as_simple_path () const
266266
locus);
267267
}
268268

269+
std::string
270+
TypePath::make_debug_string () const
271+
{
272+
rust_assert (!segments.empty ());
273+
274+
std::string output;
275+
276+
for (const auto &segment : segments)
277+
{
278+
if (segment != nullptr && !segment->is_lang_item ()
279+
&& !segment->is_error ())
280+
{
281+
if (!output.empty () || has_opening_scope_resolution_op ())
282+
output.append ("::");
283+
output.append (segment->get_ident_segment ().as_string ());
284+
}
285+
}
286+
287+
return output;
288+
}
289+
269290
// hopefully definition here will prevent circular dependency issue
270291
TraitBound *
271292
TypePath::to_trait_bound (bool in_parens) const

gcc/rust/ast/rust-path.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,6 +1211,8 @@ class TypePath : public TypeNoBounds
12111211

12121212
std::string as_string () const override;
12131213

1214+
std::string make_debug_string () const;
1215+
12141216
/* Converts TypePath to SimplePath if possible (i.e. no generic or function
12151217
* arguments). Otherwise returns an empty SimplePath. */
12161218
SimplePath as_simple_path () const;

gcc/rust/resolve/rust-default-resolver.cc

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,54 @@ DefaultResolver::visit (AST::Trait &trait)
7272
void
7373
DefaultResolver::visit (AST::InherentImpl &impl)
7474
{
75-
auto inner_fn = [this, &impl] () { AST::DefaultASTVisitor::visit (impl); };
75+
visit_outer_attrs (impl);
76+
visit (impl.get_visibility ());
77+
visit_inner_attrs (impl);
7678

77-
ctx.scoped (Rib::Kind::TraitOrImpl, impl.get_node_id (), inner_fn);
79+
auto inner_fn_inner = [this, &impl] () {
80+
for (auto &item : impl.get_impl_items ())
81+
visit (item);
82+
};
83+
84+
auto inner_fn_outer = [this, &impl, &inner_fn_inner] () {
85+
maybe_insert_big_self (impl);
86+
for (auto &generic : impl.get_generic_params ())
87+
visit (generic);
88+
if (impl.has_where_clause ())
89+
visit (impl.get_where_clause ());
90+
visit (impl.get_type ());
91+
92+
ctx.scoped (Rib::Kind::TraitOrImpl, impl.get_node_id (), inner_fn_inner);
93+
};
94+
95+
ctx.scoped (Rib::Kind::Generics, impl.get_node_id (), inner_fn_outer);
7896
}
7997

8098
void
8199
DefaultResolver::visit (AST::TraitImpl &impl)
82100
{
83-
auto inner_fn = [this, &impl] () { AST::DefaultASTVisitor::visit (impl); };
101+
visit_outer_attrs (impl);
102+
visit (impl.get_visibility ());
103+
visit_inner_attrs (impl);
104+
105+
auto inner_fn_inner = [this, &impl] () {
106+
for (auto &item : impl.get_impl_items ())
107+
visit (item);
108+
};
109+
110+
auto inner_fn_outer = [this, &impl, &inner_fn_inner] () {
111+
maybe_insert_big_self (impl);
112+
for (auto &generic : impl.get_generic_params ())
113+
visit (generic);
114+
if (impl.has_where_clause ())
115+
visit (impl.get_where_clause ());
116+
visit (impl.get_type ());
117+
visit (impl.get_trait_path ());
118+
119+
ctx.scoped (Rib::Kind::TraitOrImpl, impl.get_node_id (), inner_fn_inner);
120+
};
84121

85-
ctx.scoped (Rib::Kind::TraitOrImpl, impl.get_node_id (), inner_fn);
122+
ctx.scoped (Rib::Kind::Generics, impl.get_node_id (), inner_fn_outer);
86123
}
87124

88125
void

gcc/rust/resolve/rust-default-resolver.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ class DefaultResolver : public AST::DefaultASTVisitor
4747
void visit (AST::Function &) override;
4848
void visit (AST::ForLoopExpr &expr) override;
4949
void visit (AST::Trait &) override;
50+
// used to handle Self insertion in TopLevel
51+
virtual void maybe_insert_big_self (AST::Impl &) {}
5052
void visit (AST::InherentImpl &) override;
5153
void visit (AST::TraitImpl &) override;
5254

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -495,15 +495,16 @@ Late::visit (AST::TypePath &type)
495495
if (!resolved.has_value ())
496496
{
497497
if (!ctx.lookup (type.get_segments ().front ()->get_node_id ()))
498-
rust_error_at (type.get_locus (), "could not resolve type path %qs",
499-
type.as_string ().c_str ());
498+
rust_error_at (type.get_locus (), ErrorCode::E0412,
499+
"could not resolve type path %qs",
500+
type.make_debug_string ().c_str ());
500501
return;
501502
}
502503

503504
if (resolved->is_ambiguous ())
504505
{
505506
rust_error_at (type.get_locus (), ErrorCode::E0659, "%qs is ambiguous",
506-
type.as_string ().c_str ());
507+
type.make_debug_string ().c_str ());
507508
return;
508509
}
509510

gcc/rust/resolve/rust-rib.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ class Rib
188188
* restriction that you cannot `use` items from the Prelude
189189
*/
190190
Prelude,
191+
/* Generic rib, used to store generics */
192+
Generics,
191193
} kind;
192194

193195
static std::string kind_to_string (Rib::Kind kind)

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

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -141,33 +141,10 @@ TopLevel::visit (AST::Trait &trait)
141141
}
142142

143143
void
144-
TopLevel::visit (AST::InherentImpl &impl)
144+
TopLevel::maybe_insert_big_self (AST::Impl &impl)
145145
{
146-
auto inner_fn = [this, &impl] () {
147-
insert_or_error_out (Identifier ("Self", impl.get_type ().get_locus ()),
148-
impl.get_type (), Namespace::Types);
149-
150-
// We do want to visit with the default visitor instead of default resolver
151-
// because we don't want to insert the scope twice.
152-
AST::DefaultASTVisitor::visit (impl);
153-
};
154-
155-
ctx.scoped (Rib::Kind::TraitOrImpl, impl.get_node_id (), inner_fn);
156-
}
157-
158-
void
159-
TopLevel::visit (AST::TraitImpl &impl)
160-
{
161-
auto inner_fn = [this, &impl] () {
162-
insert_or_error_out (Identifier ("Self", impl.get_type ().get_locus ()),
163-
impl.get_type (), Namespace::Types);
164-
165-
// We do want to visit using the default visitor instead of default resolver
166-
// because we don't want to insert the scope twice.
167-
AST::DefaultASTVisitor::visit (impl);
168-
};
169-
170-
ctx.scoped (Rib::Kind::TraitOrImpl, impl.get_node_id (), inner_fn);
146+
insert_or_error_out (Identifier ("Self", impl.get_type ().get_locus ()),
147+
impl.get_type (), Namespace::Types);
171148
}
172149

173150
void

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,7 @@ class TopLevel : public DefaultResolver
160160

161161
void visit (AST::Module &module) override;
162162
void visit (AST::Trait &trait) override;
163-
void visit (AST::InherentImpl &impl) override;
164-
void visit (AST::TraitImpl &impl) override;
163+
void maybe_insert_big_self (AST::Impl &impl) override;
165164
void visit (AST::TraitItemType &trait_item) override;
166165
void visit (AST::MacroRulesDefinition &macro) override;
167166
void visit (AST::Function &function) override;

gcc/testsuite/rust/compile/nr2/exclude

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@ derive-default1.rs
1010
derive-eq-invalid.rs
1111
torture/alt_patterns1.rs
1212
torture/name_resolve1.rs
13-
issue-3663.rs
1413
issue-3671.rs
1514
issue-3652.rs
16-
issue-3649.rs
1715
issue-1487.rs
1816
issue-2015.rs
1917
issue-3454.rs

0 commit comments

Comments
 (0)