Skip to content

Add imports to types ns#4551

Merged
CohenArthur merged 19 commits into
Rust-GCC:masterfrom
CohenArthur:add-imports-to-types-ns
Jun 4, 2026
Merged

Add imports to types ns#4551
CohenArthur merged 19 commits into
Rust-GCC:masterfrom
CohenArthur:add-imports-to-types-ns

Conversation

@CohenArthur

Copy link
Copy Markdown
Member

This finally properly inserts imports and modules in the types NS, and allows proper resolution to modules and imports. Sorry it took so long

@dkm

dkm commented May 21, 2026

Copy link
Copy Markdown
Member

Drive-by comment, but there's a "[wip] try to understand resolve_segments again..." commit, not sure if that's expected?

@CohenArthur

Copy link
Copy Markdown
Member Author

Drive-by comment, but there's a "[wip] try to understand resolve_segments again..." commit, not sure if that's expected?

definitely not expected haha. thanks!

@CohenArthur CohenArthur force-pushed the add-imports-to-types-ns branch 2 times, most recently from eb1c6f9 to 5529f10 Compare May 22, 2026 15:17
@powerboat9

Copy link
Copy Markdown
Collaborator

Does every commit build? I think the commit that introduces a call to NRCtx::flatten might come before the commit that defines it.

Comment thread gcc/rust/resolve/rust-forever-stack.h
Comment thread gcc/rust/resolve/rust-early-name-resolver-2.0.cc Outdated
Comment thread gcc/rust/resolve/rust-early-name-resolver-2.0.cc Outdated
Comment thread gcc/rust/checks/lints/rust-lint-marklive.cc
Comment thread gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc Outdated
}

void
PrivacyReporter::visit (HIR::QualifiedPathInType &path)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also literally not about this PR but, let x: <MyStruct MyTrait as>::AssocType; didn't call this method.

Comment thread gcc/rust/resolve/rust-name-resolution-context.cc
@CohenArthur

Copy link
Copy Markdown
Member Author

Does every commit build? I think the commit that introduces a call to NRCtx::flatten might come before the commit that defines it.

I did runs building every commit with a git rebase upstream/master -x make ... so I think every single one should be okay. I'll do one more just to be sure

@CohenArthur CohenArthur force-pushed the add-imports-to-types-ns branch 4 times, most recently from fa895a1 to f65baf0 Compare May 27, 2026 14:01
Comment thread gcc/rust/backend/rust-compile-expr.cc
Comment thread gcc/rust/checks/errors/rust-unsafe-checker.cc Outdated
Comment thread gcc/rust/resolve/rust-finalized-name-resolution-context.h Outdated
Comment thread gcc/rust/resolve/rust-name-resolution-context.cc
Comment thread gcc/rust/resolve/rust-name-resolution-context.h Outdated
gcc/rust/ChangeLog:

	* resolve/rust-early-name-resolver-2.0.cc: Properly resolve and insert imports in the
	types NS.
	* resolve/rust-name-resolution-context.cc (find_leaf_definition_inner): New.
	(NameResolutionContext::find_leaf_definition): New.
	(NameResolutionContext::flatten): New.
	* resolve/rust-name-resolution-context.h: Declare flattening functions.
	* rust-session-manager.cc (Session::compile_crate): Call NR flattening.
gcc/rust/ChangeLog:

	* resolve/rust-forever-stack.h: Move resolve_segments and resolve_final_segment to...
	* resolve/rust-forever-stack.hxx: Likewise.
	* resolve/rust-name-resolution-context.h: ...here.
	* resolve/rust-name-resolution-context.hxx: Likewise.
gcc/rust/ChangeLog:

	* resolve/rust-early-name-resolver-2.0.cc (Early::finalize_rebind_import): Insert imports
	as possible glob containers.
	* resolve/rust-name-resolution-context.cc (NameResolutionContext::map_usage): Allow
	multiple mappings of the same usage.
	* resolve/rust-name-resolution-context.hxx: Properly handle imports and modules in segments.

gcc/testsuite/ChangeLog:

	* rust/compile/import_in_type_ns6.rs: New test.
	* rust/compile/import_in_type_ns7.rs: New test.
…ontext.

