Add imports to types ns#4551
Conversation
aeb465c to
24a29b8
Compare
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.
|
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! |
eb1c6f9 to
5529f10
Compare
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.
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-expr.cc (CompileExpr::visit): Adapt for new NR APIs. * 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: 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.
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.
|
Does every commit build? I think the commit that introduces a call to |
| { | ||
| resolved_nodes.emplace (usage, definition); | ||
|
|
||
| // auto inserted = resolved_nodes.emplace (usage, definition); |
There was a problem hiding this comment.
Should we remove these lines?
| .insert_or_error_out ( | ||
| identifier, locus, definition.first.get_node_id (), definition.second /* TODO: This isn't clear - it would be better if it was called .ns or something */); | ||
| { | ||
| // dirty = true; |
| toplevel.insert_or_error_out ( | ||
| declared_name, locus, definition.first.get_node_id (), definition.second /* TODO: This isn't clear - it would be better if it was called .ns or something */); | ||
| { | ||
| // dirty = true; |
|
|
||
| if (auto nslookup | ||
| = resolver.lookup (ast_node_id, Resolver2_0::Namespace::Values, | ||
| Resolver2_0::Namespace::Types)) |
There was a problem hiding this comment.
Should we also look up Namespace::Macros?
| path.get_locus ()); | ||
| check_for_privacy_violation ( | ||
| path.get_mappings ().get_nodeid (), path.get_locus (), | ||
| Resolver2_0::Namespace::Types /* FIXME: Is that correct? */); |
There was a problem hiding this comment.
I tested this with:
#![feature(no_core)]
#![no_core]
trait MyTrait {
type AssocType;
fn assoc_func() -> Self::AssocType;
}
struct MyStruct;
impl MyTrait for MyStruct {
type AssocType = i32;
fn assoc_func() -> i32 {
42
}
}
fn main() {
let x: <MyStruct as MyTrait>::AssocType;
x = <MyStruct as MyTrait>::assoc_func();
}and I saw that if we use Namespace::Types, it couldn't find it. But if we use Namespace::Values, it could find it successfully.
| } | ||
|
|
||
| void | ||
| PrivacyReporter::visit (HIR::QualifiedPathInType &path) |
There was a problem hiding this comment.
Also literally not about this PR but, let x: <MyStruct MyTrait as>::AssocType; didn't call this method.
| } | ||
|
|
||
| void | ||
| NameResolutionContext::flatten () |
There was a problem hiding this comment.
It looks like this flatten() method is never actually called anywhere.
This finally properly inserts imports and modules in the types NS, and allows proper resolution to modules and imports. Sorry it took so long