From c51a6a7bdddf885cb833889a4550150d839eefdc Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 16 Mar 2019 18:06:45 +0300 Subject: fix error on wrong path --- crates/ra_hir/src/marks.rs | 1 + crates/ra_hir/src/nameres/collector.rs | 24 ++++++++++++++---------- crates/ra_hir/src/nameres/tests.rs | 26 ++++++++++++++++++++++++++ crates/test_utils/src/marks.rs | 4 ++-- 4 files changed, 43 insertions(+), 12 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir/src/marks.rs b/crates/ra_hir/src/marks.rs index e8f3005f1..bbf57004d 100644 --- a/crates/ra_hir/src/marks.rs +++ b/crates/ra_hir/src/marks.rs @@ -1,4 +1,5 @@ test_utils::marks!( + bogus_paths name_res_works_for_broken_modules can_import_enum_variant type_var_cycles_resolve_completely diff --git a/crates/ra_hir/src/nameres/collector.rs b/crates/ra_hir/src/nameres/collector.rs index cbe850ba4..e7afc34c9 100644 --- a/crates/ra_hir/src/nameres/collector.rs +++ b/crates/ra_hir/src/nameres/collector.rs @@ -213,18 +213,22 @@ where } } } else { - let last_segment = import.path.segments.last().unwrap(); - let name = import.alias.clone().unwrap_or_else(|| last_segment.name.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) = def.take_types() { - self.def_map.extern_prelude.insert(name.clone(), def); + match import.path.segments.last() { + Some(last_segment) => { + let name = import.alias.clone().unwrap_or_else(|| last_segment.name.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) = def.take_types() { + self.def_map.extern_prelude.insert(name.clone(), def); + } + } + let resolution = Resolution { def, import: Some(import_id) }; + self.update(module_id, Some(import_id), &[(name, resolution)]); } + None => tested_by!(bogus_paths), } - let resolution = Resolution { def, import: Some(import_id) }; - self.update(module_id, Some(import_id), &[(name, resolution)]); } } diff --git a/crates/ra_hir/src/nameres/tests.rs b/crates/ra_hir/src/nameres/tests.rs index 36c1d74ce..f73f9d8a6 100644 --- a/crates/ra_hir/src/nameres/tests.rs +++ b/crates/ra_hir/src/nameres/tests.rs @@ -96,6 +96,32 @@ E: t ) } +#[test] +fn bogus_paths() { + covers!(bogus_paths); + let map = def_map( + " + //- /lib.rs + mod foo; + struct S; + use self; + + //- /foo/mod.rs + use super; + use crate; + + ", + ); + assert_snapshot_matches!(map, @r###" +crate +foo: t +S: t v + +crate::foo +"### + ) +} + #[test] fn use_as() { let map = def_map( diff --git a/crates/test_utils/src/marks.rs b/crates/test_utils/src/marks.rs index 107432926..1c268b49c 100644 --- a/crates/test_utils/src/marks.rs +++ b/crates/test_utils/src/marks.rs @@ -30,13 +30,13 @@ use std::sync::atomic::{AtomicUsize, Ordering}; #[macro_export] macro_rules! tested_by { - ($ident:ident) => { + ($ident:ident) => {{ #[cfg(test)] { // sic! use call-site crate crate::marks::$ident.fetch_add(1, std::sync::atomic::Ordering::SeqCst); } - }; + }}; } #[macro_export] -- cgit v1.2.3