From 49f13d3a9bb5bf3ab92c3fbf23ad79cb001b76e0 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 2 Apr 2019 13:26:09 +0300 Subject: fix a panic with glob-import missing a source map --- crates/ra_hir/src/code_model_api.rs | 6 +----- crates/ra_hir/src/code_model_impl/module.rs | 2 +- crates/ra_hir/src/nameres/raw.rs | 16 +++++++--------- crates/ra_hir/src/path.rs | 13 +++++-------- crates/ra_ide_api/src/completion/complete_path.rs | 12 ++++++++++++ 5 files changed, 26 insertions(+), 23 deletions(-) diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index 624c25c4d..46455bd83 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs @@ -117,11 +117,7 @@ impl Module { } /// Returns the syntax of the last path segment corresponding to this import - pub fn import_source( - &self, - db: &impl HirDatabase, - import: ImportId, - ) -> TreeArc { + pub fn import_source(&self, db: &impl HirDatabase, import: ImportId) -> TreeArc { self.import_source_impl(db, import) } diff --git a/crates/ra_hir/src/code_model_impl/module.rs b/crates/ra_hir/src/code_model_impl/module.rs index 0edb8ade5..ccca2b7e5 100644 --- a/crates/ra_hir/src/code_model_impl/module.rs +++ b/crates/ra_hir/src/code_model_impl/module.rs @@ -72,7 +72,7 @@ impl Module { &self, db: &impl HirDatabase, import: ImportId, - ) -> TreeArc { + ) -> TreeArc { let (file_id, source) = self.definition_source(db); let (_, source_map) = db.raw_items_with_source_map(file_id); source_map.get(&source, import) diff --git a/crates/ra_hir/src/nameres/raw.rs b/crates/ra_hir/src/nameres/raw.rs index 0936229ac..35cbe6655 100644 --- a/crates/ra_hir/src/nameres/raw.rs +++ b/crates/ra_hir/src/nameres/raw.rs @@ -31,15 +31,15 @@ pub struct RawItems { #[derive(Debug, Default, PartialEq, Eq)] pub struct ImportSourceMap { - map: ArenaMap>, + map: ArenaMap>, } impl ImportSourceMap { - fn insert(&mut self, import: ImportId, segment: &ast::PathSegment) { - self.map.insert(import, AstPtr::new(segment)) + fn insert(&mut self, import: ImportId, use_tree: &ast::UseTree) { + self.map.insert(import, AstPtr::new(use_tree)) } - pub(crate) fn get(&self, source: &ModuleSource, import: ImportId) -> TreeArc { + pub(crate) fn get(&self, source: &ModuleSource, import: ImportId) -> TreeArc { let file = match source { ModuleSource::SourceFile(file) => &*file, ModuleSource::Module(m) => m.syntax().ancestors().find_map(SourceFile::cast).unwrap(), @@ -256,17 +256,15 @@ impl RawItemsCollector { fn add_use_item(&mut self, current_module: Option, use_item: &ast::UseItem) { let is_prelude = use_item.has_atom_attr("prelude_import"); - Path::expand_use_item(use_item, |path, segment, alias| { + Path::expand_use_item(use_item, |path, use_tree, is_glob, alias| { let import = self.raw_items.imports.alloc(ImportData { path, alias, - is_glob: segment.is_none(), + is_glob, is_prelude, is_extern_crate: false, }); - if let Some(segment) = segment { - self.source_map.insert(import, segment) - } + self.source_map.insert(import, use_tree); self.push_item(current_module, RawItem::Import(import)) }) } diff --git a/crates/ra_hir/src/path.rs b/crates/ra_hir/src/path.rs index 6cc8104f4..5449cddfd 100644 --- a/crates/ra_hir/src/path.rs +++ b/crates/ra_hir/src/path.rs @@ -46,7 +46,7 @@ impl Path { /// Calls `cb` with all paths, represented by this use item. pub fn expand_use_item<'a>( item: &'a ast::UseItem, - mut cb: impl FnMut(Path, Option<&'a ast::PathSegment>, Option), + mut cb: impl FnMut(Path, &'a ast::UseTree, bool, Option), ) { if let Some(tree) = item.use_tree() { expand_use_tree(None, tree, &mut cb); @@ -156,7 +156,7 @@ impl From for Path { fn expand_use_tree<'a>( prefix: Option, tree: &'a ast::UseTree, - cb: &mut impl FnMut(Path, Option<&'a ast::PathSegment>, Option), + cb: &mut impl FnMut(Path, &'a ast::UseTree, bool, Option), ) { if let Some(use_tree_list) = tree.use_tree_list() { let prefix = match tree.path() { @@ -181,18 +181,15 @@ fn expand_use_tree<'a>( if let Some(segment) = ast_path.segment() { if segment.kind() == Some(ast::PathSegmentKind::SelfKw) { if let Some(prefix) = prefix { - cb(prefix, Some(segment), alias); + cb(prefix, tree, false, alias); return; } } } } if let Some(path) = convert_path(prefix, ast_path) { - if tree.has_star() { - cb(path, None, alias) - } else if let Some(segment) = ast_path.segment() { - cb(path, Some(segment), alias) - }; + let is_glob = tree.has_star(); + cb(path, tree, is_glob, alias) } // FIXME: report errors somewhere // We get here if we do diff --git a/crates/ra_ide_api/src/completion/complete_path.rs b/crates/ra_ide_api/src/completion/complete_path.rs index 5ff1b9927..122dd7bdd 100644 --- a/crates/ra_ide_api/src/completion/complete_path.rs +++ b/crates/ra_ide_api/src/completion/complete_path.rs @@ -72,6 +72,18 @@ mod tests { assert!(completions.is_empty()); } + #[test] + fn dont_complete_current_use_in_braces_with_glob() { + let completions = do_completion( + r" + mod foo { pub struct S; } + use self::{foo::*, bar<|>}; + ", + CompletionKind::Reference, + ); + assert_eq!(completions.len(), 2); + } + #[test] fn completes_mod_with_docs() { check_reference_completion( -- cgit v1.2.3