From: Owen Avery Date: Mon, 16 Jun 2025 21:04:22 +0000 (-0400) Subject: gccrs: Adjust external crate lowering and type checking X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=3dd992925f9b262277d61202b99bf201ea78a43d;p=thirdparty%2Fgcc.git 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 --- diff --git a/gcc/rust/hir/rust-ast-lower-item.cc b/gcc/rust/hir/rust-ast-lower-item.cc index acec008c892..0623065ddd4 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 4e142ed5e7c..dc750571d13 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 d42ae6a3fda..da11a6e57c2 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 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 5595dad22cc..70622778a1c 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, 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 56832e75ccd..414694be279 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: