Skip to content

nr2.0: Adjust resolution of impl items #3768

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions gcc/rust/ast/rust-path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,27 @@ TypePath::as_simple_path () const
locus);
}

std::string
TypePath::make_debug_string () const
{
rust_assert (!segments.empty ());

std::string output;

for (const auto &segment : segments)
{
if (segment != nullptr && !segment->is_lang_item ()
&& !segment->is_error ())
{
if (!output.empty () || has_opening_scope_resolution_op ())
output.append ("::");
output.append (segment->get_ident_segment ().as_string ());
}
}

return output;
}

// hopefully definition here will prevent circular dependency issue
TraitBound *
TypePath::to_trait_bound (bool in_parens) const
Expand Down
2 changes: 2 additions & 0 deletions gcc/rust/ast/rust-path.h
Original file line number Diff line number Diff line change
Expand Up @@ -1211,6 +1211,8 @@ class TypePath : public TypeNoBounds

std::string as_string () const override;

std::string make_debug_string () const;

/* Converts TypePath to SimplePath if possible (i.e. no generic or function
* arguments). Otherwise returns an empty SimplePath. */
SimplePath as_simple_path () const;
Expand Down
45 changes: 41 additions & 4 deletions gcc/rust/resolve/rust-default-resolver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,54 @@ DefaultResolver::visit (AST::Trait &trait)
void
DefaultResolver::visit (AST::InherentImpl &impl)
{
auto inner_fn = [this, &impl] () { AST::DefaultASTVisitor::visit (impl); };
visit_outer_attrs (impl);
visit (impl.get_visibility ());
visit_inner_attrs (impl);

ctx.scoped (Rib::Kind::TraitOrImpl, impl.get_node_id (), inner_fn);
auto inner_fn_inner = [this, &impl] () {
for (auto &item : impl.get_impl_items ())
visit (item);
};

auto inner_fn_outer = [this, &impl, &inner_fn_inner] () {
maybe_insert_big_self (impl);
for (auto &generic : impl.get_generic_params ())
visit (generic);
if (impl.has_where_clause ())
visit (impl.get_where_clause ());
visit (impl.get_type ());

ctx.scoped (Rib::Kind::TraitOrImpl, impl.get_node_id (), inner_fn_inner);
};

ctx.scoped (Rib::Kind::Generics, impl.get_node_id (), inner_fn_outer);
}

void
DefaultResolver::visit (AST::TraitImpl &impl)
{
auto inner_fn = [this, &impl] () { AST::DefaultASTVisitor::visit (impl); };
visit_outer_attrs (impl);
visit (impl.get_visibility ());
visit_inner_attrs (impl);

auto inner_fn_inner = [this, &impl] () {
for (auto &item : impl.get_impl_items ())
visit (item);
};

auto inner_fn_outer = [this, &impl, &inner_fn_inner] () {
maybe_insert_big_self (impl);
for (auto &generic : impl.get_generic_params ())
visit (generic);
if (impl.has_where_clause ())
visit (impl.get_where_clause ());
visit (impl.get_type ());
visit (impl.get_trait_path ());

ctx.scoped (Rib::Kind::TraitOrImpl, impl.get_node_id (), inner_fn_inner);
};

ctx.scoped (Rib::Kind::TraitOrImpl, impl.get_node_id (), inner_fn);
ctx.scoped (Rib::Kind::Generics, impl.get_node_id (), inner_fn_outer);
}

void
Expand Down
2 changes: 2 additions & 0 deletions gcc/rust/resolve/rust-default-resolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class DefaultResolver : public AST::DefaultASTVisitor
void visit (AST::Function &) override;
void visit (AST::ForLoopExpr &expr) override;
void visit (AST::Trait &) override;
// used to handle Self insertion in TopLevel
virtual void maybe_insert_big_self (AST::Impl &) {}
void visit (AST::InherentImpl &) override;
void visit (AST::TraitImpl &) override;

Expand Down
7 changes: 4 additions & 3 deletions gcc/rust/resolve/rust-late-name-resolver-2.0.cc
Original file line number Diff line number Diff line change
Expand Up @@ -495,15 +495,16 @@ Late::visit (AST::TypePath &type)
if (!resolved.has_value ())
{
if (!ctx.lookup (type.get_segments ().front ()->get_node_id ()))
rust_error_at (type.get_locus (), "could not resolve type path %qs",
type.as_string ().c_str ());
rust_error_at (type.get_locus (), ErrorCode::E0412,
"could not resolve type path %qs",
type.make_debug_string ().c_str ());
return;
}

if (resolved->is_ambiguous ())
{
rust_error_at (type.get_locus (), ErrorCode::E0659, "%qs is ambiguous",
type.as_string ().c_str ());
type.make_debug_string ().c_str ());
return;
}

Expand Down
2 changes: 2 additions & 0 deletions gcc/rust/resolve/rust-rib.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ class Rib
* restriction that you cannot `use` items from the Prelude
*/
Prelude,
/* Generic rib, used to store generics */
Generics,
} kind;

static std::string kind_to_string (Rib::Kind kind)
Expand Down
29 changes: 3 additions & 26 deletions gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc
Original file line number Diff line number Diff line change
Expand Up @@ -141,33 +141,10 @@ TopLevel::visit (AST::Trait &trait)
}

void
TopLevel::visit (AST::InherentImpl &impl)
TopLevel::maybe_insert_big_self (AST::Impl &impl)
{
auto inner_fn = [this, &impl] () {
insert_or_error_out (Identifier ("Self", impl.get_type ().get_locus ()),
impl.get_type (), Namespace::Types);

// We do want to visit with the default visitor instead of default resolver
// because we don't want to insert the scope twice.
AST::DefaultASTVisitor::visit (impl);
};

ctx.scoped (Rib::Kind::TraitOrImpl, impl.get_node_id (), inner_fn);
}

void
TopLevel::visit (AST::TraitImpl &impl)
{
auto inner_fn = [this, &impl] () {
insert_or_error_out (Identifier ("Self", impl.get_type ().get_locus ()),
impl.get_type (), Namespace::Types);

// We do want to visit using the default visitor instead of default resolver
// because we don't want to insert the scope twice.
AST::DefaultASTVisitor::visit (impl);
};

ctx.scoped (Rib::Kind::TraitOrImpl, impl.get_node_id (), inner_fn);
insert_or_error_out (Identifier ("Self", impl.get_type ().get_locus ()),
impl.get_type (), Namespace::Types);
}

void
Expand Down
3 changes: 1 addition & 2 deletions gcc/rust/resolve/rust-toplevel-name-resolver-2.0.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,7 @@ class TopLevel : public DefaultResolver

void visit (AST::Module &module) override;
void visit (AST::Trait &trait) override;
void visit (AST::InherentImpl &impl) override;
void visit (AST::TraitImpl &impl) override;
void maybe_insert_big_self (AST::Impl &impl) override;
void visit (AST::TraitItemType &trait_item) override;
void visit (AST::MacroRulesDefinition &macro) override;
void visit (AST::Function &function) override;
Expand Down
2 changes: 0 additions & 2 deletions gcc/testsuite/rust/compile/nr2/exclude
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ derive-default1.rs
derive-eq-invalid.rs
torture/alt_patterns1.rs
torture/name_resolve1.rs
issue-3663.rs
issue-3671.rs
issue-3652.rs
issue-3649.rs
issue-1487.rs
issue-2015.rs
issue-3454.rs
Expand Down