From fa6747f326dfbf883292d5599c7d926cbf6c62e3 Mon Sep 17 00:00:00 2001 From: Owen Avery Date: Thu, 2 Jan 2025 05:55:38 -0500 Subject: [PATCH] nr2.0: Improve default, top-level, and late resolvers gcc/rust/ChangeLog: * resolve/rust-default-resolver.cc (DefaultResolver::visit): Make sure to scope visitation of the children of type definition items. * resolve/rust-default-resolver.h (DefaultResolver::visit): Add overrides for TupleStruct, Union, and TypeAlias. * resolve/rust-late-name-resolver-2.0.cc (Late::visit): Remove override for Enum. * resolve/rust-late-name-resolver-2.0.h (Late::visit): Likewise. * resolve/rust-toplevel-name-resolver-2.0.cc (TopLevel::visit): Rely more on DefaultResolver::visit. * resolve/rust-toplevel-name-resolver-2.0.h (TopLevel::visit): Remove override for BlockExpr. gcc/testsuite/ChangeLog: * rust/compile/nr2/exclude: Remove entries. Signed-off-by: Owen Avery --- gcc/rust/resolve/rust-default-resolver.cc | 35 ++++++++++--- gcc/rust/resolve/rust-default-resolver.h | 3 ++ .../resolve/rust-late-name-resolver-2.0.cc | 7 --- .../resolve/rust-late-name-resolver-2.0.h | 1 - .../rust-toplevel-name-resolver-2.0.cc | 50 ++++--------------- .../resolve/rust-toplevel-name-resolver-2.0.h | 1 - gcc/testsuite/rust/compile/nr2/exclude | 9 ---- 7 files changed, 41 insertions(+), 65 deletions(-) diff --git a/gcc/rust/resolve/rust-default-resolver.cc b/gcc/rust/resolve/rust-default-resolver.cc index 5d0f7a0e9303..58e477660cdc 100644 --- a/gcc/rust/resolve/rust-default-resolver.cc +++ b/gcc/rust/resolve/rust-default-resolver.cc @@ -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) { diff --git a/gcc/rust/resolve/rust-default-resolver.h b/gcc/rust/resolve/rust-default-resolver.h index 21e67d8e5c5e..eb08dc87fead 100644 --- a/gcc/rust/resolve/rust-default-resolver.h +++ b/gcc/rust/resolve/rust-default-resolver.h @@ -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; diff --git a/gcc/rust/resolve/rust-late-name-resolver-2.0.cc b/gcc/rust/resolve/rust-late-name-resolver-2.0.cc index b28d38f3e399..974e1fa3129b 100644 --- a/gcc/rust/resolve/rust-late-name-resolver-2.0.cc +++ b/gcc/rust/resolve/rust-late-name-resolver-2.0.cc @@ -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) { diff --git a/gcc/rust/resolve/rust-late-name-resolver-2.0.h b/gcc/rust/resolve/rust-late-name-resolver-2.0.h index e3f636d78f89..0db21f2a0bd1 100644 --- a/gcc/rust/resolve/rust-late-name-resolver-2.0.h +++ b/gcc/rust/resolve/rust-late-name-resolver-2.0.h @@ -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 &); 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 369003765563..a88adabb094c 100644 --- a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc +++ b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc @@ -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) @@ -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) { @@ -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 @@ -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 @@ -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 @@ -338,25 +321,10 @@ 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 @@ -364,6 +332,8 @@ TopLevel::visit (AST::Union &union_item) { insert_or_error_out (union_item.get_identifier (), union_item, Namespace::Types); + + DefaultResolver::visit (union_item); } void diff --git a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.h b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.h index 081ad2ddd829..4b8a51cff9cc 100644 --- a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.h +++ b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.h @@ -151,7 +151,6 @@ class TopLevel : public DefaultResolver void visit (AST::TraitItemType &trait_item) override; void visit (AST::MacroRulesDefinition ¯o) 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; diff --git a/gcc/testsuite/rust/compile/nr2/exclude b/gcc/testsuite/rust/compile/nr2/exclude index da5880d9a57c..945a697f677c 100644 --- a/gcc/testsuite/rust/compile/nr2/exclude +++ b/gcc/testsuite/rust/compile/nr2/exclude @@ -25,7 +25,6 @@ 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 @@ -33,12 +32,10 @@ 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 @@ -46,9 +43,7 @@ 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 @@ -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 @@ -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 @@ -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 @@ -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