Skip to content

Commit 2d680ff

Browse files
bors[bot]matklad
andcommitted
Merge #1089
1089: fix a panic with glob-import missing a source map r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
2 parents bb3b159 + 49f13d3 commit 2d680ff

File tree

5 files changed

+26
-23
lines changed

5 files changed

+26
-23
lines changed

crates/ra_hir/src/code_model_api.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,7 @@ impl Module {
117117
}
118118

119119
/// Returns the syntax of the last path segment corresponding to this import
120-
pub fn import_source(
121-
&self,
122-
db: &impl HirDatabase,
123-
import: ImportId,
124-
) -> TreeArc<ast::PathSegment> {
120+
pub fn import_source(&self, db: &impl HirDatabase, import: ImportId) -> TreeArc<ast::UseTree> {
125121
self.import_source_impl(db, import)
126122
}
127123

crates/ra_hir/src/code_model_impl/module.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl Module {
7272
&self,
7373
db: &impl HirDatabase,
7474
import: ImportId,
75-
) -> TreeArc<ast::PathSegment> {
75+
) -> TreeArc<ast::UseTree> {
7676
let (file_id, source) = self.definition_source(db);
7777
let (_, source_map) = db.raw_items_with_source_map(file_id);
7878
source_map.get(&source, import)

crates/ra_hir/src/nameres/raw.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ pub struct RawItems {
3131

3232
#[derive(Debug, Default, PartialEq, Eq)]
3333
pub struct ImportSourceMap {
34-
map: ArenaMap<ImportId, AstPtr<ast::PathSegment>>,
34+
map: ArenaMap<ImportId, AstPtr<ast::UseTree>>,
3535
}
3636

3737
impl ImportSourceMap {
38-
fn insert(&mut self, import: ImportId, segment: &ast::PathSegment) {
39-
self.map.insert(import, AstPtr::new(segment))
38+
fn insert(&mut self, import: ImportId, use_tree: &ast::UseTree) {
39+
self.map.insert(import, AstPtr::new(use_tree))
4040
}
4141

42-
pub(crate) fn get(&self, source: &ModuleSource, import: ImportId) -> TreeArc<ast::PathSegment> {
42+
pub(crate) fn get(&self, source: &ModuleSource, import: ImportId) -> TreeArc<ast::UseTree> {
4343
let file = match source {
4444
ModuleSource::SourceFile(file) => &*file,
4545
ModuleSource::Module(m) => m.syntax().ancestors().find_map(SourceFile::cast).unwrap(),
@@ -256,17 +256,15 @@ impl RawItemsCollector {
256256
fn add_use_item(&mut self, current_module: Option<Module>, use_item: &ast::UseItem) {
257257
let is_prelude = use_item.has_atom_attr("prelude_import");
258258

259-
Path::expand_use_item(use_item, |path, segment, alias| {
259+
Path::expand_use_item(use_item, |path, use_tree, is_glob, alias| {
260260
let import = self.raw_items.imports.alloc(ImportData {
261261
path,
262262
alias,
263-
is_glob: segment.is_none(),
263+
is_glob,
264264
is_prelude,
265265
is_extern_crate: false,
266266
});
267-
if let Some(segment) = segment {
268-
self.source_map.insert(import, segment)
269-
}
267+
self.source_map.insert(import, use_tree);
270268
self.push_item(current_module, RawItem::Import(import))
271269
})
272270
}

crates/ra_hir/src/path.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl Path {
4646
/// Calls `cb` with all paths, represented by this use item.
4747
pub fn expand_use_item<'a>(
4848
item: &'a ast::UseItem,
49-
mut cb: impl FnMut(Path, Option<&'a ast::PathSegment>, Option<Name>),
49+
mut cb: impl FnMut(Path, &'a ast::UseTree, bool, Option<Name>),
5050
) {
5151
if let Some(tree) = item.use_tree() {
5252
expand_use_tree(None, tree, &mut cb);
@@ -156,7 +156,7 @@ impl From<Name> for Path {
156156
fn expand_use_tree<'a>(
157157
prefix: Option<Path>,
158158
tree: &'a ast::UseTree,
159-
cb: &mut impl FnMut(Path, Option<&'a ast::PathSegment>, Option<Name>),
159+
cb: &mut impl FnMut(Path, &'a ast::UseTree, bool, Option<Name>),
160160
) {
161161
if let Some(use_tree_list) = tree.use_tree_list() {
162162
let prefix = match tree.path() {
@@ -181,18 +181,15 @@ fn expand_use_tree<'a>(
181181
if let Some(segment) = ast_path.segment() {
182182
if segment.kind() == Some(ast::PathSegmentKind::SelfKw) {
183183
if let Some(prefix) = prefix {
184-
cb(prefix, Some(segment), alias);
184+
cb(prefix, tree, false, alias);
185185
return;
186186
}
187187
}
188188
}
189189
}
190190
if let Some(path) = convert_path(prefix, ast_path) {
191-
if tree.has_star() {
192-
cb(path, None, alias)
193-
} else if let Some(segment) = ast_path.segment() {
194-
cb(path, Some(segment), alias)
195-
};
191+
let is_glob = tree.has_star();
192+
cb(path, tree, is_glob, alias)
196193
}
197194
// FIXME: report errors somewhere
198195
// We get here if we do

crates/ra_ide_api/src/completion/complete_path.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,18 @@ mod tests {
7272
assert!(completions.is_empty());
7373
}
7474

75+
#[test]
76+
fn dont_complete_current_use_in_braces_with_glob() {
77+
let completions = do_completion(
78+
r"
79+
mod foo { pub struct S; }
80+
use self::{foo::*, bar<|>};
81+
",
82+
CompletionKind::Reference,
83+
);
84+
assert_eq!(completions.len(), 2);
85+
}
86+
7587
#[test]
7688
fn completes_mod_with_docs() {
7789
check_reference_completion(

0 commit comments

Comments
 (0)