The ImmutableNRCtx was actually very mutable, as part of the pipeline needs to still map
usages later down the line deep within the backend and typecheckers. Instead, add a
new `map_usage` method which maps to the leafmost definition possible.

gcc/rust/ChangeLog:

	* Make-lang.in: Rename.
	* backend/rust-compile-base.cc: Use new name and API.
	* backend/rust-compile-context.h: Likewise.
	* backend/rust-compile-expr.cc (CompileExpr::visit): Likewise.
	(CompileExpr::generate_closure_function): Likewise.
	* backend/rust-compile-implitem.cc (CompileTraitItem::visit): Likewise.
	* backend/rust-compile-item.cc (CompileItem::visit): Likewise.
	* backend/rust-compile-resolve-path.cc (ResolvePathRef::resolve): Likewise.
	* checks/errors/borrowck/rust-bir-builder-internal.h (struct BuilderContext): Likewise.
	* checks/errors/privacy/rust-privacy-check.cc (Resolver::resolve): Likewise.
	* checks/errors/privacy/rust-privacy-reporter.cc: Likewise.
	* checks/errors/privacy/rust-privacy-reporter.h: Likewise.
	* checks/errors/privacy/rust-visibility-resolver.cc: Likewise.
	* checks/errors/privacy/rust-visibility-resolver.h: Likewise.
	* checks/errors/rust-const-checker.cc (ConstChecker::visit): Likewise.
	* checks/errors/rust-hir-pattern-analysis.cc (PatternChecker::PatternChecker): Likewise.
	* checks/errors/rust-hir-pattern-analysis.h: Likewise.
	* checks/errors/rust-readonly-check.cc (ReadonlyChecker::visit): Likewise.
	* checks/errors/rust-unsafe-checker.cc (UnsafeChecker::visit): Likewise.
	* checks/lints/rust-lint-marklive.cc (MarkLive::visit_path_segment): Likewise.
	(MarkLive::visit): Likewise.
	(MarkLive::find_ref_node_id): Likewise.
	* checks/lints/unused/rust-unused-checker.cc (UnusedChecker::UnusedChecker): Likewise.
	* checks/lints/unused/rust-unused-checker.h: Likewise.
	* checks/lints/unused/rust-unused-collector.cc (UnusedCollector::UnusedCollector): Likewise.
	* checks/lints/unused/rust-unused-collector.h: Likewise.
	* resolve/rust-immutable-name-resolution-context.cc: Move to...
	* resolve/rust-finalized-name-resolution-context.cc: ...here.
	* resolve/rust-name-resolution-context.h: Likewise.
	* rust-session-manager.cc (Session::compile_crate): Likewise.
	* typecheck/rust-hir-trait-resolve.cc (TraitResolver::resolve_path_to_trait): Likewise.
	* typecheck/rust-hir-type-check-enumitem.cc (TypeCheckEnumItem::visit): Likewise.
	* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): Likewise.
	(TypeCheckExpr::resolve_fn_trait_call): Likewise.
	* typecheck/rust-hir-type-check-implitem.cc (TypeCheckImplItem::visit): Likewise.
	* typecheck/rust-hir-type-check-item.cc (TypeCheckItem::visit): Likewise.
	* typecheck/rust-hir-type-check-path.cc (TypeCheckExpr::visit): Likewise.
	(TypeCheckExpr::resolve_root_path): Likewise.
	(TypeCheckExpr::resolve_segments): Likewise.
	* typecheck/rust-hir-type-check-pattern.cc (TypeCheckPattern::visit): Likewise.
	* typecheck/rust-hir-type-check-type.cc (TypeCheckType::resolve_root_path): Likewise.
	(ResolveWhereClauseItem::visit): Likewise.
	* typecheck/rust-hir-type-check.cc (TraitItemReference::get_type_from_fn): Likewise.
	* typecheck/rust-type-util.cc (query_type): Likewise.
	* checks/errors/rust-const-checker.h: Likewise.
	* checks/errors/rust-unsafe-checker.h: Likewise.
	* checks/lints/rust-lint-marklive.h: Likewise.
	* resolve/rust-immutable-name-resolution-context.h: Removed.
	* resolve/rust-finalized-name-resolution-context.h: New file.
