Repository navigation
Bind failed imports to error items instead of blocking their names - #437
Merged
Merged
Conversation
- Bind error items through one helper, and with `bind`, for failed imports, paths through error items and glob imports - Drop the per-import cache of error items, which was never hit, and the clone of the failures each settle step - Share the closed strongly connected components between stuck lookups and reported failures - Drop checks for private error items in import paths and named imports, which never see them before their failures are final, and the entrypoint check, which no lint could observe - Test the origins that a named import merges, and that completions skip failed imports
- Only merge a glob import's error item into one that another glob brought in, since a named import that shadows the glob fails on its own - Don't offer a sibling file after `super::` when the file has an item with its name - Bind error items with `bind` in one branch, and drop the glob's second error-item branch and the unreachable binding for paths through error items - Test failures at a path segment that another namespace's error item explains, and private re-exports reaching glob importers
- An error item that a glob import brings in is the same item in every module, so merging origins on the item changed what explained it everywhere, which hid unrelated failures and made cycles depend on module names. Origins are now kept per scope, and copied along when an import binds the item - Merge a glob's error item only into one that a glob import bound, rather than skipping whenever any named import of the name exists - Link only error items in the scope where a name is looked up, and drop the unused `Base::find` - Test glob cycles and globs beside named imports in both name orders, failures that a merge in another module doesn't explain, and a merge that changes which failures are reported
When a second glob import brought in an error item where one was already bound by a glob, it merged origins but didn't export the binding or count as bringing it in, so whether the name was exported depended on which glob import ran first, and so on module names.
Rigidity
force-pushed
the
rigidity/429-error-bindings
branch
from
November 19, 2026 07:38
5c98244 to
8960cda
Compare
- Compute what a glob import exports once per item, and export a glob-imported error item through `bind` - Drop the copy of origins when a glob binds an error item, which its next pass makes anyway, so it isn't done for every real item a glob binds - Build the failure graph from failed imports alone, since error items only stand for them - Test that a glob doesn't export another module's private error item, and a failed module name used as a type
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #429.
When an import fails, its name is now bound to an error item (like rustc's
Res::Err) instead of being blocked. A use of an error item is silently unresolved, and other imports bring it in like any other item, throughsuper::*,super::X,project::, paths and globs. So follow-on errors disappear without tracking where a block came from. On every existing snapshot the diagnostics are unchanged, except for one improvement. Fuzzing turned up a few more changes, all listed below. One of them reports errors that main hid, and it's the first item under "For you to decide".Design
What an error item is. It's a fresh
Symbol::Unresolved(for values and modules) orType::Unresolved(for types), recorded inCompiler::error_items. A failed named import gets one for each namespace it fails in:Error items aren't recorded as import declarations, so lints never see them. LSP completions skip them, along with a sibling file that one hides.
When it's made. At the settle step, once nothing else can be resolved and nothing waits, and after private re-exports are reported.
ImportCache::failed), and the error items are bound. An item with the name could still be found before that point, so nothing is bound earlier.resolve_importsrun, so the error items made in that run are final too. Signatures are compiled between the two runs, so they already see them.Globs and
super::*. A glob brings in error items like other items:super::*brings in all of them, and a glob with a path brings in the exported ones.Which failures are reported.
error_links).error_origins, keyed by scope and item). A glob brings in the same item everywhere, so what explains a name in one module mustn't leak into another.reported_failures). A chain reports its start. Every member of a cycle, such asimport A::B; import B::A;orimport X::X;, is still reported.Type checking and codegen.
compile_pathreturnsPathResult::Unresolved, with no diagnostic, for an error item, or for a name with an error item in any namespace.What's removed, and what isn't
Removed:
Scope::block,retain_block_origins,is_blocked,block_originsandblocked, with the per-namespaceblockedmaps.scope.rs: 408 → 338 lines.Compiler::is_blocked_inandblock_origins_in, and the block checks insibling_item,sibling_items,resolve_item_inand the enclosed-prelude lint.compiler.rs: 1252 → 1226 lines.FailedNameand passing blocks throughsuper::*andsuper::X(error items travel by themselves), plusblock,failing_imports,record_blocked_import,Base::block_origins,Base::findandblocked_imports.failure_is_reportedandreached_through_blocks, which were roughly quadratic in the number of failing imports. They're replaced by one SCC pass, which shares its closed-component helper withstuck_lookups.forget_unresolved_path; droppingPrivateSymbol,PrivateTypeandPrivateModuleerrors once they no longer apply; and theImportError::UnresolvedPath,PrivateSymbolandPrivateTypevariants. A failure's errors now live in itsFailure.Overall, the non-test code in the crates loses 91 lines (526 added, 617 removed).
imports.rsis slightly shorter (1996 → 1980 lines), since it now does what the scopes and the compiler did for blocks.Kept:
import A::B; import B::A;would resolve through each other's error items and report nothing. What's left is a linear SCC pass over links taken from error items, with no stored block origins.report_private_reexports, with itswaitsrule for chains).const X: Int = 1; export X;) already has the item under that name, so there's no empty name to bind.2 × imports + 2passes (never more), so the doc's measurements still hold.Diagnostic changes
These drop errors that follow from another error that's still reported, unless the item says otherwise.
failed_import_origins_aand_z:import super::a::X;, whereahasexport nope::X;, is no longer reported asUnresolved import X. The exported error item explains it. Both name orders agree.Expected symbol, but found type), and the same holds the other way round. Test:failed_names_in_another_namespace.Unresolved import,Undeclared symbolorUndeclared type. Tests:exported_failed_imports, and theexported_failed_names_through_globs_a/_zpair.mod m { type X = Int; export nope::X; }followed byimport m::X;no longer reports the private type. Hadnope::Xresolved, the import would have brought that in, with no error, as in Rust. Test:private_items_beside_exported_failed_imports.import m::x;findsm's functionxand the error items ofm's failedexport nope::x;. Soimport x::d::x;is explained, and itsCannot subpath non-module symbolis gone. Test:failed_names_merged_through_named_imports.failed_globs_beside_named_imports_a/_z, andfailed_names_through_globs.mod d { export X; const X: Int = 1; }followed byexport d::*;. The recovery now runs before glob importers are final, so the glob passesXon. A use ofXas a type then reportsExpected type, but found symbol Xinstead ofUndeclared type X, and the glob'sUnused import *warning goes away. Test:private_reexports_through_globs.mod d { import X; import super::*; }when the root'simport X;failed. The named import shadows the glob, so it fails even when the root has a realX, and main reports it then too. Main only hid it when the root's import had failed as well. Test:failed_names_through_globs.export mod b { import a::d; mod a { export super::d; } }andexport mod f { import super::f::a; export a; }. In both, each import fails only because of the other. Main reported one member, because it never linked the last name of a path. Test:failed_import_cycles_through_exports.Other new tests:
failed_imports_of_one_name_a/_z: two failed imports of one name, one of them exported.failed_glob_cycles_a/_z: a cycle reported whichever glob brings its error item in first.failed_names_through_globs_of_other_modules: a merge in one module doesn't explain a failure in another.failed_path_segments_in_another_namespace, andfailed_module_names_used_as_types.private_failed_names_through_exported_globs: a glob that re-exports a module's items doesn't export its private error items.Comments and test names that described blocks now describe error items. The
blocked_*snapshot files keep their names.Fuzzing
I generated random programs with up to 5 nested inline modules. They use named, glob,
super,project, exported and failing imports, private items and re-exports, and they use the imported names in bodies and signatures. In the project runs, some top-level modules move into sibling files. Each program was compared withorigin/main, and compiled again with its module names permuted, as #393's renaming fuzzer does. The names are one letter, so locations don't move.Final runs on the head of this branch, on fresh seeds, against
origin/mainat the same base:import X;next toimport super::*;often, so real code should hit it much less._a/_ztest: whether a name's error item was exported; origins kept on the item rather than per scope; and an export lost when two globs' error items met. An earlier run found one program whose results depend on module names, and main's results for it do too. The final runs found none.check_forced_lookups. No reviewer could reach it, and its comment says it's only a guard.For you to decide
named_items,lookup_segmentand globs, and drop the link inNamedItems::Public. But it brings back those follow-on errors.Note
Medium Risk
This changes how import resolution handles failed imports, which every program with an import error goes through. A program without a failed import never binds an error item; the only change for it is that private re-exports are recovered in the first resolution run as well as the second. Every existing snapshot and unit test is unchanged except the one listed improvement, and the fuzzing found no crashes and no new order dependence.
compile/imports.rs): error items replace blocks;Failurerecords final failures; per-scope origins and an SCC pass (reported_failures) replacefailure_is_reported; private re-exports are recovered before failures are final, without the error cleanup.rue-hirscope.rs,compiler.rs,compile/path.rs): blocks are removed; error items are found silently; sibling files that an item hides aren't completed.rue-lsp): completions skip error items.