Skip to content
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

nr2.0: Improve default and top-level resolvers #3332

Merged
merged 1 commit into from
Jan 7, 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
35 changes: 28 additions & 7 deletions gcc/rust/resolve/rust-default-resolver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,27 +88,48 @@ DefaultResolver::visit (AST::TraitImpl &impl)
void
DefaultResolver::visit (AST::StructStruct &type)
{
// do we need to scope anything here? no, right?
auto inner_fn = [this, &type] () { AST::DefaultASTVisitor::visit (type); };

// we also can't visit `StructField`s by default, so there's nothing to do -
// correct? or should we do something like
ctx.scoped (Rib::Kind::Item /* FIXME: Correct? */, type.get_node_id (),
inner_fn, type.get_struct_name ());
}

AST::DefaultASTVisitor::visit (type);
void
DefaultResolver::visit (AST::TupleStruct &type)
{
auto inner_fn = [this, &type] () { AST::DefaultASTVisitor::visit (type); };

// FIXME: ???
ctx.scoped (Rib::Kind::Item /* FIXME: Correct? */, type.get_node_id (),
inner_fn, type.get_struct_name ());
}

void
DefaultResolver::visit (AST::Enum &type)
{
// FIXME: Do we need to scope anything by default?

auto variant_fn = [this, &type] () { AST::DefaultASTVisitor::visit (type); };

ctx.scoped (Rib::Kind::Item /* FIXME: Correct? */, type.get_node_id (),
variant_fn, type.get_identifier ());
}

void
DefaultResolver::visit (AST::Union &type)
{
auto inner_fn = [this, &type] () { AST::DefaultASTVisitor::visit (type); };

ctx.scoped (Rib::Kind::Item /* FIXME: Correct? */, type.get_node_id (),
inner_fn, type.get_identifier ());
}

void
DefaultResolver::visit (AST::TypeAlias &type)
{
auto inner_fn = [this, &type] () { AST::DefaultASTVisitor::visit (type); };

ctx.scoped (Rib::Kind::Item /* FIXME: Correct? */, type.get_node_id (),
inner_fn, type.get_new_type_name ());
}

void
DefaultResolver::visit (AST::ClosureExprInner &expr)
{
Expand Down
3 changes: 3 additions & 0 deletions gcc/rust/resolve/rust-default-resolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ class DefaultResolver : public AST::DefaultASTVisitor

// type dec nodes, which visit their fields or variants by default
void visit (AST::StructStruct &) override;
void visit (AST::TupleStruct &) override;
void visit (AST::Enum &) override;
void visit (AST::Union &) override;
void visit (AST::TypeAlias &) override;

// Visitors that visit their expression node(s)
void visit (AST::ClosureExprInner &) override;
Expand Down
7 changes: 0 additions & 7 deletions gcc/rust/resolve/rust-late-name-resolver-2.0.cc
Original file line number Diff line number Diff line change
Expand Up @@ -308,13 +308,6 @@ Late::visit (AST::StructStruct &s)
ctx.scoped (Rib::Kind::Item, s.get_node_id (), s_vis);
}

void
Late::visit (AST::Enum &s)
{
auto s_vis = [this, &s] () { AST::DefaultASTVisitor::visit (s); };
ctx.scoped (Rib::Kind::Item, s.get_node_id (), s_vis);
}

void
Late::visit (AST::StructExprStruct &s)
{
Expand Down
1 change: 0 additions & 1 deletion gcc/rust/resolve/rust-late-name-resolver-2.0.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ class Late : public DefaultResolver
void visit (AST::StructExprStructBase &) override;
void visit (AST::StructExprStructFields &) override;
void visit (AST::StructStruct &) override;
void visit (AST::Enum &) override;
void visit (AST::GenericArgs &) override;
void visit (AST::GenericArg &);

Expand Down
50 changes: 10 additions & 40 deletions gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,7 @@ TopLevel::visit (AST::Module &module)
if (module.get_kind () == AST::Module::UNLOADED)
module.load_items ();

auto sub_visitor = [this, &module] () {
for (auto &item : module.get_items ())
item->accept_vis (*this);
};

ctx.scoped (Rib::Kind::Module, module.get_node_id (), sub_visitor,
module.get_name ());
DefaultResolver::visit (module);

if (Analysis::Mappings::get ().lookup_ast_module (module.get_node_id ())
== tl::nullopt)
Expand Down Expand Up @@ -230,23 +224,6 @@ TopLevel::visit (AST::Function &function)
DefaultResolver::visit (function);
}