CohenArthur and others added 15 commits June 3, 2026 11:37
Flatten the name resolution context after Late name resolution to make it more
useable for subsequent passes.

gcc/rust/ChangeLog:

	* rust-session-manager.cc (Session::compile_crate): Call nr_ctx.flatten()
This now uses find_leaf_definition to better resolve macro definitions
to their actual definitions instead of a possible import, and likewise for
invocations. This also improves the robustness and error checking for
resolving definitions.

gcc/rust/ChangeLog:

	* resolve/rust-early-name-resolver-2.0.cc (Early::insert_once): Rename...
	(Early::try_insert_once): ...to this, and improve logic.
	(Early::go): Use new API.
	(Early::visit): Likewise.
	(Early::finalize_simple_import): Likewise.
	(Early::finalize_rebind_import): Likewise.
	* resolve/rust-early-name-resolver-2.0.h: Declare the new API.
…ooked-up.

This fundamentally changes the way that NR works for later pipeline - instead of having one
singular Usage -> Definition map like it was before, the consumers of finalized NRCtx need to
specify in which namespace(s) they expect usages to be looked-up. We now have one usage map
for each namespace, and add new APIs for looking up usages in one or multiple namespaces.

gcc/rust/ChangeLog:

	* backend/rust-compile-implitem.cc (CompileTraitItem::visit): Adapt for new NR APIs.
	* backend/rust-compile-item.cc (CompileItem::visit): Likewise.
	* backend/rust-compile-resolve-path.cc (ResolvePathRef::resolve): Likewise.
	* checks/errors/borrowck/rust-bir-builder-internal.h: Likewise.
	* checks/errors/privacy/rust-privacy-reporter.cc
	(PrivacyReporter::check_for_privacy_violation): Likewise.
	(PrivacyReporter::check_base_type_privacy): Likewise.
	(PrivacyReporter::visit): Likewise.
	* checks/errors/privacy/rust-privacy-reporter.h: Likewise.
	* checks/errors/privacy/rust-visibility-resolver.cc
	(VisibilityResolver::resolve_module_path): Likewise.
	* checks/errors/rust-const-checker.cc (ConstChecker::visit): Likewise.
	* checks/errors/rust-hir-pattern-analysis.cc (PatternChecker::visit): Likewise.
	* checks/errors/rust-readonly-check.cc (ReadonlyChecker::visit): Likewise.
	* checks/errors/rust-unsafe-checker.cc (UnsafeChecker::visit): Likewise.
	* checks/lints/rust-lint-marklive.cc (MarkLive::visit): Likewise.
	(MarkLive::visit_path_segment): Likewise.
	(MarkLive::find_ref_node_id): Likewise.
	(MarkLive::find_value_definition): Likewise.
	* checks/lints/rust-lint-marklive.h: Likewise.
	* checks/lints/unused/rust-unused-checker.cc (UnusedChecker::visit): Likewise.
	* checks/lints/unused/rust-unused-collector.cc (UnusedCollector::visit): Likewise.
	* checks/lints/unused/rust-unused-collector.h: Likewise.
	* resolve/rust-early-name-resolver-2.0.cc (Early::resolve_glob_import): Likewise.
	(Early::resolve_rebind_import): Likewise.
	(Early::visit): Likewise.
	(Early::visit_derive_attribute): Likewise.
	(Early::visit_non_builtin_attribute): Likewise.
	(Early::finalize_simple_import): Likewise.
	(Early::finalize_rebind_import): Likewise.
	* resolve/rust-early-name-resolver-2.0.h: Likewise.
	* resolve/rust-finalized-name-resolution-context.cc
	(FinalizedNameResolutionContext::map_usage): Likewise.
	(FinalizedNameResolutionContext::lookup): Likewise.
	(FinalizedNameResolutionContext::to_canonical_path): Likewise.
	* resolve/rust-finalized-name-resolution-context.h: Likewise.
	* resolve/rust-forever-stack.h: Likewise.
	* resolve/rust-late-name-resolver-2.0.cc (visit_identifier_as_pattern): Likewise.
	(Late::visit): Likewise.
	(Late::resolve_label): Likewise.
	(resolve_type_path_like): Likewise.
	* resolve/rust-name-resolution-context.cc
	(CanonicalPathRecordCrateRoot::as_path): Likewise.
	(CanonicalPathRecordNormal::as_path): Likewise.
	(CanonicalPathRecordLookup::as_path): Likewise.
	(CanonicalPathRecordImpl::as_path): Likewise.
	(CanonicalPathRecordTraitImpl::as_path): Likewise.
	(NameResolutionContext::map_usage): Likewise.
	(NameResolutionContext::lookup): Likewise.
	* resolve/rust-name-resolution-context.h: Likewise.
	* typecheck/rust-hir-trait-resolve.cc (TraitResolver::resolve_path_to_trait): Likewise.
	* typecheck/rust-hir-type-check-enumitem.cc (TypeCheckEnumItem::visit): Likewise.
	* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): Likewise.
	(TypeCheckExpr::resolve_fn_trait_call): Likewise.
	* typecheck/rust-hir-type-check-implitem.cc (TypeCheckImplItem::visit): Likewise.
	* typecheck/rust-hir-type-check-item.cc (TypeCheckItem::visit): Likewise.
	* typecheck/rust-hir-type-check-path.cc (TypeCheckExpr::visit): Likewise.
	(TypeCheckExpr::resolve_root_path): Likewise.
	(TypeCheckExpr::resolve_segments): Likewise.
	* typecheck/rust-hir-type-check-pattern.cc (TypeCheckPattern::visit): Likewise.
	* typecheck/rust-hir-type-check-type.cc (TypeCheckType::resolve_root_path): Likewise.
	(ResolveWhereClauseItem::visit): Likewise.
	* typecheck/rust-hir-type-check.cc (TraitItemReference::get_type_from_fn): Likewise.
	* typecheck/rust-type-util.cc (query_type): Likewise.
	* backend/rust-compile-expr.h (CompileExpr::resolve_nodeid): Likewise.
	* backend/rust-compile-expr.cc (CompileExpr::visit): Likewise.
	(CompileExpr::lookup_label): Likewise.
	(CompileExpr::lookup_temp_var): Likewise.
	(CompileExpr::resolve_NodeId): Likewise, and rename to...
	(CompileExpr::resolve_nodeid): ...this.
