From 44f4510caa6becafc3621253e8115d94b6bd4423 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Wed, 16 Sep 2020 12:35:09 +0200 Subject: [PATCH] Store `Import` indices for later reconstruction --- crates/hir_def/src/item_tree.rs | 6 +++++- crates/hir_def/src/item_tree/lower.rs | 3 ++- crates/hir_def/src/item_tree/tests.rs | 4 ++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/crates/hir_def/src/item_tree.rs b/crates/hir_def/src/item_tree.rs index e14722caebb..f02cfb0c692 100644 --- a/crates/hir_def/src/item_tree.rs +++ b/crates/hir_def/src/item_tree.rs @@ -291,7 +291,6 @@ pub enum AttrOwner { Variant(Idx), Field(Idx), - // FIXME: Store variant and field attrs, and stop reparsing them in `attrs_query`. } macro_rules! from_attrs { @@ -483,6 +482,11 @@ pub struct Import { /// AST ID of the `use` or `extern crate` item this import was derived from. Note that many /// `Import`s can map to the same `use` item. pub ast_id: FileAstId, + /// Index of this `Import` when the containing `Use` is visited via `ModPath::expand_use_item`. + /// + /// This can be used to get the `UseTree` this `Import` corresponds to and allows emitting + /// precise diagnostics. + pub index: usize, } #[derive(Debug, Clone, Eq, PartialEq)] diff --git a/crates/hir_def/src/item_tree/lower.rs b/crates/hir_def/src/item_tree/lower.rs index 6a503d7853e..9160bfafe83 100644 --- a/crates/hir_def/src/item_tree/lower.rs +++ b/crates/hir_def/src/item_tree/lower.rs @@ -482,7 +482,7 @@ fn lower_use(&mut self, use_item: &ast::Use) -> Vec> { ModPath::expand_use_item( InFile::new(self.file, use_item.clone()), &self.hygiene, - |path, _tree, is_glob, alias| { + |path, _use_tree, is_glob, alias| { imports.push(id(tree.imports.alloc(Import { path, alias, @@ -490,6 +490,7 @@ fn lower_use(&mut self, use_item: &ast::Use) -> Vec> { is_glob, is_prelude, ast_id, + index: imports.len(), }))); }, ); diff --git a/crates/hir_def/src/item_tree/tests.rs b/crates/hir_def/src/item_tree/tests.rs index 620e697d4a1..d26c032b714 100644 --- a/crates/hir_def/src/item_tree/tests.rs +++ b/crates/hir_def/src/item_tree/tests.rs @@ -228,9 +228,9 @@ union Un { top-level items: #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("attr_on_use"))] }, input: None }]) }] - Import { path: ModPath { kind: Plain, segments: [Name(Text("a"))] }, alias: None, visibility: RawVisibilityId("pub(self)"), is_glob: false, is_prelude: false, ast_id: FileAstId::(0) } + Import { path: ModPath { kind: Plain, segments: [Name(Text("a"))] }, alias: None, visibility: RawVisibilityId("pub(self)"), is_glob: false, is_prelude: false, ast_id: FileAstId::(0), index: 0 } #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("attr_on_use"))] }, input: None }]) }] - Import { path: ModPath { kind: Plain, segments: [Name(Text("b"))] }, alias: None, visibility: RawVisibilityId("pub(self)"), is_glob: true, is_prelude: false, ast_id: FileAstId::(0) } + Import { path: ModPath { kind: Plain, segments: [Name(Text("b"))] }, alias: None, visibility: RawVisibilityId("pub(self)"), is_glob: true, is_prelude: false, ast_id: FileAstId::(0), index: 1 } #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("ext_crate"))] }, input: None }]) }] ExternCrate { path: ModPath { kind: Plain, segments: [Name(Text("krate"))] }, alias: None, visibility: RawVisibilityId("pub(self)"), is_macro_use: false, ast_id: FileAstId::(1) } #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("on_trait"))] }, input: None }]) }]