void
TopLevel::visit (AST::BlockExpr &expr)
{
// extracting the lambda from the `scoped` call otherwise the code looks like
// a hot turd thanks to our .clang-format

auto sub_vis = [this, &expr] () {
for (auto &stmt : expr.get_statements ())
stmt->accept_vis (*this);

if (expr.has_tail_expr ())
expr.get_tail_expr ().accept_vis (*this);
};

ctx.scoped (Rib::Kind::Normal, expr.get_node_id (), sub_vis);
}

void
TopLevel::visit (AST::StaticItem &static_item)
{
Expand All @@ -261,6 +238,8 @@ TopLevel::visit (AST::ExternalStaticItem &static_item)
{
insert_or_error_out (static_item.get_identifier ().as_string (), static_item,
Namespace::Values);

DefaultResolver::visit (static_item);
}

void
Expand Down Expand Up @@ -291,6 +270,8 @@ TopLevel::visit (AST::TypeParam &type_param)
{
insert_or_error_out (type_param.get_type_representation (), type_param,
Namespace::Types);

DefaultResolver::visit (type_param);
}

void
Expand All @@ -309,6 +290,8 @@ TopLevel::visit (AST::TupleStruct &tuple_struct)

insert_or_error_out (tuple_struct.get_struct_name (), tuple_struct,
Namespace::Values);

DefaultResolver::visit (tuple_struct);
}

void
Expand Down Expand Up @@ -338,32 +321,19 @@ TopLevel::visit (AST::EnumItemDiscriminant &variant)
void
TopLevel::visit (AST::Enum &enum_item)
{
auto generic_vis = [this, &enum_item] () {
for (auto &g : enum_item.get_generic_params ())
{
g->accept_vis (*this);
}
};

ctx.scoped (Rib::Kind::Item, enum_item.get_node_id (), generic_vis);

insert_or_error_out (enum_item.get_identifier (), enum_item,
Namespace::Types);

auto field_vis = [this, &enum_item] () {
for (auto &variant : enum_item.get_variants ())
variant->accept_vis (*this);
};

ctx.scoped (Rib::Kind::Item /* FIXME: Is that correct? */,
enum_item.get_node_id (), field_vis, enum_item.get_identifier ());
DefaultResolver::visit (enum_item);
}

void
TopLevel::visit (AST::Union &union_item)
{
insert_or_error_out (union_item.get_identifier (), union_item,
Namespace::Types);

DefaultResolver::visit (union_item);
}

void
Expand Down
1 change: 0 additions & 1 deletion gcc/rust/resolve/rust-toplevel-name-resolver-2.0.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ class TopLevel : public DefaultResolver
void visit (AST::TraitItemType &trait_item) override;
void visit (AST::MacroRulesDefinition &macro) override;
void visit (AST::Function &function) override;
void visit (AST::BlockExpr &expr) override;
void visit (AST::StaticItem &static_item) override;
void visit (AST::ExternalStaticItem &static_item) override;
void visit (AST::StructStruct &struct_item) override;
Expand Down
9 changes: 0 additions & 9 deletions gcc/testsuite/rust/compile/nr2/exclude
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,25 @@ derive_macro3.rs
derive_macro4.rs
derive_macro6.rs
expected_type_args2.rs
expected_type_args3.rs
feature_rust_attri0.rs
for_lifetimes.rs
format_args_basic_expansion.rs
generic-default1.rs
generics1.rs
generics10.rs
generics11.rs
generics2.rs
generics3.rs
generics4.rs
generics5.rs
generics6.rs
generics8.rs
generics9.rs
if_let_expr.rs
issue-1019.rs
issue-1031.rs
issue-1034.rs
issue-1129-2.rs
issue-1130.rs
issue-1165.rs
issue-1173.rs
issue-1235.rs
issue-1272.rs
issue-1289.rs
issue-1447.rs
Expand All @@ -66,7 +61,6 @@ issue-2036.rs
issue-2037.rs
issue-2043.rs
issue-2070.rs
issue-2105.rs
issue-2135.rs
issue-2136-1.rs
issue-2136-2.rs
Expand Down Expand Up @@ -117,7 +111,6 @@ multiple_bindings1.rs
multiple_bindings2.rs
name_resolution2.rs
name_resolution4.rs
nested_generic.rs
nested_macro_use1.rs
nested_macro_use2.rs
nested_macro_use3.rs
Expand All @@ -142,7 +135,6 @@ pub_restricted_3.rs
redef_error2.rs
redef_error4.rs
redef_error5.rs
redef_error6.rs
self-path1.rs
self-path2.rs
sizeof-stray-infer-var-bug.rs
Expand Down Expand Up @@ -193,7 +185,6 @@ if_let_expr_simple.rs
iflet.rs
issue-3033.rs
issue-3009.rs
issue-2323.rs
issue-2953-2.rs
issue-1773.rs
issue-2905-1.rs
Expand Down
Loading