Add more functions for looking up Usage(s) in multiple namespaces.

gcc/rust/ChangeLog:

	* resolve/rust-finalized-name-resolution-context.cc
	(FinalizedNameResolutionContext::lookup): New function.
	* resolve/rust-finalized-name-resolution-context.h: Declare it.
	* resolve/rust-forever-stack.h: Remove assertion in map_usage.
	* resolve/rust-name-resolution-context.h: Add new NSLookup return type.
	* resolve/rust-name-resolution-context.cc (NameResolutionContext::lookup): Use it.
	* typecheck/rust-hir-type-check-path.cc (TypeCheckExpr::resolve_root_path): Likewise.
gcc/rust/ChangeLog:

	* checks/lints/rust-lint-marklive.cc (MarkLive::find_value_definition): Use Types NS
	alongside Values NS for lookup.
gcc/rust/ChangeLog:

	* resolve/rust-early-name-resolver-2.0.h: Properly insert resolutions in
	the returned vector as that was previously missing.
Namespaces, flattening and resolved_nodes are all properly separated now and in the files
where they should be.

gcc/rust/ChangeLog:

	* resolve/rust-early-name-resolver-2.0.cc (Early::try_insert_once): Use new
	Usage -> Definition map for macros.
	* resolve/rust-finalized-name-resolution-context.cc
	(FinalizedNameResolutionContext::map_usage): Dispatch to proper resolution map.
	* resolve/rust-forever-stack.h (enum class LookupFinalizeError): Move here.
	* resolve/rust-forever-stack.hxx: Make resolution take a resolve_segment lambda with
	an extra namespace parameter.
	* resolve/rust-name-resolution-context.hxx: Likewise.
	* resolve/rust-name-resolution-context.cc (find_leaf_definition_inner): Move here...
	(NameResolutionContext::find_leaf_definition): Likewise.
	(NameResolutionContext::flatten): Likewise.
	* resolve/rust-name-resolution-context.h: ...from here.
This patch fixes multiple ICEs and test failures in the Name Resolution 2.0

