diff mbox series

[093/125] gccrs: Reinject Self parameter in new resolver

Message ID 20240801145809.366388-95-arthur.cohen@embecosm.com
State New
Headers show
Series [001/125] Rust: Make 'tree'-level 'MAIN_NAME_P' work | expand

Commit Message

Arthur Cohen Aug. 1, 2024, 2:57 p.m. UTC
From: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>

The old resolver injected a Self generic parameter in order to help the
trait solver. This is clearly sketchy at best and should be fixed in
the future.

gcc/rust/ChangeLog:

	* resolve/rust-toplevel-name-resolver-2.0.cc (TopLevel::visit): Add
	Self generic parameter injection and a warning.
	* resolve/rust-toplevel-name-resolver-2.0.h: Add function prototype.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
---
 .../rust-toplevel-name-resolver-2.0.cc        | 23 +++++++++++++++++++
 .../resolve/rust-toplevel-name-resolver-2.0.h |  1 +
 2 files changed, 24 insertions(+)
diff mbox series

Patch

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 f65ec77331c..80b142678b9 100644
--- a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc
+++ b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc
@@ -186,6 +186,29 @@  TopLevel::visit (AST::Module &module)
     Analysis::Mappings::get ()->insert_ast_module (&module);
 }
 
+void
+TopLevel::visit (AST::Trait &trait)
+{
+  // FIXME: This Self injection is dodgy. It even lead to issues with metadata
+  // export in the past (#2349). We cannot tell appart injected parameters from
+  // regular ones. Dumping generic parameters highlights this Self in metadata,
+  // during debug or proc macro collection. This is clearly a hack.
+  //
+  // For now I'll keep it here in the new name resolver even if it should
+  // probably not be there. We need to find another way to solve this.
+  // Maybe an additional attribute to Trait ?
+  //
+  // From old resolver:
+  //// we need to inject an implicit self TypeParam here
+  //// FIXME: which location should be used for Rust::Identifier `Self`?
+  AST::TypeParam *implicit_self
+    = new AST::TypeParam ({"Self"}, trait.get_locus ());
+  trait.insert_implict_self (
+    std::unique_ptr<AST::GenericParam> (implicit_self));
+
+  DefaultResolver::visit (trait);
+}
+
 template <typename PROC_MACRO>
 static void
 insert_macros (std::vector<PROC_MACRO> &macros, NameResolutionContext &ctx)
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 f5e224fa049..e226c79dfbc 100644
--- a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.h
+++ b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.h
@@ -88,6 +88,7 @@  private:
   std::unordered_map<NodeId, NodeId> node_forwarding;
 
   void visit (AST::Module &module) override;
+  void visit (AST::Trait &trait) override;
   void visit (AST::MacroRulesDefinition &macro) override;
   void visit (AST::Function &function) override;
   void visit (AST::BlockExpr &expr) override;