diff options
Diffstat (limited to 'crates')
54 files changed, 2924 insertions, 203 deletions
diff --git a/crates/ra_assists/src/assist_ctx.rs b/crates/ra_assists/src/assist_ctx.rs index 993aebc47..28152f724 100644 --- a/crates/ra_assists/src/assist_ctx.rs +++ b/crates/ra_assists/src/assist_ctx.rs | |||
@@ -46,7 +46,7 @@ pub(crate) enum Assist { | |||
46 | /// | 46 | /// |
47 | /// Note, however, that we don't actually use such two-phase logic at the | 47 | /// Note, however, that we don't actually use such two-phase logic at the |
48 | /// moment, because the LSP API is pretty awkward in this place, and it's much | 48 | /// moment, because the LSP API is pretty awkward in this place, and it's much |
49 | /// easier to just compute the edit eagerly :-)#[derive(Debug, Clone)] | 49 | /// easier to just compute the edit eagerly :-) |
50 | #[derive(Debug)] | 50 | #[derive(Debug)] |
51 | pub(crate) struct AssistCtx<'a, DB> { | 51 | pub(crate) struct AssistCtx<'a, DB> { |
52 | pub(crate) db: &'a DB, | 52 | pub(crate) db: &'a DB, |
diff --git a/crates/ra_assists/src/assists/add_explicit_type.rs b/crates/ra_assists/src/assists/add_explicit_type.rs index eeb4ff39f..2c602a79e 100644 --- a/crates/ra_assists/src/assists/add_explicit_type.rs +++ b/crates/ra_assists/src/assists/add_explicit_type.rs | |||
@@ -74,6 +74,24 @@ mod tests { | |||
74 | } | 74 | } |
75 | 75 | ||
76 | #[test] | 76 | #[test] |
77 | fn add_explicit_type_works_for_macro_call() { | ||
78 | check_assist( | ||
79 | add_explicit_type, | ||
80 | "macro_rules! v { () => {0u64} } fn f() { let a<|> = v!(); }", | ||
81 | "macro_rules! v { () => {0u64} } fn f() { let a<|>: u64 = v!(); }", | ||
82 | ); | ||
83 | } | ||
84 | |||
85 | #[test] | ||
86 | fn add_explicit_type_works_for_macro_call_recursive() { | ||
87 | check_assist( | ||
88 | add_explicit_type, | ||
89 | "macro_rules! u { () => {0u64} } macro_rules! v { () => {u!()} } fn f() { let a<|> = v!(); }", | ||
90 | "macro_rules! u { () => {0u64} } macro_rules! v { () => {u!()} } fn f() { let a<|>: u64 = v!(); }", | ||
91 | ); | ||
92 | } | ||
93 | |||
94 | #[test] | ||
77 | fn add_explicit_type_not_applicable_if_ty_not_inferred() { | 95 | fn add_explicit_type_not_applicable_if_ty_not_inferred() { |
78 | check_assist_not_applicable(add_explicit_type, "fn f() { let a<|> = None; }"); | 96 | check_assist_not_applicable(add_explicit_type, "fn f() { let a<|> = None; }"); |
79 | } | 97 | } |
diff --git a/crates/ra_assists/src/assists/add_missing_impl_members.rs b/crates/ra_assists/src/assists/add_missing_impl_members.rs index cef669cb5..bc49e71fe 100644 --- a/crates/ra_assists/src/assists/add_missing_impl_members.rs +++ b/crates/ra_assists/src/assists/add_missing_impl_members.rs | |||
@@ -1,3 +1,5 @@ | |||
1 | use std::collections::HashMap; | ||
2 | |||
1 | use hir::{db::HirDatabase, HasSource}; | 3 | use hir::{db::HirDatabase, HasSource}; |
2 | use ra_syntax::{ | 4 | use ra_syntax::{ |
3 | ast::{self, edit, make, AstNode, NameOwner}, | 5 | ast::{self, edit, make, AstNode, NameOwner}, |
@@ -17,26 +19,26 @@ enum AddMissingImplMembersMode { | |||
17 | // Adds scaffold for required impl members. | 19 | // Adds scaffold for required impl members. |
18 | // | 20 | // |
19 | // ``` | 21 | // ``` |
20 | // trait T { | 22 | // trait Trait<T> { |
21 | // Type X; | 23 | // Type X; |
22 | // fn foo(&self); | 24 | // fn foo(&self) -> T; |
23 | // fn bar(&self) {} | 25 | // fn bar(&self) {} |
24 | // } | 26 | // } |
25 | // | 27 | // |
26 | // impl T for () {<|> | 28 | // impl Trait<u32> for () {<|> |
27 | // | 29 | // |
28 | // } | 30 | // } |
29 | // ``` | 31 | // ``` |
30 | // -> | 32 | // -> |
31 | // ``` | 33 | // ``` |
32 | // trait T { | 34 | // trait Trait<T> { |
33 | // Type X; | 35 | // Type X; |
34 | // fn foo(&self); | 36 | // fn foo(&self) -> T; |
35 | // fn bar(&self) {} | 37 | // fn bar(&self) {} |
36 | // } | 38 | // } |
37 | // | 39 | // |
38 | // impl T for () { | 40 | // impl Trait<u32> for () { |
39 | // fn foo(&self) { unimplemented!() } | 41 | // fn foo(&self) -> u32 { unimplemented!() } |
40 | // | 42 | // |
41 | // } | 43 | // } |
42 | // ``` | 44 | // ``` |
@@ -54,13 +56,13 @@ pub(crate) fn add_missing_impl_members(ctx: AssistCtx<impl HirDatabase>) -> Opti | |||
54 | // Adds scaffold for overriding default impl members. | 56 | // Adds scaffold for overriding default impl members. |
55 | // | 57 | // |
56 | // ``` | 58 | // ``` |
57 | // trait T { | 59 | // trait Trait { |
58 | // Type X; | 60 | // Type X; |
59 | // fn foo(&self); | 61 | // fn foo(&self); |
60 | // fn bar(&self) {} | 62 | // fn bar(&self) {} |
61 | // } | 63 | // } |
62 | // | 64 | // |
63 | // impl T for () { | 65 | // impl Trait for () { |
64 | // Type X = (); | 66 | // Type X = (); |
65 | // fn foo(&self) {}<|> | 67 | // fn foo(&self) {}<|> |
66 | // | 68 | // |
@@ -68,13 +70,13 @@ pub(crate) fn add_missing_impl_members(ctx: AssistCtx<impl HirDatabase>) -> Opti | |||
68 | // ``` | 70 | // ``` |
69 | // -> | 71 | // -> |
70 | // ``` | 72 | // ``` |
71 | // trait T { | 73 | // trait Trait { |
72 | // Type X; | 74 | // Type X; |
73 | // fn foo(&self); | 75 | // fn foo(&self); |
74 | // fn bar(&self) {} | 76 | // fn bar(&self) {} |
75 | // } | 77 | // } |
76 | // | 78 | // |
77 | // impl T for () { | 79 | // impl Trait for () { |
78 | // Type X = (); | 80 | // Type X = (); |
79 | // fn foo(&self) {} | 81 | // fn foo(&self) {} |
80 | // fn bar(&self) {} | 82 | // fn bar(&self) {} |
@@ -99,7 +101,7 @@ fn add_missing_impl_members_inner( | |||
99 | let impl_node = ctx.find_node_at_offset::<ast::ImplBlock>()?; | 101 | let impl_node = ctx.find_node_at_offset::<ast::ImplBlock>()?; |
100 | let impl_item_list = impl_node.item_list()?; | 102 | let impl_item_list = impl_node.item_list()?; |
101 | 103 | ||
102 | let trait_def = { | 104 | let (trait_, trait_def) = { |
103 | let analyzer = ctx.source_analyzer(impl_node.syntax(), None); | 105 | let analyzer = ctx.source_analyzer(impl_node.syntax(), None); |
104 | 106 | ||
105 | resolve_target_trait_def(ctx.db, &analyzer, &impl_node)? | 107 | resolve_target_trait_def(ctx.db, &analyzer, &impl_node)? |
@@ -132,10 +134,25 @@ fn add_missing_impl_members_inner( | |||
132 | return None; | 134 | return None; |
133 | } | 135 | } |
134 | 136 | ||
137 | let file_id = ctx.frange.file_id; | ||
138 | let db = ctx.db; | ||
139 | |||
135 | ctx.add_assist(AssistId(assist_id), label, |edit| { | 140 | ctx.add_assist(AssistId(assist_id), label, |edit| { |
136 | let n_existing_items = impl_item_list.impl_items().count(); | 141 | let n_existing_items = impl_item_list.impl_items().count(); |
142 | let substs = get_syntactic_substs(impl_node).unwrap_or_default(); | ||
143 | let generic_def: hir::GenericDef = trait_.into(); | ||
144 | let substs_by_param: HashMap<_, _> = generic_def | ||
145 | .params(db) | ||
146 | .into_iter() | ||
147 | // this is a trait impl, so we need to skip the first type parameter -- this is a bit hacky | ||
148 | .skip(1) | ||
149 | .zip(substs.into_iter()) | ||
150 | .collect(); | ||
137 | let items = missing_items | 151 | let items = missing_items |
138 | .into_iter() | 152 | .into_iter() |
153 | .map(|it| { | ||
154 | substitute_type_params(db, hir::InFile::new(file_id.into(), it), &substs_by_param) | ||
155 | }) | ||
139 | .map(|it| match it { | 156 | .map(|it| match it { |
140 | ast::ImplItem::FnDef(def) => ast::ImplItem::FnDef(add_body(def)), | 157 | ast::ImplItem::FnDef(def) => ast::ImplItem::FnDef(add_body(def)), |
141 | _ => it, | 158 | _ => it, |
@@ -160,13 +177,63 @@ fn add_body(fn_def: ast::FnDef) -> ast::FnDef { | |||
160 | } | 177 | } |
161 | } | 178 | } |
162 | 179 | ||
180 | // FIXME: It would probably be nicer if we could get this via HIR (i.e. get the | ||
181 | // trait ref, and then go from the types in the substs back to the syntax) | ||
182 | // FIXME: This should be a general utility (not even just for assists) | ||
183 | fn get_syntactic_substs(impl_block: ast::ImplBlock) -> Option<Vec<ast::TypeRef>> { | ||
184 | let target_trait = impl_block.target_trait()?; | ||
185 | let path_type = match target_trait { | ||
186 | ast::TypeRef::PathType(path) => path, | ||
187 | _ => return None, | ||
188 | }; | ||
189 | let type_arg_list = path_type.path()?.segment()?.type_arg_list()?; | ||
190 | let mut result = Vec::new(); | ||
191 | for type_arg in type_arg_list.type_args() { | ||
192 | let type_arg: ast::TypeArg = type_arg; | ||
193 | result.push(type_arg.type_ref()?); | ||
194 | } | ||
195 | Some(result) | ||
196 | } | ||
197 | |||
198 | // FIXME: This should be a general utility (not even just for assists) | ||
199 | fn substitute_type_params<N: AstNode>( | ||
200 | db: &impl HirDatabase, | ||
201 | node: hir::InFile<N>, | ||
202 | substs: &HashMap<hir::TypeParam, ast::TypeRef>, | ||
203 | ) -> N { | ||
204 | let type_param_replacements = node | ||
205 | .value | ||
206 | .syntax() | ||
207 | .descendants() | ||
208 | .filter_map(ast::TypeRef::cast) | ||
209 | .filter_map(|n| { | ||
210 | let path = match &n { | ||
211 | ast::TypeRef::PathType(path_type) => path_type.path()?, | ||
212 | _ => return None, | ||
213 | }; | ||
214 | let analyzer = hir::SourceAnalyzer::new(db, node.with_value(n.syntax()), None); | ||
215 | let resolution = analyzer.resolve_path(db, &path)?; | ||
216 | match resolution { | ||
217 | hir::PathResolution::TypeParam(tp) => Some((n, substs.get(&tp)?.clone())), | ||
218 | _ => None, | ||
219 | } | ||
220 | }) | ||
221 | .collect::<Vec<_>>(); | ||
222 | |||
223 | if type_param_replacements.is_empty() { | ||
224 | node.value | ||
225 | } else { | ||
226 | edit::replace_descendants(&node.value, type_param_replacements.into_iter()) | ||
227 | } | ||
228 | } | ||
229 | |||
163 | /// Given an `ast::ImplBlock`, resolves the target trait (the one being | 230 | /// Given an `ast::ImplBlock`, resolves the target trait (the one being |
164 | /// implemented) to a `ast::TraitDef`. | 231 | /// implemented) to a `ast::TraitDef`. |
165 | fn resolve_target_trait_def( | 232 | fn resolve_target_trait_def( |
166 | db: &impl HirDatabase, | 233 | db: &impl HirDatabase, |
167 | analyzer: &hir::SourceAnalyzer, | 234 | analyzer: &hir::SourceAnalyzer, |
168 | impl_block: &ast::ImplBlock, | 235 | impl_block: &ast::ImplBlock, |
169 | ) -> Option<ast::TraitDef> { | 236 | ) -> Option<(hir::Trait, ast::TraitDef)> { |
170 | let ast_path = impl_block | 237 | let ast_path = impl_block |
171 | .target_trait() | 238 | .target_trait() |
172 | .map(|it| it.syntax().clone()) | 239 | .map(|it| it.syntax().clone()) |
@@ -174,7 +241,9 @@ fn resolve_target_trait_def( | |||
174 | .path()?; | 241 | .path()?; |
175 | 242 | ||
176 | match analyzer.resolve_path(db, &ast_path) { | 243 | match analyzer.resolve_path(db, &ast_path) { |
177 | Some(hir::PathResolution::Def(hir::ModuleDef::Trait(def))) => Some(def.source(db).value), | 244 | Some(hir::PathResolution::Def(hir::ModuleDef::Trait(def))) => { |
245 | Some((def, def.source(db).value)) | ||
246 | } | ||
178 | _ => None, | 247 | _ => None, |
179 | } | 248 | } |
180 | } | 249 | } |
@@ -281,6 +350,40 @@ impl Foo for S { | |||
281 | } | 350 | } |
282 | 351 | ||
283 | #[test] | 352 | #[test] |
353 | fn fill_in_type_params_1() { | ||
354 | check_assist( | ||
355 | add_missing_impl_members, | ||
356 | " | ||
357 | trait Foo<T> { fn foo(&self, t: T) -> &T; } | ||
358 | struct S; | ||
359 | impl Foo<u32> for S { <|> }", | ||
360 | " | ||
361 | trait Foo<T> { fn foo(&self, t: T) -> &T; } | ||
362 | struct S; | ||
363 | impl Foo<u32> for S { | ||
364 | <|>fn foo(&self, t: u32) -> &u32 { unimplemented!() } | ||
365 | }", | ||
366 | ); | ||
367 | } | ||
368 | |||
369 | #[test] | ||
370 | fn fill_in_type_params_2() { | ||
371 | check_assist( | ||
372 | add_missing_impl_members, | ||
373 | " | ||
374 | trait Foo<T> { fn foo(&self, t: T) -> &T; } | ||
375 | struct S; | ||
376 | impl<U> Foo<U> for S { <|> }", | ||
377 | " | ||
378 | trait Foo<T> { fn foo(&self, t: T) -> &T; } | ||
379 | struct S; | ||
380 | impl<U> Foo<U> for S { | ||
381 | <|>fn foo(&self, t: U) -> &U { unimplemented!() } | ||
382 | }", | ||
383 | ); | ||
384 | } | ||
385 | |||
386 | #[test] | ||
284 | fn test_cursor_after_empty_impl_block() { | 387 | fn test_cursor_after_empty_impl_block() { |
285 | check_assist( | 388 | check_assist( |
286 | add_missing_impl_members, | 389 | add_missing_impl_members, |
diff --git a/crates/ra_assists/src/doc_tests/generated.rs b/crates/ra_assists/src/doc_tests/generated.rs index 4586eeb59..7d84dc8fb 100644 --- a/crates/ra_assists/src/doc_tests/generated.rs +++ b/crates/ra_assists/src/doc_tests/generated.rs | |||
@@ -101,26 +101,26 @@ fn doctest_add_impl_default_members() { | |||
101 | check( | 101 | check( |
102 | "add_impl_default_members", | 102 | "add_impl_default_members", |
103 | r#####" | 103 | r#####" |
104 | trait T { | 104 | trait Trait { |
105 | Type X; | 105 | Type X; |
106 | fn foo(&self); | 106 | fn foo(&self); |
107 | fn bar(&self) {} | 107 | fn bar(&self) {} |
108 | } | 108 | } |
109 | 109 | ||
110 | impl T for () { | 110 | impl Trait for () { |
111 | Type X = (); | 111 | Type X = (); |
112 | fn foo(&self) {}<|> | 112 | fn foo(&self) {}<|> |
113 | 113 | ||
114 | } | 114 | } |
115 | "#####, | 115 | "#####, |
116 | r#####" | 116 | r#####" |
117 | trait T { | 117 | trait Trait { |
118 | Type X; | 118 | Type X; |
119 | fn foo(&self); | 119 | fn foo(&self); |
120 | fn bar(&self) {} | 120 | fn bar(&self) {} |
121 | } | 121 | } |
122 | 122 | ||
123 | impl T for () { | 123 | impl Trait for () { |
124 | Type X = (); | 124 | Type X = (); |
125 | fn foo(&self) {} | 125 | fn foo(&self) {} |
126 | fn bar(&self) {} | 126 | fn bar(&self) {} |
@@ -135,25 +135,25 @@ fn doctest_add_impl_missing_members() { | |||
135 | check( | 135 | check( |
136 | "add_impl_missing_members", | 136 | "add_impl_missing_members", |
137 | r#####" | 137 | r#####" |
138 | trait T { | 138 | trait Trait<T> { |
139 | Type X; | 139 | Type X; |
140 | fn foo(&self); | 140 | fn foo(&self) -> T; |
141 | fn bar(&self) {} | 141 | fn bar(&self) {} |
142 | } | 142 | } |
143 | 143 | ||
144 | impl T for () {<|> | 144 | impl Trait<u32> for () {<|> |
145 | 145 | ||
146 | } | 146 | } |
147 | "#####, | 147 | "#####, |
148 | r#####" | 148 | r#####" |
149 | trait T { | 149 | trait Trait<T> { |
150 | Type X; | 150 | Type X; |
151 | fn foo(&self); | 151 | fn foo(&self) -> T; |
152 | fn bar(&self) {} | 152 | fn bar(&self) {} |
153 | } | 153 | } |
154 | 154 | ||
155 | impl T for () { | 155 | impl Trait<u32> for () { |
156 | fn foo(&self) { unimplemented!() } | 156 | fn foo(&self) -> u32 { unimplemented!() } |
157 | 157 | ||
158 | } | 158 | } |
159 | "#####, | 159 | "#####, |
diff --git a/crates/ra_cargo_watch/Cargo.toml b/crates/ra_cargo_watch/Cargo.toml new file mode 100644 index 000000000..bcc4648ff --- /dev/null +++ b/crates/ra_cargo_watch/Cargo.toml | |||
@@ -0,0 +1,17 @@ | |||
1 | [package] | ||
2 | edition = "2018" | ||
3 | name = "ra_cargo_watch" | ||
4 | version = "0.1.0" | ||
5 | authors = ["rust-analyzer developers"] | ||
6 | |||
7 | [dependencies] | ||
8 | crossbeam-channel = "0.4" | ||
9 | lsp-types = { version = "0.67.0", features = ["proposed"] } | ||
10 | log = "0.4.3" | ||
11 | cargo_metadata = "0.9.1" | ||
12 | jod-thread = "0.1.0" | ||
13 | parking_lot = "0.10.0" | ||
14 | |||
15 | [dev-dependencies] | ||
16 | insta = "0.12.0" | ||
17 | serde_json = "1.0" \ No newline at end of file | ||
diff --git a/crates/ra_cargo_watch/src/conv.rs b/crates/ra_cargo_watch/src/conv.rs new file mode 100644 index 000000000..3bd4bf7a5 --- /dev/null +++ b/crates/ra_cargo_watch/src/conv.rs | |||
@@ -0,0 +1,280 @@ | |||
1 | //! This module provides the functionality needed to convert diagnostics from | ||
2 | //! `cargo check` json format to the LSP diagnostic format. | ||
3 | use cargo_metadata::diagnostic::{ | ||
4 | Applicability, Diagnostic as RustDiagnostic, DiagnosticLevel, DiagnosticSpan, | ||
5 | DiagnosticSpanMacroExpansion, | ||
6 | }; | ||
7 | use lsp_types::{ | ||
8 | Diagnostic, DiagnosticRelatedInformation, DiagnosticSeverity, DiagnosticTag, Location, | ||
9 | NumberOrString, Position, Range, Url, | ||
10 | }; | ||
11 | use std::{fmt::Write, path::PathBuf}; | ||
12 | |||
13 | #[cfg(test)] | ||
14 | mod test; | ||
15 | |||
16 | /// Converts a Rust level string to a LSP severity | ||
17 | fn map_level_to_severity(val: DiagnosticLevel) -> Option<DiagnosticSeverity> { | ||
18 | match val { | ||
19 | DiagnosticLevel::Ice => Some(DiagnosticSeverity::Error), | ||
20 | DiagnosticLevel::Error => Some(DiagnosticSeverity::Error), | ||
21 | DiagnosticLevel::Warning => Some(DiagnosticSeverity::Warning), | ||
22 | DiagnosticLevel::Note => Some(DiagnosticSeverity::Information), | ||
23 | DiagnosticLevel::Help => Some(DiagnosticSeverity::Hint), | ||
24 | DiagnosticLevel::Unknown => None, | ||
25 | } | ||
26 | } | ||
27 | |||
28 | /// Check whether a file name is from macro invocation | ||
29 | fn is_from_macro(file_name: &str) -> bool { | ||
30 | file_name.starts_with('<') && file_name.ends_with('>') | ||
31 | } | ||
32 | |||
33 | /// Converts a Rust macro span to a LSP location recursively | ||
34 | fn map_macro_span_to_location( | ||
35 | span_macro: &DiagnosticSpanMacroExpansion, | ||
36 | workspace_root: &PathBuf, | ||
37 | ) -> Option<Location> { | ||
38 | if !is_from_macro(&span_macro.span.file_name) { | ||
39 | return Some(map_span_to_location(&span_macro.span, workspace_root)); | ||
40 | } | ||
41 | |||
42 | if let Some(expansion) = &span_macro.span.expansion { | ||
43 | return map_macro_span_to_location(&expansion, workspace_root); | ||
44 | } | ||
45 | |||
46 | None | ||
47 | } | ||
48 | |||
49 | /// Converts a Rust span to a LSP location | ||
50 | fn map_span_to_location(span: &DiagnosticSpan, workspace_root: &PathBuf) -> Location { | ||
51 | if is_from_macro(&span.file_name) && span.expansion.is_some() { | ||
52 | let expansion = span.expansion.as_ref().unwrap(); | ||
53 | if let Some(macro_range) = map_macro_span_to_location(&expansion, workspace_root) { | ||
54 | return macro_range; | ||
55 | } | ||
56 | } | ||
57 | |||
58 | let mut file_name = workspace_root.clone(); | ||
59 | file_name.push(&span.file_name); | ||
60 | let uri = Url::from_file_path(file_name).unwrap(); | ||
61 | |||
62 | let range = Range::new( | ||
63 | Position::new(span.line_start as u64 - 1, span.column_start as u64 - 1), | ||
64 | Position::new(span.line_end as u64 - 1, span.column_end as u64 - 1), | ||
65 | ); | ||
66 | |||
67 | Location { uri, range } | ||
68 | } | ||
69 | |||
70 | /// Converts a secondary Rust span to a LSP related information | ||
71 | /// | ||
72 | /// If the span is unlabelled this will return `None`. | ||
73 | fn map_secondary_span_to_related( | ||
74 | span: &DiagnosticSpan, | ||
75 | workspace_root: &PathBuf, | ||
76 | ) -> Option<DiagnosticRelatedInformation> { | ||
77 | if let Some(label) = &span.label { | ||
78 | let location = map_span_to_location(span, workspace_root); | ||
79 | Some(DiagnosticRelatedInformation { location, message: label.clone() }) | ||
80 | } else { | ||
81 | // Nothing to label this with | ||
82 | None | ||
83 | } | ||
84 | } | ||
85 | |||
86 | /// Determines if diagnostic is related to unused code | ||
87 | fn is_unused_or_unnecessary(rd: &RustDiagnostic) -> bool { | ||
88 | if let Some(code) = &rd.code { | ||
89 | match code.code.as_str() { | ||
90 | "dead_code" | "unknown_lints" | "unreachable_code" | "unused_attributes" | ||
91 | | "unused_imports" | "unused_macros" | "unused_variables" => true, | ||
92 | _ => false, | ||
93 | } | ||
94 | } else { | ||
95 | false | ||
96 | } | ||
97 | } | ||
98 | |||
99 | /// Determines if diagnostic is related to deprecated code | ||
100 | fn is_deprecated(rd: &RustDiagnostic) -> bool { | ||
101 | if let Some(code) = &rd.code { | ||
102 | match code.code.as_str() { | ||
103 | "deprecated" => true, | ||
104 | _ => false, | ||
105 | } | ||
106 | } else { | ||
107 | false | ||
108 | } | ||
109 | } | ||
110 | |||
111 | #[derive(Debug)] | ||
112 | pub struct SuggestedFix { | ||
113 | pub title: String, | ||
114 | pub location: Location, | ||
115 | pub replacement: String, | ||
116 | pub applicability: Applicability, | ||
117 | pub diagnostics: Vec<Diagnostic>, | ||
118 | } | ||
119 | |||
120 | impl std::cmp::PartialEq<SuggestedFix> for SuggestedFix { | ||
121 | fn eq(&self, other: &SuggestedFix) -> bool { | ||
122 | if self.title == other.title | ||
123 | && self.location == other.location | ||
124 | && self.replacement == other.replacement | ||
125 | { | ||
126 | // Applicability doesn't impl PartialEq... | ||
127 | match (&self.applicability, &other.applicability) { | ||
128 | (Applicability::MachineApplicable, Applicability::MachineApplicable) => true, | ||
129 | (Applicability::HasPlaceholders, Applicability::HasPlaceholders) => true, | ||
130 | (Applicability::MaybeIncorrect, Applicability::MaybeIncorrect) => true, | ||
131 | (Applicability::Unspecified, Applicability::Unspecified) => true, | ||
132 | _ => false, | ||
133 | } | ||
134 | } else { | ||
135 | false | ||
136 | } | ||
137 | } | ||
138 | } | ||
139 | |||
140 | enum MappedRustChildDiagnostic { | ||
141 | Related(DiagnosticRelatedInformation), | ||
142 | SuggestedFix(SuggestedFix), | ||
143 | MessageLine(String), | ||
144 | } | ||
145 | |||
146 | fn map_rust_child_diagnostic( | ||
147 | rd: &RustDiagnostic, | ||
148 | workspace_root: &PathBuf, | ||
149 | ) -> MappedRustChildDiagnostic { | ||
150 | let span: &DiagnosticSpan = match rd.spans.iter().find(|s| s.is_primary) { | ||
151 | Some(span) => span, | ||
152 | None => { | ||
153 | // `rustc` uses these spanless children as a way to print multi-line | ||
154 | // messages | ||
155 | return MappedRustChildDiagnostic::MessageLine(rd.message.clone()); | ||
156 | } | ||
157 | }; | ||
158 | |||
159 | // If we have a primary span use its location, otherwise use the parent | ||
160 | let location = map_span_to_location(&span, workspace_root); | ||
161 | |||
162 | if let Some(suggested_replacement) = &span.suggested_replacement { | ||
163 | // Include our replacement in the title unless it's empty | ||
164 | let title = if !suggested_replacement.is_empty() { | ||
165 | format!("{}: '{}'", rd.message, suggested_replacement) | ||
166 | } else { | ||
167 | rd.message.clone() | ||
168 | }; | ||
169 | |||
170 | MappedRustChildDiagnostic::SuggestedFix(SuggestedFix { | ||
171 | title, | ||
172 | location, | ||
173 | replacement: suggested_replacement.clone(), | ||
174 | applicability: span.suggestion_applicability.clone().unwrap_or(Applicability::Unknown), | ||
175 | diagnostics: vec![], | ||
176 | }) | ||
177 | } else { | ||
178 | MappedRustChildDiagnostic::Related(DiagnosticRelatedInformation { | ||
179 | location, | ||
180 | message: rd.message.clone(), | ||
181 | }) | ||
182 | } | ||
183 | } | ||
184 | |||
185 | #[derive(Debug)] | ||
186 | pub(crate) struct MappedRustDiagnostic { | ||
187 | pub location: Location, | ||
188 | pub diagnostic: Diagnostic, | ||
189 | pub suggested_fixes: Vec<SuggestedFix>, | ||
190 | } | ||
191 | |||
192 | /// Converts a Rust root diagnostic to LSP form | ||
193 | /// | ||
194 | /// This flattens the Rust diagnostic by: | ||
195 | /// | ||
196 | /// 1. Creating a LSP diagnostic with the root message and primary span. | ||
197 | /// 2. Adding any labelled secondary spans to `relatedInformation` | ||
198 | /// 3. Categorising child diagnostics as either `SuggestedFix`es, | ||
199 | /// `relatedInformation` or additional message lines. | ||
200 | /// | ||
201 | /// If the diagnostic has no primary span this will return `None` | ||
202 | pub(crate) fn map_rust_diagnostic_to_lsp( | ||
203 | rd: &RustDiagnostic, | ||
204 | workspace_root: &PathBuf, | ||
205 | ) -> Option<MappedRustDiagnostic> { | ||
206 | let primary_span = rd.spans.iter().find(|s| s.is_primary)?; | ||
207 | |||
208 | let location = map_span_to_location(&primary_span, workspace_root); | ||
209 | |||
210 | let severity = map_level_to_severity(rd.level); | ||
211 | let mut primary_span_label = primary_span.label.as_ref(); | ||
212 | |||
213 | let mut source = String::from("rustc"); | ||
214 | let mut code = rd.code.as_ref().map(|c| c.code.clone()); | ||
215 | if let Some(code_val) = &code { | ||
216 | // See if this is an RFC #2103 scoped lint (e.g. from Clippy) | ||
217 | let scoped_code: Vec<&str> = code_val.split("::").collect(); | ||
218 | if scoped_code.len() == 2 { | ||
219 | source = String::from(scoped_code[0]); | ||
220 | code = Some(String::from(scoped_code[1])); | ||
221 | } | ||
222 | } | ||
223 | |||
224 | let mut related_information = vec![]; | ||
225 | let mut tags = vec![]; | ||
226 | |||
227 | for secondary_span in rd.spans.iter().filter(|s| !s.is_primary) { | ||
228 | let related = map_secondary_span_to_related(secondary_span, workspace_root); | ||
229 | if let Some(related) = related { | ||
230 | related_information.push(related); | ||
231 | } | ||
232 | } | ||
233 | |||
234 | let mut suggested_fixes = vec![]; | ||
235 | let mut message = rd.message.clone(); | ||
236 | for child in &rd.children { | ||
237 | let child = map_rust_child_diagnostic(&child, workspace_root); | ||
238 | match child { | ||
239 | MappedRustChildDiagnostic::Related(related) => related_information.push(related), | ||
240 | MappedRustChildDiagnostic::SuggestedFix(suggested_fix) => { | ||
241 | suggested_fixes.push(suggested_fix) | ||
242 | } | ||
243 | MappedRustChildDiagnostic::MessageLine(message_line) => { | ||
244 | write!(&mut message, "\n{}", message_line).unwrap(); | ||
245 | |||
246 | // These secondary messages usually duplicate the content of the | ||
247 | // primary span label. | ||
248 | primary_span_label = None; | ||
249 | } | ||
250 | } | ||
251 | } | ||
252 | |||
253 | if let Some(primary_span_label) = primary_span_label { | ||
254 | write!(&mut message, "\n{}", primary_span_label).unwrap(); | ||
255 | } | ||
256 | |||
257 | if is_unused_or_unnecessary(rd) { | ||
258 | tags.push(DiagnosticTag::Unnecessary); | ||
259 | } | ||
260 | |||
261 | if is_deprecated(rd) { | ||
262 | tags.push(DiagnosticTag::Deprecated); | ||
263 | } | ||
264 | |||
265 | let diagnostic = Diagnostic { | ||
266 | range: location.range, | ||
267 | severity, | ||
268 | code: code.map(NumberOrString::String), | ||
269 | source: Some(source), | ||
270 | message, | ||
271 | related_information: if !related_information.is_empty() { | ||
272 | Some(related_information) | ||
273 | } else { | ||
274 | None | ||
275 | }, | ||
276 | tags: if !tags.is_empty() { Some(tags) } else { None }, | ||
277 | }; | ||
278 | |||
279 | Some(MappedRustDiagnostic { location, diagnostic, suggested_fixes }) | ||
280 | } | ||
diff --git a/crates/ra_cargo_watch/src/conv/snapshots/test__snap_clippy_pass_by_ref.snap b/crates/ra_cargo_watch/src/conv/snapshots/test__snap_clippy_pass_by_ref.snap new file mode 100644 index 000000000..cb0920914 --- /dev/null +++ b/crates/ra_cargo_watch/src/conv/snapshots/test__snap_clippy_pass_by_ref.snap | |||
@@ -0,0 +1,85 @@ | |||
1 | --- | ||
2 | source: crates/ra_cargo_watch/src/conv/test.rs | ||
3 | expression: diag | ||
4 | --- | ||
5 | MappedRustDiagnostic { | ||
6 | location: Location { | ||
7 | uri: "file:///test/compiler/mir/tagset.rs", | ||
8 | range: Range { | ||
9 | start: Position { | ||
10 | line: 41, | ||
11 | character: 23, | ||
12 | }, | ||
13 | end: Position { | ||
14 | line: 41, | ||
15 | character: 28, | ||
16 | }, | ||
17 | }, | ||
18 | }, | ||
19 | diagnostic: Diagnostic { | ||
20 | range: Range { | ||
21 | start: Position { | ||
22 | line: 41, | ||
23 | character: 23, | ||
24 | }, | ||
25 | end: Position { | ||
26 | line: 41, | ||
27 | character: 28, | ||
28 | }, | ||
29 | }, | ||
30 | severity: Some( | ||
31 | Warning, | ||
32 | ), | ||
33 | code: Some( | ||
34 | String( | ||
35 | "trivially_copy_pass_by_ref", | ||
36 | ), | ||
37 | ), | ||
38 | source: Some( | ||
39 | "clippy", | ||
40 | ), | ||
41 | message: "this argument is passed by reference, but would be more efficient if passed by value\n#[warn(clippy::trivially_copy_pass_by_ref)] implied by #[warn(clippy::all)]\nfor further information visit https://rust-lang.github.io/rust-clippy/master/index.html#trivially_copy_pass_by_ref", | ||
42 | related_information: Some( | ||
43 | [ | ||
44 | DiagnosticRelatedInformation { | ||
45 | location: Location { | ||
46 | uri: "file:///test/compiler/lib.rs", | ||
47 | range: Range { | ||
48 | start: Position { | ||
49 | line: 0, | ||
50 | character: 8, | ||
51 | }, | ||
52 | end: Position { | ||
53 | line: 0, | ||
54 | character: 19, | ||
55 | }, | ||
56 | }, | ||
57 | }, | ||
58 | message: "lint level defined here", | ||
59 | }, | ||
60 | ], | ||
61 | ), | ||
62 | tags: None, | ||
63 | }, | ||
64 | suggested_fixes: [ | ||
65 | SuggestedFix { | ||
66 | title: "consider passing by value instead: \'self\'", | ||
67 | location: Location { | ||
68 | uri: "file:///test/compiler/mir/tagset.rs", | ||
69 | range: Range { | ||
70 | start: Position { | ||
71 | line: 41, | ||
72 | character: 23, | ||
73 | }, | ||
74 | end: Position { | ||
75 | line: 41, | ||
76 | character: 28, | ||
77 | }, | ||
78 | }, | ||
79 | }, | ||
80 | replacement: "self", | ||
81 | applicability: Unspecified, | ||
82 | diagnostics: [], | ||
83 | }, | ||
84 | ], | ||
85 | } | ||
diff --git a/crates/ra_cargo_watch/src/conv/snapshots/test__snap_handles_macro_location.snap b/crates/ra_cargo_watch/src/conv/snapshots/test__snap_handles_macro_location.snap new file mode 100644 index 000000000..19510ecc1 --- /dev/null +++ b/crates/ra_cargo_watch/src/conv/snapshots/test__snap_handles_macro_location.snap | |||
@@ -0,0 +1,46 @@ | |||
1 | --- | ||
2 | source: crates/ra_cargo_watch/src/conv/test.rs | ||
3 | expression: diag | ||
4 | --- | ||
5 | MappedRustDiagnostic { | ||
6 | location: Location { | ||
7 | uri: "file:///test/src/main.rs", | ||
8 | range: Range { | ||
9 | start: Position { | ||
10 | line: 1, | ||
11 | character: 4, | ||
12 | }, | ||
13 | end: Position { | ||
14 | line: 1, | ||
15 | character: 26, | ||
16 | }, | ||
17 | }, | ||
18 | }, | ||
19 | diagnostic: Diagnostic { | ||
20 | range: Range { | ||
21 | start: Position { | ||
22 | line: 1, | ||
23 | character: 4, | ||
24 | }, | ||
25 | end: Position { | ||
26 | line: 1, | ||
27 | character: 26, | ||
28 | }, | ||
29 | }, | ||
30 | severity: Some( | ||
31 | Error, | ||
32 | ), | ||
33 | code: Some( | ||
34 | String( | ||
35 | "E0277", | ||
36 | ), | ||
37 | ), | ||
38 | source: Some( | ||
39 | "rustc", | ||
40 | ), | ||
41 | message: "can\'t compare `{integer}` with `&str`\nthe trait `std::cmp::PartialEq<&str>` is not implemented for `{integer}`", | ||
42 | related_information: None, | ||
43 | tags: None, | ||
44 | }, | ||
45 | suggested_fixes: [], | ||
46 | } | ||
diff --git a/crates/ra_cargo_watch/src/conv/snapshots/test__snap_rustc_incompatible_type_for_trait.snap b/crates/ra_cargo_watch/src/conv/snapshots/test__snap_rustc_incompatible_type_for_trait.snap new file mode 100644 index 000000000..cf683e4b6 --- /dev/null +++ b/crates/ra_cargo_watch/src/conv/snapshots/test__snap_rustc_incompatible_type_for_trait.snap | |||
@@ -0,0 +1,46 @@ | |||
1 | --- | ||
2 | source: crates/ra_cargo_watch/src/conv/test.rs | ||
3 | expression: diag | ||
4 | --- | ||
5 | MappedRustDiagnostic { | ||
6 | location: Location { | ||
7 | uri: "file:///test/compiler/ty/list_iter.rs", | ||
8 | range: Range { | ||
9 | start: Position { | ||
10 | line: 51, | ||
11 | character: 4, | ||
12 | }, | ||
13 | end: Position { | ||
14 | line: 51, | ||
15 | character: 47, | ||
16 | }, | ||
17 | }, | ||
18 | }, | ||
19 | diagnostic: Diagnostic { | ||
20 | range: Range { | ||
21 | start: Position { | ||
22 | line: 51, | ||
23 | character: 4, | ||
24 | }, | ||
25 | end: Position { | ||
26 | line: 51, | ||
27 | character: 47, | ||
28 | }, | ||
29 | }, | ||
30 | severity: Some( | ||
31 | Error, | ||
32 | ), | ||
33 | code: Some( | ||
34 | String( | ||
35 | "E0053", | ||
36 | ), | ||
37 | ), | ||
38 | source: Some( | ||
39 | "rustc", | ||
40 | ), | ||
41 | message: "method `next` has an incompatible type for trait\nexpected type `fn(&mut ty::list_iter::ListIterator<\'list, M>) -> std::option::Option<&ty::Ref<M>>`\n found type `fn(&ty::list_iter::ListIterator<\'list, M>) -> std::option::Option<&\'list ty::Ref<M>>`", | ||
42 | related_information: None, | ||
43 | tags: None, | ||
44 | }, | ||
45 | suggested_fixes: [], | ||
46 | } | ||
diff --git a/crates/ra_cargo_watch/src/conv/snapshots/test__snap_rustc_mismatched_type.snap b/crates/ra_cargo_watch/src/conv/snapshots/test__snap_rustc_mismatched_type.snap new file mode 100644 index 000000000..8c1483c74 --- /dev/null +++ b/crates/ra_cargo_watch/src/conv/snapshots/test__snap_rustc_mismatched_type.snap | |||
@@ -0,0 +1,46 @@ | |||
1 | --- | ||
2 | source: crates/ra_cargo_watch/src/conv/test.rs | ||
3 | expression: diag | ||
4 | --- | ||
5 | MappedRustDiagnostic { | ||
6 | location: Location { | ||
7 | uri: "file:///test/runtime/compiler_support.rs", | ||
8 | range: Range { | ||
9 | start: Position { | ||
10 | line: 47, | ||
11 | character: 64, | ||
12 | }, | ||
13 | end: Position { | ||
14 | line: 47, | ||
15 | character: 69, | ||
16 | }, | ||
17 | }, | ||
18 | }, | ||
19 | diagnostic: Diagnostic { | ||
20 | range: Range { | ||
21 | start: Position { | ||
22 | line: 47, | ||
23 | character: 64, | ||
24 | }, | ||
25 | end: Position { | ||
26 | line: 47, | ||
27 | character: 69, | ||
28 | }, | ||
29 | }, | ||
30 | severity: Some( | ||
31 | Error, | ||
32 | ), | ||
33 | code: Some( | ||
34 | String( | ||
35 | "E0308", | ||
36 | ), | ||
37 | ), | ||
38 | source: Some( | ||
39 | "rustc", | ||
40 | ), | ||
41 | message: "mismatched types\nexpected usize, found u32", | ||
42 | related_information: None, | ||
43 | tags: None, | ||
44 | }, | ||
45 | suggested_fixes: [], | ||
46 | } | ||
diff --git a/crates/ra_cargo_watch/src/conv/snapshots/test__snap_rustc_unused_variable.snap b/crates/ra_cargo_watch/src/conv/snapshots/test__snap_rustc_unused_variable.snap new file mode 100644 index 000000000..eb5a2247b --- /dev/null +++ b/crates/ra_cargo_watch/src/conv/snapshots/test__snap_rustc_unused_variable.snap | |||
@@ -0,0 +1,70 @@ | |||
1 | --- | ||
2 | source: crates/ra_cargo_watch/src/conv/test.rs | ||
3 | expression: diag | ||
4 | --- | ||
5 | MappedRustDiagnostic { | ||
6 | location: Location { | ||
7 | uri: "file:///test/driver/subcommand/repl.rs", | ||
8 | range: Range { | ||
9 | start: Position { | ||
10 | line: 290, | ||
11 | character: 8, | ||
12 | }, | ||
13 | end: Position { | ||
14 | line: 290, | ||
15 | character: 11, | ||
16 | }, | ||
17 | }, | ||
18 | }, | ||
19 | diagnostic: Diagnostic { | ||
20 | range: Range { | ||
21 | start: Position { | ||
22 | line: 290, | ||
23 | character: 8, | ||
24 | }, | ||
25 | end: Position { | ||
26 | line: 290, | ||
27 | character: 11, | ||
28 | }, | ||
29 | }, | ||
30 | severity: Some( | ||
31 | Warning, | ||
32 | ), | ||
33 | code: Some( | ||
34 | String( | ||
35 | "unused_variables", | ||
36 | ), | ||
37 | ), | ||
38 | source: Some( | ||
39 | "rustc", | ||
40 | ), | ||
41 | message: "unused variable: `foo`\n#[warn(unused_variables)] on by default", | ||
42 | related_information: None, | ||
43 | tags: Some( | ||
44 | [ | ||
45 | Unnecessary, | ||
46 | ], | ||
47 | ), | ||
48 | }, | ||
49 | suggested_fixes: [ | ||
50 | SuggestedFix { | ||
51 | title: "consider prefixing with an underscore: \'_foo\'", | ||
52 | location: Location { | ||
53 | uri: "file:///test/driver/subcommand/repl.rs", | ||
54 | range: Range { | ||
55 | start: Position { | ||
56 | line: 290, | ||
57 | character: 8, | ||
58 | }, | ||
59 | end: Position { | ||
60 | line: 290, | ||
61 | character: 11, | ||
62 | }, | ||
63 | }, | ||
64 | }, | ||
65 | replacement: "_foo", | ||
66 | applicability: MachineApplicable, | ||
67 | diagnostics: [], | ||
68 | }, | ||
69 | ], | ||
70 | } | ||
diff --git a/crates/ra_cargo_watch/src/conv/snapshots/test__snap_rustc_wrong_number_of_parameters.snap b/crates/ra_cargo_watch/src/conv/snapshots/test__snap_rustc_wrong_number_of_parameters.snap new file mode 100644 index 000000000..2f4518931 --- /dev/null +++ b/crates/ra_cargo_watch/src/conv/snapshots/test__snap_rustc_wrong_number_of_parameters.snap | |||
@@ -0,0 +1,65 @@ | |||
1 | --- | ||
2 | source: crates/ra_cargo_watch/src/conv/test.rs | ||
3 | expression: diag | ||
4 | --- | ||
5 | MappedRustDiagnostic { | ||
6 | location: Location { | ||
7 | uri: "file:///test/compiler/ty/select.rs", | ||
8 | range: Range { | ||
9 | start: Position { | ||
10 | line: 103, | ||
11 | character: 17, | ||
12 | }, | ||
13 | end: Position { | ||
14 | line: 103, | ||
15 | character: 29, | ||
16 | }, | ||
17 | }, | ||
18 | }, | ||
19 | diagnostic: Diagnostic { | ||
20 | range: Range { | ||
21 | start: Position { | ||
22 | line: 103, | ||
23 | character: 17, | ||
24 | }, | ||
25 | end: Position { | ||
26 | line: 103, | ||
27 | character: 29, | ||
28 | }, | ||
29 | }, | ||
30 | severity: Some( | ||
31 | Error, | ||
32 | ), | ||
33 | code: Some( | ||
34 | String( | ||
35 | "E0061", | ||
36 | ), | ||
37 | ), | ||
38 | source: Some( | ||
39 | "rustc", | ||
40 | ), | ||
41 | message: "this function takes 2 parameters but 3 parameters were supplied\nexpected 2 parameters", | ||
42 | related_information: Some( | ||
43 | [ | ||
44 | DiagnosticRelatedInformation { | ||
45 | location: Location { | ||
46 | uri: "file:///test/compiler/ty/select.rs", | ||
47 | range: Range { | ||
48 | start: Position { | ||
49 | line: 218, | ||
50 | character: 4, | ||
51 | }, | ||
52 | end: Position { | ||
53 | line: 230, | ||
54 | character: 5, | ||
55 | }, | ||
56 | }, | ||
57 | }, | ||
58 | message: "defined here", | ||
59 | }, | ||
60 | ], | ||
61 | ), | ||
62 | tags: None, | ||
63 | }, | ||
64 | suggested_fixes: [], | ||
65 | } | ||
diff --git a/crates/ra_cargo_watch/src/conv/test.rs b/crates/ra_cargo_watch/src/conv/test.rs new file mode 100644 index 000000000..6817245c2 --- /dev/null +++ b/crates/ra_cargo_watch/src/conv/test.rs | |||
@@ -0,0 +1,700 @@ | |||
1 | //! This module contains the large and verbose snapshot tests for the | ||
2 | //! conversions between `cargo check` json and LSP diagnostics. | ||
3 | use crate::*; | ||
4 | |||
5 | fn parse_diagnostic(val: &str) -> cargo_metadata::diagnostic::Diagnostic { | ||
6 | serde_json::from_str::<cargo_metadata::diagnostic::Diagnostic>(val).unwrap() | ||
7 | } | ||
8 | |||
9 | #[test] | ||
10 | fn snap_rustc_incompatible_type_for_trait() { | ||
11 | let diag = parse_diagnostic( | ||
12 | r##"{ | ||
13 | "message": "method `next` has an incompatible type for trait", | ||
14 | "code": { | ||
15 | "code": "E0053", | ||
16 | "explanation": "\nThe parameters of any trait method must match between a trait implementation\nand the trait definition.\n\nHere are a couple examples of this error:\n\n```compile_fail,E0053\ntrait Foo {\n fn foo(x: u16);\n fn bar(&self);\n}\n\nstruct Bar;\n\nimpl Foo for Bar {\n // error, expected u16, found i16\n fn foo(x: i16) { }\n\n // error, types differ in mutability\n fn bar(&mut self) { }\n}\n```\n" | ||
17 | }, | ||
18 | "level": "error", | ||
19 | "spans": [ | ||
20 | { | ||
21 | "file_name": "compiler/ty/list_iter.rs", | ||
22 | "byte_start": 1307, | ||
23 | "byte_end": 1350, | ||
24 | "line_start": 52, | ||
25 | "line_end": 52, | ||
26 | "column_start": 5, | ||
27 | "column_end": 48, | ||
28 | "is_primary": true, | ||
29 | "text": [ | ||
30 | { | ||
31 | "text": " fn next(&self) -> Option<&'list ty::Ref<M>> {", | ||
32 | "highlight_start": 5, | ||
33 | "highlight_end": 48 | ||
34 | } | ||
35 | ], | ||
36 | "label": "types differ in mutability", | ||
37 | "suggested_replacement": null, | ||
38 | "suggestion_applicability": null, | ||
39 | "expansion": null | ||
40 | } | ||
41 | ], | ||
42 | "children": [ | ||
43 | { | ||
44 | "message": "expected type `fn(&mut ty::list_iter::ListIterator<'list, M>) -> std::option::Option<&ty::Ref<M>>`\n found type `fn(&ty::list_iter::ListIterator<'list, M>) -> std::option::Option<&'list ty::Ref<M>>`", | ||
45 | "code": null, | ||
46 | "level": "note", | ||
47 | "spans": [], | ||
48 | "children": [], | ||
49 | "rendered": null | ||
50 | } | ||
51 | ], | ||
52 | "rendered": "error[E0053]: method `next` has an incompatible type for trait\n --> compiler/ty/list_iter.rs:52:5\n |\n52 | fn next(&self) -> Option<&'list ty::Ref<M>> {\n | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ types differ in mutability\n |\n = note: expected type `fn(&mut ty::list_iter::ListIterator<'list, M>) -> std::option::Option<&ty::Ref<M>>`\n found type `fn(&ty::list_iter::ListIterator<'list, M>) -> std::option::Option<&'list ty::Ref<M>>`\n\n" | ||
53 | } | ||
54 | "##, | ||
55 | ); | ||
56 | |||
57 | let workspace_root = PathBuf::from("/test/"); | ||
58 | let diag = map_rust_diagnostic_to_lsp(&diag, &workspace_root).expect("couldn't map diagnostic"); | ||
59 | insta::assert_debug_snapshot!(diag); | ||
60 | } | ||
61 | |||
62 | #[test] | ||
63 | fn snap_rustc_unused_variable() { | ||
64 | let diag = parse_diagnostic( | ||
65 | r##"{ | ||
66 | "message": "unused variable: `foo`", | ||
67 | "code": { | ||
68 | "code": "unused_variables", | ||
69 | "explanation": null | ||
70 | }, | ||
71 | "level": "warning", | ||
72 | "spans": [ | ||
73 | { | ||
74 | "file_name": "driver/subcommand/repl.rs", | ||
75 | "byte_start": 9228, | ||
76 | "byte_end": 9231, | ||
77 | "line_start": 291, | ||
78 | "line_end": 291, | ||
79 | "column_start": 9, | ||
80 | "column_end": 12, | ||
81 | "is_primary": true, | ||
82 | "text": [ | ||
83 | { | ||
84 | "text": " let foo = 42;", | ||
85 | "highlight_start": 9, | ||
86 | "highlight_end": 12 | ||
87 | } | ||
88 | ], | ||
89 | "label": null, | ||
90 | "suggested_replacement": null, | ||
91 | "suggestion_applicability": null, | ||
92 | "expansion": null | ||
93 | } | ||
94 | ], | ||
95 | "children": [ | ||
96 | { | ||
97 | "message": "#[warn(unused_variables)] on by default", | ||
98 | "code": null, | ||
99 | "level": "note", | ||
100 | "spans": [], | ||
101 | "children": [], | ||
102 | "rendered": null | ||
103 | }, | ||
104 | { | ||
105 | "message": "consider prefixing with an underscore", | ||
106 | "code": null, | ||
107 | "level": "help", | ||
108 | "spans": [ | ||
109 | { | ||
110 | "file_name": "driver/subcommand/repl.rs", | ||
111 | "byte_start": 9228, | ||
112 | "byte_end": 9231, | ||
113 | "line_start": 291, | ||
114 | "line_end": 291, | ||
115 | "column_start": 9, | ||
116 | "column_end": 12, | ||
117 | "is_primary": true, | ||
118 | "text": [ | ||
119 | { | ||
120 | "text": " let foo = 42;", | ||
121 | "highlight_start": 9, | ||
122 | "highlight_end": 12 | ||
123 | } | ||
124 | ], | ||
125 | "label": null, | ||
126 | "suggested_replacement": "_foo", | ||
127 | "suggestion_applicability": "MachineApplicable", | ||
128 | "expansion": null | ||
129 | } | ||
130 | ], | ||
131 | "children": [], | ||
132 | "rendered": null | ||
133 | } | ||
134 | ], | ||
135 | "rendered": "warning: unused variable: `foo`\n --> driver/subcommand/repl.rs:291:9\n |\n291 | let foo = 42;\n | ^^^ help: consider prefixing with an underscore: `_foo`\n |\n = note: #[warn(unused_variables)] on by default\n\n" | ||
136 | }"##, | ||
137 | ); | ||
138 | |||
139 | let workspace_root = PathBuf::from("/test/"); | ||
140 | let diag = map_rust_diagnostic_to_lsp(&diag, &workspace_root).expect("couldn't map diagnostic"); | ||
141 | insta::assert_debug_snapshot!(diag); | ||
142 | } | ||
143 | |||
144 | #[test] | ||
145 | fn snap_rustc_wrong_number_of_parameters() { | ||
146 | let diag = parse_diagnostic( | ||
147 | r##"{ | ||
148 | "message": "this function takes 2 parameters but 3 parameters were supplied", | ||
149 | "code": { | ||
150 | "code": "E0061", | ||
151 | "explanation": "\nThe number of arguments passed to a function must match the number of arguments\nspecified in the function signature.\n\nFor example, a function like:\n\n```\nfn f(a: u16, b: &str) {}\n```\n\nMust always be called with exactly two arguments, e.g., `f(2, \"test\")`.\n\nNote that Rust does not have a notion of optional function arguments or\nvariadic functions (except for its C-FFI).\n" | ||
152 | }, | ||
153 | "level": "error", | ||
154 | "spans": [ | ||
155 | { | ||
156 | "file_name": "compiler/ty/select.rs", | ||
157 | "byte_start": 8787, | ||
158 | "byte_end": 9241, | ||
159 | "line_start": 219, | ||
160 | "line_end": 231, | ||
161 | "column_start": 5, | ||
162 | "column_end": 6, | ||
163 | "is_primary": false, | ||
164 | "text": [ | ||
165 | { | ||
166 | "text": " pub fn add_evidence(", | ||
167 | "highlight_start": 5, | ||
168 | "highlight_end": 25 | ||
169 | }, | ||
170 | { | ||
171 | "text": " &mut self,", | ||
172 | "highlight_start": 1, | ||
173 | "highlight_end": 19 | ||
174 | }, | ||
175 | { | ||
176 | "text": " target_poly: &ty::Ref<ty::Poly>,", | ||
177 | "highlight_start": 1, | ||
178 | "highlight_end": 41 | ||
179 | }, | ||
180 | { | ||
181 | "text": " evidence_poly: &ty::Ref<ty::Poly>,", | ||
182 | "highlight_start": 1, | ||
183 | "highlight_end": 43 | ||
184 | }, | ||
185 | { | ||
186 | "text": " ) {", | ||
187 | "highlight_start": 1, | ||
188 | "highlight_end": 8 | ||
189 | }, | ||
190 | { | ||
191 | "text": " match target_poly {", | ||
192 | "highlight_start": 1, | ||
193 | "highlight_end": 28 | ||
194 | }, | ||
195 | { | ||
196 | "text": " ty::Ref::Var(tvar, _) => self.add_var_evidence(tvar, evidence_poly),", | ||
197 | "highlight_start": 1, | ||
198 | "highlight_end": 81 | ||
199 | }, | ||
200 | { | ||
201 | "text": " ty::Ref::Fixed(target_ty) => {", | ||
202 | "highlight_start": 1, | ||
203 | "highlight_end": 43 | ||
204 | }, | ||
205 | { | ||
206 | "text": " let evidence_ty = evidence_poly.resolve_to_ty();", | ||
207 | "highlight_start": 1, | ||
208 | "highlight_end": 65 | ||
209 | }, | ||
210 | { | ||
211 | "text": " self.add_evidence_ty(target_ty, evidence_poly, evidence_ty)", | ||
212 | "highlight_start": 1, | ||
213 | "highlight_end": 76 | ||
214 | }, | ||
215 | { | ||
216 | "text": " }", | ||
217 | "highlight_start": 1, | ||
218 | "highlight_end": 14 | ||
219 | }, | ||
220 | { | ||
221 | "text": " }", | ||
222 | "highlight_start": 1, | ||
223 | "highlight_end": 10 | ||
224 | }, | ||
225 | { | ||
226 | "text": " }", | ||
227 | "highlight_start": 1, | ||
228 | "highlight_end": 6 | ||
229 | } | ||
230 | ], | ||
231 | "label": "defined here", | ||
232 | "suggested_replacement": null, | ||
233 | "suggestion_applicability": null, | ||
234 | "expansion": null | ||
235 | }, | ||
236 | { | ||
237 | "file_name": "compiler/ty/select.rs", | ||
238 | "byte_start": 4045, | ||
239 | "byte_end": 4057, | ||
240 | "line_start": 104, | ||
241 | "line_end": 104, | ||
242 | "column_start": 18, | ||
243 | "column_end": 30, | ||
244 | "is_primary": true, | ||
245 | "text": [ | ||
246 | { | ||
247 | "text": " self.add_evidence(target_fixed, evidence_fixed, false);", | ||
248 | "highlight_start": 18, | ||
249 | "highlight_end": 30 | ||
250 | } | ||
251 | ], | ||
252 | "label": "expected 2 parameters", | ||
253 | "suggested_replacement": null, | ||
254 | "suggestion_applicability": null, | ||
255 | "expansion": null | ||
256 | } | ||
257 | ], | ||
258 | "children": [], | ||
259 | "rendered": "error[E0061]: this function takes 2 parameters but 3 parameters were supplied\n --> compiler/ty/select.rs:104:18\n |\n104 | self.add_evidence(target_fixed, evidence_fixed, false);\n | ^^^^^^^^^^^^ expected 2 parameters\n...\n219 | / pub fn add_evidence(\n220 | | &mut self,\n221 | | target_poly: &ty::Ref<ty::Poly>,\n222 | | evidence_poly: &ty::Ref<ty::Poly>,\n... |\n230 | | }\n231 | | }\n | |_____- defined here\n\n" | ||
260 | }"##, | ||
261 | ); | ||
262 | |||
263 | let workspace_root = PathBuf::from("/test/"); | ||
264 | let diag = map_rust_diagnostic_to_lsp(&diag, &workspace_root).expect("couldn't map diagnostic"); | ||
265 | insta::assert_debug_snapshot!(diag); | ||
266 | } | ||
267 | |||
268 | #[test] | ||
269 | fn snap_clippy_pass_by_ref() { | ||
270 | let diag = parse_diagnostic( | ||
271 | r##"{ | ||
272 | "message": "this argument is passed by reference, but would be more efficient if passed by value", | ||
273 | "code": { | ||
274 | "code": "clippy::trivially_copy_pass_by_ref", | ||
275 | "explanation": null | ||
276 | }, | ||
277 | "level": "warning", | ||
278 | "spans": [ | ||
279 | { | ||
280 | "file_name": "compiler/mir/tagset.rs", | ||
281 | "byte_start": 941, | ||
282 | "byte_end": 946, | ||
283 | "line_start": 42, | ||
284 | "line_end": 42, | ||
285 | "column_start": 24, | ||
286 | "column_end": 29, | ||
287 | "is_primary": true, | ||
288 | "text": [ | ||
289 | { | ||
290 | "text": " pub fn is_disjoint(&self, other: Self) -> bool {", | ||
291 | "highlight_start": 24, | ||
292 | "highlight_end": 29 | ||
293 | } | ||
294 | ], | ||
295 | "label": null, | ||
296 | "suggested_replacement": null, | ||
297 | "suggestion_applicability": null, | ||
298 | "expansion": null | ||
299 | } | ||
300 | ], | ||
301 | "children": [ | ||
302 | { | ||
303 | "message": "lint level defined here", | ||
304 | "code": null, | ||
305 | "level": "note", | ||
306 | "spans": [ | ||
307 | { | ||
308 | "file_name": "compiler/lib.rs", | ||
309 | "byte_start": 8, | ||
310 | "byte_end": 19, | ||
311 | "line_start": 1, | ||
312 | "line_end": 1, | ||
313 | "column_start": 9, | ||
314 | "column_end": 20, | ||
315 | "is_primary": true, | ||
316 | "text": [ | ||
317 | { | ||
318 | "text": "#![warn(clippy::all)]", | ||
319 | "highlight_start": 9, | ||
320 | "highlight_end": 20 | ||
321 | } | ||
322 | ], | ||
323 | "label": null, | ||
324 | "suggested_replacement": null, | ||
325 | "suggestion_applicability": null, | ||
326 | "expansion": null | ||
327 | } | ||
328 | ], | ||
329 | "children": [], | ||
330 | "rendered": null | ||
331 | }, | ||
332 | { | ||
333 | "message": "#[warn(clippy::trivially_copy_pass_by_ref)] implied by #[warn(clippy::all)]", | ||
334 | "code": null, | ||
335 | "level": "note", | ||
336 | "spans": [], | ||
337 | "children": [], | ||
338 | "rendered": null | ||
339 | }, | ||
340 | { | ||
341 | "message": "for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#trivially_copy_pass_by_ref", | ||
342 | "code": null, | ||
343 | "level": "help", | ||
344 | "spans": [], | ||
345 | "children": [], | ||
346 | "rendered": null | ||
347 | }, | ||
348 | { | ||
349 | "message": "consider passing by value instead", | ||
350 | "code": null, | ||
351 | "level": "help", | ||
352 | "spans": [ | ||
353 | { | ||
354 | "file_name": "compiler/mir/tagset.rs", | ||
355 | "byte_start": 941, | ||
356 | "byte_end": 946, | ||
357 | "line_start": 42, | ||
358 | "line_end": 42, | ||
359 | "column_start": 24, | ||
360 | "column_end": 29, | ||
361 | "is_primary": true, | ||
362 | "text": [ | ||
363 | { | ||
364 | "text": " pub fn is_disjoint(&self, other: Self) -> bool {", | ||
365 | "highlight_start": 24, | ||
366 | "highlight_end": 29 | ||
367 | } | ||
368 | ], | ||
369 | "label": null, | ||
370 | "suggested_replacement": "self", | ||
371 | "suggestion_applicability": "Unspecified", | ||
372 | "expansion": null | ||
373 | } | ||
374 | ], | ||
375 | "children": [], | ||
376 | "rendered": null | ||
377 | } | ||
378 | ], | ||
379 | "rendered": "warning: this argument is passed by reference, but would be more efficient if passed by value\n --> compiler/mir/tagset.rs:42:24\n |\n42 | pub fn is_disjoint(&self, other: Self) -> bool {\n | ^^^^^ help: consider passing by value instead: `self`\n |\nnote: lint level defined here\n --> compiler/lib.rs:1:9\n |\n1 | #![warn(clippy::all)]\n | ^^^^^^^^^^^\n = note: #[warn(clippy::trivially_copy_pass_by_ref)] implied by #[warn(clippy::all)]\n = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#trivially_copy_pass_by_ref\n\n" | ||
380 | }"##, | ||
381 | ); | ||
382 | |||
383 | let workspace_root = PathBuf::from("/test/"); | ||
384 | let diag = map_rust_diagnostic_to_lsp(&diag, &workspace_root).expect("couldn't map diagnostic"); | ||
385 | insta::assert_debug_snapshot!(diag); | ||
386 | } | ||
387 | |||
388 | #[test] | ||
389 | fn snap_rustc_mismatched_type() { | ||
390 | let diag = parse_diagnostic( | ||
391 | r##"{ | ||
392 | "message": "mismatched types", | ||
393 | "code": { | ||
394 | "code": "E0308", | ||
395 | "explanation": "\nThis error occurs when the compiler was unable to infer the concrete type of a\nvariable. It can occur for several cases, the most common of which is a\nmismatch in the expected type that the compiler inferred for a variable's\ninitializing expression, and the actual type explicitly assigned to the\nvariable.\n\nFor example:\n\n```compile_fail,E0308\nlet x: i32 = \"I am not a number!\";\n// ~~~ ~~~~~~~~~~~~~~~~~~~~\n// | |\n// | initializing expression;\n// | compiler infers type `&str`\n// |\n// type `i32` assigned to variable `x`\n```\n" | ||
396 | }, | ||
397 | "level": "error", | ||
398 | "spans": [ | ||
399 | { | ||
400 | "file_name": "runtime/compiler_support.rs", | ||
401 | "byte_start": 1589, | ||
402 | "byte_end": 1594, | ||
403 | "line_start": 48, | ||
404 | "line_end": 48, | ||
405 | "column_start": 65, | ||
406 | "column_end": 70, | ||
407 | "is_primary": true, | ||
408 | "text": [ | ||
409 | { | ||
410 | "text": " let layout = alloc::Layout::from_size_align_unchecked(size, align);", | ||
411 | "highlight_start": 65, | ||
412 | "highlight_end": 70 | ||
413 | } | ||
414 | ], | ||
415 | "label": "expected usize, found u32", | ||
416 | "suggested_replacement": null, | ||
417 | "suggestion_applicability": null, | ||
418 | "expansion": null | ||
419 | } | ||
420 | ], | ||
421 | "children": [], | ||
422 | "rendered": "error[E0308]: mismatched types\n --> runtime/compiler_support.rs:48:65\n |\n48 | let layout = alloc::Layout::from_size_align_unchecked(size, align);\n | ^^^^^ expected usize, found u32\n\n" | ||
423 | }"##, | ||
424 | ); | ||
425 | |||
426 | let workspace_root = PathBuf::from("/test/"); | ||
427 | let diag = map_rust_diagnostic_to_lsp(&diag, &workspace_root).expect("couldn't map diagnostic"); | ||
428 | insta::assert_debug_snapshot!(diag); | ||
429 | } | ||
430 | |||
431 | #[test] | ||
432 | fn snap_handles_macro_location() { | ||
433 | let diag = parse_diagnostic( | ||
434 | r##"{ | ||
435 | "rendered": "error[E0277]: can't compare `{integer}` with `&str`\n --> src/main.rs:2:5\n |\n2 | assert_eq!(1, \"love\");\n | ^^^^^^^^^^^^^^^^^^^^^^ no implementation for `{integer} == &str`\n |\n = help: the trait `std::cmp::PartialEq<&str>` is not implemented for `{integer}`\n = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)\n\n", | ||
436 | "children": [ | ||
437 | { | ||
438 | "children": [], | ||
439 | "code": null, | ||
440 | "level": "help", | ||
441 | "message": "the trait `std::cmp::PartialEq<&str>` is not implemented for `{integer}`", | ||
442 | "rendered": null, | ||
443 | "spans": [] | ||
444 | } | ||
445 | ], | ||
446 | "code": { | ||
447 | "code": "E0277", | ||
448 | "explanation": "\nYou tried to use a type which doesn't implement some trait in a place which\nexpected that trait. Erroneous code example:\n\n```compile_fail,E0277\n// here we declare the Foo trait with a bar method\ntrait Foo {\n fn bar(&self);\n}\n\n// we now declare a function which takes an object implementing the Foo trait\nfn some_func<T: Foo>(foo: T) {\n foo.bar();\n}\n\nfn main() {\n // we now call the method with the i32 type, which doesn't implement\n // the Foo trait\n some_func(5i32); // error: the trait bound `i32 : Foo` is not satisfied\n}\n```\n\nIn order to fix this error, verify that the type you're using does implement\nthe trait. Example:\n\n```\ntrait Foo {\n fn bar(&self);\n}\n\nfn some_func<T: Foo>(foo: T) {\n foo.bar(); // we can now use this method since i32 implements the\n // Foo trait\n}\n\n// we implement the trait on the i32 type\nimpl Foo for i32 {\n fn bar(&self) {}\n}\n\nfn main() {\n some_func(5i32); // ok!\n}\n```\n\nOr in a generic context, an erroneous code example would look like:\n\n```compile_fail,E0277\nfn some_func<T>(foo: T) {\n println!(\"{:?}\", foo); // error: the trait `core::fmt::Debug` is not\n // implemented for the type `T`\n}\n\nfn main() {\n // We now call the method with the i32 type,\n // which *does* implement the Debug trait.\n some_func(5i32);\n}\n```\n\nNote that the error here is in the definition of the generic function: Although\nwe only call it with a parameter that does implement `Debug`, the compiler\nstill rejects the function: It must work with all possible input types. In\norder to make this example compile, we need to restrict the generic type we're\naccepting:\n\n```\nuse std::fmt;\n\n// Restrict the input type to types that implement Debug.\nfn some_func<T: fmt::Debug>(foo: T) {\n println!(\"{:?}\", foo);\n}\n\nfn main() {\n // Calling the method is still fine, as i32 implements Debug.\n some_func(5i32);\n\n // This would fail to compile now:\n // struct WithoutDebug;\n // some_func(WithoutDebug);\n}\n```\n\nRust only looks at the signature of the called function, as such it must\nalready specify all requirements that will be used for every type parameter.\n" | ||
449 | }, | ||
450 | "level": "error", | ||
451 | "message": "can't compare `{integer}` with `&str`", | ||
452 | "spans": [ | ||
453 | { | ||
454 | "byte_end": 155, | ||
455 | "byte_start": 153, | ||
456 | "column_end": 33, | ||
457 | "column_start": 31, | ||
458 | "expansion": { | ||
459 | "def_site_span": { | ||
460 | "byte_end": 940, | ||
461 | "byte_start": 0, | ||
462 | "column_end": 6, | ||
463 | "column_start": 1, | ||
464 | "expansion": null, | ||
465 | "file_name": "<::core::macros::assert_eq macros>", | ||
466 | "is_primary": false, | ||
467 | "label": null, | ||
468 | "line_end": 36, | ||
469 | "line_start": 1, | ||
470 | "suggested_replacement": null, | ||
471 | "suggestion_applicability": null, | ||
472 | "text": [ | ||
473 | { | ||
474 | "highlight_end": 35, | ||
475 | "highlight_start": 1, | ||
476 | "text": "($ left : expr, $ right : expr) =>" | ||
477 | }, | ||
478 | { | ||
479 | "highlight_end": 3, | ||
480 | "highlight_start": 1, | ||
481 | "text": "({" | ||
482 | }, | ||
483 | { | ||
484 | "highlight_end": 33, | ||
485 | "highlight_start": 1, | ||
486 | "text": " match (& $ left, & $ right)" | ||
487 | }, | ||
488 | { | ||
489 | "highlight_end": 7, | ||
490 | "highlight_start": 1, | ||
491 | "text": " {" | ||
492 | }, | ||
493 | { | ||
494 | "highlight_end": 34, | ||
495 | "highlight_start": 1, | ||
496 | "text": " (left_val, right_val) =>" | ||
497 | }, | ||
498 | { | ||
499 | "highlight_end": 11, | ||
500 | "highlight_start": 1, | ||
501 | "text": " {" | ||
502 | }, | ||
503 | { | ||
504 | "highlight_end": 46, | ||
505 | "highlight_start": 1, | ||
506 | "text": " if ! (* left_val == * right_val)" | ||
507 | }, | ||
508 | { | ||
509 | "highlight_end": 15, | ||
510 | "highlight_start": 1, | ||
511 | "text": " {" | ||
512 | }, | ||
513 | { | ||
514 | "highlight_end": 25, | ||
515 | "highlight_start": 1, | ||
516 | "text": " panic !" | ||
517 | }, | ||
518 | { | ||
519 | "highlight_end": 57, | ||
520 | "highlight_start": 1, | ||
521 | "text": " (r#\"assertion failed: `(left == right)`" | ||
522 | }, | ||
523 | { | ||
524 | "highlight_end": 16, | ||
525 | "highlight_start": 1, | ||
526 | "text": " left: `{:?}`," | ||
527 | }, | ||
528 | { | ||
529 | "highlight_end": 18, | ||
530 | "highlight_start": 1, | ||
531 | "text": " right: `{:?}`\"#," | ||
532 | }, | ||
533 | { | ||
534 | "highlight_end": 47, | ||
535 | "highlight_start": 1, | ||
536 | "text": " & * left_val, & * right_val)" | ||
537 | }, | ||
538 | { | ||
539 | "highlight_end": 15, | ||
540 | "highlight_start": 1, | ||
541 | "text": " }" | ||
542 | }, | ||
543 | { | ||
544 | "highlight_end": 11, | ||
545 | "highlight_start": 1, | ||
546 | "text": " }" | ||
547 | }, | ||
548 | { | ||
549 | "highlight_end": 7, | ||
550 | "highlight_start": 1, | ||
551 | "text": " }" | ||
552 | }, | ||
553 | { | ||
554 | "highlight_end": 42, | ||
555 | "highlight_start": 1, | ||
556 | "text": " }) ; ($ left : expr, $ right : expr,) =>" | ||
557 | }, | ||
558 | { | ||
559 | "highlight_end": 49, | ||
560 | "highlight_start": 1, | ||
561 | "text": "({ $ crate :: assert_eq ! ($ left, $ right) }) ;" | ||
562 | }, | ||
563 | { | ||
564 | "highlight_end": 53, | ||
565 | "highlight_start": 1, | ||
566 | "text": "($ left : expr, $ right : expr, $ ($ arg : tt) +) =>" | ||
567 | }, | ||
568 | { | ||
569 | "highlight_end": 3, | ||
570 | "highlight_start": 1, | ||
571 | "text": "({" | ||
572 | }, | ||
573 | { | ||
574 | "highlight_end": 37, | ||
575 | "highlight_start": 1, | ||
576 | "text": " match (& ($ left), & ($ right))" | ||
577 | }, | ||
578 | { | ||
579 | "highlight_end": 7, | ||
580 | "highlight_start": 1, | ||
581 | "text": " {" | ||
582 | }, | ||
583 | { | ||
584 | "highlight_end": 34, | ||
585 | "highlight_start": 1, | ||
586 | "text": " (left_val, right_val) =>" | ||
587 | }, | ||
588 | { | ||
589 | "highlight_end": 11, | ||
590 | "highlight_start": 1, | ||
591 | "text": " {" | ||
592 | }, | ||
593 | { | ||
594 | "highlight_end": 46, | ||
595 | "highlight_start": 1, | ||
596 | "text": " if ! (* left_val == * right_val)" | ||
597 | }, | ||
598 | { | ||
599 | "highlight_end": 15, | ||
600 | "highlight_start": 1, | ||
601 | "text": " {" | ||
602 | }, | ||
603 | { | ||
604 | "highlight_end": 25, | ||
605 | "highlight_start": 1, | ||
606 | "text": " panic !" | ||
607 | }, | ||
608 | { | ||
609 | "highlight_end": 57, | ||
610 | "highlight_start": 1, | ||
611 | "text": " (r#\"assertion failed: `(left == right)`" | ||
612 | }, | ||
613 | { | ||
614 | "highlight_end": 16, | ||
615 | "highlight_start": 1, | ||
616 | "text": " left: `{:?}`," | ||
617 | }, | ||
618 | { | ||
619 | "highlight_end": 22, | ||
620 | "highlight_start": 1, | ||
621 | "text": " right: `{:?}`: {}\"#," | ||
622 | }, | ||
623 | { | ||
624 | "highlight_end": 72, | ||
625 | "highlight_start": 1, | ||
626 | "text": " & * left_val, & * right_val, $ crate :: format_args !" | ||
627 | }, | ||
628 | { | ||
629 | "highlight_end": 33, | ||
630 | "highlight_start": 1, | ||
631 | "text": " ($ ($ arg) +))" | ||
632 | }, | ||
633 | { | ||
634 | "highlight_end": 15, | ||
635 | "highlight_start": 1, | ||
636 | "text": " }" | ||
637 | }, | ||
638 | { | ||
639 | "highlight_end": 11, | ||
640 | "highlight_start": 1, | ||
641 | "text": " }" | ||
642 | }, | ||
643 | { | ||
644 | "highlight_end": 7, | ||
645 | "highlight_start": 1, | ||
646 | "text": " }" | ||
647 | }, | ||
648 | { | ||
649 | "highlight_end": 6, | ||
650 | "highlight_start": 1, | ||
651 | "text": " }) ;" | ||
652 | } | ||
653 | ] | ||
654 | }, | ||
655 | "macro_decl_name": "assert_eq!", | ||
656 | "span": { | ||
657 | "byte_end": 38, | ||
658 | "byte_start": 16, | ||
659 | "column_end": 27, | ||
660 | "column_start": 5, | ||
661 | "expansion": null, | ||
662 | "file_name": "src/main.rs", | ||
663 | "is_primary": false, | ||
664 | "label": null, | ||
665 | "line_end": 2, | ||
666 | "line_start": 2, | ||
667 | "suggested_replacement": null, | ||
668 | "suggestion_applicability": null, | ||
669 | "text": [ | ||
670 | { | ||
671 | "highlight_end": 27, | ||
672 | "highlight_start": 5, | ||
673 | "text": " assert_eq!(1, \"love\");" | ||
674 | } | ||
675 | ] | ||
676 | } | ||
677 | }, | ||
678 | "file_name": "<::core::macros::assert_eq macros>", | ||
679 | "is_primary": true, | ||
680 | "label": "no implementation for `{integer} == &str`", | ||
681 | "line_end": 7, | ||
682 | "line_start": 7, | ||
683 | "suggested_replacement": null, | ||
684 | "suggestion_applicability": null, | ||
685 | "text": [ | ||
686 | { | ||
687 | "highlight_end": 33, | ||
688 | "highlight_start": 31, | ||
689 | "text": " if ! (* left_val == * right_val)" | ||
690 | } | ||
691 | ] | ||
692 | } | ||
693 | ] | ||
694 | }"##, | ||
695 | ); | ||
696 | |||
697 | let workspace_root = PathBuf::from("/test/"); | ||
698 | let diag = map_rust_diagnostic_to_lsp(&diag, &workspace_root).expect("couldn't map diagnostic"); | ||
699 | insta::assert_debug_snapshot!(diag); | ||
700 | } | ||
diff --git a/crates/ra_cargo_watch/src/lib.rs b/crates/ra_cargo_watch/src/lib.rs new file mode 100644 index 000000000..e5c22e599 --- /dev/null +++ b/crates/ra_cargo_watch/src/lib.rs | |||
@@ -0,0 +1,392 @@ | |||
1 | //! cargo_check provides the functionality needed to run `cargo check` or | ||
2 | //! another compatible command (f.x. clippy) in a background thread and provide | ||
3 | //! LSP diagnostics based on the output of the command. | ||
4 | use cargo_metadata::Message; | ||
5 | use crossbeam_channel::{never, select, unbounded, Receiver, RecvError, Sender}; | ||
6 | use lsp_types::{ | ||
7 | Diagnostic, Url, WorkDoneProgress, WorkDoneProgressBegin, WorkDoneProgressEnd, | ||
8 | WorkDoneProgressReport, | ||
9 | }; | ||
10 | use parking_lot::RwLock; | ||
11 | use std::{ | ||
12 | collections::HashMap, | ||
13 | path::PathBuf, | ||
14 | process::{Command, Stdio}, | ||
15 | sync::Arc, | ||
16 | thread::JoinHandle, | ||
17 | time::Instant, | ||
18 | }; | ||
19 | |||
20 | mod conv; | ||
21 | |||
22 | use crate::conv::{map_rust_diagnostic_to_lsp, MappedRustDiagnostic, SuggestedFix}; | ||
23 | |||
24 | #[derive(Clone, Debug)] | ||
25 | pub struct CheckOptions { | ||
26 | pub enable: bool, | ||
27 | pub args: Vec<String>, | ||
28 | pub command: String, | ||
29 | pub all_targets: bool, | ||
30 | } | ||
31 | |||
32 | /// CheckWatcher wraps the shared state and communication machinery used for | ||
33 | /// running `cargo check` (or other compatible command) and providing | ||
34 | /// diagnostics based on the output. | ||
35 | /// The spawned thread is shut down when this struct is dropped. | ||
36 | #[derive(Debug)] | ||
37 | pub struct CheckWatcher { | ||
38 | pub task_recv: Receiver<CheckTask>, | ||
39 | pub shared: Arc<RwLock<CheckWatcherSharedState>>, | ||
40 | cmd_send: Option<Sender<CheckCommand>>, | ||
41 | handle: Option<JoinHandle<()>>, | ||
42 | } | ||
43 | |||
44 | impl CheckWatcher { | ||
45 | pub fn new(options: &CheckOptions, workspace_root: PathBuf) -> CheckWatcher { | ||
46 | let options = options.clone(); | ||
47 | let shared = Arc::new(RwLock::new(CheckWatcherSharedState::new())); | ||
48 | |||
49 | let (task_send, task_recv) = unbounded::<CheckTask>(); | ||
50 | let (cmd_send, cmd_recv) = unbounded::<CheckCommand>(); | ||
51 | let shared_ = shared.clone(); | ||
52 | let handle = std::thread::spawn(move || { | ||
53 | let mut check = CheckWatcherState::new(options, workspace_root, shared_); | ||
54 | check.run(&task_send, &cmd_recv); | ||
55 | }); | ||
56 | CheckWatcher { task_recv, cmd_send: Some(cmd_send), handle: Some(handle), shared } | ||
57 | } | ||
58 | |||
59 | /// Schedule a re-start of the cargo check worker. | ||
60 | pub fn update(&self) { | ||
61 | if let Some(cmd_send) = &self.cmd_send { | ||
62 | cmd_send.send(CheckCommand::Update).unwrap(); | ||
63 | } | ||
64 | } | ||
65 | } | ||
66 | |||
67 | impl std::ops::Drop for CheckWatcher { | ||
68 | fn drop(&mut self) { | ||
69 | if let Some(handle) = self.handle.take() { | ||
70 | // Take the sender out of the option | ||
71 | let recv = self.cmd_send.take(); | ||
72 | |||
73 | // Dropping the sender finishes the thread loop | ||
74 | drop(recv); | ||
75 | |||
76 | // Join the thread, it should finish shortly. We don't really care | ||
77 | // whether it panicked, so it is safe to ignore the result | ||
78 | let _ = handle.join(); | ||
79 | } | ||
80 | } | ||
81 | } | ||
82 | |||
83 | #[derive(Debug)] | ||
84 | pub struct CheckWatcherSharedState { | ||
85 | diagnostic_collection: HashMap<Url, Vec<Diagnostic>>, | ||
86 | suggested_fix_collection: HashMap<Url, Vec<SuggestedFix>>, | ||
87 | } | ||
88 | |||
89 | impl CheckWatcherSharedState { | ||
90 | fn new() -> CheckWatcherSharedState { | ||
91 | CheckWatcherSharedState { | ||
92 | diagnostic_collection: HashMap::new(), | ||
93 | suggested_fix_collection: HashMap::new(), | ||
94 | } | ||
95 | } | ||
96 | |||
97 | /// Clear the cached diagnostics, and schedule updating diagnostics by the | ||
98 | /// server, to clear stale results. | ||
99 | pub fn clear(&mut self, task_send: &Sender<CheckTask>) { | ||
100 | let cleared_files: Vec<Url> = self.diagnostic_collection.keys().cloned().collect(); | ||
101 | |||
102 | self.diagnostic_collection.clear(); | ||
103 | self.suggested_fix_collection.clear(); | ||
104 | |||
105 | for uri in cleared_files { | ||
106 | task_send.send(CheckTask::Update(uri.clone())).unwrap(); | ||
107 | } | ||
108 | } | ||
109 | |||
110 | pub fn diagnostics_for(&self, uri: &Url) -> Option<&[Diagnostic]> { | ||
111 | self.diagnostic_collection.get(uri).map(|d| d.as_slice()) | ||
112 | } | ||
113 | |||
114 | pub fn fixes_for(&self, uri: &Url) -> Option<&[SuggestedFix]> { | ||
115 | self.suggested_fix_collection.get(uri).map(|d| d.as_slice()) | ||
116 | } | ||
117 | |||
118 | fn add_diagnostic(&mut self, file_uri: Url, diagnostic: Diagnostic) { | ||
119 | let diagnostics = self.diagnostic_collection.entry(file_uri).or_default(); | ||
120 | |||
121 | // If we're building multiple targets it's possible we've already seen this diagnostic | ||
122 | let is_duplicate = diagnostics.iter().any(|d| are_diagnostics_equal(d, &diagnostic)); | ||
123 | if is_duplicate { | ||
124 | return; | ||
125 | } | ||
126 | |||
127 | diagnostics.push(diagnostic); | ||
128 | } | ||
129 | |||
130 | fn add_suggested_fix_for_diagnostic( | ||
131 | &mut self, | ||
132 | mut suggested_fix: SuggestedFix, | ||
133 | diagnostic: &Diagnostic, | ||
134 | ) { | ||
135 | let file_uri = suggested_fix.location.uri.clone(); | ||
136 | let file_suggestions = self.suggested_fix_collection.entry(file_uri).or_default(); | ||
137 | |||
138 | let existing_suggestion: Option<&mut SuggestedFix> = | ||
139 | file_suggestions.iter_mut().find(|s| s == &&suggested_fix); | ||
140 | if let Some(existing_suggestion) = existing_suggestion { | ||
141 | // The existing suggestion also applies to this new diagnostic | ||
142 | existing_suggestion.diagnostics.push(diagnostic.clone()); | ||
143 | } else { | ||
144 | // We haven't seen this suggestion before | ||
145 | suggested_fix.diagnostics.push(diagnostic.clone()); | ||
146 | file_suggestions.push(suggested_fix); | ||
147 | } | ||
148 | } | ||
149 | } | ||
150 | |||
151 | #[derive(Debug)] | ||
152 | pub enum CheckTask { | ||
153 | /// Request a update of the given files diagnostics | ||
154 | Update(Url), | ||
155 | |||
156 | /// Request check progress notification to client | ||
157 | Status(WorkDoneProgress), | ||
158 | } | ||
159 | |||
160 | pub enum CheckCommand { | ||
161 | /// Request re-start of check thread | ||
162 | Update, | ||
163 | } | ||
164 | |||
165 | struct CheckWatcherState { | ||
166 | options: CheckOptions, | ||
167 | workspace_root: PathBuf, | ||
168 | watcher: WatchThread, | ||
169 | last_update_req: Option<Instant>, | ||
170 | shared: Arc<RwLock<CheckWatcherSharedState>>, | ||
171 | } | ||
172 | |||
173 | impl CheckWatcherState { | ||
174 | fn new( | ||
175 | options: CheckOptions, | ||
176 | workspace_root: PathBuf, | ||
177 | shared: Arc<RwLock<CheckWatcherSharedState>>, | ||
178 | ) -> CheckWatcherState { | ||
179 | let watcher = WatchThread::new(&options, &workspace_root); | ||
180 | CheckWatcherState { options, workspace_root, watcher, last_update_req: None, shared } | ||
181 | } | ||
182 | |||
183 | fn run(&mut self, task_send: &Sender<CheckTask>, cmd_recv: &Receiver<CheckCommand>) { | ||
184 | loop { | ||
185 | select! { | ||
186 | recv(&cmd_recv) -> cmd => match cmd { | ||
187 | Ok(cmd) => self.handle_command(cmd), | ||
188 | Err(RecvError) => { | ||
189 | // Command channel has closed, so shut down | ||
190 | break; | ||
191 | }, | ||
192 | }, | ||
193 | recv(self.watcher.message_recv) -> msg => match msg { | ||
194 | Ok(msg) => self.handle_message(msg, task_send), | ||
195 | Err(RecvError) => { | ||
196 | // Watcher finished, replace it with a never channel to | ||
197 | // avoid busy-waiting. | ||
198 | std::mem::replace(&mut self.watcher.message_recv, never()); | ||
199 | }, | ||
200 | } | ||
201 | }; | ||
202 | |||
203 | if self.should_recheck() { | ||
204 | self.last_update_req.take(); | ||
205 | self.shared.write().clear(task_send); | ||
206 | |||
207 | // By replacing the watcher, we drop the previous one which | ||
208 | // causes it to shut down automatically. | ||
209 | self.watcher = WatchThread::new(&self.options, &self.workspace_root); | ||
210 | } | ||
211 | } | ||
212 | } | ||
213 | |||
214 | fn should_recheck(&mut self) -> bool { | ||
215 | if let Some(_last_update_req) = &self.last_update_req { | ||
216 | // We currently only request an update on save, as we need up to | ||
217 | // date source on disk for cargo check to do it's magic, so we | ||
218 | // don't really need to debounce the requests at this point. | ||
219 | return true; | ||
220 | } | ||
221 | false | ||
222 | } | ||
223 | |||
224 | fn handle_command(&mut self, cmd: CheckCommand) { | ||
225 | match cmd { | ||
226 | CheckCommand::Update => self.last_update_req = Some(Instant::now()), | ||
227 | } | ||
228 | } | ||
229 | |||
230 | fn handle_message(&mut self, msg: CheckEvent, task_send: &Sender<CheckTask>) { | ||
231 | match msg { | ||
232 | CheckEvent::Begin => { | ||
233 | task_send | ||
234 | .send(CheckTask::Status(WorkDoneProgress::Begin(WorkDoneProgressBegin { | ||
235 | title: "Running 'cargo check'".to_string(), | ||
236 | cancellable: Some(false), | ||
237 | message: None, | ||
238 | percentage: None, | ||
239 | }))) | ||
240 | .unwrap(); | ||
241 | } | ||
242 | |||
243 | CheckEvent::End => { | ||
244 | task_send | ||
245 | .send(CheckTask::Status(WorkDoneProgress::End(WorkDoneProgressEnd { | ||
246 | message: None, | ||
247 | }))) | ||
248 | .unwrap(); | ||
249 | } | ||
250 | |||
251 | CheckEvent::Msg(Message::CompilerArtifact(msg)) => { | ||
252 | task_send | ||
253 | .send(CheckTask::Status(WorkDoneProgress::Report(WorkDoneProgressReport { | ||
254 | cancellable: Some(false), | ||
255 | message: Some(msg.target.name), | ||
256 | percentage: None, | ||
257 | }))) | ||
258 | .unwrap(); | ||
259 | } | ||
260 | |||
261 | CheckEvent::Msg(Message::CompilerMessage(msg)) => { | ||
262 | let map_result = | ||
263 | match map_rust_diagnostic_to_lsp(&msg.message, &self.workspace_root) { | ||
264 | Some(map_result) => map_result, | ||
265 | None => return, | ||
266 | }; | ||
267 | |||
268 | let MappedRustDiagnostic { location, diagnostic, suggested_fixes } = map_result; | ||
269 | let file_uri = location.uri.clone(); | ||
270 | |||
271 | if !suggested_fixes.is_empty() { | ||
272 | for suggested_fix in suggested_fixes { | ||
273 | self.shared | ||
274 | .write() | ||
275 | .add_suggested_fix_for_diagnostic(suggested_fix, &diagnostic); | ||
276 | } | ||
277 | } | ||
278 | self.shared.write().add_diagnostic(file_uri, diagnostic); | ||
279 | |||
280 | task_send.send(CheckTask::Update(location.uri)).unwrap(); | ||
281 | } | ||
282 | |||
283 | CheckEvent::Msg(Message::BuildScriptExecuted(_msg)) => {} | ||
284 | CheckEvent::Msg(Message::Unknown) => {} | ||
285 | } | ||
286 | } | ||
287 | } | ||
288 | |||
289 | /// WatchThread exists to wrap around the communication needed to be able to | ||
290 | /// run `cargo check` without blocking. Currently the Rust standard library | ||
291 | /// doesn't provide a way to read sub-process output without blocking, so we | ||
292 | /// have to wrap sub-processes output handling in a thread and pass messages | ||
293 | /// back over a channel. | ||
294 | /// The correct way to dispose of the thread is to drop it, on which the | ||
295 | /// sub-process will be killed, and the thread will be joined. | ||
296 | struct WatchThread { | ||
297 | handle: Option<JoinHandle<()>>, | ||
298 | message_recv: Receiver<CheckEvent>, | ||
299 | } | ||
300 | |||
301 | enum CheckEvent { | ||
302 | Begin, | ||
303 | Msg(cargo_metadata::Message), | ||
304 | End, | ||
305 | } | ||
306 | |||
307 | impl WatchThread { | ||
308 | fn new(options: &CheckOptions, workspace_root: &PathBuf) -> WatchThread { | ||
309 | let mut args: Vec<String> = vec![ | ||
310 | options.command.clone(), | ||
311 | "--message-format=json".to_string(), | ||
312 | "--manifest-path".to_string(), | ||
313 | format!("{}/Cargo.toml", workspace_root.to_string_lossy()), | ||
314 | ]; | ||
315 | if options.all_targets { | ||
316 | args.push("--all-targets".to_string()); | ||
317 | } | ||
318 | args.extend(options.args.iter().cloned()); | ||
319 | |||
320 | let (message_send, message_recv) = unbounded(); | ||
321 | let enabled = options.enable; | ||
322 | let handle = std::thread::spawn(move || { | ||
323 | if !enabled { | ||
324 | return; | ||
325 | } | ||
326 | |||
327 | let mut command = Command::new("cargo") | ||
328 | .args(&args) | ||
329 | .stdout(Stdio::piped()) | ||
330 | .stderr(Stdio::null()) | ||
331 | .spawn() | ||
332 | .expect("couldn't launch cargo"); | ||
333 | |||
334 | // If we trigger an error here, we will do so in the loop instead, | ||
335 | // which will break out of the loop, and continue the shutdown | ||
336 | let _ = message_send.send(CheckEvent::Begin); | ||
337 | |||
338 | for message in cargo_metadata::parse_messages(command.stdout.take().unwrap()) { | ||
339 | let message = match message { | ||
340 | Ok(message) => message, | ||
341 | Err(err) => { | ||
342 | log::error!("Invalid json from cargo check, ignoring: {}", err); | ||
343 | continue; | ||
344 | } | ||
345 | }; | ||
346 | |||
347 | match message_send.send(CheckEvent::Msg(message)) { | ||
348 | Ok(()) => {} | ||
349 | Err(_err) => { | ||
350 | // The send channel was closed, so we want to shutdown | ||
351 | break; | ||
352 | } | ||
353 | } | ||
354 | } | ||
355 | |||
356 | // We can ignore any error here, as we are already in the progress | ||
357 | // of shutting down. | ||
358 | let _ = message_send.send(CheckEvent::End); | ||
359 | |||
360 | // It is okay to ignore the result, as it only errors if the process is already dead | ||
361 | let _ = command.kill(); | ||
362 | |||
363 | // Again, we don't care about the exit status so just ignore the result | ||
364 | let _ = command.wait(); | ||
365 | }); | ||
366 | WatchThread { handle: Some(handle), message_recv } | ||
367 | } | ||
368 | } | ||
369 | |||
370 | impl std::ops::Drop for WatchThread { | ||
371 | fn drop(&mut self) { | ||
372 | if let Some(handle) = self.handle.take() { | ||
373 | // Replace our reciever with dummy one, so we can drop and close the | ||
374 | // one actually communicating with the thread | ||
375 | let recv = std::mem::replace(&mut self.message_recv, never()); | ||
376 | |||
377 | // Dropping the original reciever initiates thread sub-process shutdown | ||
378 | drop(recv); | ||
379 | |||
380 | // Join the thread, it should finish shortly. We don't really care | ||
381 | // whether it panicked, so it is safe to ignore the result | ||
382 | let _ = handle.join(); | ||
383 | } | ||
384 | } | ||
385 | } | ||
386 | |||
387 | fn are_diagnostics_equal(left: &Diagnostic, right: &Diagnostic) -> bool { | ||
388 | left.source == right.source | ||
389 | && left.severity == right.severity | ||
390 | && left.range == right.range | ||
391 | && left.message == right.message | ||
392 | } | ||
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index bcfc0d03e..488f74cfb 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs | |||
@@ -118,7 +118,7 @@ impl_froms!( | |||
118 | BuiltinType | 118 | BuiltinType |
119 | ); | 119 | ); |
120 | 120 | ||
121 | pub use hir_def::attr::Attrs; | 121 | pub use hir_def::{attr::Attrs, visibility::Visibility}; |
122 | 122 | ||
123 | impl Module { | 123 | impl Module { |
124 | pub(crate) fn new(krate: Crate, crate_module_id: LocalModuleId) -> Module { | 124 | pub(crate) fn new(krate: Crate, crate_module_id: LocalModuleId) -> Module { |
@@ -255,6 +255,15 @@ impl StructField { | |||
255 | } | 255 | } |
256 | } | 256 | } |
257 | 257 | ||
258 | impl HasVisibility for StructField { | ||
259 | fn visibility(&self, db: &impl HirDatabase) -> Visibility { | ||
260 | let variant_data = self.parent.variant_data(db); | ||
261 | let visibility = &variant_data.fields()[self.id].visibility; | ||
262 | let parent_id: hir_def::VariantId = self.parent.into(); | ||
263 | visibility.resolve(db, &parent_id.resolver(db)) | ||
264 | } | ||
265 | } | ||
266 | |||
258 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 267 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
259 | pub struct Struct { | 268 | pub struct Struct { |
260 | pub(crate) id: StructId, | 269 | pub(crate) id: StructId, |
@@ -644,6 +653,17 @@ impl_froms!( | |||
644 | Const | 653 | Const |
645 | ); | 654 | ); |
646 | 655 | ||
656 | impl GenericDef { | ||
657 | pub fn params(self, db: &impl HirDatabase) -> Vec<TypeParam> { | ||
658 | let generics: Arc<hir_def::generics::GenericParams> = db.generic_params(self.into()); | ||
659 | generics | ||
660 | .types | ||
661 | .iter() | ||
662 | .map(|(local_id, _)| TypeParam { id: TypeParamId { parent: self.into(), local_id } }) | ||
663 | .collect() | ||
664 | } | ||
665 | } | ||
666 | |||
647 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | 667 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] |
648 | pub struct Local { | 668 | pub struct Local { |
649 | pub(crate) parent: DefWithBody, | 669 | pub(crate) parent: DefWithBody, |
@@ -1030,3 +1050,11 @@ impl<T: Into<AttrDef> + Copy> Docs for T { | |||
1030 | db.documentation(def.into()) | 1050 | db.documentation(def.into()) |
1031 | } | 1051 | } |
1032 | } | 1052 | } |
1053 | |||
1054 | pub trait HasVisibility { | ||
1055 | fn visibility(&self, db: &impl HirDatabase) -> Visibility; | ||
1056 | fn is_visible_from(&self, db: &impl HirDatabase, module: Module) -> bool { | ||
1057 | let vis = self.visibility(db); | ||
1058 | vis.is_visible_from(db, module.id) | ||
1059 | } | ||
1060 | } | ||
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 0008a8858..3d13978d4 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs | |||
@@ -40,8 +40,8 @@ mod from_source; | |||
40 | pub use crate::{ | 40 | pub use crate::{ |
41 | code_model::{ | 41 | code_model::{ |
42 | Adt, AssocItem, AttrDef, Const, Crate, CrateDependency, DefWithBody, Docs, Enum, | 42 | Adt, AssocItem, AttrDef, Const, Crate, CrateDependency, DefWithBody, Docs, Enum, |
43 | EnumVariant, FieldSource, Function, GenericDef, HasAttrs, ImplBlock, Local, MacroDef, | 43 | EnumVariant, FieldSource, Function, GenericDef, HasAttrs, HasVisibility, ImplBlock, Local, |
44 | Module, ModuleDef, ScopeDef, Static, Struct, StructField, Trait, Type, TypeAlias, | 44 | MacroDef, Module, ModuleDef, ScopeDef, Static, Struct, StructField, Trait, Type, TypeAlias, |
45 | TypeParam, Union, VariantDef, | 45 | TypeParam, Union, VariantDef, |
46 | }, | 46 | }, |
47 | from_source::FromSource, | 47 | from_source::FromSource, |
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 85b378483..2c422af8b 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs | |||
@@ -215,8 +215,32 @@ impl SourceAnalyzer { | |||
215 | self.body_source_map.as_ref()?.node_pat(src) | 215 | self.body_source_map.as_ref()?.node_pat(src) |
216 | } | 216 | } |
217 | 217 | ||
218 | fn expand_expr( | ||
219 | &self, | ||
220 | db: &impl HirDatabase, | ||
221 | expr: InFile<&ast::Expr>, | ||
222 | ) -> Option<InFile<ast::Expr>> { | ||
223 | let macro_call = ast::MacroCall::cast(expr.value.syntax().clone())?; | ||
224 | let macro_file = | ||
225 | self.body_source_map.as_ref()?.node_macro_file(expr.with_value(¯o_call))?; | ||
226 | let expanded = db.parse_or_expand(macro_file)?; | ||
227 | let kind = expanded.kind(); | ||
228 | let expr = InFile::new(macro_file, ast::Expr::cast(expanded)?); | ||
229 | |||
230 | if ast::MacroCall::can_cast(kind) { | ||
231 | self.expand_expr(db, expr.as_ref()) | ||
232 | } else { | ||
233 | Some(expr) | ||
234 | } | ||
235 | } | ||
236 | |||
218 | pub fn type_of(&self, db: &impl HirDatabase, expr: &ast::Expr) -> Option<Type> { | 237 | pub fn type_of(&self, db: &impl HirDatabase, expr: &ast::Expr) -> Option<Type> { |
219 | let expr_id = self.expr_id(expr)?; | 238 | let expr_id = if let Some(expr) = self.expand_expr(db, InFile::new(self.file_id, expr)) { |
239 | self.body_source_map.as_ref()?.node_expr(expr.as_ref())? | ||
240 | } else { | ||
241 | self.expr_id(expr)? | ||
242 | }; | ||
243 | |||
220 | let ty = self.infer.as_ref()?[expr_id].clone(); | 244 | let ty = self.infer.as_ref()?[expr_id].clone(); |
221 | let environment = TraitEnvironment::lower(db, &self.resolver); | 245 | let environment = TraitEnvironment::lower(db, &self.resolver); |
222 | Some(Type { krate: self.resolver.krate()?, ty: InEnvironment { value: ty, environment } }) | 246 | Some(Type { krate: self.resolver.krate()?, ty: InEnvironment { value: ty, environment } }) |
diff --git a/crates/ra_hir_def/src/adt.rs b/crates/ra_hir_def/src/adt.rs index d9ea693e3..aac5f3e15 100644 --- a/crates/ra_hir_def/src/adt.rs +++ b/crates/ra_hir_def/src/adt.rs | |||
@@ -9,11 +9,12 @@ use hir_expand::{ | |||
9 | }; | 9 | }; |
10 | use ra_arena::{map::ArenaMap, Arena}; | 10 | use ra_arena::{map::ArenaMap, Arena}; |
11 | use ra_prof::profile; | 11 | use ra_prof::profile; |
12 | use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner}; | 12 | use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner, VisibilityOwner}; |
13 | 13 | ||
14 | use crate::{ | 14 | use crate::{ |
15 | db::DefDatabase, src::HasChildSource, src::HasSource, trace::Trace, type_ref::TypeRef, EnumId, | 15 | db::DefDatabase, src::HasChildSource, src::HasSource, trace::Trace, type_ref::TypeRef, |
16 | LocalEnumVariantId, LocalStructFieldId, Lookup, StructId, UnionId, VariantId, | 16 | visibility::RawVisibility, EnumId, LocalEnumVariantId, LocalStructFieldId, Lookup, StructId, |
17 | UnionId, VariantId, | ||
17 | }; | 18 | }; |
18 | 19 | ||
19 | /// Note that we use `StructData` for unions as well! | 20 | /// Note that we use `StructData` for unions as well! |
@@ -47,13 +48,14 @@ pub enum VariantData { | |||
47 | pub struct StructFieldData { | 48 | pub struct StructFieldData { |
48 | pub name: Name, | 49 | pub name: Name, |
49 | pub type_ref: TypeRef, | 50 | pub type_ref: TypeRef, |
51 | pub visibility: RawVisibility, | ||
50 | } | 52 | } |
51 | 53 | ||
52 | impl StructData { | 54 | impl StructData { |
53 | pub(crate) fn struct_data_query(db: &impl DefDatabase, id: StructId) -> Arc<StructData> { | 55 | pub(crate) fn struct_data_query(db: &impl DefDatabase, id: StructId) -> Arc<StructData> { |
54 | let src = id.lookup(db).source(db); | 56 | let src = id.lookup(db).source(db); |
55 | let name = src.value.name().map_or_else(Name::missing, |n| n.as_name()); | 57 | let name = src.value.name().map_or_else(Name::missing, |n| n.as_name()); |
56 | let variant_data = VariantData::new(src.value.kind()); | 58 | let variant_data = VariantData::new(db, src.map(|s| s.kind())); |
57 | let variant_data = Arc::new(variant_data); | 59 | let variant_data = Arc::new(variant_data); |
58 | Arc::new(StructData { name, variant_data }) | 60 | Arc::new(StructData { name, variant_data }) |
59 | } | 61 | } |
@@ -61,10 +63,12 @@ impl StructData { | |||
61 | let src = id.lookup(db).source(db); | 63 | let src = id.lookup(db).source(db); |
62 | let name = src.value.name().map_or_else(Name::missing, |n| n.as_name()); | 64 | let name = src.value.name().map_or_else(Name::missing, |n| n.as_name()); |
63 | let variant_data = VariantData::new( | 65 | let variant_data = VariantData::new( |
64 | src.value | 66 | db, |
65 | .record_field_def_list() | 67 | src.map(|s| { |
66 | .map(ast::StructKind::Record) | 68 | s.record_field_def_list() |
67 | .unwrap_or(ast::StructKind::Unit), | 69 | .map(ast::StructKind::Record) |
70 | .unwrap_or(ast::StructKind::Unit) | ||
71 | }), | ||
68 | ); | 72 | ); |
69 | let variant_data = Arc::new(variant_data); | 73 | let variant_data = Arc::new(variant_data); |
70 | Arc::new(StructData { name, variant_data }) | 74 | Arc::new(StructData { name, variant_data }) |
@@ -77,7 +81,7 @@ impl EnumData { | |||
77 | let src = e.lookup(db).source(db); | 81 | let src = e.lookup(db).source(db); |
78 | let name = src.value.name().map_or_else(Name::missing, |n| n.as_name()); | 82 | let name = src.value.name().map_or_else(Name::missing, |n| n.as_name()); |
79 | let mut trace = Trace::new_for_arena(); | 83 | let mut trace = Trace::new_for_arena(); |
80 | lower_enum(&mut trace, &src.value); | 84 | lower_enum(db, &mut trace, &src); |
81 | Arc::new(EnumData { name, variants: trace.into_arena() }) | 85 | Arc::new(EnumData { name, variants: trace.into_arena() }) |
82 | } | 86 | } |
83 | 87 | ||
@@ -93,30 +97,31 @@ impl HasChildSource for EnumId { | |||
93 | fn child_source(&self, db: &impl DefDatabase) -> InFile<ArenaMap<Self::ChildId, Self::Value>> { | 97 | fn child_source(&self, db: &impl DefDatabase) -> InFile<ArenaMap<Self::ChildId, Self::Value>> { |
94 | let src = self.lookup(db).source(db); | 98 | let src = self.lookup(db).source(db); |
95 | let mut trace = Trace::new_for_map(); | 99 | let mut trace = Trace::new_for_map(); |
96 | lower_enum(&mut trace, &src.value); | 100 | lower_enum(db, &mut trace, &src); |
97 | src.with_value(trace.into_map()) | 101 | src.with_value(trace.into_map()) |
98 | } | 102 | } |
99 | } | 103 | } |
100 | 104 | ||
101 | fn lower_enum( | 105 | fn lower_enum( |
106 | db: &impl DefDatabase, | ||
102 | trace: &mut Trace<LocalEnumVariantId, EnumVariantData, ast::EnumVariant>, | 107 | trace: &mut Trace<LocalEnumVariantId, EnumVariantData, ast::EnumVariant>, |
103 | ast: &ast::EnumDef, | 108 | ast: &InFile<ast::EnumDef>, |
104 | ) { | 109 | ) { |
105 | for var in ast.variant_list().into_iter().flat_map(|it| it.variants()) { | 110 | for var in ast.value.variant_list().into_iter().flat_map(|it| it.variants()) { |
106 | trace.alloc( | 111 | trace.alloc( |
107 | || var.clone(), | 112 | || var.clone(), |
108 | || EnumVariantData { | 113 | || EnumVariantData { |
109 | name: var.name().map_or_else(Name::missing, |it| it.as_name()), | 114 | name: var.name().map_or_else(Name::missing, |it| it.as_name()), |
110 | variant_data: Arc::new(VariantData::new(var.kind())), | 115 | variant_data: Arc::new(VariantData::new(db, ast.with_value(var.kind()))), |
111 | }, | 116 | }, |
112 | ); | 117 | ); |
113 | } | 118 | } |
114 | } | 119 | } |
115 | 120 | ||
116 | impl VariantData { | 121 | impl VariantData { |
117 | fn new(flavor: ast::StructKind) -> Self { | 122 | fn new(db: &impl DefDatabase, flavor: InFile<ast::StructKind>) -> Self { |
118 | let mut trace = Trace::new_for_arena(); | 123 | let mut trace = Trace::new_for_arena(); |
119 | match lower_struct(&mut trace, &flavor) { | 124 | match lower_struct(db, &mut trace, &flavor) { |
120 | StructKind::Tuple => VariantData::Tuple(trace.into_arena()), | 125 | StructKind::Tuple => VariantData::Tuple(trace.into_arena()), |
121 | StructKind::Record => VariantData::Record(trace.into_arena()), | 126 | StructKind::Record => VariantData::Record(trace.into_arena()), |
122 | StructKind::Unit => VariantData::Unit, | 127 | StructKind::Unit => VariantData::Unit, |
@@ -163,7 +168,7 @@ impl HasChildSource for VariantId { | |||
163 | }), | 168 | }), |
164 | }; | 169 | }; |
165 | let mut trace = Trace::new_for_map(); | 170 | let mut trace = Trace::new_for_map(); |
166 | lower_struct(&mut trace, &src.value); | 171 | lower_struct(db, &mut trace, &src); |
167 | src.with_value(trace.into_map()) | 172 | src.with_value(trace.into_map()) |
168 | } | 173 | } |
169 | } | 174 | } |
@@ -175,14 +180,15 @@ enum StructKind { | |||
175 | } | 180 | } |
176 | 181 | ||
177 | fn lower_struct( | 182 | fn lower_struct( |
183 | db: &impl DefDatabase, | ||
178 | trace: &mut Trace< | 184 | trace: &mut Trace< |
179 | LocalStructFieldId, | 185 | LocalStructFieldId, |
180 | StructFieldData, | 186 | StructFieldData, |
181 | Either<ast::TupleFieldDef, ast::RecordFieldDef>, | 187 | Either<ast::TupleFieldDef, ast::RecordFieldDef>, |
182 | >, | 188 | >, |
183 | ast: &ast::StructKind, | 189 | ast: &InFile<ast::StructKind>, |
184 | ) -> StructKind { | 190 | ) -> StructKind { |
185 | match ast { | 191 | match &ast.value { |
186 | ast::StructKind::Tuple(fl) => { | 192 | ast::StructKind::Tuple(fl) => { |
187 | for (i, fd) in fl.fields().enumerate() { | 193 | for (i, fd) in fl.fields().enumerate() { |
188 | trace.alloc( | 194 | trace.alloc( |
@@ -190,6 +196,7 @@ fn lower_struct( | |||
190 | || StructFieldData { | 196 | || StructFieldData { |
191 | name: Name::new_tuple_field(i), | 197 | name: Name::new_tuple_field(i), |
192 | type_ref: TypeRef::from_ast_opt(fd.type_ref()), | 198 | type_ref: TypeRef::from_ast_opt(fd.type_ref()), |
199 | visibility: RawVisibility::from_ast(db, ast.with_value(fd.visibility())), | ||
193 | }, | 200 | }, |
194 | ); | 201 | ); |
195 | } | 202 | } |
@@ -202,6 +209,7 @@ fn lower_struct( | |||
202 | || StructFieldData { | 209 | || StructFieldData { |
203 | name: fd.name().map(|n| n.as_name()).unwrap_or_else(Name::missing), | 210 | name: fd.name().map(|n| n.as_name()).unwrap_or_else(Name::missing), |
204 | type_ref: TypeRef::from_ast_opt(fd.ascribed_type()), | 211 | type_ref: TypeRef::from_ast_opt(fd.ascribed_type()), |
212 | visibility: RawVisibility::from_ast(db, ast.with_value(fd.visibility())), | ||
205 | }, | 213 | }, |
206 | ); | 214 | ); |
207 | } | 215 | } |
diff --git a/crates/ra_hir_def/src/body.rs b/crates/ra_hir_def/src/body.rs index d3e4c50ae..142c52d35 100644 --- a/crates/ra_hir_def/src/body.rs +++ b/crates/ra_hir_def/src/body.rs | |||
@@ -163,6 +163,7 @@ pub struct BodySourceMap { | |||
163 | pat_map: FxHashMap<PatSource, PatId>, | 163 | pat_map: FxHashMap<PatSource, PatId>, |
164 | pat_map_back: ArenaMap<PatId, PatSource>, | 164 | pat_map_back: ArenaMap<PatId, PatSource>, |
165 | field_map: FxHashMap<(ExprId, usize), AstPtr<ast::RecordField>>, | 165 | field_map: FxHashMap<(ExprId, usize), AstPtr<ast::RecordField>>, |
166 | expansions: FxHashMap<InFile<AstPtr<ast::MacroCall>>, HirFileId>, | ||
166 | } | 167 | } |
167 | 168 | ||
168 | impl Body { | 169 | impl Body { |
@@ -237,6 +238,11 @@ impl BodySourceMap { | |||
237 | self.expr_map.get(&src).cloned() | 238 | self.expr_map.get(&src).cloned() |
238 | } | 239 | } |
239 | 240 | ||
241 | pub fn node_macro_file(&self, node: InFile<&ast::MacroCall>) -> Option<HirFileId> { | ||
242 | let src = node.map(|it| AstPtr::new(it)); | ||
243 | self.expansions.get(&src).cloned() | ||
244 | } | ||
245 | |||
240 | pub fn field_init_shorthand_expr(&self, node: InFile<&ast::RecordField>) -> Option<ExprId> { | 246 | pub fn field_init_shorthand_expr(&self, node: InFile<&ast::RecordField>) -> Option<ExprId> { |
241 | let src = node.map(|it| Either::Right(AstPtr::new(it))); | 247 | let src = node.map(|it| Either::Right(AstPtr::new(it))); |
242 | self.expr_map.get(&src).cloned() | 248 | self.expr_map.get(&src).cloned() |
diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs index 5323af097..e656f9a41 100644 --- a/crates/ra_hir_def/src/body/lower.rs +++ b/crates/ra_hir_def/src/body/lower.rs | |||
@@ -446,14 +446,20 @@ where | |||
446 | } | 446 | } |
447 | } | 447 | } |
448 | // FIXME expand to statements in statement position | 448 | // FIXME expand to statements in statement position |
449 | ast::Expr::MacroCall(e) => match self.expander.enter_expand(self.db, e) { | 449 | ast::Expr::MacroCall(e) => { |
450 | Some((mark, expansion)) => { | 450 | let macro_call = self.expander.to_source(AstPtr::new(&e)); |
451 | let id = self.collect_expr(expansion); | 451 | match self.expander.enter_expand(self.db, e.clone()) { |
452 | self.expander.exit(self.db, mark); | 452 | Some((mark, expansion)) => { |
453 | id | 453 | self.source_map |
454 | .expansions | ||
455 | .insert(macro_call, self.expander.current_file_id); | ||
456 | let id = self.collect_expr(expansion); | ||
457 | self.expander.exit(self.db, mark); | ||
458 | id | ||
459 | } | ||
460 | None => self.alloc_expr(Expr::Missing, syntax_ptr), | ||
454 | } | 461 | } |
455 | None => self.alloc_expr(Expr::Missing, syntax_ptr), | 462 | } |
456 | }, | ||
457 | 463 | ||
458 | // FIXME implement HIR for these: | 464 | // FIXME implement HIR for these: |
459 | ast::Expr::Label(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), | 465 | ast::Expr::Label(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), |
@@ -543,7 +549,10 @@ where | |||
543 | }; | 549 | }; |
544 | self.body.item_scope.define_def(def); | 550 | self.body.item_scope.define_def(def); |
545 | if let Some(name) = name { | 551 | if let Some(name) = name { |
546 | self.body.item_scope.push_res(name.as_name(), def.into()); | 552 | let vis = crate::visibility::Visibility::Public; // FIXME determine correctly |
553 | self.body | ||
554 | .item_scope | ||
555 | .push_res(name.as_name(), crate::per_ns::PerNs::from_def(def, vis)); | ||
547 | } | 556 | } |
548 | } | 557 | } |
549 | } | 558 | } |
diff --git a/crates/ra_hir_def/src/data.rs b/crates/ra_hir_def/src/data.rs index 1aa9a9b7d..c900a6a18 100644 --- a/crates/ra_hir_def/src/data.rs +++ b/crates/ra_hir_def/src/data.rs | |||
@@ -10,8 +10,9 @@ use ra_syntax::ast::{self, AstNode, ImplItem, ModuleItemOwner, NameOwner, TypeAs | |||
10 | 10 | ||
11 | use crate::{ | 11 | use crate::{ |
12 | db::DefDatabase, | 12 | db::DefDatabase, |
13 | path::{path, GenericArgs, Path}, | ||
13 | src::HasSource, | 14 | src::HasSource, |
14 | type_ref::{Mutability, TypeRef}, | 15 | type_ref::{Mutability, TypeBound, TypeRef}, |
15 | AssocContainerId, AssocItemId, ConstId, ConstLoc, Expander, FunctionId, FunctionLoc, HasModule, | 16 | AssocContainerId, AssocItemId, ConstId, ConstLoc, Expander, FunctionId, FunctionLoc, HasModule, |
16 | ImplId, Intern, Lookup, ModuleId, StaticId, TraitId, TypeAliasId, TypeAliasLoc, | 17 | ImplId, Intern, Lookup, ModuleId, StaticId, TraitId, TypeAliasId, TypeAliasLoc, |
17 | }; | 18 | }; |
@@ -62,11 +63,29 @@ impl FunctionData { | |||
62 | TypeRef::unit() | 63 | TypeRef::unit() |
63 | }; | 64 | }; |
64 | 65 | ||
66 | let ret_type = if src.value.is_async() { | ||
67 | let future_impl = desugar_future_path(ret_type); | ||
68 | let ty_bound = TypeBound::Path(future_impl); | ||
69 | TypeRef::ImplTrait(vec![ty_bound]) | ||
70 | } else { | ||
71 | ret_type | ||
72 | }; | ||
73 | |||
65 | let sig = FunctionData { name, params, ret_type, has_self_param }; | 74 | let sig = FunctionData { name, params, ret_type, has_self_param }; |
66 | Arc::new(sig) | 75 | Arc::new(sig) |
67 | } | 76 | } |
68 | } | 77 | } |
69 | 78 | ||
79 | fn desugar_future_path(orig: TypeRef) -> Path { | ||
80 | let path = path![std::future::Future]; | ||
81 | let mut generic_args: Vec<_> = std::iter::repeat(None).take(path.segments.len() - 1).collect(); | ||
82 | let mut last = GenericArgs::empty(); | ||
83 | last.bindings.push((name![Output], orig)); | ||
84 | generic_args.push(Some(Arc::new(last))); | ||
85 | |||
86 | Path::from_known_path(path, generic_args) | ||
87 | } | ||
88 | |||
70 | #[derive(Debug, Clone, PartialEq, Eq)] | 89 | #[derive(Debug, Clone, PartialEq, Eq)] |
71 | pub struct TypeAliasData { | 90 | pub struct TypeAliasData { |
72 | pub name: Name, | 91 | pub name: Name, |
diff --git a/crates/ra_hir_def/src/item_scope.rs b/crates/ra_hir_def/src/item_scope.rs index b0288ee8d..fe7bb9779 100644 --- a/crates/ra_hir_def/src/item_scope.rs +++ b/crates/ra_hir_def/src/item_scope.rs | |||
@@ -5,7 +5,10 @@ use hir_expand::name::Name; | |||
5 | use once_cell::sync::Lazy; | 5 | use once_cell::sync::Lazy; |
6 | use rustc_hash::FxHashMap; | 6 | use rustc_hash::FxHashMap; |
7 | 7 | ||
8 | use crate::{per_ns::PerNs, AdtId, BuiltinType, ImplId, MacroDefId, ModuleDefId, TraitId}; | 8 | use crate::{ |
9 | per_ns::PerNs, visibility::Visibility, AdtId, BuiltinType, ImplId, MacroDefId, ModuleDefId, | ||
10 | TraitId, | ||
11 | }; | ||
9 | 12 | ||
10 | #[derive(Debug, Default, PartialEq, Eq)] | 13 | #[derive(Debug, Default, PartialEq, Eq)] |
11 | pub struct ItemScope { | 14 | pub struct ItemScope { |
@@ -30,7 +33,7 @@ pub struct ItemScope { | |||
30 | static BUILTIN_SCOPE: Lazy<FxHashMap<Name, PerNs>> = Lazy::new(|| { | 33 | static BUILTIN_SCOPE: Lazy<FxHashMap<Name, PerNs>> = Lazy::new(|| { |
31 | BuiltinType::ALL | 34 | BuiltinType::ALL |
32 | .iter() | 35 | .iter() |
33 | .map(|(name, ty)| (name.clone(), PerNs::types(ty.clone().into()))) | 36 | .map(|(name, ty)| (name.clone(), PerNs::types(ty.clone().into(), Visibility::Public))) |
34 | .collect() | 37 | .collect() |
35 | }); | 38 | }); |
36 | 39 | ||
@@ -144,8 +147,8 @@ impl ItemScope { | |||
144 | changed | 147 | changed |
145 | } | 148 | } |
146 | 149 | ||
147 | pub(crate) fn collect_resolutions(&self) -> Vec<(Name, PerNs)> { | 150 | pub(crate) fn resolutions<'a>(&'a self) -> impl Iterator<Item = (Name, PerNs)> + 'a { |
148 | self.visible.iter().map(|(name, res)| (name.clone(), res.clone())).collect() | 151 | self.visible.iter().map(|(name, res)| (name.clone(), res.clone())) |
149 | } | 152 | } |
150 | 153 | ||
151 | pub(crate) fn collect_legacy_macros(&self) -> FxHashMap<Name, MacroDefId> { | 154 | pub(crate) fn collect_legacy_macros(&self) -> FxHashMap<Name, MacroDefId> { |
@@ -153,20 +156,20 @@ impl ItemScope { | |||
153 | } | 156 | } |
154 | } | 157 | } |
155 | 158 | ||
156 | impl From<ModuleDefId> for PerNs { | 159 | impl PerNs { |
157 | fn from(def: ModuleDefId) -> PerNs { | 160 | pub(crate) fn from_def(def: ModuleDefId, v: Visibility) -> PerNs { |
158 | match def { | 161 | match def { |
159 | ModuleDefId::ModuleId(_) => PerNs::types(def), | 162 | ModuleDefId::ModuleId(_) => PerNs::types(def, v), |
160 | ModuleDefId::FunctionId(_) => PerNs::values(def), | 163 | ModuleDefId::FunctionId(_) => PerNs::values(def, v), |
161 | ModuleDefId::AdtId(adt) => match adt { | 164 | ModuleDefId::AdtId(adt) => match adt { |
162 | AdtId::StructId(_) | AdtId::UnionId(_) => PerNs::both(def, def), | 165 | AdtId::StructId(_) | AdtId::UnionId(_) => PerNs::both(def, def, v), |
163 | AdtId::EnumId(_) => PerNs::types(def), | 166 | AdtId::EnumId(_) => PerNs::types(def, v), |
164 | }, | 167 | }, |
165 | ModuleDefId::EnumVariantId(_) => PerNs::both(def, def), | 168 | ModuleDefId::EnumVariantId(_) => PerNs::both(def, def, v), |
166 | ModuleDefId::ConstId(_) | ModuleDefId::StaticId(_) => PerNs::values(def), | 169 | ModuleDefId::ConstId(_) | ModuleDefId::StaticId(_) => PerNs::values(def, v), |
167 | ModuleDefId::TraitId(_) => PerNs::types(def), | 170 | ModuleDefId::TraitId(_) => PerNs::types(def, v), |
168 | ModuleDefId::TypeAliasId(_) => PerNs::types(def), | 171 | ModuleDefId::TypeAliasId(_) => PerNs::types(def, v), |
169 | ModuleDefId::BuiltinType(_) => PerNs::types(def), | 172 | ModuleDefId::BuiltinType(_) => PerNs::types(def, v), |
170 | } | 173 | } |
171 | } | 174 | } |
172 | } | 175 | } |
diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs index f6c7f38d1..61f044ecf 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs | |||
@@ -36,6 +36,8 @@ pub mod nameres; | |||
36 | pub mod src; | 36 | pub mod src; |
37 | pub mod child_by_source; | 37 | pub mod child_by_source; |
38 | 38 | ||
39 | pub mod visibility; | ||
40 | |||
39 | #[cfg(test)] | 41 | #[cfg(test)] |
40 | mod test_db; | 42 | mod test_db; |
41 | #[cfg(test)] | 43 | #[cfg(test)] |
diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index b9f40d3dd..8a22b0585 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs | |||
@@ -24,6 +24,7 @@ use crate::{ | |||
24 | }, | 24 | }, |
25 | path::{ModPath, PathKind}, | 25 | path::{ModPath, PathKind}, |
26 | per_ns::PerNs, | 26 | per_ns::PerNs, |
27 | visibility::Visibility, | ||
27 | AdtId, AstId, ConstLoc, ContainerId, EnumLoc, EnumVariantId, FunctionLoc, ImplLoc, Intern, | 28 | AdtId, AstId, ConstLoc, ContainerId, EnumLoc, EnumVariantId, FunctionLoc, ImplLoc, Intern, |
28 | LocalModuleId, ModuleDefId, ModuleId, StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc, | 29 | LocalModuleId, ModuleDefId, ModuleId, StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc, |
29 | }; | 30 | }; |
@@ -108,7 +109,7 @@ struct MacroDirective { | |||
108 | struct DefCollector<'a, DB> { | 109 | struct DefCollector<'a, DB> { |
109 | db: &'a DB, | 110 | db: &'a DB, |
110 | def_map: CrateDefMap, | 111 | def_map: CrateDefMap, |
111 | glob_imports: FxHashMap<LocalModuleId, Vec<(LocalModuleId, raw::Import)>>, | 112 | glob_imports: FxHashMap<LocalModuleId, Vec<(LocalModuleId, Visibility)>>, |
112 | unresolved_imports: Vec<ImportDirective>, | 113 | unresolved_imports: Vec<ImportDirective>, |
113 | resolved_imports: Vec<ImportDirective>, | 114 | resolved_imports: Vec<ImportDirective>, |
114 | unexpanded_macros: Vec<MacroDirective>, | 115 | unexpanded_macros: Vec<MacroDirective>, |
@@ -214,7 +215,11 @@ where | |||
214 | // In Rust, `#[macro_export]` macros are unconditionally visible at the | 215 | // In Rust, `#[macro_export]` macros are unconditionally visible at the |
215 | // crate root, even if the parent modules is **not** visible. | 216 | // crate root, even if the parent modules is **not** visible. |
216 | if export { | 217 | if export { |
217 | self.update(self.def_map.root, &[(name, PerNs::macros(macro_))]); | 218 | self.update( |
219 | self.def_map.root, | ||
220 | &[(name, PerNs::macros(macro_, Visibility::Public))], | ||
221 | Visibility::Public, | ||
222 | ); | ||
218 | } | 223 | } |
219 | } | 224 | } |
220 | 225 | ||
@@ -348,9 +353,12 @@ where | |||
348 | 353 | ||
349 | fn record_resolved_import(&mut self, directive: &ImportDirective) { | 354 | fn record_resolved_import(&mut self, directive: &ImportDirective) { |
350 | let module_id = directive.module_id; | 355 | let module_id = directive.module_id; |
351 | let import_id = directive.import_id; | ||
352 | let import = &directive.import; | 356 | let import = &directive.import; |
353 | let def = directive.status.namespaces(); | 357 | let def = directive.status.namespaces(); |
358 | let vis = self | ||
359 | .def_map | ||
360 | .resolve_visibility(self.db, module_id, &directive.import.visibility) | ||
361 | .unwrap_or(Visibility::Public); | ||
354 | 362 | ||
355 | if import.is_glob { | 363 | if import.is_glob { |
356 | log::debug!("glob import: {:?}", import); | 364 | log::debug!("glob import: {:?}", import); |
@@ -366,9 +374,16 @@ where | |||
366 | let scope = &item_map[m.local_id].scope; | 374 | let scope = &item_map[m.local_id].scope; |
367 | 375 | ||
368 | // Module scoped macros is included | 376 | // Module scoped macros is included |
369 | let items = scope.collect_resolutions(); | 377 | let items = scope |
370 | 378 | .resolutions() | |
371 | self.update(module_id, &items); | 379 | // only keep visible names... |
380 | .map(|(n, res)| { | ||
381 | (n, res.filter_visibility(|v| v.is_visible_from_other_crate())) | ||
382 | }) | ||
383 | .filter(|(_, res)| !res.is_none()) | ||
384 | .collect::<Vec<_>>(); | ||
385 | |||
386 | self.update(module_id, &items, vis); | ||
372 | } else { | 387 | } else { |
373 | // glob import from same crate => we do an initial | 388 | // glob import from same crate => we do an initial |
374 | // import, and then need to propagate any further | 389 | // import, and then need to propagate any further |
@@ -376,13 +391,25 @@ where | |||
376 | let scope = &self.def_map[m.local_id].scope; | 391 | let scope = &self.def_map[m.local_id].scope; |
377 | 392 | ||
378 | // Module scoped macros is included | 393 | // Module scoped macros is included |
379 | let items = scope.collect_resolutions(); | 394 | let items = scope |
380 | 395 | .resolutions() | |
381 | self.update(module_id, &items); | 396 | // only keep visible names... |
397 | .map(|(n, res)| { | ||
398 | ( | ||
399 | n, | ||
400 | res.filter_visibility(|v| { | ||
401 | v.is_visible_from_def_map(&self.def_map, module_id) | ||
402 | }), | ||
403 | ) | ||
404 | }) | ||
405 | .filter(|(_, res)| !res.is_none()) | ||
406 | .collect::<Vec<_>>(); | ||
407 | |||
408 | self.update(module_id, &items, vis); | ||
382 | // record the glob import in case we add further items | 409 | // record the glob import in case we add further items |
383 | let glob = self.glob_imports.entry(m.local_id).or_default(); | 410 | let glob = self.glob_imports.entry(m.local_id).or_default(); |
384 | if !glob.iter().any(|it| *it == (module_id, import_id)) { | 411 | if !glob.iter().any(|(mid, _)| *mid == module_id) { |
385 | glob.push((module_id, import_id)); | 412 | glob.push((module_id, vis)); |
386 | } | 413 | } |
387 | } | 414 | } |
388 | } | 415 | } |
@@ -396,11 +423,11 @@ where | |||
396 | .map(|(local_id, variant_data)| { | 423 | .map(|(local_id, variant_data)| { |
397 | let name = variant_data.name.clone(); | 424 | let name = variant_data.name.clone(); |
398 | let variant = EnumVariantId { parent: e, local_id }; | 425 | let variant = EnumVariantId { parent: e, local_id }; |
399 | let res = PerNs::both(variant.into(), variant.into()); | 426 | let res = PerNs::both(variant.into(), variant.into(), vis); |
400 | (name, res) | 427 | (name, res) |
401 | }) | 428 | }) |
402 | .collect::<Vec<_>>(); | 429 | .collect::<Vec<_>>(); |
403 | self.update(module_id, &resolutions); | 430 | self.update(module_id, &resolutions, vis); |
404 | } | 431 | } |
405 | Some(d) => { | 432 | Some(d) => { |
406 | log::debug!("glob import {:?} from non-module/enum {:?}", import, d); | 433 | log::debug!("glob import {:?} from non-module/enum {:?}", import, d); |
@@ -422,21 +449,24 @@ where | |||
422 | } | 449 | } |
423 | } | 450 | } |
424 | 451 | ||
425 | self.update(module_id, &[(name, def)]); | 452 | self.update(module_id, &[(name, def)], vis); |
426 | } | 453 | } |
427 | None => tested_by!(bogus_paths), | 454 | None => tested_by!(bogus_paths), |
428 | } | 455 | } |
429 | } | 456 | } |
430 | } | 457 | } |
431 | 458 | ||
432 | fn update(&mut self, module_id: LocalModuleId, resolutions: &[(Name, PerNs)]) { | 459 | fn update(&mut self, module_id: LocalModuleId, resolutions: &[(Name, PerNs)], vis: Visibility) { |
433 | self.update_recursive(module_id, resolutions, 0) | 460 | self.update_recursive(module_id, resolutions, vis, 0) |
434 | } | 461 | } |
435 | 462 | ||
436 | fn update_recursive( | 463 | fn update_recursive( |
437 | &mut self, | 464 | &mut self, |
438 | module_id: LocalModuleId, | 465 | module_id: LocalModuleId, |
439 | resolutions: &[(Name, PerNs)], | 466 | resolutions: &[(Name, PerNs)], |
467 | // All resolutions are imported with this visibility; the visibilies in | ||
468 | // the `PerNs` values are ignored and overwritten | ||
469 | vis: Visibility, | ||
440 | depth: usize, | 470 | depth: usize, |
441 | ) { | 471 | ) { |
442 | if depth > 100 { | 472 | if depth > 100 { |
@@ -446,7 +476,7 @@ where | |||
446 | let scope = &mut self.def_map.modules[module_id].scope; | 476 | let scope = &mut self.def_map.modules[module_id].scope; |
447 | let mut changed = false; | 477 | let mut changed = false; |
448 | for (name, res) in resolutions { | 478 | for (name, res) in resolutions { |
449 | changed |= scope.push_res(name.clone(), *res); | 479 | changed |= scope.push_res(name.clone(), res.with_visibility(vis)); |
450 | } | 480 | } |
451 | 481 | ||
452 | if !changed { | 482 | if !changed { |
@@ -459,9 +489,13 @@ where | |||
459 | .flat_map(|v| v.iter()) | 489 | .flat_map(|v| v.iter()) |
460 | .cloned() | 490 | .cloned() |
461 | .collect::<Vec<_>>(); | 491 | .collect::<Vec<_>>(); |
462 | for (glob_importing_module, _glob_import) in glob_imports { | 492 | for (glob_importing_module, glob_import_vis) in glob_imports { |
463 | // We pass the glob import so that the tracked import in those modules is that glob import | 493 | // we know all resolutions have the same visibility (`vis`), so we |
464 | self.update_recursive(glob_importing_module, resolutions, depth + 1); | 494 | // just need to check that once |
495 | if !vis.is_visible_from_def_map(&self.def_map, glob_importing_module) { | ||
496 | continue; | ||
497 | } | ||
498 | self.update_recursive(glob_importing_module, resolutions, glob_import_vis, depth + 1); | ||
465 | } | 499 | } |
466 | } | 500 | } |
467 | 501 | ||
@@ -633,9 +667,13 @@ where | |||
633 | let is_macro_use = attrs.by_key("macro_use").exists(); | 667 | let is_macro_use = attrs.by_key("macro_use").exists(); |
634 | match module { | 668 | match module { |
635 | // inline module, just recurse | 669 | // inline module, just recurse |
636 | raw::ModuleData::Definition { name, items, ast_id } => { | 670 | raw::ModuleData::Definition { name, visibility, items, ast_id } => { |
637 | let module_id = | 671 | let module_id = self.push_child_module( |
638 | self.push_child_module(name.clone(), AstId::new(self.file_id, *ast_id), None); | 672 | name.clone(), |
673 | AstId::new(self.file_id, *ast_id), | ||
674 | None, | ||
675 | &visibility, | ||
676 | ); | ||
639 | 677 | ||
640 | ModCollector { | 678 | ModCollector { |
641 | def_collector: &mut *self.def_collector, | 679 | def_collector: &mut *self.def_collector, |
@@ -650,7 +688,7 @@ where | |||
650 | } | 688 | } |
651 | } | 689 | } |
652 | // out of line module, resolve, parse and recurse | 690 | // out of line module, resolve, parse and recurse |
653 | raw::ModuleData::Declaration { name, ast_id } => { | 691 | raw::ModuleData::Declaration { name, visibility, ast_id } => { |
654 | let ast_id = AstId::new(self.file_id, *ast_id); | 692 | let ast_id = AstId::new(self.file_id, *ast_id); |
655 | match self.mod_dir.resolve_declaration( | 693 | match self.mod_dir.resolve_declaration( |
656 | self.def_collector.db, | 694 | self.def_collector.db, |
@@ -659,7 +697,12 @@ where | |||
659 | path_attr, | 697 | path_attr, |
660 | ) { | 698 | ) { |
661 | Ok((file_id, mod_dir)) => { | 699 | Ok((file_id, mod_dir)) => { |
662 | let module_id = self.push_child_module(name.clone(), ast_id, Some(file_id)); | 700 | let module_id = self.push_child_module( |
701 | name.clone(), | ||
702 | ast_id, | ||
703 | Some(file_id), | ||
704 | &visibility, | ||
705 | ); | ||
663 | let raw_items = self.def_collector.db.raw_items(file_id.into()); | 706 | let raw_items = self.def_collector.db.raw_items(file_id.into()); |
664 | ModCollector { | 707 | ModCollector { |
665 | def_collector: &mut *self.def_collector, | 708 | def_collector: &mut *self.def_collector, |
@@ -690,7 +733,13 @@ where | |||
690 | name: Name, | 733 | name: Name, |
691 | declaration: AstId<ast::Module>, | 734 | declaration: AstId<ast::Module>, |
692 | definition: Option<FileId>, | 735 | definition: Option<FileId>, |
736 | visibility: &crate::visibility::RawVisibility, | ||
693 | ) -> LocalModuleId { | 737 | ) -> LocalModuleId { |
738 | let vis = self | ||
739 | .def_collector | ||
740 | .def_map | ||
741 | .resolve_visibility(self.def_collector.db, self.module_id, visibility) | ||
742 | .unwrap_or(Visibility::Public); | ||
694 | let modules = &mut self.def_collector.def_map.modules; | 743 | let modules = &mut self.def_collector.def_map.modules; |
695 | let res = modules.alloc(ModuleData::default()); | 744 | let res = modules.alloc(ModuleData::default()); |
696 | modules[res].parent = Some(self.module_id); | 745 | modules[res].parent = Some(self.module_id); |
@@ -702,7 +751,7 @@ where | |||
702 | let module = ModuleId { krate: self.def_collector.def_map.krate, local_id: res }; | 751 | let module = ModuleId { krate: self.def_collector.def_map.krate, local_id: res }; |
703 | let def: ModuleDefId = module.into(); | 752 | let def: ModuleDefId = module.into(); |
704 | self.def_collector.def_map.modules[self.module_id].scope.define_def(def); | 753 | self.def_collector.def_map.modules[self.module_id].scope.define_def(def); |
705 | self.def_collector.update(self.module_id, &[(name, def.into())]); | 754 | self.def_collector.update(self.module_id, &[(name, PerNs::from_def(def, vis))], vis); |
706 | res | 755 | res |
707 | } | 756 | } |
708 | 757 | ||
@@ -716,6 +765,7 @@ where | |||
716 | 765 | ||
717 | let name = def.name.clone(); | 766 | let name = def.name.clone(); |
718 | let container = ContainerId::ModuleId(module); | 767 | let container = ContainerId::ModuleId(module); |
768 | let vis = &def.visibility; | ||
719 | let def: ModuleDefId = match def.kind { | 769 | let def: ModuleDefId = match def.kind { |
720 | raw::DefKind::Function(ast_id) => FunctionLoc { | 770 | raw::DefKind::Function(ast_id) => FunctionLoc { |
721 | container: container.into(), | 771 | container: container.into(), |
@@ -761,7 +811,12 @@ where | |||
761 | .into(), | 811 | .into(), |
762 | }; | 812 | }; |
763 | self.def_collector.def_map.modules[self.module_id].scope.define_def(def); | 813 | self.def_collector.def_map.modules[self.module_id].scope.define_def(def); |
764 | self.def_collector.update(self.module_id, &[(name, def.into())]) | 814 | let vis = self |
815 | .def_collector | ||
816 | .def_map | ||
817 | .resolve_visibility(self.def_collector.db, self.module_id, vis) | ||
818 | .unwrap_or(Visibility::Public); | ||
819 | self.def_collector.update(self.module_id, &[(name, PerNs::from_def(def, vis))], vis) | ||
765 | } | 820 | } |
766 | 821 | ||
767 | fn collect_derives(&mut self, attrs: &Attrs, def: &raw::DefData) { | 822 | fn collect_derives(&mut self, attrs: &Attrs, def: &raw::DefData) { |
diff --git a/crates/ra_hir_def/src/nameres/path_resolution.rs b/crates/ra_hir_def/src/nameres/path_resolution.rs index 695014c7b..fd6422d60 100644 --- a/crates/ra_hir_def/src/nameres/path_resolution.rs +++ b/crates/ra_hir_def/src/nameres/path_resolution.rs | |||
@@ -21,6 +21,7 @@ use crate::{ | |||
21 | nameres::{BuiltinShadowMode, CrateDefMap}, | 21 | nameres::{BuiltinShadowMode, CrateDefMap}, |
22 | path::{ModPath, PathKind}, | 22 | path::{ModPath, PathKind}, |
23 | per_ns::PerNs, | 23 | per_ns::PerNs, |
24 | visibility::{RawVisibility, Visibility}, | ||
24 | AdtId, CrateId, EnumVariantId, LocalModuleId, ModuleDefId, ModuleId, | 25 | AdtId, CrateId, EnumVariantId, LocalModuleId, ModuleDefId, ModuleId, |
25 | }; | 26 | }; |
26 | 27 | ||
@@ -61,7 +62,35 @@ impl ResolvePathResult { | |||
61 | 62 | ||
62 | impl CrateDefMap { | 63 | impl CrateDefMap { |
63 | pub(super) fn resolve_name_in_extern_prelude(&self, name: &Name) -> PerNs { | 64 | pub(super) fn resolve_name_in_extern_prelude(&self, name: &Name) -> PerNs { |
64 | self.extern_prelude.get(name).map_or(PerNs::none(), |&it| PerNs::types(it)) | 65 | self.extern_prelude |
66 | .get(name) | ||
67 | .map_or(PerNs::none(), |&it| PerNs::types(it, Visibility::Public)) | ||
68 | } | ||
69 | |||
70 | pub(crate) fn resolve_visibility( | ||
71 | &self, | ||
72 | db: &impl DefDatabase, | ||
73 | original_module: LocalModuleId, | ||
74 | visibility: &RawVisibility, | ||
75 | ) -> Option<Visibility> { | ||
76 | match visibility { | ||
77 | RawVisibility::Module(path) => { | ||
78 | let (result, remaining) = | ||
79 | self.resolve_path(db, original_module, &path, BuiltinShadowMode::Module); | ||
80 | if remaining.is_some() { | ||
81 | return None; | ||
82 | } | ||
83 | let types = result.take_types()?; | ||
84 | match types { | ||
85 | ModuleDefId::ModuleId(m) => Some(Visibility::Module(m)), | ||
86 | _ => { | ||
87 | // error: visibility needs to refer to module | ||
88 | None | ||
89 | } | ||
90 | } | ||
91 | } | ||
92 | RawVisibility::Public => Some(Visibility::Public), | ||
93 | } | ||
65 | } | 94 | } |
66 | 95 | ||
67 | // Returns Yes if we are sure that additions to `ItemMap` wouldn't change | 96 | // Returns Yes if we are sure that additions to `ItemMap` wouldn't change |
@@ -88,17 +117,21 @@ impl CrateDefMap { | |||
88 | PathKind::DollarCrate(krate) => { | 117 | PathKind::DollarCrate(krate) => { |
89 | if krate == self.krate { | 118 | if krate == self.krate { |
90 | tested_by!(macro_dollar_crate_self); | 119 | tested_by!(macro_dollar_crate_self); |
91 | PerNs::types(ModuleId { krate: self.krate, local_id: self.root }.into()) | 120 | PerNs::types( |
121 | ModuleId { krate: self.krate, local_id: self.root }.into(), | ||
122 | Visibility::Public, | ||
123 | ) | ||
92 | } else { | 124 | } else { |
93 | let def_map = db.crate_def_map(krate); | 125 | let def_map = db.crate_def_map(krate); |
94 | let module = ModuleId { krate, local_id: def_map.root }; | 126 | let module = ModuleId { krate, local_id: def_map.root }; |
95 | tested_by!(macro_dollar_crate_other); | 127 | tested_by!(macro_dollar_crate_other); |
96 | PerNs::types(module.into()) | 128 | PerNs::types(module.into(), Visibility::Public) |
97 | } | 129 | } |
98 | } | 130 | } |
99 | PathKind::Crate => { | 131 | PathKind::Crate => PerNs::types( |
100 | PerNs::types(ModuleId { krate: self.krate, local_id: self.root }.into()) | 132 | ModuleId { krate: self.krate, local_id: self.root }.into(), |
101 | } | 133 | Visibility::Public, |
134 | ), | ||
102 | // plain import or absolute path in 2015: crate-relative with | 135 | // plain import or absolute path in 2015: crate-relative with |
103 | // fallback to extern prelude (with the simplification in | 136 | // fallback to extern prelude (with the simplification in |
104 | // rust-lang/rust#57745) | 137 | // rust-lang/rust#57745) |
@@ -126,7 +159,10 @@ impl CrateDefMap { | |||
126 | let m = successors(Some(original_module), |m| self.modules[*m].parent) | 159 | let m = successors(Some(original_module), |m| self.modules[*m].parent) |
127 | .nth(lvl as usize); | 160 | .nth(lvl as usize); |
128 | if let Some(local_id) = m { | 161 | if let Some(local_id) = m { |
129 | PerNs::types(ModuleId { krate: self.krate, local_id }.into()) | 162 | PerNs::types( |
163 | ModuleId { krate: self.krate, local_id }.into(), | ||
164 | Visibility::Public, | ||
165 | ) | ||
130 | } else { | 166 | } else { |
131 | log::debug!("super path in root module"); | 167 | log::debug!("super path in root module"); |
132 | return ResolvePathResult::empty(ReachedFixedPoint::Yes); | 168 | return ResolvePathResult::empty(ReachedFixedPoint::Yes); |
@@ -140,7 +176,7 @@ impl CrateDefMap { | |||
140 | }; | 176 | }; |
141 | if let Some(def) = self.extern_prelude.get(&segment) { | 177 | if let Some(def) = self.extern_prelude.get(&segment) { |
142 | log::debug!("absolute path {:?} resolved to crate {:?}", path, def); | 178 | log::debug!("absolute path {:?} resolved to crate {:?}", path, def); |
143 | PerNs::types(*def) | 179 | PerNs::types(*def, Visibility::Public) |
144 | } else { | 180 | } else { |
145 | return ResolvePathResult::empty(ReachedFixedPoint::No); // extern crate declarations can add to the extern prelude | 181 | return ResolvePathResult::empty(ReachedFixedPoint::No); // extern crate declarations can add to the extern prelude |
146 | } | 182 | } |
@@ -148,7 +184,7 @@ impl CrateDefMap { | |||
148 | }; | 184 | }; |
149 | 185 | ||
150 | for (i, segment) in segments { | 186 | for (i, segment) in segments { |
151 | let curr = match curr_per_ns.take_types() { | 187 | let (curr, vis) = match curr_per_ns.take_types_vis() { |
152 | Some(r) => r, | 188 | Some(r) => r, |
153 | None => { | 189 | None => { |
154 | // we still have path segments left, but the path so far | 190 | // we still have path segments left, but the path so far |
@@ -189,11 +225,11 @@ impl CrateDefMap { | |||
189 | match enum_data.variant(&segment) { | 225 | match enum_data.variant(&segment) { |
190 | Some(local_id) => { | 226 | Some(local_id) => { |
191 | let variant = EnumVariantId { parent: e, local_id }; | 227 | let variant = EnumVariantId { parent: e, local_id }; |
192 | PerNs::both(variant.into(), variant.into()) | 228 | PerNs::both(variant.into(), variant.into(), Visibility::Public) |
193 | } | 229 | } |
194 | None => { | 230 | None => { |
195 | return ResolvePathResult::with( | 231 | return ResolvePathResult::with( |
196 | PerNs::types(e.into()), | 232 | PerNs::types(e.into(), vis), |
197 | ReachedFixedPoint::Yes, | 233 | ReachedFixedPoint::Yes, |
198 | Some(i), | 234 | Some(i), |
199 | Some(self.krate), | 235 | Some(self.krate), |
@@ -211,7 +247,7 @@ impl CrateDefMap { | |||
211 | ); | 247 | ); |
212 | 248 | ||
213 | return ResolvePathResult::with( | 249 | return ResolvePathResult::with( |
214 | PerNs::types(s), | 250 | PerNs::types(s, vis), |
215 | ReachedFixedPoint::Yes, | 251 | ReachedFixedPoint::Yes, |
216 | Some(i), | 252 | Some(i), |
217 | Some(self.krate), | 253 | Some(self.krate), |
@@ -235,11 +271,15 @@ impl CrateDefMap { | |||
235 | // - current module / scope | 271 | // - current module / scope |
236 | // - extern prelude | 272 | // - extern prelude |
237 | // - std prelude | 273 | // - std prelude |
238 | let from_legacy_macro = | 274 | let from_legacy_macro = self[module] |
239 | self[module].scope.get_legacy_macro(name).map_or_else(PerNs::none, PerNs::macros); | 275 | .scope |
276 | .get_legacy_macro(name) | ||
277 | .map_or_else(PerNs::none, |m| PerNs::macros(m, Visibility::Public)); | ||
240 | let from_scope = self[module].scope.get(name, shadow); | 278 | let from_scope = self[module].scope.get(name, shadow); |
241 | let from_extern_prelude = | 279 | let from_extern_prelude = self |
242 | self.extern_prelude.get(name).map_or(PerNs::none(), |&it| PerNs::types(it)); | 280 | .extern_prelude |
281 | .get(name) | ||
282 | .map_or(PerNs::none(), |&it| PerNs::types(it, Visibility::Public)); | ||
243 | let from_prelude = self.resolve_in_prelude(db, name, shadow); | 283 | let from_prelude = self.resolve_in_prelude(db, name, shadow); |
244 | 284 | ||
245 | from_legacy_macro.or(from_scope).or(from_extern_prelude).or(from_prelude) | 285 | from_legacy_macro.or(from_scope).or(from_extern_prelude).or(from_prelude) |
diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs index 73dc08745..fac1169ef 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs | |||
@@ -16,12 +16,15 @@ use hir_expand::{ | |||
16 | use ra_arena::{impl_arena_id, Arena, RawId}; | 16 | use ra_arena::{impl_arena_id, Arena, RawId}; |
17 | use ra_prof::profile; | 17 | use ra_prof::profile; |
18 | use ra_syntax::{ | 18 | use ra_syntax::{ |
19 | ast::{self, AttrsOwner, NameOwner}, | 19 | ast::{self, AttrsOwner, NameOwner, VisibilityOwner}, |
20 | AstNode, | 20 | AstNode, |
21 | }; | 21 | }; |
22 | use test_utils::tested_by; | 22 | use test_utils::tested_by; |
23 | 23 | ||
24 | use crate::{attr::Attrs, db::DefDatabase, path::ModPath, FileAstId, HirFileId, InFile}; | 24 | use crate::{ |
25 | attr::Attrs, db::DefDatabase, path::ModPath, visibility::RawVisibility, FileAstId, HirFileId, | ||
26 | InFile, | ||
27 | }; | ||
25 | 28 | ||
26 | /// `RawItems` is a set of top-level items in a file (except for impls). | 29 | /// `RawItems` is a set of top-level items in a file (except for impls). |
27 | /// | 30 | /// |
@@ -122,8 +125,17 @@ impl_arena_id!(Module); | |||
122 | 125 | ||
123 | #[derive(Debug, PartialEq, Eq)] | 126 | #[derive(Debug, PartialEq, Eq)] |
124 | pub(super) enum ModuleData { | 127 | pub(super) enum ModuleData { |
125 | Declaration { name: Name, ast_id: FileAstId<ast::Module> }, | 128 | Declaration { |
126 | Definition { name: Name, ast_id: FileAstId<ast::Module>, items: Vec<RawItem> }, | 129 | name: Name, |
130 | visibility: RawVisibility, | ||
131 | ast_id: FileAstId<ast::Module>, | ||
132 | }, | ||
133 | Definition { | ||
134 | name: Name, | ||
135 | visibility: RawVisibility, | ||
136 | ast_id: FileAstId<ast::Module>, | ||
137 | items: Vec<RawItem>, | ||
138 | }, | ||
127 | } | 139 | } |
128 | 140 | ||
129 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 141 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
@@ -138,6 +150,7 @@ pub struct ImportData { | |||
138 | pub(super) is_prelude: bool, | 150 | pub(super) is_prelude: bool, |
139 | pub(super) is_extern_crate: bool, | 151 | pub(super) is_extern_crate: bool, |
140 | pub(super) is_macro_use: bool, | 152 | pub(super) is_macro_use: bool, |
153 | pub(super) visibility: RawVisibility, | ||
141 | } | 154 | } |
142 | 155 | ||
143 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 156 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
@@ -148,6 +161,7 @@ impl_arena_id!(Def); | |||
148 | pub(super) struct DefData { | 161 | pub(super) struct DefData { |
149 | pub(super) name: Name, | 162 | pub(super) name: Name, |
150 | pub(super) kind: DefKind, | 163 | pub(super) kind: DefKind, |
164 | pub(super) visibility: RawVisibility, | ||
151 | } | 165 | } |
152 | 166 | ||
153 | #[derive(Debug, PartialEq, Eq, Clone, Copy)] | 167 | #[derive(Debug, PartialEq, Eq, Clone, Copy)] |
@@ -218,6 +232,7 @@ impl RawItemsCollector { | |||
218 | 232 | ||
219 | fn add_item(&mut self, current_module: Option<Module>, item: ast::ModuleItem) { | 233 | fn add_item(&mut self, current_module: Option<Module>, item: ast::ModuleItem) { |
220 | let attrs = self.parse_attrs(&item); | 234 | let attrs = self.parse_attrs(&item); |
235 | let visibility = RawVisibility::from_ast_with_hygiene(item.visibility(), &self.hygiene); | ||
221 | let (kind, name) = match item { | 236 | let (kind, name) = match item { |
222 | ast::ModuleItem::Module(module) => { | 237 | ast::ModuleItem::Module(module) => { |
223 | self.add_module(current_module, module); | 238 | self.add_module(current_module, module); |
@@ -266,7 +281,7 @@ impl RawItemsCollector { | |||
266 | }; | 281 | }; |
267 | if let Some(name) = name { | 282 | if let Some(name) = name { |
268 | let name = name.as_name(); | 283 | let name = name.as_name(); |
269 | let def = self.raw_items.defs.alloc(DefData { name, kind }); | 284 | let def = self.raw_items.defs.alloc(DefData { name, kind, visibility }); |
270 | self.push_item(current_module, attrs, RawItemKind::Def(def)); | 285 | self.push_item(current_module, attrs, RawItemKind::Def(def)); |
271 | } | 286 | } |
272 | } | 287 | } |
@@ -277,10 +292,12 @@ impl RawItemsCollector { | |||
277 | None => return, | 292 | None => return, |
278 | }; | 293 | }; |
279 | let attrs = self.parse_attrs(&module); | 294 | let attrs = self.parse_attrs(&module); |
295 | let visibility = RawVisibility::from_ast_with_hygiene(module.visibility(), &self.hygiene); | ||
280 | 296 | ||
281 | let ast_id = self.source_ast_id_map.ast_id(&module); | 297 | let ast_id = self.source_ast_id_map.ast_id(&module); |
282 | if module.has_semi() { | 298 | if module.has_semi() { |
283 | let item = self.raw_items.modules.alloc(ModuleData::Declaration { name, ast_id }); | 299 | let item = |
300 | self.raw_items.modules.alloc(ModuleData::Declaration { name, visibility, ast_id }); | ||
284 | self.push_item(current_module, attrs, RawItemKind::Module(item)); | 301 | self.push_item(current_module, attrs, RawItemKind::Module(item)); |
285 | return; | 302 | return; |
286 | } | 303 | } |
@@ -288,6 +305,7 @@ impl RawItemsCollector { | |||
288 | if let Some(item_list) = module.item_list() { | 305 | if let Some(item_list) = module.item_list() { |
289 | let item = self.raw_items.modules.alloc(ModuleData::Definition { | 306 | let item = self.raw_items.modules.alloc(ModuleData::Definition { |
290 | name, | 307 | name, |
308 | visibility, | ||
291 | ast_id, | 309 | ast_id, |
292 | items: Vec::new(), | 310 | items: Vec::new(), |
293 | }); | 311 | }); |
@@ -302,6 +320,7 @@ impl RawItemsCollector { | |||
302 | // FIXME: cfg_attr | 320 | // FIXME: cfg_attr |
303 | let is_prelude = use_item.has_atom_attr("prelude_import"); | 321 | let is_prelude = use_item.has_atom_attr("prelude_import"); |
304 | let attrs = self.parse_attrs(&use_item); | 322 | let attrs = self.parse_attrs(&use_item); |
323 | let visibility = RawVisibility::from_ast_with_hygiene(use_item.visibility(), &self.hygiene); | ||
305 | 324 | ||
306 | let mut buf = Vec::new(); | 325 | let mut buf = Vec::new(); |
307 | ModPath::expand_use_item( | 326 | ModPath::expand_use_item( |
@@ -315,6 +334,7 @@ impl RawItemsCollector { | |||
315 | is_prelude, | 334 | is_prelude, |
316 | is_extern_crate: false, | 335 | is_extern_crate: false, |
317 | is_macro_use: false, | 336 | is_macro_use: false, |
337 | visibility: visibility.clone(), | ||
318 | }; | 338 | }; |
319 | buf.push(import_data); | 339 | buf.push(import_data); |
320 | }, | 340 | }, |
@@ -331,6 +351,8 @@ impl RawItemsCollector { | |||
331 | ) { | 351 | ) { |
332 | if let Some(name_ref) = extern_crate.name_ref() { | 352 | if let Some(name_ref) = extern_crate.name_ref() { |
333 | let path = ModPath::from_name_ref(&name_ref); | 353 | let path = ModPath::from_name_ref(&name_ref); |
354 | let visibility = | ||
355 | RawVisibility::from_ast_with_hygiene(extern_crate.visibility(), &self.hygiene); | ||
334 | let alias = extern_crate.alias().and_then(|a| a.name()).map(|it| it.as_name()); | 356 | let alias = extern_crate.alias().and_then(|a| a.name()).map(|it| it.as_name()); |
335 | let attrs = self.parse_attrs(&extern_crate); | 357 | let attrs = self.parse_attrs(&extern_crate); |
336 | // FIXME: cfg_attr | 358 | // FIXME: cfg_attr |
@@ -342,6 +364,7 @@ impl RawItemsCollector { | |||
342 | is_prelude: false, | 364 | is_prelude: false, |
343 | is_extern_crate: true, | 365 | is_extern_crate: true, |
344 | is_macro_use, | 366 | is_macro_use, |
367 | visibility, | ||
345 | }; | 368 | }; |
346 | self.push_import(current_module, attrs, import_data); | 369 | self.push_import(current_module, attrs, import_data); |
347 | } | 370 | } |
diff --git a/crates/ra_hir_def/src/nameres/tests.rs b/crates/ra_hir_def/src/nameres/tests.rs index ff474b53b..78bcdc850 100644 --- a/crates/ra_hir_def/src/nameres/tests.rs +++ b/crates/ra_hir_def/src/nameres/tests.rs | |||
@@ -12,8 +12,8 @@ use test_utils::covers; | |||
12 | 12 | ||
13 | use crate::{db::DefDatabase, nameres::*, test_db::TestDB, LocalModuleId}; | 13 | use crate::{db::DefDatabase, nameres::*, test_db::TestDB, LocalModuleId}; |
14 | 14 | ||
15 | fn def_map(fixtute: &str) -> String { | 15 | fn def_map(fixture: &str) -> String { |
16 | let dm = compute_crate_def_map(fixtute); | 16 | let dm = compute_crate_def_map(fixture); |
17 | render_crate_def_map(&dm) | 17 | render_crate_def_map(&dm) |
18 | } | 18 | } |
19 | 19 | ||
@@ -32,7 +32,7 @@ fn render_crate_def_map(map: &CrateDefMap) -> String { | |||
32 | *buf += path; | 32 | *buf += path; |
33 | *buf += "\n"; | 33 | *buf += "\n"; |
34 | 34 | ||
35 | let mut entries = map.modules[module].scope.collect_resolutions(); | 35 | let mut entries: Vec<_> = map.modules[module].scope.resolutions().collect(); |
36 | entries.sort_by_key(|(name, _)| name.clone()); | 36 | entries.sort_by_key(|(name, _)| name.clone()); |
37 | 37 | ||
38 | for (name, def) in entries { | 38 | for (name, def) in entries { |
diff --git a/crates/ra_hir_def/src/nameres/tests/globs.rs b/crates/ra_hir_def/src/nameres/tests/globs.rs index 5e24cb94d..71fa0abe8 100644 --- a/crates/ra_hir_def/src/nameres/tests/globs.rs +++ b/crates/ra_hir_def/src/nameres/tests/globs.rs | |||
@@ -74,6 +74,83 @@ fn glob_2() { | |||
74 | } | 74 | } |
75 | 75 | ||
76 | #[test] | 76 | #[test] |
77 | fn glob_privacy_1() { | ||
78 | let map = def_map( | ||
79 | " | ||
80 | //- /lib.rs | ||
81 | mod foo; | ||
82 | use foo::*; | ||
83 | |||
84 | //- /foo/mod.rs | ||
85 | pub mod bar; | ||
86 | pub use self::bar::*; | ||
87 | struct PrivateStructFoo; | ||
88 | |||
89 | //- /foo/bar.rs | ||
90 | pub struct Baz; | ||
91 | struct PrivateStructBar; | ||
92 | pub use super::*; | ||
93 | ", | ||
94 | ); | ||
95 | assert_snapshot!(map, @r###" | ||
96 | crate | ||
97 | Baz: t v | ||
98 | bar: t | ||
99 | foo: t | ||
100 | |||
101 | crate::foo | ||
102 | Baz: t v | ||
103 | PrivateStructFoo: t v | ||
104 | bar: t | ||
105 | |||
106 | crate::foo::bar | ||
107 | Baz: t v | ||
108 | PrivateStructBar: t v | ||
109 | PrivateStructFoo: t v | ||
110 | bar: t | ||
111 | "### | ||
112 | ); | ||
113 | } | ||
114 | |||
115 | #[test] | ||
116 | fn glob_privacy_2() { | ||
117 | let map = def_map( | ||
118 | " | ||
119 | //- /lib.rs | ||
120 | mod foo; | ||
121 | use foo::*; | ||
122 | use foo::bar::*; | ||
123 | |||
124 | //- /foo/mod.rs | ||
125 | mod bar; | ||
126 | fn Foo() {}; | ||
127 | pub struct Foo {}; | ||
128 | |||
129 | //- /foo/bar.rs | ||
130 | pub(super) struct PrivateBaz; | ||
131 | struct PrivateBar; | ||
132 | pub(crate) struct PubCrateStruct; | ||
133 | ", | ||
134 | ); | ||
135 | assert_snapshot!(map, @r###" | ||
136 | crate | ||
137 | Foo: t | ||
138 | PubCrateStruct: t v | ||
139 | foo: t | ||
140 | |||
141 | crate::foo | ||
142 | Foo: t v | ||
143 | bar: t | ||
144 | |||
145 | crate::foo::bar | ||
146 | PrivateBar: t v | ||
147 | PrivateBaz: t v | ||
148 | PubCrateStruct: t v | ||
149 | "### | ||
150 | ); | ||
151 | } | ||
152 | |||
153 | #[test] | ||
77 | fn glob_across_crates() { | 154 | fn glob_across_crates() { |
78 | covers!(glob_across_crates); | 155 | covers!(glob_across_crates); |
79 | let map = def_map( | 156 | let map = def_map( |
@@ -93,6 +170,26 @@ fn glob_across_crates() { | |||
93 | } | 170 | } |
94 | 171 | ||
95 | #[test] | 172 | #[test] |
173 | fn glob_privacy_across_crates() { | ||
174 | covers!(glob_across_crates); | ||
175 | let map = def_map( | ||
176 | " | ||
177 | //- /main.rs crate:main deps:test_crate | ||
178 | use test_crate::*; | ||
179 | |||
180 | //- /lib.rs crate:test_crate | ||
181 | pub struct Baz; | ||
182 | struct Foo; | ||
183 | ", | ||
184 | ); | ||
185 | assert_snapshot!(map, @r###" | ||
186 | ⋮crate | ||
187 | ⋮Baz: t v | ||
188 | "### | ||
189 | ); | ||
190 | } | ||
191 | |||
192 | #[test] | ||
96 | fn glob_enum() { | 193 | fn glob_enum() { |
97 | covers!(glob_enum); | 194 | covers!(glob_enum); |
98 | let map = def_map( | 195 | let map = def_map( |
diff --git a/crates/ra_hir_def/src/nameres/tests/incremental.rs b/crates/ra_hir_def/src/nameres/tests/incremental.rs index ef2e9435c..faeb7aa4d 100644 --- a/crates/ra_hir_def/src/nameres/tests/incremental.rs +++ b/crates/ra_hir_def/src/nameres/tests/incremental.rs | |||
@@ -116,7 +116,7 @@ fn typing_inside_a_macro_should_not_invalidate_def_map() { | |||
116 | let events = db.log_executed(|| { | 116 | let events = db.log_executed(|| { |
117 | let crate_def_map = db.crate_def_map(krate); | 117 | let crate_def_map = db.crate_def_map(krate); |
118 | let (_, module_data) = crate_def_map.modules.iter().last().unwrap(); | 118 | let (_, module_data) = crate_def_map.modules.iter().last().unwrap(); |
119 | assert_eq!(module_data.scope.collect_resolutions().len(), 1); | 119 | assert_eq!(module_data.scope.resolutions().collect::<Vec<_>>().len(), 1); |
120 | }); | 120 | }); |
121 | assert!(format!("{:?}", events).contains("crate_def_map"), "{:#?}", events) | 121 | assert!(format!("{:?}", events).contains("crate_def_map"), "{:#?}", events) |
122 | } | 122 | } |
@@ -126,7 +126,7 @@ fn typing_inside_a_macro_should_not_invalidate_def_map() { | |||
126 | let events = db.log_executed(|| { | 126 | let events = db.log_executed(|| { |
127 | let crate_def_map = db.crate_def_map(krate); | 127 | let crate_def_map = db.crate_def_map(krate); |
128 | let (_, module_data) = crate_def_map.modules.iter().last().unwrap(); | 128 | let (_, module_data) = crate_def_map.modules.iter().last().unwrap(); |
129 | assert_eq!(module_data.scope.collect_resolutions().len(), 1); | 129 | assert_eq!(module_data.scope.resolutions().collect::<Vec<_>>().len(), 1); |
130 | }); | 130 | }); |
131 | assert!(!format!("{:?}", events).contains("crate_def_map"), "{:#?}", events) | 131 | assert!(!format!("{:?}", events).contains("crate_def_map"), "{:#?}", events) |
132 | } | 132 | } |
diff --git a/crates/ra_hir_def/src/path.rs b/crates/ra_hir_def/src/path.rs index 8e1294201..107d2d799 100644 --- a/crates/ra_hir_def/src/path.rs +++ b/crates/ra_hir_def/src/path.rs | |||
@@ -130,6 +130,14 @@ impl Path { | |||
130 | Path { type_anchor: None, mod_path: name_ref.as_name().into(), generic_args: vec![None] } | 130 | Path { type_anchor: None, mod_path: name_ref.as_name().into(), generic_args: vec![None] } |
131 | } | 131 | } |
132 | 132 | ||
133 | /// Converts a known |