- Map directly to definition IDs instead of intermediate import IDs.
- Remove redundant `flatten()` calls. Flattening stripped generic  arguments
  from aliases like `Self`, causing typecheck errors.
- Prevent ICEs on invalid `use self;` imports by adding early returns.
- Add `Namespace::Types` to pattern lookups to correctly emit E0532 instead
  of crashing.
- Remove an unsafe debug loop in `Rib::get` that caused ICEs on  ambiguous names.
- Use `value_or` for safe optional unwrapping in module resolution.

gcc/rust/ChangeLog:

	* checks/lints/rust-lint-marklive.cc (MarkLive::visit_path_segment): Add Types namespace
	lookup.
	* checks/lints/unused/rust-unused-checker.cc (UnusedChecker::visit): Use safe optional
	unwrapping.
	* checks/lints/unused/rust-unused-collector.cc (UnusedCollector::visit): Likewise.
	* checks/lints/unused/rust-unused-collector.h: Use helper for multiple namespace lookups.
	* resolve/rust-early-name-resolver-2.0.cc (Early::finalize_simple_import): Prevent ICE on
	single self imports.
	(Early::finalize_rebind_import): Return early to prevent ICE.
	* resolve/rust-name-resolution-context.hxx: Use value_or for safe leaf module lookup.
	* rust-session-manager.cc (Session::compile_crate): Remove obsolete flatten() calls.
	* typecheck/rust-hir-type-check-pattern.cc (TypeCheckPattern::visit): Look up enum variants
	in Types namespace.

Signed-off-by: Enes Cevik <nsvke@proton.me>
gcc/rust/ChangeLog:

	* resolve/rust-early-name-resolver-2.0.cc
	(Early::finalize_rebind_import): Remove locus variable.
gcc/rust/ChangeLog:

	* resolve/rust-forever-stack.h: Comment out flatten with an explanation.
	* resolve/rust-forever-stack.hxx: Likewise.
	* resolve/rust-name-resolution-context.cc (NameResolutionContext::flatten): Likewise.
	* resolve/rust-name-resolution-context.h: Likewise.
gcc/rust/ChangeLog:

	* resolve/rust-early-name-resolver-2.0.cc (Early::finalize_simple_import): Cleanup
	dead code.
	(Early::finalize_rebind_import): Likewise.
	* resolve/rust-finalized-name-resolution-context.h: Add class documentation.
Which enables looking up resolved nodes in three different namespaces.
This is used in select cases but still important.

gcc/rust/ChangeLog:

	* resolve/rust-finalized-name-resolution-context.cc
	(FinalizedNameResolutionContext::lookup): New function.
	* resolve/rust-finalized-name-resolution-context.h: Declare it.
	* resolve/rust-name-resolution-context.cc (NameResolutionContext::lookup): New function.
	* resolve/rust-name-resolution-context.h: Declare it.
And improve code factoring to reduce the complexity of the check_for_privacy_violation function.

gcc/rust/ChangeLog:

	* checks/errors/privacy/rust-privacy-reporter.cc
	(PrivacyReporter::check_for_privacy_violation): Add new overload for two namespaces.
	(PrivacyReporter::visit): Call it.
	(PrivacyReporter::check_violation_inner): New function.
	* checks/errors/privacy/rust-privacy-reporter.h: Declare it.
gcc/rust/ChangeLog:

	* checks/errors/rust-unsafe-checker.cc (UnsafeChecker::visit): Add Types as a
	namespace in which to lookup PathInExpressions.
Since this function is only used in a labels context, let's make it clearer.

gcc/rust/ChangeLog:

	* backend/rust-compile-expr.cc (CompileExpr::visit): Call the new name.
	(CompileExpr::lookup_temp_var): Rename it to...
	(CompileExpr::lookup_label_temp_var): ...this.
	* backend/rust-compile-expr.h: Likewise.
@CohenArthur CohenArthur force-pushed the add-imports-to-types-ns branch from f65baf0 to 2910c19 Compare June 3, 2026 09:44
@CohenArthur CohenArthur added this pull request to the merge queue Jun 4, 2026
Merged via the queue into Rust-GCC:master with commit 58e4887 Jun 4, 2026
12 checks passed
@dkm

dkm commented Jun 4, 2026

Copy link
Copy Markdown
Member

🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants