Skip to content

nr2.0: Add extra check when resolving identifier patterns#4117

Open
CohenArthur wants to merge 15 commits into
Rust-GCC:masterfrom
CohenArthur:fix-issue-4116
Open

nr2.0: Add extra check when resolving identifier patterns#4117
CohenArthur wants to merge 15 commits into
Rust-GCC:masterfrom
CohenArthur:fix-issue-4116

Conversation

@CohenArthur
Copy link
Copy Markdown
Member

gcc/rust/ChangeLog:

* resolve/rust-late-name-resolver-2.0.cc (Late::visit): Check if an identifier pattern
can be resolved in the types namespace before visiting it as an identifier proper.

gcc/testsuite/ChangeLog:

* rust/compile/issue-4116.rs: New test.

Fixes #4116

@powerboat9
Copy link
Copy Markdown
Collaborator

https://doc.rust-lang.org/reference/patterns.html#path-patterns

It looks like path patterns should only resolve to a few types of items, which seems like it'd fix the testsuite issues

Comment thread gcc/rust/resolve/rust-late-name-resolver-2.0.cc
@powerboat9
Copy link
Copy Markdown
Collaborator

@CohenArthur it looks like this is blocking late resolution of libcore

CohenArthur and others added 14 commits May 21, 2026 16:04
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.
	* resolve/rust-immutable-name-resolution-context.h: Removed.
	* resolve/rust-finalized-name-resolution-context.h: New file.
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-resolve-path.cc (ResolvePathRef::resolve): Adapt for new NR APIs.
	* 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/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_rebind_import): Likewise.
	(Early::finalize_simple_import): Likewise.
	(Early::finalize_rebind_import): 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-item.cc (TypeCheckItem::visit): Likewise.
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.
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.
	* resolve/rust-rib.cc (Rib::get): Remove debug loop causing ICE.
	* 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.
@CohenArthur
Copy link
Copy Markdown
Member Author

Rebasing on top of #4551

gcc/rust/ChangeLog:

	* resolve/rust-late-name-resolver-2.0.cc (Late::visit): Check if an identifier pattern
	can be resolved in the types namespace before visiting it as an identifier proper.

gcc/testsuite/ChangeLog:

	* rust/compile/issue-4116.rs: New test.
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.

Single segment variants are name resolved to identifiers

3 participants