diff options
Diffstat (limited to 'crates/hir_def')
-rw-r--r-- | crates/hir_def/src/attr.rs | 4 | ||||
-rw-r--r-- | crates/hir_def/src/body.rs | 26 | ||||
-rw-r--r-- | crates/hir_def/src/body/lower.rs | 42 | ||||
-rw-r--r-- | crates/hir_def/src/body/tests.rs | 10 | ||||
-rw-r--r-- | crates/hir_def/src/body/tests/block.rs | 24 | ||||
-rw-r--r-- | crates/hir_def/src/diagnostics.rs | 5 | ||||
-rw-r--r-- | crates/hir_def/src/find_path.rs | 695 | ||||
-rw-r--r-- | crates/hir_def/src/item_tree.rs | 12 | ||||
-rw-r--r-- | crates/hir_def/src/lib.rs | 40 | ||||
-rw-r--r-- | crates/hir_def/src/nameres.rs | 27 | ||||
-rw-r--r-- | crates/hir_def/src/nameres/collector.rs | 9 | ||||
-rw-r--r-- | crates/hir_def/src/nameres/path_resolution.rs | 8 | ||||
-rw-r--r-- | crates/hir_def/src/nameres/tests/diagnostics.rs | 4 | ||||
-rw-r--r-- | crates/hir_def/src/path.rs | 7 | ||||
-rw-r--r-- | crates/hir_def/src/path/lower.rs | 17 | ||||
-rw-r--r-- | crates/hir_def/src/test_db.rs | 51 | ||||
-rw-r--r-- | crates/hir_def/src/type_ref.rs | 18 | ||||
-rw-r--r-- | crates/hir_def/src/visibility.rs | 10 |
18 files changed, 675 insertions, 334 deletions
diff --git a/crates/hir_def/src/attr.rs b/crates/hir_def/src/attr.rs index 786fad6e1..d9294d93a 100644 --- a/crates/hir_def/src/attr.rs +++ b/crates/hir_def/src/attr.rs | |||
@@ -545,7 +545,7 @@ fn inner_attributes( | |||
545 | _ => return None, | 545 | _ => return None, |
546 | } | 546 | } |
547 | }; | 547 | }; |
548 | let attrs = attrs.filter(|attr| attr.excl_token().is_some()); | 548 | let attrs = attrs.filter(|attr| attr.kind().is_inner()); |
549 | let docs = docs.filter(|doc| doc.is_inner()); | 549 | let docs = docs.filter(|doc| doc.is_inner()); |
550 | Some((attrs, docs)) | 550 | Some((attrs, docs)) |
551 | } | 551 | } |
@@ -740,7 +740,7 @@ fn collect_attrs( | |||
740 | let (inner_attrs, inner_docs) = inner_attributes(owner.syntax()) | 740 | let (inner_attrs, inner_docs) = inner_attributes(owner.syntax()) |
741 | .map_or((None, None), |(attrs, docs)| (Some(attrs), Some(docs))); | 741 | .map_or((None, None), |(attrs, docs)| (Some(attrs), Some(docs))); |
742 | 742 | ||
743 | let outer_attrs = owner.attrs().filter(|attr| attr.excl_token().is_none()); | 743 | let outer_attrs = owner.attrs().filter(|attr| attr.kind().is_outer()); |
744 | let attrs = outer_attrs | 744 | let attrs = outer_attrs |
745 | .chain(inner_attrs.into_iter().flatten()) | 745 | .chain(inner_attrs.into_iter().flatten()) |
746 | .map(|attr| (attr.syntax().text_range().start(), Either::Left(attr))); | 746 | .map(|attr| (attr.syntax().text_range().start(), Either::Left(attr))); |
diff --git a/crates/hir_def/src/body.rs b/crates/hir_def/src/body.rs index 96b959967..131f424cc 100644 --- a/crates/hir_def/src/body.rs +++ b/crates/hir_def/src/body.rs | |||
@@ -21,7 +21,7 @@ use profile::Count; | |||
21 | use rustc_hash::FxHashMap; | 21 | use rustc_hash::FxHashMap; |
22 | use syntax::{ast, AstNode, AstPtr}; | 22 | use syntax::{ast, AstNode, AstPtr}; |
23 | 23 | ||
24 | pub(crate) use lower::LowerCtx; | 24 | pub use lower::LowerCtx; |
25 | 25 | ||
26 | use crate::{ | 26 | use crate::{ |
27 | attr::{Attrs, RawAttrs}, | 27 | attr::{Attrs, RawAttrs}, |
@@ -37,13 +37,15 @@ use crate::{ | |||
37 | 37 | ||
38 | /// A subset of Expander that only deals with cfg attributes. We only need it to | 38 | /// A subset of Expander that only deals with cfg attributes. We only need it to |
39 | /// avoid cyclic queries in crate def map during enum processing. | 39 | /// avoid cyclic queries in crate def map during enum processing. |
40 | #[derive(Debug)] | ||
40 | pub(crate) struct CfgExpander { | 41 | pub(crate) struct CfgExpander { |
41 | cfg_options: CfgOptions, | 42 | cfg_options: CfgOptions, |
42 | hygiene: Hygiene, | 43 | hygiene: Hygiene, |
43 | krate: CrateId, | 44 | krate: CrateId, |
44 | } | 45 | } |
45 | 46 | ||
46 | pub(crate) struct Expander { | 47 | #[derive(Debug)] |
48 | pub struct Expander { | ||
47 | cfg_expander: CfgExpander, | 49 | cfg_expander: CfgExpander, |
48 | def_map: Arc<DefMap>, | 50 | def_map: Arc<DefMap>, |
49 | current_file_id: HirFileId, | 51 | current_file_id: HirFileId, |
@@ -80,11 +82,7 @@ impl CfgExpander { | |||
80 | } | 82 | } |
81 | 83 | ||
82 | impl Expander { | 84 | impl Expander { |
83 | pub(crate) fn new( | 85 | pub fn new(db: &dyn DefDatabase, current_file_id: HirFileId, module: ModuleId) -> Expander { |
84 | db: &dyn DefDatabase, | ||
85 | current_file_id: HirFileId, | ||
86 | module: ModuleId, | ||
87 | ) -> Expander { | ||
88 | let cfg_expander = CfgExpander::new(db, current_file_id, module.krate); | 86 | let cfg_expander = CfgExpander::new(db, current_file_id, module.krate); |
89 | let def_map = module.def_map(db); | 87 | let def_map = module.def_map(db); |
90 | let ast_id_map = db.ast_id_map(current_file_id); | 88 | let ast_id_map = db.ast_id_map(current_file_id); |
@@ -98,7 +96,7 @@ impl Expander { | |||
98 | } | 96 | } |
99 | } | 97 | } |
100 | 98 | ||
101 | pub(crate) fn enter_expand<T: ast::AstNode>( | 99 | pub fn enter_expand<T: ast::AstNode>( |
102 | &mut self, | 100 | &mut self, |
103 | db: &dyn DefDatabase, | 101 | db: &dyn DefDatabase, |
104 | macro_call: ast::MacroCall, | 102 | macro_call: ast::MacroCall, |
@@ -170,7 +168,7 @@ impl Expander { | |||
170 | Ok(ExpandResult { value: Some((mark, node)), err }) | 168 | Ok(ExpandResult { value: Some((mark, node)), err }) |
171 | } | 169 | } |
172 | 170 | ||
173 | pub(crate) fn exit(&mut self, db: &dyn DefDatabase, mut mark: Mark) { | 171 | pub fn exit(&mut self, db: &dyn DefDatabase, mut mark: Mark) { |
174 | self.cfg_expander.hygiene = Hygiene::new(db.upcast(), mark.file_id); | 172 | self.cfg_expander.hygiene = Hygiene::new(db.upcast(), mark.file_id); |
175 | self.current_file_id = mark.file_id; | 173 | self.current_file_id = mark.file_id; |
176 | self.ast_id_map = mem::take(&mut mark.ast_id_map); | 174 | self.ast_id_map = mem::take(&mut mark.ast_id_map); |
@@ -190,8 +188,13 @@ impl Expander { | |||
190 | &self.cfg_expander.cfg_options | 188 | &self.cfg_expander.cfg_options |
191 | } | 189 | } |
192 | 190 | ||
191 | pub fn current_file_id(&self) -> HirFileId { | ||
192 | self.current_file_id | ||
193 | } | ||
194 | |||
193 | fn parse_path(&mut self, path: ast::Path) -> Option<Path> { | 195 | fn parse_path(&mut self, path: ast::Path) -> Option<Path> { |
194 | Path::from_src(path, &self.cfg_expander.hygiene) | 196 | let ctx = LowerCtx::with_hygiene(&self.cfg_expander.hygiene); |
197 | Path::from_src(path, &ctx) | ||
195 | } | 198 | } |
196 | 199 | ||
197 | fn resolve_path_as_macro(&self, db: &dyn DefDatabase, path: &ModPath) -> Option<MacroDefId> { | 200 | fn resolve_path_as_macro(&self, db: &dyn DefDatabase, path: &ModPath) -> Option<MacroDefId> { |
@@ -204,7 +207,8 @@ impl Expander { | |||
204 | } | 207 | } |
205 | } | 208 | } |
206 | 209 | ||
207 | pub(crate) struct Mark { | 210 | #[derive(Debug)] |
211 | pub struct Mark { | ||
208 | file_id: HirFileId, | 212 | file_id: HirFileId, |
209 | ast_id_map: Arc<AstIdMap>, | 213 | ast_id_map: Arc<AstIdMap>, |
210 | bomb: DropBomb, | 214 | bomb: DropBomb, |
diff --git a/crates/hir_def/src/body/lower.rs b/crates/hir_def/src/body/lower.rs index ed07d6928..c11da30d2 100644 --- a/crates/hir_def/src/body/lower.rs +++ b/crates/hir_def/src/body/lower.rs | |||
@@ -1,10 +1,11 @@ | |||
1 | //! Transforms `ast::Expr` into an equivalent `hir_def::expr::Expr` | 1 | //! Transforms `ast::Expr` into an equivalent `hir_def::expr::Expr` |
2 | //! representation. | 2 | //! representation. |
3 | 3 | ||
4 | use std::mem; | 4 | use std::{mem, sync::Arc}; |
5 | 5 | ||
6 | use either::Either; | 6 | use either::Either; |
7 | use hir_expand::{ | 7 | use hir_expand::{ |
8 | ast_id_map::{AstIdMap, FileAstId}, | ||
8 | hygiene::Hygiene, | 9 | hygiene::Hygiene, |
9 | name::{name, AsName, Name}, | 10 | name::{name, AsName, Name}, |
10 | ExpandError, HirFileId, | 11 | ExpandError, HirFileId, |
@@ -39,20 +40,39 @@ use crate::{ | |||
39 | 40 | ||
40 | use super::{diagnostics::BodyDiagnostic, ExprSource, PatSource}; | 41 | use super::{diagnostics::BodyDiagnostic, ExprSource, PatSource}; |
41 | 42 | ||
42 | pub(crate) struct LowerCtx { | 43 | pub struct LowerCtx { |
43 | hygiene: Hygiene, | 44 | hygiene: Hygiene, |
45 | file_id: Option<HirFileId>, | ||
46 | source_ast_id_map: Option<Arc<AstIdMap>>, | ||
44 | } | 47 | } |
45 | 48 | ||
46 | impl LowerCtx { | 49 | impl LowerCtx { |
47 | pub(crate) fn new(db: &dyn DefDatabase, file_id: HirFileId) -> Self { | 50 | pub fn new(db: &dyn DefDatabase, file_id: HirFileId) -> Self { |
48 | LowerCtx { hygiene: Hygiene::new(db.upcast(), file_id) } | 51 | LowerCtx { |
52 | hygiene: Hygiene::new(db.upcast(), file_id), | ||
53 | file_id: Some(file_id), | ||
54 | source_ast_id_map: Some(db.ast_id_map(file_id)), | ||
55 | } | ||
56 | } | ||
57 | |||
58 | pub fn with_hygiene(hygiene: &Hygiene) -> Self { | ||
59 | LowerCtx { hygiene: hygiene.clone(), file_id: None, source_ast_id_map: None } | ||
60 | } | ||
61 | |||
62 | pub(crate) fn hygiene(&self) -> &Hygiene { | ||
63 | &self.hygiene | ||
49 | } | 64 | } |
50 | pub(crate) fn with_hygiene(hygiene: &Hygiene) -> Self { | 65 | |
51 | LowerCtx { hygiene: hygiene.clone() } | 66 | pub(crate) fn file_id(&self) -> HirFileId { |
67 | self.file_id.unwrap() | ||
52 | } | 68 | } |
53 | 69 | ||
54 | pub(crate) fn lower_path(&self, ast: ast::Path) -> Option<Path> { | 70 | pub(crate) fn lower_path(&self, ast: ast::Path) -> Option<Path> { |
55 | Path::from_src(ast, &self.hygiene) | 71 | Path::from_src(ast, self) |
72 | } | ||
73 | |||
74 | pub(crate) fn ast_id<N: AstNode>(&self, item: &N) -> Option<FileAstId<N>> { | ||
75 | self.source_ast_id_map.as_ref().map(|ast_id_map| ast_id_map.ast_id(item)) | ||
56 | } | 76 | } |
57 | } | 77 | } |
58 | 78 | ||
@@ -568,9 +588,13 @@ impl ExprCollector<'_> { | |||
568 | 588 | ||
569 | let res = match res { | 589 | let res = match res { |
570 | Ok(res) => res, | 590 | Ok(res) => res, |
571 | Err(UnresolvedMacro) => { | 591 | Err(UnresolvedMacro { path }) => { |
572 | self.source_map.diagnostics.push(BodyDiagnostic::UnresolvedMacroCall( | 592 | self.source_map.diagnostics.push(BodyDiagnostic::UnresolvedMacroCall( |
573 | UnresolvedMacroCall { file: outer_file, node: syntax_ptr.cast().unwrap() }, | 593 | UnresolvedMacroCall { |
594 | file: outer_file, | ||
595 | node: syntax_ptr.cast().unwrap(), | ||
596 | path, | ||
597 | }, | ||
574 | )); | 598 | )); |
575 | collector(self, None); | 599 | collector(self, None); |
576 | return; | 600 | return; |
diff --git a/crates/hir_def/src/body/tests.rs b/crates/hir_def/src/body/tests.rs index c1d3e998f..3e8f16306 100644 --- a/crates/hir_def/src/body/tests.rs +++ b/crates/hir_def/src/body/tests.rs | |||
@@ -40,6 +40,14 @@ fn block_def_map_at(ra_fixture: &str) -> String { | |||
40 | module.def_map(&db).dump(&db) | 40 | module.def_map(&db).dump(&db) |
41 | } | 41 | } |
42 | 42 | ||
43 | fn check_block_scopes_at(ra_fixture: &str, expect: Expect) { | ||
44 | let (db, position) = crate::test_db::TestDB::with_position(ra_fixture); | ||
45 | |||
46 | let module = db.module_at_position(position); | ||
47 | let actual = module.def_map(&db).dump_block_scopes(&db); | ||
48 | expect.assert_eq(&actual); | ||
49 | } | ||
50 | |||
43 | fn check_at(ra_fixture: &str, expect: Expect) { | 51 | fn check_at(ra_fixture: &str, expect: Expect) { |
44 | let actual = block_def_map_at(ra_fixture); | 52 | let actual = block_def_map_at(ra_fixture); |
45 | expect.assert_eq(&actual); | 53 | expect.assert_eq(&actual); |
@@ -180,7 +188,7 @@ fn unresolved_macro_diag() { | |||
180 | r#" | 188 | r#" |
181 | fn f() { | 189 | fn f() { |
182 | m!(); | 190 | m!(); |
183 | //^^^^ unresolved macro call | 191 | //^^^^ unresolved macro `m!` |
184 | } | 192 | } |
185 | "#, | 193 | "#, |
186 | ); | 194 | ); |
diff --git a/crates/hir_def/src/body/tests/block.rs b/crates/hir_def/src/body/tests/block.rs index 3b6ba4cde..bc3d0f138 100644 --- a/crates/hir_def/src/body/tests/block.rs +++ b/crates/hir_def/src/body/tests/block.rs | |||
@@ -134,6 +134,30 @@ struct Struct {} | |||
134 | } | 134 | } |
135 | 135 | ||
136 | #[test] | 136 | #[test] |
137 | fn nested_module_scoping() { | ||
138 | check_block_scopes_at( | ||
139 | r#" | ||
140 | fn f() { | ||
141 | mod module { | ||
142 | struct Struct {} | ||
143 | fn f() { | ||
144 | use self::Struct; | ||
145 | $0 | ||
146 | } | ||
147 | } | ||
148 | } | ||
149 | "#, | ||
150 | expect![[r#" | ||
151 | BlockId(1) in ModuleId { krate: CrateId(0), block: Some(BlockId(0)), local_id: Idx::<ModuleData>(0) } | ||
152 | BlockId(0) in ModuleId { krate: CrateId(0), block: None, local_id: Idx::<ModuleData>(0) } | ||
153 | crate scope | ||
154 | "#]], | ||
155 | ); | ||
156 | // FIXME: The module nesting here is wrong! | ||
157 | // The first block map should be located in module #1 (`mod module`), not #0 (BlockId(0) root module) | ||
158 | } | ||
159 | |||
160 | #[test] | ||
137 | fn legacy_macro_items() { | 161 | fn legacy_macro_items() { |
138 | // Checks that legacy-scoped `macro_rules!` from parent namespaces are resolved and expanded | 162 | // Checks that legacy-scoped `macro_rules!` from parent namespaces are resolved and expanded |
139 | // correctly. | 163 | // correctly. |
diff --git a/crates/hir_def/src/diagnostics.rs b/crates/hir_def/src/diagnostics.rs index 97abf8653..a71ae2668 100644 --- a/crates/hir_def/src/diagnostics.rs +++ b/crates/hir_def/src/diagnostics.rs | |||
@@ -8,7 +8,7 @@ use hir_expand::diagnostics::{Diagnostic, DiagnosticCode, DiagnosticSink}; | |||
8 | use hir_expand::{HirFileId, InFile}; | 8 | use hir_expand::{HirFileId, InFile}; |
9 | use syntax::{ast, AstPtr, SyntaxNodePtr, TextRange}; | 9 | use syntax::{ast, AstPtr, SyntaxNodePtr, TextRange}; |
10 | 10 | ||
11 | use crate::{db::DefDatabase, DefWithBodyId}; | 11 | use crate::{db::DefDatabase, path::ModPath, DefWithBodyId}; |
12 | 12 | ||
13 | pub fn validate_body(db: &dyn DefDatabase, owner: DefWithBodyId, sink: &mut DiagnosticSink<'_>) { | 13 | pub fn validate_body(db: &dyn DefDatabase, owner: DefWithBodyId, sink: &mut DiagnosticSink<'_>) { |
14 | let source_map = db.body_with_source_map(owner).1; | 14 | let source_map = db.body_with_source_map(owner).1; |
@@ -103,6 +103,7 @@ impl Diagnostic for UnresolvedImport { | |||
103 | pub struct UnresolvedMacroCall { | 103 | pub struct UnresolvedMacroCall { |
104 | pub file: HirFileId, | 104 | pub file: HirFileId, |
105 | pub node: AstPtr<ast::MacroCall>, | 105 | pub node: AstPtr<ast::MacroCall>, |
106 | pub path: ModPath, | ||
106 | } | 107 | } |
107 | 108 | ||
108 | impl Diagnostic for UnresolvedMacroCall { | 109 | impl Diagnostic for UnresolvedMacroCall { |
@@ -110,7 +111,7 @@ impl Diagnostic for UnresolvedMacroCall { | |||
110 | DiagnosticCode("unresolved-macro-call") | 111 | DiagnosticCode("unresolved-macro-call") |
111 | } | 112 | } |
112 | fn message(&self) -> String { | 113 | fn message(&self) -> String { |
113 | "unresolved macro call".to_string() | 114 | format!("unresolved macro `{}!`", self.path) |
114 | } | 115 | } |
115 | fn display_source(&self) -> InFile<SyntaxNodePtr> { | 116 | fn display_source(&self) -> InFile<SyntaxNodePtr> { |
116 | InFile::new(self.file, self.node.clone().into()) | 117 | InFile::new(self.file, self.node.clone().into()) |
diff --git a/crates/hir_def/src/find_path.rs b/crates/hir_def/src/find_path.rs index 109d3552f..c06a37294 100644 --- a/crates/hir_def/src/find_path.rs +++ b/crates/hir_def/src/find_path.rs | |||
@@ -119,8 +119,7 @@ fn find_path_inner( | |||
119 | 119 | ||
120 | // - if the item is the crate root, return `crate` | 120 | // - if the item is the crate root, return `crate` |
121 | let root = def_map.crate_root(db); | 121 | let root = def_map.crate_root(db); |
122 | if item == ItemInNs::Types(ModuleDefId::ModuleId(root)) && def_map.block_id().is_none() { | 122 | if item == ItemInNs::Types(ModuleDefId::ModuleId(root)) { |
123 | // FIXME: the `block_id()` check should be unnecessary, but affects the result | ||
124 | return Some(ModPath::from_segments(PathKind::Crate, Vec::new())); | 123 | return Some(ModPath::from_segments(PathKind::Crate, Vec::new())); |
125 | } | 124 | } |
126 | 125 | ||
@@ -131,7 +130,8 @@ fn find_path_inner( | |||
131 | } | 130 | } |
132 | 131 | ||
133 | // - if the item is the crate root of a dependency crate, return the name from the extern prelude | 132 | // - if the item is the crate root of a dependency crate, return the name from the extern prelude |
134 | for (name, def_id) in def_map.extern_prelude() { | 133 | let root_def_map = root.def_map(db); |
134 | for (name, def_id) in root_def_map.extern_prelude() { | ||
135 | if item == ItemInNs::Types(*def_id) { | 135 | if item == ItemInNs::Types(*def_id) { |
136 | let name = scope_name.unwrap_or_else(|| name.clone()); | 136 | let name = scope_name.unwrap_or_else(|| name.clone()); |
137 | return Some(ModPath::from_segments(PathKind::Plain, vec![name])); | 137 | return Some(ModPath::from_segments(PathKind::Plain, vec![name])); |
@@ -139,7 +139,8 @@ fn find_path_inner( | |||
139 | } | 139 | } |
140 | 140 | ||
141 | // - if the item is in the prelude, return the name from there | 141 | // - if the item is in the prelude, return the name from there |
142 | if let Some(prelude_module) = def_map.prelude() { | 142 | if let Some(prelude_module) = root_def_map.prelude() { |
143 | // Preludes in block DefMaps are ignored, only the crate DefMap is searched | ||
143 | let prelude_def_map = prelude_module.def_map(db); | 144 | let prelude_def_map = prelude_module.def_map(db); |
144 | let prelude_scope: &crate::item_scope::ItemScope = | 145 | let prelude_scope: &crate::item_scope::ItemScope = |
145 | &prelude_def_map[prelude_module.local_id].scope; | 146 | &prelude_def_map[prelude_module.local_id].scope; |
@@ -298,6 +299,7 @@ fn find_local_import_locations( | |||
298 | let data = &def_map[from.local_id]; | 299 | let data = &def_map[from.local_id]; |
299 | let mut worklist = | 300 | let mut worklist = |
300 | data.children.values().map(|child| def_map.module_id(*child)).collect::<Vec<_>>(); | 301 | data.children.values().map(|child| def_map.module_id(*child)).collect::<Vec<_>>(); |
302 | // FIXME: do we need to traverse out of block expressions here? | ||
301 | for ancestor in iter::successors(from.containing_module(db), |m| m.containing_module(db)) { | 303 | for ancestor in iter::successors(from.containing_module(db), |m| m.containing_module(db)) { |
302 | worklist.push(ancestor); | 304 | worklist.push(ancestor); |
303 | } | 305 | } |
@@ -425,106 +427,142 @@ mod tests { | |||
425 | 427 | ||
426 | #[test] | 428 | #[test] |
427 | fn same_module() { | 429 | fn same_module() { |
428 | let code = r#" | 430 | check_found_path( |
429 | //- /main.rs | 431 | r#" |
430 | struct S; | 432 | struct S; |
431 | $0 | 433 | $0 |
432 | "#; | 434 | "#, |
433 | check_found_path(code, "S", "S", "crate::S", "self::S"); | 435 | "S", |
436 | "S", | ||
437 | "crate::S", | ||
438 | "self::S", | ||
439 | ); | ||
434 | } | 440 | } |
435 | 441 | ||
436 | #[test] | 442 | #[test] |
437 | fn enum_variant() { | 443 | fn enum_variant() { |
438 | let code = r#" | 444 | check_found_path( |
439 | //- /main.rs | 445 | r#" |
440 | enum E { A } | 446 | enum E { A } |
441 | $0 | 447 | $0 |
442 | "#; | 448 | "#, |
443 | check_found_path(code, "E::A", "E::A", "E::A", "E::A"); | 449 | "E::A", |
450 | "E::A", | ||
451 | "E::A", | ||
452 | "E::A", | ||
453 | ); | ||
444 | } | 454 | } |
445 | 455 | ||
446 | #[test] | 456 | #[test] |
447 | fn sub_module() { | 457 | fn sub_module() { |
448 | let code = r#" | 458 | check_found_path( |
449 | //- /main.rs | 459 | r#" |
450 | mod foo { | 460 | mod foo { |
451 | pub struct S; | 461 | pub struct S; |
452 | } | 462 | } |
453 | $0 | 463 | $0 |
454 | "#; | 464 | "#, |
455 | check_found_path(code, "foo::S", "foo::S", "crate::foo::S", "self::foo::S"); | 465 | "foo::S", |
466 | "foo::S", | ||
467 | "crate::foo::S", | ||
468 | "self::foo::S", | ||
469 | ); | ||
456 | } | 470 | } |
457 | 471 | ||
458 | #[test] | 472 | #[test] |
459 | fn super_module() { | 473 | fn super_module() { |
460 | let code = r#" | 474 | check_found_path( |
461 | //- /main.rs | 475 | r#" |
462 | mod foo; | 476 | //- /main.rs |
463 | //- /foo.rs | 477 | mod foo; |
464 | mod bar; | 478 | //- /foo.rs |
465 | struct S; | 479 | mod bar; |
466 | //- /foo/bar.rs | 480 | struct S; |
467 | $0 | 481 | //- /foo/bar.rs |
468 | "#; | 482 | $0 |
469 | check_found_path(code, "super::S", "super::S", "crate::foo::S", "super::S"); | 483 | "#, |
484 | "super::S", | ||
485 | "super::S", | ||
486 | "crate::foo::S", | ||
487 | "super::S", | ||
488 | ); | ||
470 | } | 489 | } |
471 | 490 | ||
472 | #[test] | 491 | #[test] |
473 | fn self_module() { | 492 | fn self_module() { |
474 | let code = r#" | 493 | check_found_path( |
475 | //- /main.rs | 494 | r#" |
476 | mod foo; | 495 | //- /main.rs |
477 | //- /foo.rs | 496 | mod foo; |
478 | $0 | 497 | //- /foo.rs |
479 | "#; | 498 | $0 |
480 | check_found_path(code, "self", "self", "crate::foo", "self"); | 499 | "#, |
500 | "self", | ||
501 | "self", | ||
502 | "crate::foo", | ||
503 | "self", | ||
504 | ); | ||
481 | } | 505 | } |
482 | 506 | ||
483 | #[test] | 507 | #[test] |
484 | fn crate_root() { | 508 | fn crate_root() { |
485 | let code = r#" | 509 | check_found_path( |
486 | //- /main.rs | 510 | r#" |
487 | mod foo; | 511 | //- /main.rs |
488 | //- /foo.rs | 512 | mod foo; |
489 | $0 | 513 | //- /foo.rs |
490 | "#; | 514 | $0 |
491 | check_found_path(code, "crate", "crate", "crate", "crate"); | 515 | "#, |
516 | "crate", | ||
517 | "crate", | ||
518 | "crate", | ||
519 | "crate", | ||
520 | ); | ||
492 | } | 521 | } |
493 | 522 | ||
494 | #[test] | 523 | #[test] |
495 | fn same_crate() { | 524 | fn same_crate() { |
496 | let code = r#" | 525 | check_found_path( |
497 | //- /main.rs | 526 | r#" |
498 | mod foo; | 527 | //- /main.rs |
499 | struct S; | 528 | mod foo; |
500 | //- /foo.rs | 529 | struct S; |
501 | $0 | 530 | //- /foo.rs |
502 | "#; | 531 | $0 |
503 | check_found_path(code, "crate::S", "crate::S", "crate::S", "crate::S"); | 532 | "#, |
533 | "crate::S", | ||
534 | "crate::S", | ||
535 | "crate::S", | ||
536 | "crate::S", | ||
537 | ); | ||
504 | } | 538 | } |
505 | 539 | ||
506 | #[test] | 540 | #[test] |
507 | fn different_crate() { | 541 | fn different_crate() { |
508 | let code = r#" | 542 | check_found_path( |
509 | //- /main.rs crate:main deps:std | 543 | r#" |
510 | $0 | 544 | //- /main.rs crate:main deps:std |
511 | //- /std.rs crate:std | 545 | $0 |
512 | pub struct S; | 546 | //- /std.rs crate:std |
513 | "#; | 547 | pub struct S; |
514 | check_found_path(code, "std::S", "std::S", "std::S", "std::S"); | 548 | "#, |
549 | "std::S", | ||
550 | "std::S", | ||
551 | "std::S", | ||
552 | "std::S", | ||
553 | ); | ||
515 | } | 554 | } |
516 | 555 | ||
517 | #[test] | 556 | #[test] |
518 | fn different_crate_renamed() { | 557 | fn different_crate_renamed() { |
519 | let code = r#" | ||
520 | //- /main.rs crate:main deps:std | ||
521 | extern crate std as std_renamed; | ||
522 | $0 | ||
523 | //- /std.rs crate:std | ||
524 | pub struct S; | ||
525 | "#; | ||
526 | check_found_path( | 558 | check_found_path( |
527 | code, | 559 | r#" |
560 | //- /main.rs crate:main deps:std | ||
561 | extern crate std as std_renamed; | ||
562 | $0 | ||
563 | //- /std.rs crate:std | ||
564 | pub struct S; | ||
565 | "#, | ||
528 | "std_renamed::S", | 566 | "std_renamed::S", |
529 | "std_renamed::S", | 567 | "std_renamed::S", |
530 | "std_renamed::S", | 568 | "std_renamed::S", |
@@ -537,41 +575,38 @@ mod tests { | |||
537 | cov_mark::check!(partially_imported); | 575 | cov_mark::check!(partially_imported); |
538 | // Tests that short paths are used even for external items, when parts of the path are | 576 | // Tests that short paths are used even for external items, when parts of the path are |
539 | // already in scope. | 577 | // already in scope. |
540 | let code = r#" | 578 | check_found_path( |
541 | //- /main.rs crate:main deps:syntax | 579 | r#" |
580 | //- /main.rs crate:main deps:syntax | ||
542 | 581 | ||
543 | use syntax::ast; | 582 | use syntax::ast; |
544 | $0 | 583 | $0 |
545 | 584 | ||
546 | //- /lib.rs crate:syntax | 585 | //- /lib.rs crate:syntax |
547 | pub mod ast { | 586 | pub mod ast { |
548 | pub enum ModuleItem { | 587 | pub enum ModuleItem { |
549 | A, B, C, | 588 | A, B, C, |
550 | } | 589 | } |
551 | } | 590 | } |
552 | "#; | 591 | "#, |
553 | check_found_path( | ||
554 | code, | ||
555 | "ast::ModuleItem", | 592 | "ast::ModuleItem", |
556 | "syntax::ast::ModuleItem", | 593 | "syntax::ast::ModuleItem", |
557 | "syntax::ast::ModuleItem", | 594 | "syntax::ast::ModuleItem", |
558 | "syntax::ast::ModuleItem", | 595 | "syntax::ast::ModuleItem", |
559 | ); | 596 | ); |
560 | 597 | ||
561 | let code = r#" | ||
562 | //- /main.rs crate:main deps:syntax | ||
563 | |||
564 | $0 | ||
565 | |||
566 | //- /lib.rs crate:syntax | ||
567 | pub mod ast { | ||
568 | pub enum ModuleItem { | ||
569 | A, B, C, | ||
570 | } | ||
571 | } | ||
572 | "#; | ||
573 | check_found_path( | 598 | check_found_path( |
574 | code, | 599 | r#" |
600 | //- /main.rs crate:main deps:syntax | ||
601 | $0 | ||
602 | |||
603 | //- /lib.rs crate:syntax | ||
604 | pub mod ast { | ||
605 | pub enum ModuleItem { | ||
606 | A, B, C, | ||
607 | } | ||
608 | } | ||
609 | "#, | ||
575 | "syntax::ast::ModuleItem", | 610 | "syntax::ast::ModuleItem", |
576 | "syntax::ast::ModuleItem", | 611 | "syntax::ast::ModuleItem", |
577 | "syntax::ast::ModuleItem", | 612 | "syntax::ast::ModuleItem", |
@@ -581,68 +616,86 @@ mod tests { | |||
581 | 616 | ||
582 | #[test] | 617 | #[test] |
583 | fn same_crate_reexport() { | 618 | fn same_crate_reexport() { |
584 | let code = r#" | 619 | check_found_path( |
585 | //- /main.rs | 620 | r#" |
586 | mod bar { | 621 | mod bar { |
587 | mod foo { pub(super) struct S; } | 622 | mod foo { pub(super) struct S; } |
588 | pub(crate) use foo::*; | 623 | pub(crate) use foo::*; |
589 | } | 624 | } |
590 | $0 | 625 | $0 |
591 | "#; | 626 | "#, |
592 | check_found_path(code, "bar::S", "bar::S", "crate::bar::S", "self::bar::S"); | 627 | "bar::S", |
628 | "bar::S", | ||
629 | "crate::bar::S", | ||
630 | "self::bar::S", | ||
631 | ); | ||
593 | } | 632 | } |
594 | 633 | ||
595 | #[test] | 634 | #[test] |
596 | fn same_crate_reexport_rename() { | 635 | fn same_crate_reexport_rename() { |
597 | let code = r#" | 636 | check_found_path( |
598 | //- /main.rs | 637 | r#" |
599 | mod bar { | 638 | mod bar { |
600 | mod foo { pub(super) struct S; } | 639 | mod foo { pub(super) struct S; } |
601 | pub(crate) use foo::S as U; | 640 | pub(crate) use foo::S as U; |
602 | } | 641 | } |
603 | $0 | 642 | $0 |
604 | "#; | 643 | "#, |
605 | check_found_path(code, "bar::U", "bar::U", "crate::bar::U", "self::bar::U"); | 644 | "bar::U", |
645 | "bar::U", | ||
646 | "crate::bar::U", | ||
647 | "self::bar::U", | ||
648 | ); | ||
606 | } | 649 | } |
607 | 650 | ||
608 | #[test] | 651 | #[test] |
609 | fn different_crate_reexport() { | 652 | fn different_crate_reexport() { |
610 | let code = r#" | 653 | check_found_path( |
611 | //- /main.rs crate:main deps:std | 654 | r#" |
612 | $0 | 655 | //- /main.rs crate:main deps:std |
613 | //- /std.rs crate:std deps:core | 656 | $0 |
614 | pub use core::S; | 657 | //- /std.rs crate:std deps:core |
615 | //- /core.rs crate:core | 658 | pub use core::S; |
616 | pub struct S; | 659 | //- /core.rs crate:core |
617 | "#; | 660 | pub struct S; |
618 | check_found_path(code, "std::S", "std::S", "std::S", "std::S"); | 661 | "#, |
662 | "std::S", | ||
663 | "std::S", | ||
664 | "std::S", | ||
665 | "std::S", | ||
666 | ); | ||
619 | } | 667 | } |
620 | 668 | ||
621 | #[test] | 669 | #[test] |
622 | fn prelude() { | 670 | fn prelude() { |
623 | let code = r#" | 671 | check_found_path( |
624 | //- /main.rs crate:main deps:std | 672 | r#" |
625 | $0 | 673 | //- /main.rs crate:main deps:std |
626 | //- /std.rs crate:std | 674 | $0 |
627 | pub mod prelude { pub struct S; } | 675 | //- /std.rs crate:std |
628 | #[prelude_import] | 676 | pub mod prelude { pub struct S; } |
629 | pub use prelude::*; | 677 | #[prelude_import] |
630 | "#; | 678 | pub use prelude::*; |
631 | check_found_path(code, "S", "S", "S", "S"); | 679 | "#, |
680 | "S", | ||
681 | "S", | ||
682 | "S", | ||
683 | "S", | ||
684 | ); | ||
632 | } | 685 | } |
633 | 686 | ||
634 | #[test] | 687 | #[test] |
635 | fn enum_variant_from_prelude() { | 688 | fn enum_variant_from_prelude() { |
636 | let code = r#" | 689 | let code = r#" |
637 | //- /main.rs crate:main deps:std | 690 | //- /main.rs crate:main deps:std |
638 | $0 | 691 | $0 |
639 | //- /std.rs crate:std | 692 | //- /std.rs crate:std |
640 | pub mod prelude { | 693 | pub mod prelude { |
641 | pub enum Option<T> { Some(T), None } | 694 | pub enum Option<T> { Some(T), None } |
642 | pub use Option::*; | 695 | pub use Option::*; |
643 | } | 696 | } |
644 | #[prelude_import] | 697 | #[prelude_import] |
645 | pub use prelude::*; | 698 | pub use prelude::*; |
646 | "#; | 699 | "#; |
647 | check_found_path(code, "None", "None", "None", "None"); | 700 | check_found_path(code, "None", "None", "None", "None"); |
648 | check_found_path(code, "Some", "Some", "Some", "Some"); | 701 | check_found_path(code, "Some", "Some", "Some", "Some"); |
@@ -650,71 +703,85 @@ mod tests { | |||
650 | 703 | ||
651 | #[test] | 704 | #[test] |
652 | fn shortest_path() { | 705 | fn shortest_path() { |
653 | let code = r#" | 706 | check_found_path( |
654 | //- /main.rs | 707 | r#" |
655 | pub mod foo; | 708 | //- /main.rs |
656 | pub mod baz; | 709 | pub mod foo; |
657 | struct S; | 710 | pub mod baz; |
658 | $0 | 711 | struct S; |
659 | //- /foo.rs | 712 | $0 |
660 | pub mod bar { pub struct S; } | 713 | //- /foo.rs |
661 | //- /baz.rs | 714 | pub mod bar { pub struct S; } |
662 | pub use crate::foo::bar::S; | 715 | //- /baz.rs |
663 | "#; | 716 | pub use crate::foo::bar::S; |
664 | check_found_path(code, "baz::S", "baz::S", "crate::baz::S", "self::baz::S"); | 717 | "#, |
718 | "baz::S", | ||
719 | "baz::S", | ||
720 | "crate::baz::S", | ||
721 | "self::baz::S", | ||
722 | ); | ||
665 | } | 723 | } |
666 | 724 | ||
667 | #[test] | 725 | #[test] |
668 | fn discount_private_imports() { | 726 | fn discount_private_imports() { |
669 | let code = r#" | 727 | check_found_path( |
670 | //- /main.rs | 728 | r#" |
671 | mod foo; | 729 | //- /main.rs |
672 | pub mod bar { pub struct S; } | 730 | mod foo; |
673 | use bar::S; | 731 | pub mod bar { pub struct S; } |
674 | //- /foo.rs | 732 | use bar::S; |
675 | $0 | 733 | //- /foo.rs |
676 | "#; | 734 | $0 |
677 | // crate::S would be shorter, but using private imports seems wrong | 735 | "#, |
678 | check_found_path(code, "crate::bar::S", "crate::bar::S", "crate::bar::S", "crate::bar::S"); | 736 | // crate::S would be shorter, but using private imports seems wrong |
737 | "crate::bar::S", | ||
738 | "crate::bar::S", | ||
739 | "crate::bar::S", | ||
740 | "crate::bar::S", | ||
741 | ); | ||
679 | } | 742 | } |
680 | 743 | ||
681 | #[test] | 744 | #[test] |
682 | fn import_cycle() { | 745 | fn import_cycle() { |
683 | let code = r#" | 746 | check_found_path( |
684 | //- /main.rs | 747 | r#" |
685 | pub mod foo; | 748 | //- /main.rs |
686 | pub mod bar; | 749 | pub mod foo; |
687 | pub mod baz; | 750 | pub mod bar; |
688 | //- /bar.rs | 751 | pub mod baz; |
689 | $0 | 752 | //- /bar.rs |
690 | //- /foo.rs | 753 | $0 |
691 | pub use super::baz; | 754 | //- /foo.rs |
692 | pub struct S; | 755 | pub use super::baz; |
693 | //- /baz.rs | 756 | pub struct S; |
694 | pub use super::foo; | 757 | //- /baz.rs |
695 | "#; | 758 | pub use super::foo; |
696 | check_found_path(code, "crate::foo::S", "crate::foo::S", "crate::foo::S", "crate::foo::S"); | 759 | "#, |
760 | "crate::foo::S", | ||
761 | "crate::foo::S", | ||
762 | "crate::foo::S", | ||
763 | "crate::foo::S", | ||
764 | ); | ||
697 | } | 765 | } |
698 | 766 | ||
699 | #[test] | 767 | #[test] |
700 | fn prefer_std_paths_over_alloc() { | 768 | fn prefer_std_paths_over_alloc() { |
701 | cov_mark::check!(prefer_std_paths); | 769 | cov_mark::check!(prefer_std_paths); |
702 | let code = r#" | 770 | check_found_path( |
703 | //- /main.rs crate:main deps:alloc,std | 771 | r#" |
704 | $0 | 772 | //- /main.rs crate:main deps:alloc,std |
773 | $0 | ||
705 | 774 | ||
706 | //- /std.rs crate:std deps:alloc | 775 | //- /std.rs crate:std deps:alloc |
707 | pub mod sync { | 776 | pub mod sync { |
708 | pub use alloc::sync::Arc; | 777 | pub use alloc::sync::Arc; |
709 | } | 778 | } |
710 | 779 | ||
711 | //- /zzz.rs crate:alloc | 780 | //- /zzz.rs crate:alloc |
712 | pub mod sync { | 781 | pub mod sync { |
713 | pub struct Arc; | 782 | pub struct Arc; |
714 | } | 783 | } |
715 | "#; | 784 | "#, |
716 | check_found_path( | ||
717 | code, | ||
718 | "std::sync::Arc", | 785 | "std::sync::Arc", |
719 | "std::sync::Arc", | 786 | "std::sync::Arc", |
720 | "std::sync::Arc", | 787 | "std::sync::Arc", |
@@ -725,26 +792,25 @@ mod tests { | |||
725 | #[test] | 792 | #[test] |
726 | fn prefer_core_paths_over_std() { | 793 | fn prefer_core_paths_over_std() { |
727 | cov_mark::check!(prefer_no_std_paths); | 794 | cov_mark::check!(prefer_no_std_paths); |
728 | let code = r#" | 795 | check_found_path( |
729 | //- /main.rs crate:main deps:core,std | 796 | r#" |
730 | #![no_std] | 797 | //- /main.rs crate:main deps:core,std |
798 | #![no_std] | ||
731 | 799 | ||
732 | $0 | 800 | $0 |
733 | 801 | ||
734 | //- /std.rs crate:std deps:core | 802 | //- /std.rs crate:std deps:core |
735 | 803 | ||
736 | pub mod fmt { | 804 | pub mod fmt { |
737 | pub use core::fmt::Error; | 805 | pub use core::fmt::Error; |
738 | } | 806 | } |
739 | 807 | ||
740 | //- /zzz.rs crate:core | 808 | //- /zzz.rs crate:core |
741 | 809 | ||
742 | pub mod fmt { | 810 | pub mod fmt { |
743 | pub struct Error; | 811 | pub struct Error; |
744 | } | 812 | } |
745 | "#; | 813 | "#, |
746 | check_found_path( | ||
747 | code, | ||
748 | "core::fmt::Error", | 814 | "core::fmt::Error", |
749 | "core::fmt::Error", | 815 | "core::fmt::Error", |
750 | "core::fmt::Error", | 816 | "core::fmt::Error", |
@@ -754,26 +820,25 @@ mod tests { | |||
754 | 820 | ||
755 | #[test] | 821 | #[test] |
756 | fn prefer_alloc_paths_over_std() { | 822 | fn prefer_alloc_paths_over_std() { |
757 | let code = r#" | 823 | check_found_path( |
758 | //- /main.rs crate:main deps:alloc,std | 824 | r#" |
759 | #![no_std] | 825 | //- /main.rs crate:main deps:alloc,std |
826 | #![no_std] | ||
760 | 827 | ||
761 | $0 | 828 | $0 |
762 | 829 | ||
763 | //- /std.rs crate:std deps:alloc | 830 | //- /std.rs crate:std deps:alloc |
764 | 831 | ||
765 | pub mod sync { | 832 | pub mod sync { |
766 | pub use alloc::sync::Arc; | 833 | pub use alloc::sync::Arc; |
767 | } | 834 | } |
768 | 835 | ||
769 | //- /zzz.rs crate:alloc | 836 | //- /zzz.rs crate:alloc |
770 | 837 | ||
771 | pub mod sync { | 838 | pub mod sync { |
772 | pub struct Arc; | 839 | pub struct Arc; |
773 | } | 840 | } |
774 | "#; | 841 | "#, |
775 | check_found_path( | ||
776 | code, | ||
777 | "alloc::sync::Arc", | 842 | "alloc::sync::Arc", |
778 | "alloc::sync::Arc", | 843 | "alloc::sync::Arc", |
779 | "alloc::sync::Arc", | 844 | "alloc::sync::Arc", |
@@ -783,20 +848,19 @@ mod tests { | |||
783 | 848 | ||
784 | #[test] | 849 | #[test] |
785 | fn prefer_shorter_paths_if_not_alloc() { | 850 | fn prefer_shorter_paths_if_not_alloc() { |
786 | let code = r#" | 851 | check_found_path( |
787 | //- /main.rs crate:main deps:megaalloc,std | 852 | r#" |
788 | $0 | 853 | //- /main.rs crate:main deps:megaalloc,std |
854 | $0 | ||
789 | 855 | ||
790 | //- /std.rs crate:std deps:megaalloc | 856 | //- /std.rs crate:std deps:megaalloc |
791 | pub mod sync { | 857 | pub mod sync { |
792 | pub use megaalloc::sync::Arc; | 858 | pub use megaalloc::sync::Arc; |
793 | } | 859 | } |
794 | 860 | ||
795 | //- /zzz.rs crate:megaalloc | 861 | //- /zzz.rs crate:megaalloc |
796 | pub struct Arc; | 862 | pub struct Arc; |
797 | "#; | 863 | "#, |
798 | check_found_path( | ||
799 | code, | ||
800 | "megaalloc::Arc", | 864 | "megaalloc::Arc", |
801 | "megaalloc::Arc", | 865 | "megaalloc::Arc", |
802 | "megaalloc::Arc", | 866 | "megaalloc::Arc", |
@@ -807,12 +871,11 @@ mod tests { | |||
807 | #[test] | 871 | #[test] |
808 | fn builtins_are_in_scope() { | 872 | fn builtins_are_in_scope() { |
809 | let code = r#" | 873 | let code = r#" |
810 | //- /main.rs | 874 | $0 |
811 | $0 | ||
812 | 875 | ||
813 | pub mod primitive { | 876 | pub mod primitive { |
814 | pub use u8; | 877 | pub use u8; |
815 | } | 878 | } |
816 | "#; | 879 | "#; |
817 | check_found_path(code, "u8", "u8", "u8", "u8"); | 880 | check_found_path(code, "u8", "u8", "u8", "u8"); |
818 | check_found_path(code, "u16", "u16", "u16", "u16"); | 881 | check_found_path(code, "u16", "u16", "u16", "u16"); |
@@ -822,10 +885,10 @@ mod tests { | |||
822 | fn inner_items() { | 885 | fn inner_items() { |
823 | check_found_path( | 886 | check_found_path( |
824 | r#" | 887 | r#" |
825 | fn main() { | 888 | fn main() { |
826 | struct Inner {} | 889 | struct Inner {} |
827 | $0 | 890 | $0 |
828 | } | 891 | } |
829 | "#, | 892 | "#, |
830 | "Inner", | 893 | "Inner", |
831 | "Inner", | 894 | "Inner", |
@@ -838,12 +901,12 @@ mod tests { | |||
838 | fn inner_items_from_outer_scope() { | 901 | fn inner_items_from_outer_scope() { |
839 | check_found_path( | 902 | check_found_path( |
840 | r#" | 903 | r#" |
841 | fn main() { | 904 | fn main() { |
842 | struct Struct {} | 905 | struct Struct {} |
843 | { | 906 | { |
844 | $0 | 907 | $0 |
845 | } | 908 | } |
846 | } | 909 | } |
847 | "#, | 910 | "#, |
848 | "Struct", | 911 | "Struct", |
849 | "Struct", | 912 | "Struct", |
@@ -857,14 +920,14 @@ mod tests { | |||
857 | cov_mark::check!(prefixed_in_block_expression); | 920 | cov_mark::check!(prefixed_in_block_expression); |
858 | check_found_path( | 921 | check_found_path( |
859 | r#" | 922 | r#" |
860 | fn main() { | 923 | fn main() { |
861 | mod module { | 924 | mod module { |
862 | struct Struct {} | 925 | struct Struct {} |
863 | } | 926 | } |
864 | { | 927 | { |
865 | $0 | 928 | $0 |
866 | } | 929 | } |
867 | } | 930 | } |
868 | "#, | 931 | "#, |
869 | "module::Struct", | 932 | "module::Struct", |
870 | "module::Struct", | 933 | "module::Struct", |
@@ -877,19 +940,65 @@ mod tests { | |||
877 | fn outer_items_with_inner_items_present() { | 940 | fn outer_items_with_inner_items_present() { |
878 | check_found_path( | 941 | check_found_path( |
879 | r#" | 942 | r#" |
880 | mod module { | 943 | mod module { |
881 | pub struct CompleteMe; | 944 | pub struct CompleteMe; |
882 | } | 945 | } |
883 | 946 | ||
884 | fn main() { | 947 | fn main() { |
885 | fn inner() {} | 948 | fn inner() {} |
886 | $0 | 949 | $0 |
887 | } | 950 | } |
888 | "#, | 951 | "#, |
952 | // FIXME: these could use fewer/better prefixes | ||
889 | "module::CompleteMe", | 953 | "module::CompleteMe", |
890 | "module::CompleteMe", | ||
891 | "crate::module::CompleteMe", | 954 | "crate::module::CompleteMe", |
892 | "self::module::CompleteMe", | 955 | "crate::module::CompleteMe", |
956 | "crate::module::CompleteMe", | ||
957 | ) | ||
958 | } | ||
959 | |||
960 | #[test] | ||
961 | fn from_inside_module() { | ||
962 | // This worked correctly, but the test suite logic was broken. | ||
963 | cov_mark::check!(submodule_in_testdb); | ||
964 | check_found_path( | ||
965 | r#" | ||
966 | mod baz { | ||
967 | pub struct Foo {} | ||
968 | } | ||
969 | |||
970 | mod bar { | ||
971 | fn bar() { | ||
972 | $0 | ||
973 | } | ||
974 | } | ||
975 | "#, | ||
976 | "crate::baz::Foo", | ||
977 | "crate::baz::Foo", | ||
978 | "crate::baz::Foo", | ||
979 | "crate::baz::Foo", | ||
980 | ) | ||
981 | } | ||
982 | |||
983 | #[test] | ||
984 | fn from_inside_module_with_inner_items() { | ||
985 | check_found_path( | ||
986 | r#" | ||
987 | mod baz { | ||
988 | pub struct Foo {} | ||
989 | } | ||
990 | |||
991 | mod bar { | ||
992 | fn bar() { | ||
993 | fn inner() {} | ||
994 | $0 | ||
995 | } | ||
996 | } | ||
997 | "#, | ||
998 | "crate::baz::Foo", | ||
999 | "crate::baz::Foo", | ||
1000 | "crate::baz::Foo", | ||
1001 | "crate::baz::Foo", | ||
893 | ) | 1002 | ) |
894 | } | 1003 | } |
895 | 1004 | ||
@@ -920,4 +1029,58 @@ pub mod name { | |||
920 | "self::name::AsName", | 1029 | "self::name::AsName", |
921 | ); | 1030 | ); |
922 | } | 1031 | } |
1032 | |||
1033 | #[test] | ||
1034 | fn extern_crate() { | ||
1035 | check_found_path( | ||
1036 | r#" | ||
1037 | //- /main.rs crate:main deps:dep | ||
1038 | $0 | ||
1039 | //- /dep.rs crate:dep | ||
1040 | "#, | ||
1041 | "dep", | ||
1042 | "dep", | ||
1043 | "dep", | ||
1044 | "dep", | ||
1045 | ); | ||
1046 | |||
1047 | check_found_path( | ||
1048 | r#" | ||
1049 | //- /main.rs crate:main deps:dep | ||
1050 | fn f() { | ||
1051 | fn inner() {} | ||
1052 | $0 | ||
1053 | } | ||
1054 | //- /dep.rs crate:dep | ||
1055 | "#, | ||
1056 | "dep", | ||
1057 | "dep", | ||
1058 | "dep", | ||
1059 | "dep", | ||
1060 | ); | ||
1061 | } | ||
1062 | |||
1063 | #[test] | ||
1064 | fn prelude_with_inner_items() { | ||
1065 | check_found_path( | ||
1066 | r#" | ||
1067 | //- /main.rs crate:main deps:std | ||
1068 | fn f() { | ||
1069 | fn inner() {} | ||
1070 | $0 | ||
1071 | } | ||
1072 | //- /std.rs crate:std | ||
1073 | pub mod prelude { | ||
1074 | pub enum Option { None } | ||
1075 | pub use Option::*; | ||
1076 | } | ||
1077 | #[prelude_import] | ||
1078 | pub use prelude::*; | ||
1079 | "#, | ||
1080 | "None", | ||
1081 | "None", | ||
1082 | "None", | ||
1083 | "None", | ||
1084 | ); | ||
1085 | } | ||
923 | } | 1086 | } |
diff --git a/crates/hir_def/src/item_tree.rs b/crates/hir_def/src/item_tree.rs index 94e08f835..eaeca01bd 100644 --- a/crates/hir_def/src/item_tree.rs +++ b/crates/hir_def/src/item_tree.rs | |||
@@ -104,6 +104,11 @@ impl ItemTree { | |||
104 | // items and expanded during block DefMap computation | 104 | // items and expanded during block DefMap computation |
105 | return Default::default(); | 105 | return Default::default(); |
106 | }, | 106 | }, |
107 | ast::Type(ty) => { | ||
108 | // Types can contain inner items. We return an empty item tree in this case, but | ||
109 | // still need to collect inner items. | ||
110 | ctx.lower_inner_items(ty.syntax()) | ||
111 | }, | ||
107 | ast::Expr(e) => { | 112 | ast::Expr(e) => { |
108 | // Macros can expand to expressions. We return an empty item tree in this case, but | 113 | // Macros can expand to expressions. We return an empty item tree in this case, but |
109 | // still need to collect inner items. | 114 | // still need to collect inner items. |
@@ -191,13 +196,6 @@ impl ItemTree { | |||
191 | self.raw_attrs(of).clone().filter(db, krate) | 196 | self.raw_attrs(of).clone().filter(db, krate) |
192 | } | 197 | } |
193 | 198 | ||
194 | pub fn all_inner_items(&self) -> impl Iterator<Item = ModItem> + '_ { | ||
195 | match &self.data { | ||
196 | Some(data) => Some(data.inner_items.values().flatten().copied()).into_iter().flatten(), | ||
197 | None => None.into_iter().flatten(), | ||
198 | } | ||
199 | } | ||
200 | |||
201 | pub fn inner_items_of_block(&self, block: FileAstId<ast::BlockExpr>) -> &[ModItem] { | 199 | pub fn inner_items_of_block(&self, block: FileAstId<ast::BlockExpr>) -> &[ModItem] { |
202 | match &self.data { | 200 | match &self.data { |
203 | Some(data) => data.inner_items.get(&block).map(|it| &**it).unwrap_or(&[]), | 201 | Some(data) => data.inner_items.get(&block).map(|it| &**it).unwrap_or(&[]), |
diff --git a/crates/hir_def/src/lib.rs b/crates/hir_def/src/lib.rs index d69116d51..25694f037 100644 --- a/crates/hir_def/src/lib.rs +++ b/crates/hir_def/src/lib.rs | |||
@@ -66,6 +66,7 @@ use hir_expand::{ | |||
66 | }; | 66 | }; |
67 | use la_arena::Idx; | 67 | use la_arena::Idx; |
68 | use nameres::DefMap; | 68 | use nameres::DefMap; |
69 | use path::ModPath; | ||
69 | use syntax::ast; | 70 | use syntax::ast; |
70 | 71 | ||
71 | use crate::builtin_type::BuiltinType; | 72 | use crate::builtin_type::BuiltinType; |
@@ -107,6 +108,18 @@ impl ModuleId { | |||
107 | pub fn containing_module(&self, db: &dyn db::DefDatabase) -> Option<ModuleId> { | 108 | pub fn containing_module(&self, db: &dyn db::DefDatabase) -> Option<ModuleId> { |
108 | self.def_map(db).containing_module(self.local_id) | 109 | self.def_map(db).containing_module(self.local_id) |
109 | } | 110 | } |
111 | |||
112 | /// Returns `true` if this module represents a block expression. | ||
113 | /// | ||
114 | /// Returns `false` if this module is a submodule *inside* a block expression | ||
115 | /// (eg. `m` in `{ mod m {} }`). | ||
116 | pub fn is_block_root(&self, db: &dyn db::DefDatabase) -> bool { | ||
117 | if self.block.is_none() { | ||
118 | return false; | ||
119 | } | ||
120 | |||
121 | self.def_map(db)[self.local_id].parent.is_none() | ||
122 | } | ||
110 | } | 123 | } |
111 | 124 | ||
112 | /// An ID of a module, **local** to a specific crate | 125 | /// An ID of a module, **local** to a specific crate |
@@ -435,6 +448,16 @@ impl_from!( | |||
435 | for AttrDefId | 448 | for AttrDefId |
436 | ); | 449 | ); |
437 | 450 | ||
451 | impl From<AssocContainerId> for AttrDefId { | ||
452 | fn from(acid: AssocContainerId) -> Self { | ||
453 | match acid { | ||
454 | AssocContainerId::ModuleId(mid) => AttrDefId::ModuleId(mid), | ||
455 | AssocContainerId::ImplId(iid) => AttrDefId::ImplId(iid), | ||
456 | AssocContainerId::TraitId(tid) => AttrDefId::TraitId(tid), | ||
457 | } | ||
458 | } | ||
459 | } | ||
460 | |||
438 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 461 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
439 | pub enum VariantId { | 462 | pub enum VariantId { |
440 | EnumVariantId(EnumVariantId), | 463 | EnumVariantId(EnumVariantId), |
@@ -665,7 +688,10 @@ impl<T: ast::AstNode> AstIdWithPath<T> { | |||
665 | } | 688 | } |
666 | } | 689 | } |
667 | 690 | ||
668 | pub struct UnresolvedMacro; | 691 | #[derive(Debug)] |
692 | pub struct UnresolvedMacro { | ||
693 | pub path: ModPath, | ||
694 | } | ||
669 | 695 | ||
670 | fn macro_call_as_call_id( | 696 | fn macro_call_as_call_id( |
671 | call: &AstIdWithPath<ast::MacroCall>, | 697 | call: &AstIdWithPath<ast::MacroCall>, |
@@ -674,7 +700,8 @@ fn macro_call_as_call_id( | |||
674 | resolver: impl Fn(path::ModPath) -> Option<MacroDefId>, | 700 | resolver: impl Fn(path::ModPath) -> Option<MacroDefId>, |
675 | error_sink: &mut dyn FnMut(mbe::ExpandError), | 701 | error_sink: &mut dyn FnMut(mbe::ExpandError), |
676 | ) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro> { | 702 | ) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro> { |
677 | let def: MacroDefId = resolver(call.path.clone()).ok_or(UnresolvedMacro)?; | 703 | let def: MacroDefId = |
704 | resolver(call.path.clone()).ok_or_else(|| UnresolvedMacro { path: call.path.clone() })?; | ||
678 | 705 | ||
679 | let res = if let MacroDefKind::BuiltInEager(..) = def.kind { | 706 | let res = if let MacroDefKind::BuiltInEager(..) = def.kind { |
680 | let macro_call = InFile::new(call.ast_id.file_id, call.ast_id.to_node(db.upcast())); | 707 | let macro_call = InFile::new(call.ast_id.file_id, call.ast_id.to_node(db.upcast())); |
@@ -704,8 +731,13 @@ fn derive_macro_as_call_id( | |||
704 | krate: CrateId, | 731 | krate: CrateId, |
705 | resolver: impl Fn(path::ModPath) -> Option<MacroDefId>, | 732 | resolver: impl Fn(path::ModPath) -> Option<MacroDefId>, |
706 | ) -> Result<MacroCallId, UnresolvedMacro> { | 733 | ) -> Result<MacroCallId, UnresolvedMacro> { |
707 | let def: MacroDefId = resolver(item_attr.path.clone()).ok_or(UnresolvedMacro)?; | 734 | let def: MacroDefId = resolver(item_attr.path.clone()) |
708 | let last_segment = item_attr.path.segments().last().ok_or(UnresolvedMacro)?; | 735 | .ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?; |
736 | let last_segment = item_attr | ||
737 | .path | ||
738 | .segments() | ||
739 | .last() | ||
740 | .ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?; | ||
709 | let res = def | 741 | let res = def |
710 | .as_lazy_macro( | 742 | .as_lazy_macro( |
711 | db.upcast(), | 743 | db.upcast(), |
diff --git a/crates/hir_def/src/nameres.rs b/crates/hir_def/src/nameres.rs index 9e181751c..ba027c44a 100644 --- a/crates/hir_def/src/nameres.rs +++ b/crates/hir_def/src/nameres.rs | |||
@@ -410,6 +410,20 @@ impl DefMap { | |||
410 | } | 410 | } |
411 | } | 411 | } |
412 | 412 | ||
413 | pub fn dump_block_scopes(&self, db: &dyn DefDatabase) -> String { | ||
414 | let mut buf = String::new(); | ||
415 | let mut arc; | ||
416 | let mut current_map = self; | ||
417 | while let Some(block) = ¤t_map.block { | ||
418 | format_to!(buf, "{:?} in {:?}\n", block.block, block.parent); | ||
419 | arc = block.parent.def_map(db); | ||
420 | current_map = &*arc; | ||
421 | } | ||
422 | |||
423 | format_to!(buf, "crate scope\n"); | ||
424 | buf | ||
425 | } | ||
426 | |||
413 | fn shrink_to_fit(&mut self) { | 427 | fn shrink_to_fit(&mut self) { |
414 | // Exhaustive match to require handling new fields. | 428 | // Exhaustive match to require handling new fields. |
415 | let Self { | 429 | let Self { |
@@ -481,7 +495,7 @@ mod diagnostics { | |||
481 | 495 | ||
482 | UnresolvedProcMacro { ast: MacroCallKind }, | 496 | UnresolvedProcMacro { ast: MacroCallKind }, |
483 | 497 | ||
484 | UnresolvedMacroCall { ast: AstId<ast::MacroCall> }, | 498 | UnresolvedMacroCall { ast: AstId<ast::MacroCall>, path: ModPath }, |
485 | 499 | ||
486 | MacroError { ast: MacroCallKind, message: String }, | 500 | MacroError { ast: MacroCallKind, message: String }, |
487 | } | 501 | } |
@@ -546,8 +560,9 @@ mod diagnostics { | |||
546 | pub(super) fn unresolved_macro_call( | 560 | pub(super) fn unresolved_macro_call( |
547 | container: LocalModuleId, | 561 | container: LocalModuleId, |
548 | ast: AstId<ast::MacroCall>, | 562 | ast: AstId<ast::MacroCall>, |
563 | path: ModPath, | ||
549 | ) -> Self { | 564 | ) -> Self { |
550 | Self { in_module: container, kind: DiagnosticKind::UnresolvedMacroCall { ast } } | 565 | Self { in_module: container, kind: DiagnosticKind::UnresolvedMacroCall { ast, path } } |
551 | } | 566 | } |
552 | 567 | ||
553 | pub(super) fn add_to( | 568 | pub(super) fn add_to( |
@@ -662,9 +677,13 @@ mod diagnostics { | |||
662 | }); | 677 | }); |
663 | } | 678 | } |
664 | 679 | ||
665 | DiagnosticKind::UnresolvedMacroCall { ast } => { | 680 | DiagnosticKind::UnresolvedMacroCall { ast, path } => { |
666 | let node = ast.to_node(db.upcast()); | 681 | let node = ast.to_node(db.upcast()); |
667 | sink.push(UnresolvedMacroCall { file: ast.file_id, node: AstPtr::new(&node) }); | 682 | sink.push(UnresolvedMacroCall { |
683 | file: ast.file_id, | ||
684 | node: AstPtr::new(&node), | ||
685 | path: path.clone(), | ||
686 | }); | ||
668 | } | 687 | } |
669 | 688 | ||
670 | DiagnosticKind::MacroError { ast, message } => { | 689 | DiagnosticKind::MacroError { ast, message } => { |
diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs index fb4ddff5e..05ceb1efb 100644 --- a/crates/hir_def/src/nameres/collector.rs +++ b/crates/hir_def/src/nameres/collector.rs | |||
@@ -829,7 +829,7 @@ impl DefCollector<'_> { | |||
829 | res = ReachedFixedPoint::No; | 829 | res = ReachedFixedPoint::No; |
830 | return false; | 830 | return false; |
831 | } | 831 | } |
832 | Err(UnresolvedMacro) | Ok(Err(_)) => {} | 832 | Err(UnresolvedMacro { .. }) | Ok(Err(_)) => {} |
833 | } | 833 | } |
834 | } | 834 | } |
835 | MacroDirectiveKind::Derive { ast_id, derive_attr } => { | 835 | MacroDirectiveKind::Derive { ast_id, derive_attr } => { |
@@ -845,7 +845,7 @@ impl DefCollector<'_> { | |||
845 | res = ReachedFixedPoint::No; | 845 | res = ReachedFixedPoint::No; |
846 | return false; | 846 | return false; |
847 | } | 847 | } |
848 | Err(UnresolvedMacro) => (), | 848 | Err(UnresolvedMacro { .. }) => (), |
849 | } | 849 | } |
850 | } | 850 | } |
851 | } | 851 | } |
@@ -943,10 +943,11 @@ impl DefCollector<'_> { | |||
943 | &mut |_| (), | 943 | &mut |_| (), |
944 | ) { | 944 | ) { |
945 | Ok(_) => (), | 945 | Ok(_) => (), |
946 | Err(UnresolvedMacro) => { | 946 | Err(UnresolvedMacro { path }) => { |
947 | self.def_map.diagnostics.push(DefDiagnostic::unresolved_macro_call( | 947 | self.def_map.diagnostics.push(DefDiagnostic::unresolved_macro_call( |
948 | directive.module_id, | 948 | directive.module_id, |
949 | ast_id.ast_id, | 949 | ast_id.ast_id, |
950 | path, | ||
950 | )); | 951 | )); |
951 | } | 952 | } |
952 | }, | 953 | }, |
@@ -1530,7 +1531,7 @@ impl ModCollector<'_, '_> { | |||
1530 | )); | 1531 | )); |
1531 | return; | 1532 | return; |
1532 | } | 1533 | } |
1533 | Err(UnresolvedMacro) => (), | 1534 | Err(UnresolvedMacro { .. }) => (), |
1534 | } | 1535 | } |
1535 | 1536 | ||
1536 | // Case 2: resolve in module scope, expand during name resolution. | 1537 | // Case 2: resolve in module scope, expand during name resolution. |
diff --git a/crates/hir_def/src/nameres/path_resolution.rs b/crates/hir_def/src/nameres/path_resolution.rs index ccc9f22eb..c984148c3 100644 --- a/crates/hir_def/src/nameres/path_resolution.rs +++ b/crates/hir_def/src/nameres/path_resolution.rs | |||
@@ -387,7 +387,13 @@ impl DefMap { | |||
387 | .get_legacy_macro(name) | 387 | .get_legacy_macro(name) |
388 | .map_or_else(PerNs::none, |m| PerNs::macros(m, Visibility::Public)); | 388 | .map_or_else(PerNs::none, |m| PerNs::macros(m, Visibility::Public)); |
389 | let from_scope = self[module].scope.get(name); | 389 | let from_scope = self[module].scope.get(name); |
390 | let from_builtin = BUILTIN_SCOPE.get(name).copied().unwrap_or_else(PerNs::none); | 390 | let from_builtin = match self.block { |
391 | Some(_) => { | ||
392 | // Only resolve to builtins in the root `DefMap`. | ||
393 | PerNs::none() | ||
394 | } | ||
395 | None => BUILTIN_SCOPE.get(name).copied().unwrap_or_else(PerNs::none), | ||
396 | }; | ||
391 | let from_scope_or_builtin = match shadow { | 397 | let from_scope_or_builtin = match shadow { |
392 | BuiltinShadowMode::Module => from_scope.or(from_builtin), | 398 | BuiltinShadowMode::Module => from_scope.or(from_builtin), |
393 | BuiltinShadowMode::Other => { | 399 | BuiltinShadowMode::Other => { |
diff --git a/crates/hir_def/src/nameres/tests/diagnostics.rs b/crates/hir_def/src/nameres/tests/diagnostics.rs index 1ac88fc89..543975e07 100644 --- a/crates/hir_def/src/nameres/tests/diagnostics.rs +++ b/crates/hir_def/src/nameres/tests/diagnostics.rs | |||
@@ -170,7 +170,7 @@ fn unresolved_legacy_scope_macro() { | |||
170 | 170 | ||
171 | m!(); | 171 | m!(); |
172 | m2!(); | 172 | m2!(); |
173 | //^^^^^^ unresolved macro call | 173 | //^^^^^^ unresolved macro `self::m2!` |
174 | "#, | 174 | "#, |
175 | ); | 175 | ); |
176 | } | 176 | } |
@@ -187,7 +187,7 @@ fn unresolved_module_scope_macro() { | |||
187 | 187 | ||
188 | self::m!(); | 188 | self::m!(); |
189 | self::m2!(); | 189 | self::m2!(); |
190 | //^^^^^^^^^^^^ unresolved macro call | 190 | //^^^^^^^^^^^^ unresolved macro `self::m2!` |
191 | "#, | 191 | "#, |
192 | ); | 192 | ); |
193 | } | 193 | } |
diff --git a/crates/hir_def/src/path.rs b/crates/hir_def/src/path.rs index b528ff8ba..509f77850 100644 --- a/crates/hir_def/src/path.rs +++ b/crates/hir_def/src/path.rs | |||
@@ -48,7 +48,8 @@ pub enum ImportAlias { | |||
48 | 48 | ||
49 | impl ModPath { | 49 | impl ModPath { |
50 | pub fn from_src(path: ast::Path, hygiene: &Hygiene) -> Option<ModPath> { | 50 | pub fn from_src(path: ast::Path, hygiene: &Hygiene) -> Option<ModPath> { |
51 | lower::lower_path(path, hygiene).map(|it| (*it.mod_path).clone()) | 51 | let ctx = LowerCtx::with_hygiene(hygiene); |
52 | lower::lower_path(path, &ctx).map(|it| (*it.mod_path).clone()) | ||
52 | } | 53 | } |
53 | 54 | ||
54 | pub fn from_segments(kind: PathKind, segments: impl IntoIterator<Item = Name>) -> ModPath { | 55 | pub fn from_segments(kind: PathKind, segments: impl IntoIterator<Item = Name>) -> ModPath { |
@@ -167,8 +168,8 @@ pub enum GenericArg { | |||
167 | impl Path { | 168 | impl Path { |
168 | /// Converts an `ast::Path` to `Path`. Works with use trees. | 169 | /// Converts an `ast::Path` to `Path`. Works with use trees. |
169 | /// It correctly handles `$crate` based path from macro call. | 170 | /// It correctly handles `$crate` based path from macro call. |
170 | pub fn from_src(path: ast::Path, hygiene: &Hygiene) -> Option<Path> { | 171 | pub fn from_src(path: ast::Path, ctx: &LowerCtx) -> Option<Path> { |
171 | lower::lower_path(path, hygiene) | 172 | lower::lower_path(path, ctx) |
172 | } | 173 | } |
173 | 174 | ||
174 | /// Converts a known mod path to `Path`. | 175 | /// Converts a known mod path to `Path`. |
diff --git a/crates/hir_def/src/path/lower.rs b/crates/hir_def/src/path/lower.rs index 7b29d9d4f..1df6db525 100644 --- a/crates/hir_def/src/path/lower.rs +++ b/crates/hir_def/src/path/lower.rs | |||
@@ -6,10 +6,7 @@ use crate::intern::Interned; | |||
6 | use std::sync::Arc; | 6 | use std::sync::Arc; |
7 | 7 | ||
8 | use either::Either; | 8 | use either::Either; |
9 | use hir_expand::{ | 9 | use hir_expand::name::{name, AsName}; |
10 | hygiene::Hygiene, | ||
11 | name::{name, AsName}, | ||
12 | }; | ||
13 | use syntax::ast::{self, AstNode, TypeBoundsOwner}; | 10 | use syntax::ast::{self, AstNode, TypeBoundsOwner}; |
14 | 11 | ||
15 | use super::AssociatedTypeBinding; | 12 | use super::AssociatedTypeBinding; |
@@ -23,12 +20,12 @@ pub(super) use lower_use::lower_use_tree; | |||
23 | 20 | ||
24 | /// Converts an `ast::Path` to `Path`. Works with use trees. | 21 | /// Converts an `ast::Path` to `Path`. Works with use trees. |
25 | /// It correctly handles `$crate` based path from macro call. | 22 | /// It correctly handles `$crate` based path from macro call. |
26 | pub(super) fn lower_path(mut path: ast::Path, hygiene: &Hygiene) -> Option<Path> { | 23 | pub(super) fn lower_path(mut path: ast::Path, ctx: &LowerCtx) -> Option<Path> { |
27 | let mut kind = PathKind::Plain; | 24 | let mut kind = PathKind::Plain; |
28 | let mut type_anchor = None; | 25 | let mut type_anchor = None; |
29 | let mut segments = Vec::new(); | 26 | let mut segments = Vec::new(); |
30 | let mut generic_args = Vec::new(); | 27 | let mut generic_args = Vec::new(); |
31 | let ctx = LowerCtx::with_hygiene(hygiene); | 28 | let hygiene = ctx.hygiene(); |
32 | loop { | 29 | loop { |
33 | let segment = path.segment()?; | 30 | let segment = path.segment()?; |
34 | 31 | ||
@@ -43,10 +40,10 @@ pub(super) fn lower_path(mut path: ast::Path, hygiene: &Hygiene) -> Option<Path> | |||
43 | Either::Left(name) => { | 40 | Either::Left(name) => { |
44 | let args = segment | 41 | let args = segment |
45 | .generic_arg_list() | 42 | .generic_arg_list() |
46 | .and_then(|it| lower_generic_args(&ctx, it)) | 43 | .and_then(|it| lower_generic_args(ctx, it)) |
47 | .or_else(|| { | 44 | .or_else(|| { |
48 | lower_generic_args_from_fn_path( | 45 | lower_generic_args_from_fn_path( |
49 | &ctx, | 46 | ctx, |
50 | segment.param_list(), | 47 | segment.param_list(), |
51 | segment.ret_type(), | 48 | segment.ret_type(), |
52 | ) | 49 | ) |
@@ -64,7 +61,7 @@ pub(super) fn lower_path(mut path: ast::Path, hygiene: &Hygiene) -> Option<Path> | |||
64 | ast::PathSegmentKind::Type { type_ref, trait_ref } => { | 61 | ast::PathSegmentKind::Type { type_ref, trait_ref } => { |
65 | assert!(path.qualifier().is_none()); // this can only occur at the first segment | 62 | assert!(path.qualifier().is_none()); // this can only occur at the first segment |
66 | 63 | ||
67 | let self_type = TypeRef::from_ast(&ctx, type_ref?); | 64 | let self_type = TypeRef::from_ast(ctx, type_ref?); |
68 | 65 | ||
69 | match trait_ref { | 66 | match trait_ref { |
70 | // <T>::foo | 67 | // <T>::foo |
@@ -74,7 +71,7 @@ pub(super) fn lower_path(mut path: ast::Path, hygiene: &Hygiene) -> Option<Path> | |||
74 | } | 71 | } |
75 | // <T as Trait<A>>::Foo desugars to Trait<Self=T, A>::Foo | 72 | // <T as Trait<A>>::Foo desugars to Trait<Self=T, A>::Foo |
76 | Some(trait_ref) => { | 73 | Some(trait_ref) => { |
77 | let path = Path::from_src(trait_ref.path()?, hygiene)?; | 74 | let path = Path::from_src(trait_ref.path()?, ctx)?; |
78 | let mod_path = (*path.mod_path).clone(); | 75 | let mod_path = (*path.mod_path).clone(); |
79 | let num_segments = path.mod_path.segments.len(); | 76 | let num_segments = path.mod_path.segments.len(); |
80 | kind = mod_path.kind; | 77 | kind = mod_path.kind; |
diff --git a/crates/hir_def/src/test_db.rs b/crates/hir_def/src/test_db.rs index dd36106f8..8fa703a57 100644 --- a/crates/hir_def/src/test_db.rs +++ b/crates/hir_def/src/test_db.rs | |||
@@ -15,7 +15,12 @@ use rustc_hash::FxHashSet; | |||
15 | use syntax::{algo, ast, AstNode, TextRange, TextSize}; | 15 | use syntax::{algo, ast, AstNode, TextRange, TextSize}; |
16 | use test_utils::extract_annotations; | 16 | use test_utils::extract_annotations; |
17 | 17 | ||
18 | use crate::{db::DefDatabase, nameres::DefMap, src::HasSource, Lookup, ModuleDefId, ModuleId}; | 18 | use crate::{ |
19 | db::DefDatabase, | ||
20 | nameres::{DefMap, ModuleSource}, | ||
21 | src::HasSource, | ||
22 | LocalModuleId, Lookup, ModuleDefId, ModuleId, | ||
23 | }; | ||
19 | 24 | ||
20 | #[salsa::database( | 25 | #[salsa::database( |
21 | base_db::SourceDatabaseExtStorage, | 26 | base_db::SourceDatabaseExtStorage, |
@@ -87,10 +92,11 @@ impl TestDB { | |||
87 | pub(crate) fn module_at_position(&self, position: FilePosition) -> ModuleId { | 92 | pub(crate) fn module_at_position(&self, position: FilePosition) -> ModuleId { |
88 | let file_module = self.module_for_file(position.file_id); | 93 | let file_module = self.module_for_file(position.file_id); |
89 | let mut def_map = file_module.def_map(self); | 94 | let mut def_map = file_module.def_map(self); |
95 | let module = self.mod_at_position(&def_map, position); | ||
90 | 96 | ||
91 | def_map = match self.block_at_position(&def_map, position) { | 97 | def_map = match self.block_at_position(&def_map, position) { |
92 | Some(it) => it, | 98 | Some(it) => it, |
93 | None => return file_module, | 99 | None => return def_map.module_id(module), |
94 | }; | 100 | }; |
95 | loop { | 101 | loop { |
96 | let new_map = self.block_at_position(&def_map, position); | 102 | let new_map = self.block_at_position(&def_map, position); |
@@ -106,6 +112,47 @@ impl TestDB { | |||
106 | } | 112 | } |
107 | } | 113 | } |
108 | 114 | ||
115 | /// Finds the smallest/innermost module in `def_map` containing `position`. | ||
116 | fn mod_at_position(&self, def_map: &DefMap, position: FilePosition) -> LocalModuleId { | ||
117 | let mut size = None; | ||
118 | let mut res = def_map.root(); | ||
119 | for (module, data) in def_map.modules() { | ||
120 | let src = data.definition_source(self); | ||
121 | if src.file_id != position.file_id.into() { | ||
122 | continue; | ||
123 | } | ||
124 | |||
125 | let range = match src.value { | ||
126 | ModuleSource::SourceFile(it) => it.syntax().text_range(), | ||
127 | ModuleSource::Module(it) => it.syntax().text_range(), | ||
128 | ModuleSource::BlockExpr(it) => it.syntax().text_range(), | ||
129 | }; | ||
130 | |||
131 | if !range.contains(position.offset) { | ||
132 | continue; | ||
133 | } | ||
134 | |||
135 | let new_size = match size { | ||
136 | None => range.len(), | ||
137 | Some(size) => { | ||
138 | if range.len() < size { | ||
139 | range.len() | ||
140 | } else { | ||
141 | size | ||
142 | } | ||
143 | } | ||
144 | }; | ||
145 | |||
146 | if size != Some(new_size) { | ||
147 | cov_mark::hit!(submodule_in_testdb); | ||
148 | size = Some(new_size); | ||
149 | res = module; | ||
150 | } | ||
151 | } | ||
152 | |||
153 | res | ||
154 | } | ||
155 | |||
109 | fn block_at_position(&self, def_map: &DefMap, position: FilePosition) -> Option<Arc<DefMap>> { | 156 | fn block_at_position(&self, def_map: &DefMap, position: FilePosition) -> Option<Arc<DefMap>> { |
110 | // Find the smallest (innermost) function in `def_map` containing the cursor. | 157 | // Find the smallest (innermost) function in `def_map` containing the cursor. |
111 | let mut size = None; | 158 | let mut size = None; |
diff --git a/crates/hir_def/src/type_ref.rs b/crates/hir_def/src/type_ref.rs index 4c24aae94..ea29da5da 100644 --- a/crates/hir_def/src/type_ref.rs +++ b/crates/hir_def/src/type_ref.rs | |||
@@ -1,6 +1,7 @@ | |||
1 | //! HIR for references to types. Paths in these are not yet resolved. They can | 1 | //! HIR for references to types. Paths in these are not yet resolved. They can |
2 | //! be directly created from an ast::TypeRef, without further queries. | 2 | //! be directly created from an ast::TypeRef, without further queries. |
3 | use hir_expand::name::Name; | 3 | |
4 | use hir_expand::{name::Name, AstId, InFile}; | ||
4 | use syntax::ast; | 5 | use syntax::ast; |
5 | 6 | ||
6 | use crate::{body::LowerCtx, path::Path}; | 7 | use crate::{body::LowerCtx, path::Path}; |
@@ -68,6 +69,7 @@ impl TraitRef { | |||
68 | } | 69 | } |
69 | } | 70 | } |
70 | } | 71 | } |
72 | |||
71 | /// Compare ty::Ty | 73 | /// Compare ty::Ty |
72 | #[derive(Clone, PartialEq, Eq, Hash, Debug)] | 74 | #[derive(Clone, PartialEq, Eq, Hash, Debug)] |
73 | pub enum TypeRef { | 75 | pub enum TypeRef { |
@@ -84,6 +86,7 @@ pub enum TypeRef { | |||
84 | // For | 86 | // For |
85 | ImplTrait(Vec<TypeBound>), | 87 | ImplTrait(Vec<TypeBound>), |
86 | DynTrait(Vec<TypeBound>), | 88 | DynTrait(Vec<TypeBound>), |
89 | Macro(AstId<ast::MacroCall>), | ||
87 | Error, | 90 | Error, |
88 | } | 91 | } |
89 | 92 | ||
@@ -116,7 +119,7 @@ pub enum TypeBound { | |||
116 | 119 | ||
117 | impl TypeRef { | 120 | impl TypeRef { |
118 | /// Converts an `ast::TypeRef` to a `hir::TypeRef`. | 121 | /// Converts an `ast::TypeRef` to a `hir::TypeRef`. |
119 | pub(crate) fn from_ast(ctx: &LowerCtx, node: ast::Type) -> Self { | 122 | pub fn from_ast(ctx: &LowerCtx, node: ast::Type) -> Self { |
120 | match node { | 123 | match node { |
121 | ast::Type::ParenType(inner) => TypeRef::from_ast_opt(&ctx, inner.ty()), | 124 | ast::Type::ParenType(inner) => TypeRef::from_ast_opt(&ctx, inner.ty()), |
122 | ast::Type::TupleType(inner) => { | 125 | ast::Type::TupleType(inner) => { |
@@ -176,8 +179,13 @@ impl TypeRef { | |||
176 | ast::Type::DynTraitType(inner) => { | 179 | ast::Type::DynTraitType(inner) => { |
177 | TypeRef::DynTrait(type_bounds_from_ast(ctx, inner.type_bound_list())) | 180 | TypeRef::DynTrait(type_bounds_from_ast(ctx, inner.type_bound_list())) |
178 | } | 181 | } |
179 | // FIXME: Macros in type position are not yet supported. | 182 | ast::Type::MacroType(mt) => match mt.macro_call() { |
180 | ast::Type::MacroType(_) => TypeRef::Error, | 183 | Some(mc) => ctx |
184 | .ast_id(&mc) | ||
185 | .map(|mc| TypeRef::Macro(InFile::new(ctx.file_id(), mc))) | ||
186 | .unwrap_or(TypeRef::Error), | ||
187 | None => TypeRef::Error, | ||
188 | }, | ||
181 | } | 189 | } |
182 | } | 190 | } |
183 | 191 | ||
@@ -215,7 +223,7 @@ impl TypeRef { | |||
215 | } | 223 | } |
216 | } | 224 | } |
217 | TypeRef::Path(path) => go_path(path, f), | 225 | TypeRef::Path(path) => go_path(path, f), |
218 | TypeRef::Never | TypeRef::Placeholder | TypeRef::Error => {} | 226 | TypeRef::Never | TypeRef::Placeholder | TypeRef::Macro(_) | TypeRef::Error => {} |
219 | }; | 227 | }; |
220 | } | 228 | } |
221 | 229 | ||
diff --git a/crates/hir_def/src/visibility.rs b/crates/hir_def/src/visibility.rs index 9908cd926..d4b7c9970 100644 --- a/crates/hir_def/src/visibility.rs +++ b/crates/hir_def/src/visibility.rs | |||
@@ -123,11 +123,19 @@ impl Visibility { | |||
123 | def_map: &DefMap, | 123 | def_map: &DefMap, |
124 | mut from_module: crate::LocalModuleId, | 124 | mut from_module: crate::LocalModuleId, |
125 | ) -> bool { | 125 | ) -> bool { |
126 | let to_module = match self { | 126 | let mut to_module = match self { |
127 | Visibility::Module(m) => m, | 127 | Visibility::Module(m) => m, |
128 | Visibility::Public => return true, | 128 | Visibility::Public => return true, |
129 | }; | 129 | }; |
130 | 130 | ||
131 | // `to_module` might be the root module of a block expression. Those have the same | ||
132 | // visibility as the containing module (even though no items are directly nameable from | ||
133 | // there, getting this right is important for method resolution). | ||
134 | // In that case, we adjust the visibility of `to_module` to point to the containing module. | ||
135 | if to_module.is_block_root(db) { | ||
136 | to_module = to_module.containing_module(db).unwrap(); | ||
137 | } | ||
138 | |||
131 | // from_module needs to be a descendant of to_module | 139 | // from_module needs to be a descendant of to_module |
132 | let mut def_map = def_map; | 140 | let mut def_map = def_map; |
133 | let mut parent_arc; | 141 | let mut parent_arc; |