https://gcc.gnu.org/g:3dd992925f9b262277d61202b99bf201ea78a43d

commit r16-2880-g3dd992925f9b262277d61202b99bf201ea78a43d
Author: Owen Avery <powerboat9.ga...@gmail.com>
Date:   Mon Jun 16 17:04:22 2025 -0400

    gccrs: Adjust external crate lowering and type checking
    
    The 2.0 name resolver is provided through
    ImmutableNameResolutionContext after it is done being mutated. The
    typechecker attempts to use ImmutableNameResolutionContext, so it needs
    to be run after ImmutableNameResolutionContext has been initialized
    (after all name resolution has been completed). Additionally, although I
    haven't seen any issues with lowering AST to HIR before name resolution
    2.0 is complete, it makes sense to perform all lowering in lockstep as
    well.
    
    gcc/rust/ChangeLog:
    
            * hir/rust-ast-lower-item.cc (ASTLoweringItem::visit): Add
            visitor for ExternCrate.
            * hir/rust-ast-lower-item.h (ASTLoweringItem::visit): Likewise.
            * rust-session-manager.cc (Session::load_extern_crate): Avoid
            lowering or type resolving external crates here.
            * typecheck/rust-hir-type-check-item.cc (TypeCheckItem::visit):
            Add visitor for ExternCrate.
            * typecheck/rust-hir-type-check-item.h (TypeCheckItem::visit):
            Replace empty definition with a declaration.
    
    Signed-off-by: Owen Avery <powerboat9.ga...@gmail.com>

Diff:
---
 gcc/rust/hir/rust-ast-lower-item.cc            | 19 +++++++++++++++++++
 gcc/rust/hir/rust-ast-lower-item.h             |  1 +
 gcc/rust/rust-session-manager.cc               |  8 --------
 gcc/rust/typecheck/rust-hir-type-check-item.cc | 19 +++++++++++++++++++
 gcc/rust/typecheck/rust-hir-type-check-item.h  |  2 +-
 5 files changed, 40 insertions(+), 9 deletions(-)

diff --git a/gcc/rust/hir/rust-ast-lower-item.cc 
b/gcc/rust/hir/rust-ast-lower-item.cc
index acec008c8923..0623065ddd46 100644
--- a/gcc/rust/hir/rust-ast-lower-item.cc
+++ b/gcc/rust/hir/rust-ast-lower-item.cc
@@ -732,6 +732,25 @@ ASTLoweringItem::visit (AST::MacroRulesDefinition &def)
   lower_macro_definition (def);
 }
 
+void
+ASTLoweringItem::visit (AST::ExternCrate &extern_crate)
+{
+  if (extern_crate.references_self ())
+    return;
+
+  auto &mappings = Analysis::Mappings::get ();
+  CrateNum num
+    = mappings.lookup_crate_name (extern_crate.get_referenced_crate ())
+       .value ();
+  AST::Crate &crate = mappings.get_ast_crate (num);
+
+  auto saved_crate_num = mappings.get_current_crate ();
+  mappings.set_current_crate (num);
+  auto lowered = ASTLowering::Resolve (crate);
+  mappings.insert_hir_crate (std::move (lowered));
+  mappings.set_current_crate (saved_crate_num);
+}
+
 HIR::SimplePath
 ASTLoweringSimplePath::translate (const AST::SimplePath &path)
 {
diff --git a/gcc/rust/hir/rust-ast-lower-item.h 
b/gcc/rust/hir/rust-ast-lower-item.h
index 4e142ed5e7c4..dc750571d138 100644
--- a/gcc/rust/hir/rust-ast-lower-item.h
+++ b/gcc/rust/hir/rust-ast-lower-item.h
@@ -45,6 +45,7 @@ public:
   void visit (AST::TraitImpl &impl_block) override;
   void visit (AST::ExternBlock &extern_block) override;
   void visit (AST::MacroRulesDefinition &rules_def) override;
+  void visit (AST::ExternCrate &extern_crate) override;
 
 private:
   ASTLoweringItem () : translated (nullptr) {}
diff --git a/gcc/rust/rust-session-manager.cc b/gcc/rust/rust-session-manager.cc
index d42ae6a3fda5..da11a6e57c27 100644
--- a/gcc/rust/rust-session-manager.cc
+++ b/gcc/rust/rust-session-manager.cc
@@ -1182,14 +1182,6 @@ Session::load_extern_crate (const std::string 
&crate_name, location_t locus)
   // name resolve it
   Resolver::NameResolution::Resolve (parsed_crate);
 
-  // perform hir lowering
-  std::unique_ptr<HIR::Crate> lowered
-    = HIR::ASTLowering::Resolve (parsed_crate);
-  HIR::Crate &hir = mappings.insert_hir_crate (std::move (lowered));
-
-  // perform type resolution
-  Resolver::TypeResolution::Resolve (hir);
-
   // always restore the crate_num
   mappings.set_current_crate (saved_crate_num);
 
diff --git a/gcc/rust/typecheck/rust-hir-type-check-item.cc 
b/gcc/rust/typecheck/rust-hir-type-check-item.cc
index 5595dad22cc5..70622778a1c0 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-item.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-item.cc
@@ -737,6 +737,25 @@ TypeCheckItem::visit (HIR::ExternBlock &extern_block)
     }
 }
 
+void
+TypeCheckItem::visit (HIR::ExternCrate &extern_crate)
+{
+  if (extern_crate.references_self ())
+    return;
+
+  auto &mappings = Analysis::Mappings::get ();
+  CrateNum num
+    = mappings.lookup_crate_name (extern_crate.get_referenced_crate ())
+       .value ();
+  HIR::Crate &crate = mappings.get_hir_crate (num);
+
+  CrateNum saved_crate_num = mappings.get_current_crate ();
+  mappings.set_current_crate (num);
+  for (auto &item : crate.get_items ())
+    TypeCheckItem::Resolve (*item);
+  mappings.set_current_crate (saved_crate_num);
+}
+
 std::pair<std::vector<TyTy::SubstitutionParamMapping>, TyTy::RegionConstraints>
 TypeCheckItem::resolve_impl_block_substitutions (HIR::ImplBlock &impl_block,
                                                 bool &failure_flag)
diff --git a/gcc/rust/typecheck/rust-hir-type-check-item.h 
b/gcc/rust/typecheck/rust-hir-type-check-item.h
index 56832e75ccdb..414694be2790 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-item.h
+++ b/gcc/rust/typecheck/rust-hir-type-check-item.h
@@ -51,9 +51,9 @@ public:
   void visit (HIR::ImplBlock &impl_block) override;
   void visit (HIR::ExternBlock &extern_block) override;
   void visit (HIR::Trait &trait_block) override;
+  void visit (HIR::ExternCrate &extern_crate) override;
 
   // nothing to do
-  void visit (HIR::ExternCrate &) override {}
   void visit (HIR::UseDeclaration &) override {}
 
 protected:

Reply via email to