From dbc472b15467e68cb138282a1b0af8b8cfc3f995 Mon Sep 17 00:00:00 2001 From: Roland Xu Date: Fri, 25 Sep 2026 13:15:25 +0800 Subject: [PATCH] Use get to avoid ICE on unresolved imports --- compiler/rustc_resolve/src/check_unused.rs | 2 +- .../ui/imports/unused-import-issue-163294.rs | 9 ++++++++ .../imports/unused-import-issue-163294.stderr | 23 +++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 tests/ui/imports/unused-import-issue-163294.rs create mode 100644 tests/ui/imports/unused-import-issue-163294.stderr diff --git a/compiler/rustc_resolve/src/check_unused.rs b/compiler/rustc_resolve/src/check_unused.rs index daa816c276482..aac244ad6cf22 100644 --- a/compiler/rustc_resolve/src/check_unused.rs +++ b/compiler/rustc_resolve/src/check_unused.rs @@ -136,7 +136,7 @@ impl<'a, 'ra, 'tcx> UnusedImportCheckVisitor<'a, 'ra, 'tcx> { ast::UseTreeKind::Simple(Some(ident)) => { if ident.name == kw::Underscore && !matches!( - self.r.current_owner.import_res[&id].type_ns, + self.r.current_owner.import_res.get(&id).and_then(|res| res.type_ns), Some(Res::Def(DefKind::Trait | DefKind::TraitAlias, _)) ) { diff --git a/tests/ui/imports/unused-import-issue-163294.rs b/tests/ui/imports/unused-import-issue-163294.rs new file mode 100644 index 0000000000000..7793f1da760e2 --- /dev/null +++ b/tests/ui/imports/unused-import-issue-163294.rs @@ -0,0 +1,9 @@ +//@ edition: 2015 + +#![deny(unused_imports)] + +pub use core as _; +//~^ ERROR unresolved import `core` +//~^^ ERROR unused import: `core as _` + +fn main() {} diff --git a/tests/ui/imports/unused-import-issue-163294.stderr b/tests/ui/imports/unused-import-issue-163294.stderr new file mode 100644 index 0000000000000..ebb319562189a --- /dev/null +++ b/tests/ui/imports/unused-import-issue-163294.stderr @@ -0,0 +1,23 @@ +error[E0432]: unresolved import `core` + --> $DIR/unused-import-issue-163294.rs:5:9 + | +LL | pub use core as _; + | ----^^^^^ + | | + | no `core` in the root + +error: unused import: `core as _` + --> $DIR/unused-import-issue-163294.rs:5:9 + | +LL | pub use core as _; + | ^^^^^^^^^ + | +note: the lint level is defined here + --> $DIR/unused-import-issue-163294.rs:3:9 + | +LL | #![deny(unused_imports)] + | ^^^^^^^^^^^^^^ + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0432`.