]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: nr2.0: Adjust resolution of impl items
authorOwen Avery <powerboat9.gamer@gmail.com>
Fri, 25 Apr 2025 22:06:41 +0000 (18:06 -0400)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 5 Aug 2025 14:36:41 +0000 (16:36 +0200)
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 <powerboat9.gamer@gmail.com>
gcc/rust/ast/rust-path.cc
gcc/rust/ast/rust-path.h
gcc/rust/resolve/rust-default-resolver.cc
gcc/rust/resolve/rust-default-resolver.h
gcc/rust/resolve/rust-late-name-resolver-2.0.cc
gcc/rust/resolve/rust-rib.h
gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc
gcc/rust/resolve/rust-toplevel-name-resolver-2.0.h
gcc/testsuite/rust/compile/nr2/exclude

index 8e43ddf648a91b889fbffbf572375d379a1327d1..793423aa6ee8b0c0bc352adbc50813303090e030 100644 (file)
@@ -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
index 73e2f83104b3789d218baa33a437b04f3e147044..11f72480fe0715cf32b1b924045dc7cebe88c3ed 100644 (file)
@@ -1211,6 +1211,8 @@ public:
 
   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;
index 480034c89741c8f01be5421dcd44291c6a0deb87..dce5aaa6a11a02c4de63568cf685cea93386def4 100644 (file)
@@ -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
index 2a987efdf5c49c31990a9a22b3aa459c1bd97ec0..87281377d70f0156546906c7957679029d644204 100644 (file)
@@ -47,6 +47,8 @@ public:
   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;
 
index 3328c8b00d856bcbd8ba7943017f1f798f07dd40..328d5af0b1cc31f51ee19bf9c8b9f7702321351d 100644 (file)
@@ -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;
     }
 
index c498328a8087e85260f68fa66e94fecf095c6cf1..62189f7a6d10fdedf705ac9db48d968f89c8fd4e 100644 (file)
@@ -188,6 +188,8 @@ public:
      * 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)
index dad9205cc26fea1292ece11e7b519e65ab191e83..122436bb22fecb8d62da985f3fb23444711b5dc8 100644 (file)
@@ -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
index 3ff37eda16d18df908705153a46510db933050de..0dfd654861b3bafa97894e8eef7efb92f07c9452 100644 (file)
@@ -160,8 +160,7 @@ private:
 
   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;
index b9b9e39aa82c4c9bba7c44c6ff261603c3c022eb..312964c6b1445a4d7001456bca3cf2a680acf2a2 100644 (file)
@@ -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