From a54564378b16d871bdb29d29fa40a81963ea82df Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Wed, 17 Mar 2021 22:24:51 +0100 Subject: Fix `use crate as ;` imports --- crates/hir_def/src/nameres/collector.rs | 34 +++++++++++++++++---------------- crates/hir_def/src/nameres/tests.rs | 19 ++++++++++++++++++ 2 files changed, 37 insertions(+), 16 deletions(-) diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs index d73f895b7..b5ae5a9e4 100644 --- a/crates/hir_def/src/nameres/collector.rs +++ b/crates/hir_def/src/nameres/collector.rs @@ -656,26 +656,28 @@ impl DefCollector<'_> { } } } else { - match import.path.segments().last() { - Some(last_segment) => { - let name = match &import.alias { - Some(ImportAlias::Alias(name)) => Some(name.clone()), - Some(ImportAlias::Underscore) => None, - None => Some(last_segment.clone()), - }; - log::debug!("resolved import {:?} ({:?}) to {:?}", name, import, def); - - // extern crates in the crate root are special-cased to insert entries into the extern prelude: rust-lang/rust#54658 - if import.is_extern_crate && module_id == self.def_map.root { - if let (Some(def), Some(name)) = (def.take_types(), name.as_ref()) { - self.def_map.extern_prelude.insert(name.clone(), def); - } + let name = match &import.alias { + Some(ImportAlias::Alias(name)) => Some(name.clone()), + Some(ImportAlias::Underscore) => None, + None => match import.path.segments().last() { + Some(last_segment) => Some(last_segment.clone()), + None => { + cov_mark::hit!(bogus_paths); + return; } + }, + }; + + log::debug!("resolved import {:?} ({:?}) to {:?}", name, import, def); - self.update(module_id, &[(name, def)], vis, ImportType::Named); + // extern crates in the crate root are special-cased to insert entries into the extern prelude: rust-lang/rust#54658 + if import.is_extern_crate && module_id == self.def_map.root { + if let (Some(def), Some(name)) = (def.take_types(), name.as_ref()) { + self.def_map.extern_prelude.insert(name.clone(), def); } - None => cov_mark::hit!(bogus_paths), } + + self.update(module_id, &[(name, def)], vis, ImportType::Named); } } diff --git a/crates/hir_def/src/nameres/tests.rs b/crates/hir_def/src/nameres/tests.rs index de3aa4f9a..4f2e7a2f9 100644 --- a/crates/hir_def/src/nameres/tests.rs +++ b/crates/hir_def/src/nameres/tests.rs @@ -713,3 +713,22 @@ pub fn f() {} "#]], ); } + +#[test] +fn use_crate_as() { + check( + r#" +use crate as foo; + +use foo::bar as baz; + +fn bar() {} + "#, + expect![[r#" + crate + bar: v + baz: v + foo: t + "#]], + ); +} -- cgit v1.2.3