Skip to content

Commit 3b4d11c

Browse files
powerboat9philberty
authored andcommitted
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 <[email protected]>
1 parent 5c2b06a commit 3b4d11c

File tree

5 files changed

+40
-9
lines changed

5 files changed

+40
-9
lines changed

gcc/rust/hir/rust-ast-lower-item.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,25 @@ ASTLoweringItem::visit (AST::MacroRulesDefinition &def)
732732
lower_macro_definition (def);
733733
}
734734

735+
void
736+
ASTLoweringItem::visit (AST::ExternCrate &extern_crate)
737+
{
738+
if (extern_crate.references_self ())
739+
return;
740+
741+
auto &mappings = Analysis::Mappings::get ();
742+
CrateNum num
743+
= mappings.lookup_crate_name (extern_crate.get_referenced_crate ())
744+
.value ();
745+
AST::Crate &crate = mappings.get_ast_crate (num);
746+
747+
auto saved_crate_num = mappings.get_current_crate ();
748+
mappings.set_current_crate (num);
749+
auto lowered = ASTLowering::Resolve (crate);
750+
mappings.insert_hir_crate (std::move (lowered));
751+
mappings.set_current_crate (saved_crate_num);
752+
}
753+
735754
HIR::SimplePath
736755
ASTLoweringSimplePath::translate (const AST::SimplePath &path)
737756
{

gcc/rust/hir/rust-ast-lower-item.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class ASTLoweringItem : public ASTLoweringBase
4545
void visit (AST::TraitImpl &impl_block) override;
4646
void visit (AST::ExternBlock &extern_block) override;
4747
void visit (AST::MacroRulesDefinition &rules_def) override;
48+
void visit (AST::ExternCrate &extern_crate) override;
4849

4950
private:
5051
ASTLoweringItem () : translated (nullptr) {}

gcc/rust/rust-session-manager.cc

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,14 +1182,6 @@ Session::load_extern_crate (const std::string &crate_name, location_t locus)
11821182
// name resolve it
11831183
Resolver::NameResolution::Resolve (parsed_crate);
11841184

1185-
// perform hir lowering
1186-
std::unique_ptr<HIR::Crate> lowered
1187-
= HIR::ASTLowering::Resolve (parsed_crate);
1188-
HIR::Crate &hir = mappings.insert_hir_crate (std::move (lowered));
1189-
1190-
// perform type resolution
1191-
Resolver::TypeResolution::Resolve (hir);
1192-
11931185
// always restore the crate_num
11941186
mappings.set_current_crate (saved_crate_num);
11951187

gcc/rust/typecheck/rust-hir-type-check-item.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,25 @@ TypeCheckItem::visit (HIR::ExternBlock &extern_block)
737737
}
738738
}
739739

740+
void
741+
TypeCheckItem::visit (HIR::ExternCrate &extern_crate)
742+
{
743+
if (extern_crate.references_self ())
744+
return;
745+
746+
auto &mappings = Analysis::Mappings::get ();
747+
CrateNum num
748+
= mappings.lookup_crate_name (extern_crate.get_referenced_crate ())
749+
.value ();
750+
HIR::Crate &crate = mappings.get_hir_crate (num);
751+
752+
CrateNum saved_crate_num = mappings.get_current_crate ();
753+
mappings.set_current_crate (num);
754+
for (auto &item : crate.get_items ())
755+
TypeCheckItem::Resolve (*item);
756+
mappings.set_current_crate (saved_crate_num);
757+
}
758+
740759
std::pair<std::vector<TyTy::SubstitutionParamMapping>, TyTy::RegionConstraints>
741760
TypeCheckItem::resolve_impl_block_substitutions (HIR::ImplBlock &impl_block,
742761
bool &failure_flag)

gcc/rust/typecheck/rust-hir-type-check-item.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ class TypeCheckItem : private TypeCheckBase, private HIR::HIRVisItemVisitor
5151
void visit (HIR::ImplBlock &impl_block) override;
5252
void visit (HIR::ExternBlock &extern_block) override;
5353
void visit (HIR::Trait &trait_block) override;
54+
void visit (HIR::ExternCrate &extern_crate) override;
5455

5556
// nothing to do
56-
void visit (HIR::ExternCrate &) override {}
5757
void visit (HIR::UseDeclaration &) override {}
5858

5959
protected:

0 commit comments

Comments
 (0)