diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/hir/src/semantics.rs | 6 | ||||
-rw-r--r-- | crates/hir/src/source_analyzer.rs | 30 | ||||
-rw-r--r-- | crates/hir_def/src/item_tree/lower.rs | 6 | ||||
-rw-r--r-- | crates/hir_ty/src/lower.rs | 2 | ||||
-rw-r--r-- | crates/hir_ty/src/tests/macros.rs | 26 | ||||
-rw-r--r-- | crates/ide/src/hover.rs | 63 | ||||
-rw-r--r-- | crates/ide/src/syntax_highlighting.rs | 3 | ||||
-rw-r--r-- | crates/ide/src/typing.rs | 3 | ||||
-rw-r--r-- | crates/ide_assists/src/assist_context.rs | 5 | ||||
-rw-r--r-- | crates/ide_assists/src/handlers/extract_type_alias.rs | 149 | ||||
-rw-r--r-- | crates/ide_assists/src/lib.rs | 2 | ||||
-rw-r--r-- | crates/ide_assists/src/tests/generated.rs | 19 | ||||
-rw-r--r-- | crates/ide_completion/src/completions/qualified_path.rs | 2 | ||||
-rw-r--r-- | crates/rust-analyzer/src/diagnostics/test_data/macro_compiler_error.txt | 138 | ||||
-rw-r--r-- | crates/rust-analyzer/src/diagnostics/to_proto.rs | 101 | ||||
-rw-r--r-- | crates/rust-analyzer/src/handlers.rs | 3 |
16 files changed, 502 insertions, 56 deletions
diff --git a/crates/hir/src/semantics.rs b/crates/hir/src/semantics.rs index d3caeef4e..3bf722d2a 100644 --- a/crates/hir/src/semantics.rs +++ b/crates/hir/src/semantics.rs | |||
@@ -76,9 +76,11 @@ impl PathResolution { | |||
76 | pub fn assoc_type_shorthand_candidates<R>( | 76 | pub fn assoc_type_shorthand_candidates<R>( |
77 | &self, | 77 | &self, |
78 | db: &dyn HirDatabase, | 78 | db: &dyn HirDatabase, |
79 | mut cb: impl FnMut(TypeAlias) -> Option<R>, | 79 | mut cb: impl FnMut(&Name, TypeAlias) -> Option<R>, |
80 | ) -> Option<R> { | 80 | ) -> Option<R> { |
81 | associated_type_shorthand_candidates(db, self.in_type_ns()?, |_, _, id| cb(id.into())) | 81 | associated_type_shorthand_candidates(db, self.in_type_ns()?, |name, _, id| { |
82 | cb(name, id.into()) | ||
83 | }) | ||
82 | } | 84 | } |
83 | } | 85 | } |
84 | 86 | ||
diff --git a/crates/hir/src/source_analyzer.rs b/crates/hir/src/source_analyzer.rs index 37d162b32..8e9ea0a03 100644 --- a/crates/hir/src/source_analyzer.rs +++ b/crates/hir/src/source_analyzer.rs | |||
@@ -20,7 +20,7 @@ use hir_def::{ | |||
20 | use hir_expand::{hygiene::Hygiene, name::AsName, HirFileId, InFile}; | 20 | use hir_expand::{hygiene::Hygiene, name::AsName, HirFileId, InFile}; |
21 | use hir_ty::{ | 21 | use hir_ty::{ |
22 | diagnostics::{record_literal_missing_fields, record_pattern_missing_fields}, | 22 | diagnostics::{record_literal_missing_fields, record_pattern_missing_fields}, |
23 | InferenceResult, Substitution, | 23 | InferenceResult, Substitution, TyLoweringContext, |
24 | }; | 24 | }; |
25 | use syntax::{ | 25 | use syntax::{ |
26 | ast::{self, AstNode}, | 26 | ast::{self, AstNode}, |
@@ -466,7 +466,21 @@ fn resolve_hir_path_( | |||
466 | prefer_value_ns: bool, | 466 | prefer_value_ns: bool, |
467 | ) -> Option<PathResolution> { | 467 | ) -> Option<PathResolution> { |
468 | let types = || { | 468 | let types = || { |
469 | resolver.resolve_path_in_type_ns_fully(db.upcast(), path.mod_path()).map(|ty| match ty { | 469 | let (ty, unresolved) = match path.type_anchor() { |
470 | Some(type_ref) => { | ||
471 | let (_, res) = TyLoweringContext::new(db, resolver).lower_ty_ext(type_ref); | ||
472 | res.map(|ty_ns| (ty_ns, path.segments().first())) | ||
473 | } | ||
474 | None => { | ||
475 | let (ty, remaining) = | ||
476 | resolver.resolve_path_in_type_ns(db.upcast(), path.mod_path())?; | ||
477 | match remaining { | ||
478 | Some(remaining) if remaining > 1 => None, | ||
479 | _ => Some((ty, path.segments().get(1))), | ||
480 | } | ||
481 | } | ||
482 | }?; | ||
483 | let res = match ty { | ||
470 | TypeNs::SelfType(it) => PathResolution::SelfType(it.into()), | 484 | TypeNs::SelfType(it) => PathResolution::SelfType(it.into()), |
471 | TypeNs::GenericParam(id) => PathResolution::TypeParam(TypeParam { id }), | 485 | TypeNs::GenericParam(id) => PathResolution::TypeParam(TypeParam { id }), |
472 | TypeNs::AdtSelfType(it) | TypeNs::AdtId(it) => { | 486 | TypeNs::AdtSelfType(it) | TypeNs::AdtId(it) => { |
@@ -476,7 +490,17 @@ fn resolve_hir_path_( | |||
476 | TypeNs::TypeAliasId(it) => PathResolution::Def(TypeAlias::from(it).into()), | 490 | TypeNs::TypeAliasId(it) => PathResolution::Def(TypeAlias::from(it).into()), |
477 | TypeNs::BuiltinType(it) => PathResolution::Def(BuiltinType::from(it).into()), | 491 | TypeNs::BuiltinType(it) => PathResolution::Def(BuiltinType::from(it).into()), |
478 | TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()), | 492 | TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()), |
479 | }) | 493 | }; |
494 | match unresolved { | ||
495 | Some(unresolved) => res | ||
496 | .assoc_type_shorthand_candidates(db, |name, alias| { | ||
497 | (name == unresolved.name).then(|| alias) | ||
498 | }) | ||
499 | .map(TypeAlias::from) | ||
500 | .map(Into::into) | ||
501 | .map(PathResolution::Def), | ||
502 | None => Some(res), | ||
503 | } | ||
480 | }; | 504 | }; |
481 | 505 | ||
482 | let body_owner = resolver.body_owner(); | 506 | let body_owner = resolver.body_owner(); |
diff --git a/crates/hir_def/src/item_tree/lower.rs b/crates/hir_def/src/item_tree/lower.rs index 8d3862811..124dcc866 100644 --- a/crates/hir_def/src/item_tree/lower.rs +++ b/crates/hir_def/src/item_tree/lower.rs | |||
@@ -174,6 +174,12 @@ impl Ctx { | |||
174 | let forced_vis = self.forced_visibility.take(); | 174 | let forced_vis = self.forced_visibility.take(); |
175 | 175 | ||
176 | let mut block_stack = Vec::new(); | 176 | let mut block_stack = Vec::new(); |
177 | |||
178 | // if container itself is block, add it to the stack | ||
179 | if let Some(block) = ast::BlockExpr::cast(container.clone()) { | ||
180 | block_stack.push(self.source_ast_id_map.ast_id(&block)); | ||
181 | } | ||
182 | |||
177 | for event in container.preorder().skip(1) { | 183 | for event in container.preorder().skip(1) { |
178 | match event { | 184 | match event { |
179 | WalkEvent::Enter(node) => { | 185 | WalkEvent::Enter(node) => { |
diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs index afbfa12d5..14f34d73c 100644 --- a/crates/hir_ty/src/lower.rs +++ b/crates/hir_ty/src/lower.rs | |||
@@ -146,7 +146,7 @@ impl<'a> TyLoweringContext<'a> { | |||
146 | self.lower_ty_ext(type_ref).0 | 146 | self.lower_ty_ext(type_ref).0 |
147 | } | 147 | } |
148 | 148 | ||
149 | fn lower_ty_ext(&self, type_ref: &TypeRef) -> (Ty, Option<TypeNs>) { | 149 | pub fn lower_ty_ext(&self, type_ref: &TypeRef) -> (Ty, Option<TypeNs>) { |
150 | let mut res = None; | 150 | let mut res = None; |
151 | let ty = match type_ref { | 151 | let ty = match type_ref { |
152 | TypeRef::Never => TyKind::Never.intern(&Interner), | 152 | TypeRef::Never => TyKind::Never.intern(&Interner), |
diff --git a/crates/hir_ty/src/tests/macros.rs b/crates/hir_ty/src/tests/macros.rs index 3eb01dbd0..86e3d8b86 100644 --- a/crates/hir_ty/src/tests/macros.rs +++ b/crates/hir_ty/src/tests/macros.rs | |||
@@ -373,6 +373,32 @@ fn recursive_inner_item_macro_rules() { | |||
373 | } | 373 | } |
374 | 374 | ||
375 | #[test] | 375 | #[test] |
376 | fn infer_macro_defining_block_with_items() { | ||
377 | check_infer( | ||
378 | r#" | ||
379 | macro_rules! foo { | ||
380 | () => {{ | ||
381 | fn bar() -> usize { 0 } | ||
382 | bar() | ||
383 | }}; | ||
384 | } | ||
385 | fn main() { | ||
386 | let _a = foo!(); | ||
387 | } | ||
388 | "#, | ||
389 | expect![[r#" | ||
390 | !15..18 '{0}': usize | ||
391 | !16..17 '0': usize | ||
392 | !0..24 '{fnbar...bar()}': usize | ||
393 | !18..21 'bar': fn bar() -> usize | ||
394 | !18..23 'bar()': usize | ||
395 | 98..122 '{ ...!(); }': () | ||
396 | 108..110 '_a': usize | ||
397 | "#]], | ||
398 | ); | ||
399 | } | ||
400 | |||
401 | #[test] | ||
376 | fn infer_type_value_macro_having_same_name() { | 402 | fn infer_type_value_macro_having_same_name() { |
377 | check_infer( | 403 | check_infer( |
378 | r#" | 404 | r#" |
diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index 5f9edb476..28e2e17dc 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs | |||
@@ -3834,4 +3834,67 @@ fn foo() {} | |||
3834 | "#]], | 3834 | "#]], |
3835 | ); | 3835 | ); |
3836 | } | 3836 | } |
3837 | |||
3838 | #[test] | ||
3839 | fn hover_generic_assoc() { | ||
3840 | check( | ||
3841 | r#" | ||
3842 | fn foo<T: A>() where T::Assoc$0: {} | ||
3843 | |||
3844 | trait A { | ||
3845 | type Assoc; | ||
3846 | }"#, | ||
3847 | expect![[r#" | ||
3848 | *Assoc* | ||
3849 | |||
3850 | ```rust | ||
3851 | test | ||
3852 | ``` | ||
3853 | |||
3854 | ```rust | ||
3855 | type Assoc | ||
3856 | ``` | ||
3857 | "#]], | ||
3858 | ); | ||
3859 | check( | ||
3860 | r#" | ||
3861 | fn foo<T: A>() { | ||
3862 | let _: <T>::Assoc$0; | ||
3863 | } | ||
3864 | |||
3865 | trait A { | ||
3866 | type Assoc; | ||
3867 | }"#, | ||
3868 | expect![[r#" | ||
3869 | *Assoc* | ||
3870 | |||
3871 | ```rust | ||
3872 | test | ||
3873 | ``` | ||
3874 | |||
3875 | ```rust | ||
3876 | type Assoc | ||
3877 | ``` | ||
3878 | "#]], | ||
3879 | ); | ||
3880 | check( | ||
3881 | r#" | ||
3882 | trait A where | ||
3883 | Self::Assoc$0: , | ||
3884 | { | ||
3885 | type Assoc; | ||
3886 | }"#, | ||
3887 | expect![[r#" | ||
3888 | *Assoc* | ||
3889 | |||
3890 | ```rust | ||
3891 | test | ||
3892 | ``` | ||
3893 | |||
3894 | ```rust | ||
3895 | type Assoc | ||
3896 | ``` | ||
3897 | "#]], | ||
3898 | ); | ||
3899 | } | ||
3837 | } | 3900 | } |
diff --git a/crates/ide/src/syntax_highlighting.rs b/crates/ide/src/syntax_highlighting.rs index 67a10766b..9df8d21af 100644 --- a/crates/ide/src/syntax_highlighting.rs +++ b/crates/ide/src/syntax_highlighting.rs | |||
@@ -48,6 +48,9 @@ pub struct HlRange { | |||
48 | // | 48 | // |
49 | // The general rule is that a reference to an entity gets colored the same way as the entity itself. | 49 | // The general rule is that a reference to an entity gets colored the same way as the entity itself. |
50 | // We also give special modifier for `mut` and `&mut` local variables. | 50 | // We also give special modifier for `mut` and `&mut` local variables. |
51 | // | ||
52 | // image::https://user-images.githubusercontent.com/48062697/113164457-06cfb980-9239-11eb-819b-0f93e646acf8.png[] | ||
53 | // image::https://user-images.githubusercontent.com/48062697/113187625-f7f50100-9250-11eb-825e-91c58f236071.png[] | ||
51 | pub(crate) fn highlight( | 54 | pub(crate) fn highlight( |
52 | db: &RootDatabase, | 55 | db: &RootDatabase, |
53 | file_id: FileId, | 56 | file_id: FileId, |
diff --git a/crates/ide/src/typing.rs b/crates/ide/src/typing.rs index e10b7d98e..11408d445 100644 --- a/crates/ide/src/typing.rs +++ b/crates/ide/src/typing.rs | |||
@@ -49,6 +49,9 @@ pub(crate) const TRIGGER_CHARS: &str = ".=>"; | |||
49 | // ---- | 49 | // ---- |
50 | // "editor.formatOnType": true, | 50 | // "editor.formatOnType": true, |
51 | // ---- | 51 | // ---- |
52 | // | ||
53 | // image::https://user-images.githubusercontent.com/48062697/113166163-69758500-923a-11eb-81ee-eb33ec380399.gif[] | ||
54 | // image::https://user-images.githubusercontent.com/48062697/113171066-105c2000-923f-11eb-87ab-f4a263346567.gif[] | ||
52 | pub(crate) fn on_char_typed( | 55 | pub(crate) fn on_char_typed( |
53 | db: &RootDatabase, | 56 | db: &RootDatabase, |
54 | position: FilePosition, | 57 | position: FilePosition, |
diff --git a/crates/ide_assists/src/assist_context.rs b/crates/ide_assists/src/assist_context.rs index 1482d37f8..8714e4978 100644 --- a/crates/ide_assists/src/assist_context.rs +++ b/crates/ide_assists/src/assist_context.rs | |||
@@ -13,7 +13,7 @@ use ide_db::{ | |||
13 | RootDatabase, | 13 | RootDatabase, |
14 | }; | 14 | }; |
15 | use syntax::{ | 15 | use syntax::{ |
16 | algo::{self, find_node_at_offset, SyntaxRewriter}, | 16 | algo::{self, find_node_at_offset, find_node_at_range, SyntaxRewriter}, |
17 | AstNode, AstToken, SourceFile, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxNodePtr, | 17 | AstNode, AstToken, SourceFile, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxNodePtr, |
18 | SyntaxToken, TextRange, TextSize, TokenAtOffset, | 18 | SyntaxToken, TextRange, TextSize, TokenAtOffset, |
19 | }; | 19 | }; |
@@ -89,6 +89,9 @@ impl<'a> AssistContext<'a> { | |||
89 | pub(crate) fn find_node_at_offset<N: AstNode>(&self) -> Option<N> { | 89 | pub(crate) fn find_node_at_offset<N: AstNode>(&self) -> Option<N> { |
90 | find_node_at_offset(self.source_file.syntax(), self.offset()) | 90 | find_node_at_offset(self.source_file.syntax(), self.offset()) |
91 | } | 91 | } |
92 | pub(crate) fn find_node_at_range<N: AstNode>(&self) -> Option<N> { | ||
93 | find_node_at_range(self.source_file.syntax(), self.frange.range) | ||
94 | } | ||
92 | pub(crate) fn find_node_at_offset_with_descend<N: AstNode>(&self) -> Option<N> { | 95 | pub(crate) fn find_node_at_offset_with_descend<N: AstNode>(&self) -> Option<N> { |
93 | self.sema.find_node_at_offset_with_descend(self.source_file.syntax(), self.offset()) | 96 | self.sema.find_node_at_offset_with_descend(self.source_file.syntax(), self.offset()) |
94 | } | 97 | } |
diff --git a/crates/ide_assists/src/handlers/extract_type_alias.rs b/crates/ide_assists/src/handlers/extract_type_alias.rs new file mode 100644 index 000000000..442a209b9 --- /dev/null +++ b/crates/ide_assists/src/handlers/extract_type_alias.rs | |||
@@ -0,0 +1,149 @@ | |||
1 | use syntax::ast::{self, AstNode}; | ||
2 | |||
3 | use crate::{AssistContext, AssistId, AssistKind, Assists}; | ||
4 | |||
5 | // Assist: extract_type_alias | ||
6 | // | ||
7 | // Extracts the selected type as a type alias. | ||
8 | // | ||
9 | // ``` | ||
10 | // struct S { | ||
11 | // field: $0(u8, u8, u8)$0, | ||
12 | // } | ||
13 | // ``` | ||
14 | // -> | ||
15 | // ``` | ||
16 | // type $0Type = (u8, u8, u8); | ||
17 | // | ||
18 | // struct S { | ||
19 | // field: Type, | ||
20 | // } | ||
21 | // ``` | ||
22 | pub(crate) fn extract_type_alias(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { | ||
23 | if ctx.frange.range.is_empty() { | ||
24 | return None; | ||
25 | } | ||
26 | |||
27 | let node = ctx.find_node_at_range::<ast::Type>()?; | ||
28 | let insert = ctx.find_node_at_offset::<ast::Item>()?.syntax().text_range().start(); | ||
29 | let target = node.syntax().text_range(); | ||
30 | |||
31 | acc.add( | ||
32 | AssistId("extract_type_alias", AssistKind::RefactorExtract), | ||
33 | "Extract type as type alias", | ||
34 | target, | ||
35 | |builder| { | ||
36 | builder.edit_file(ctx.frange.file_id); | ||
37 | builder.replace(target, "Type"); | ||
38 | match ctx.config.snippet_cap { | ||
39 | Some(cap) => { | ||
40 | builder.insert_snippet(cap, insert, format!("type $0Type = {};\n\n", node)); | ||
41 | } | ||
42 | None => { | ||
43 | builder.insert(insert, format!("type Type = {};\n\n", node)); | ||
44 | } | ||
45 | } | ||
46 | }, | ||
47 | ) | ||
48 | } | ||
49 | |||
50 | #[cfg(test)] | ||
51 | mod tests { | ||
52 | use crate::tests::{check_assist, check_assist_not_applicable}; | ||
53 | |||
54 | use super::*; | ||
55 | |||
56 | #[test] | ||
57 | fn test_not_applicable_without_selection() { | ||
58 | check_assist_not_applicable( | ||
59 | extract_type_alias, | ||
60 | r" | ||
61 | struct S { | ||
62 | field: $0(u8, u8, u8), | ||
63 | } | ||
64 | ", | ||
65 | ); | ||
66 | } | ||
67 | |||
68 | #[test] | ||
69 | fn test_simple_types() { | ||
70 | check_assist( | ||
71 | extract_type_alias, | ||
72 | r" | ||
73 | struct S { | ||
74 | field: $0u8$0, | ||
75 | } | ||
76 | ", | ||
77 | r#" | ||
78 | type $0Type = u8; | ||
79 | |||
80 | struct S { | ||
81 | field: Type, | ||
82 | } | ||
83 | "#, | ||
84 | ); | ||
85 | } | ||
86 | |||
87 | #[test] | ||
88 | fn test_generic_type_arg() { | ||
89 | check_assist( | ||
90 | extract_type_alias, | ||
91 | r" | ||
92 | fn generic<T>() {} | ||
93 | |||
94 | fn f() { | ||
95 | generic::<$0()$0>(); | ||
96 | } | ||
97 | ", | ||
98 | r#" | ||
99 | fn generic<T>() {} | ||
100 | |||
101 | type $0Type = (); | ||
102 | |||
103 | fn f() { | ||
104 | generic::<Type>(); | ||
105 | } | ||
106 | "#, | ||
107 | ); | ||
108 | } | ||
109 | |||
110 | #[test] | ||
111 | fn test_inner_type_arg() { | ||
112 | check_assist( | ||
113 | extract_type_alias, | ||
114 | r" | ||
115 | struct Vec<T> {} | ||
116 | struct S { | ||
117 | v: Vec<Vec<$0Vec<u8>$0>>, | ||
118 | } | ||
119 | ", | ||
120 | r#" | ||
121 | struct Vec<T> {} | ||
122 | type $0Type = Vec<u8>; | ||
123 | |||
124 | struct S { | ||
125 | v: Vec<Vec<Type>>, | ||
126 | } | ||
127 | "#, | ||
128 | ); | ||
129 | } | ||
130 | |||
131 | #[test] | ||
132 | fn test_extract_inner_type() { | ||
133 | check_assist( | ||
134 | extract_type_alias, | ||
135 | r" | ||
136 | struct S { | ||
137 | field: ($0u8$0,), | ||
138 | } | ||
139 | ", | ||
140 | r#" | ||
141 | type $0Type = u8; | ||
142 | |||
143 | struct S { | ||
144 | field: (Type,), | ||
145 | } | ||
146 | "#, | ||
147 | ); | ||
148 | } | ||
149 | } | ||
diff --git a/crates/ide_assists/src/lib.rs b/crates/ide_assists/src/lib.rs index 8c068a6c0..3d1dcef4c 100644 --- a/crates/ide_assists/src/lib.rs +++ b/crates/ide_assists/src/lib.rs | |||
@@ -121,6 +121,7 @@ mod handlers { | |||
121 | mod expand_glob_import; | 121 | mod expand_glob_import; |
122 | mod extract_function; | 122 | mod extract_function; |
123 | mod extract_struct_from_enum_variant; | 123 | mod extract_struct_from_enum_variant; |
124 | mod extract_type_alias; | ||
124 | mod extract_variable; | 125 | mod extract_variable; |
125 | mod fill_match_arms; | 126 | mod fill_match_arms; |
126 | mod fix_visibility; | 127 | mod fix_visibility; |
@@ -187,6 +188,7 @@ mod handlers { | |||
187 | early_return::convert_to_guarded_return, | 188 | early_return::convert_to_guarded_return, |
188 | expand_glob_import::expand_glob_import, | 189 | expand_glob_import::expand_glob_import, |
189 | extract_struct_from_enum_variant::extract_struct_from_enum_variant, | 190 | extract_struct_from_enum_variant::extract_struct_from_enum_variant, |
191 | extract_type_alias::extract_type_alias, | ||
190 | fill_match_arms::fill_match_arms, | 192 | fill_match_arms::fill_match_arms, |
191 | fix_visibility::fix_visibility, | 193 | fix_visibility::fix_visibility, |
192 | flip_binexpr::flip_binexpr, | 194 | flip_binexpr::flip_binexpr, |
diff --git a/crates/ide_assists/src/tests/generated.rs b/crates/ide_assists/src/tests/generated.rs index 736027ff0..03b7fb366 100644 --- a/crates/ide_assists/src/tests/generated.rs +++ b/crates/ide_assists/src/tests/generated.rs | |||
@@ -329,6 +329,25 @@ enum A { One(One) } | |||
329 | } | 329 | } |
330 | 330 | ||
331 | #[test] | 331 | #[test] |
332 | fn doctest_extract_type_alias() { | ||
333 | check_doc_test( | ||
334 | "extract_type_alias", | ||
335 | r#####" | ||
336 | struct S { | ||
337 | field: $0(u8, u8, u8)$0, | ||
338 | } | ||
339 | "#####, | ||
340 | r#####" | ||
341 | type $0Type = (u8, u8, u8); | ||
342 | |||
343 | struct S { | ||
344 | field: Type, | ||
345 | } | ||
346 | "#####, | ||
347 | ) | ||
348 | } | ||
349 | |||
350 | #[test] | ||
332 | fn doctest_extract_variable() { | 351 | fn doctest_extract_variable() { |
333 | check_doc_test( | 352 | check_doc_test( |
334 | "extract_variable", | 353 | "extract_variable", |
diff --git a/crates/ide_completion/src/completions/qualified_path.rs b/crates/ide_completion/src/completions/qualified_path.rs index 1891eb5b3..969249df6 100644 --- a/crates/ide_completion/src/completions/qualified_path.rs +++ b/crates/ide_completion/src/completions/qualified_path.rs | |||
@@ -24,7 +24,7 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon | |||
24 | }; | 24 | }; |
25 | 25 | ||
26 | // Add associated types on type parameters and `Self`. | 26 | // Add associated types on type parameters and `Self`. |
27 | resolution.assoc_type_shorthand_candidates(ctx.db, |alias| { | 27 | resolution.assoc_type_shorthand_candidates(ctx.db, |_, alias| { |
28 | acc.add_type_alias(ctx, alias); | 28 | acc.add_type_alias(ctx, alias); |
29 | None::<()> | 29 | None::<()> |
30 | }); | 30 | }); |
diff --git a/crates/rust-analyzer/src/diagnostics/test_data/macro_compiler_error.txt b/crates/rust-analyzer/src/diagnostics/test_data/macro_compiler_error.txt index f999848a7..c847bbb35 100644 --- a/crates/rust-analyzer/src/diagnostics/test_data/macro_compiler_error.txt +++ b/crates/rust-analyzer/src/diagnostics/test_data/macro_compiler_error.txt | |||
@@ -13,16 +13,16 @@ | |||
13 | diagnostic: Diagnostic { | 13 | diagnostic: Diagnostic { |
14 | range: Range { | 14 | range: Range { |
15 | start: Position { | 15 | start: Position { |
16 | line: 264, | 16 | line: 271, |
17 | character: 8, | 17 | character: 8, |
18 | }, | 18 | }, |
19 | end: Position { | 19 | end: Position { |
20 | line: 264, | 20 | line: 271, |
21 | character: 76, | 21 | character: 50, |
22 | }, | 22 | }, |
23 | }, | 23 | }, |
24 | severity: Some( | 24 | severity: Some( |
25 | Error, | 25 | Hint, |
26 | ), | 26 | ), |
27 | code: None, | 27 | code: None, |
28 | code_description: None, | 28 | code_description: None, |
@@ -40,18 +40,18 @@ | |||
40 | password: None, | 40 | password: None, |
41 | host: None, | 41 | host: None, |
42 | port: None, | 42 | port: None, |
43 | path: "/test/crates/hir_def/src/data.rs", | 43 | path: "/test/crates/hir_def/src/path.rs", |
44 | query: None, | 44 | query: None, |
45 | fragment: None, | 45 | fragment: None, |
46 | }, | 46 | }, |
47 | range: Range { | 47 | range: Range { |
48 | start: Position { | 48 | start: Position { |
49 | line: 79, | 49 | line: 264, |
50 | character: 15, | 50 | character: 8, |
51 | }, | 51 | }, |
52 | end: Position { | 52 | end: Position { |
53 | line: 79, | 53 | line: 264, |
54 | character: 41, | 54 | character: 76, |
55 | }, | 55 | }, |
56 | }, | 56 | }, |
57 | }, | 57 | }, |
@@ -87,6 +87,71 @@ | |||
87 | }, | 87 | }, |
88 | }, | 88 | }, |
89 | severity: Some( | 89 | severity: Some( |
90 | Hint, | ||
91 | ), | ||
92 | code: None, | ||
93 | code_description: None, | ||
94 | source: Some( | ||
95 | "rustc", | ||
96 | ), | ||
97 | message: "Please register your known path in the path module", | ||
98 | related_information: Some( | ||
99 | [ | ||
100 | DiagnosticRelatedInformation { | ||
101 | location: Location { | ||
102 | uri: Url { | ||
103 | scheme: "file", | ||
104 | username: "", | ||
105 | password: None, | ||
106 | host: None, | ||
107 | port: None, | ||
108 | path: "/test/crates/hir_def/src/path.rs", | ||
109 | query: None, | ||
110 | fragment: None, | ||
111 | }, | ||
112 | range: Range { | ||
113 | start: Position { | ||
114 | line: 264, | ||
115 | character: 8, | ||
116 | }, | ||
117 | end: Position { | ||
118 | line: 264, | ||
119 | character: 76, | ||
120 | }, | ||
121 | }, | ||
122 | }, | ||
123 | message: "Exact error occurred here", | ||
124 | }, | ||
125 | ], | ||
126 | ), | ||
127 | tags: None, | ||
128 | data: None, | ||
129 | }, | ||
130 | fixes: [], | ||
131 | }, | ||
132 | MappedRustDiagnostic { | ||
133 | url: Url { | ||
134 | scheme: "file", | ||
135 | username: "", | ||
136 | password: None, | ||
137 | host: None, | ||
138 | port: None, | ||
139 | path: "/test/crates/hir_def/src/path.rs", | ||
140 | query: None, | ||
141 | fragment: None, | ||
142 | }, | ||
143 | diagnostic: Diagnostic { | ||
144 | range: Range { | ||
145 | start: Position { | ||
146 | line: 264, | ||
147 | character: 8, | ||
148 | }, | ||
149 | end: Position { | ||
150 | line: 264, | ||
151 | character: 76, | ||
152 | }, | ||
153 | }, | ||
154 | severity: Some( | ||
90 | Error, | 155 | Error, |
91 | ), | 156 | ), |
92 | code: None, | 157 | code: None, |
@@ -95,7 +160,60 @@ | |||
95 | "rustc", | 160 | "rustc", |
96 | ), | 161 | ), |
97 | message: "Please register your known path in the path module", | 162 | message: "Please register your known path in the path module", |
98 | related_information: None, | 163 | related_information: Some( |
164 | [ | ||
165 | DiagnosticRelatedInformation { | ||
166 | location: Location { | ||
167 | uri: Url { | ||
168 | scheme: "file", | ||
169 | username: "", | ||
170 | password: None, | ||
171 | host: None, | ||
172 | port: None, | ||
173 | path: "/test/crates/hir_def/src/path.rs", | ||
174 | query: None, | ||
175 | fragment: None, | ||
176 | }, | ||
177 | range: Range { | ||
178 | start: Position { | ||
179 | line: 271, | ||
180 | character: 8, | ||
181 | }, | ||
182 | end: Position { | ||
183 | line: 271, | ||
184 | character: 50, | ||
185 | }, | ||
186 | }, | ||
187 | }, | ||
188 | message: "Error originated from macro call here", | ||
189 | }, | ||
190 | DiagnosticRelatedInformation { | ||
191 | location: Location { | ||
192 | uri: Url { | ||
193 | scheme: "file", | ||
194 | username: "", | ||
195 | password: None, | ||
196 | host: None, | ||
197 | port: None, | ||
198 | path: "/test/crates/hir_def/src/data.rs", | ||
199 | query: None, | ||
200 | fragment: None, | ||
201 | }, | ||
202 | range: Range { | ||
203 | start: Position { | ||
204 | line: 79, | ||
205 | character: 15, | ||
206 | }, | ||
207 | end: Position { | ||
208 | line: 79, | ||
209 | character: 41, | ||
210 | }, | ||
211 | }, | ||
212 | }, | ||
213 | message: "Error originated from macro call here", | ||
214 | }, | ||
215 | ], | ||
216 | ), | ||
99 | tags: None, | 217 | tags: None, |
100 | data: None, | 218 | data: None, |
101 | }, | 219 | }, |
diff --git a/crates/rust-analyzer/src/diagnostics/to_proto.rs b/crates/rust-analyzer/src/diagnostics/to_proto.rs index 76994de71..e2f319f6b 100644 --- a/crates/rust-analyzer/src/diagnostics/to_proto.rs +++ b/crates/rust-analyzer/src/diagnostics/to_proto.rs | |||
@@ -34,22 +34,14 @@ fn diagnostic_severity( | |||
34 | Some(res) | 34 | Some(res) |
35 | } | 35 | } |
36 | 36 | ||
37 | /// Check whether a file name is from macro invocation | 37 | /// Checks whether a file name is from macro invocation and does not refer to an actual file. |
38 | fn is_from_macro(file_name: &str) -> bool { | 38 | fn is_dummy_macro_file(file_name: &str) -> bool { |
39 | // FIXME: current rustc does not seem to emit `<macro file>` files anymore? | ||
39 | file_name.starts_with('<') && file_name.ends_with('>') | 40 | file_name.starts_with('<') && file_name.ends_with('>') |
40 | } | 41 | } |
41 | 42 | ||
42 | /// Converts a Rust span to a LSP location, resolving macro expansion site if neccesary | ||
43 | fn location(workspace_root: &Path, span: &DiagnosticSpan) -> lsp_types::Location { | ||
44 | let mut span = span.clone(); | ||
45 | while let Some(expansion) = span.expansion { | ||
46 | span = expansion.span; | ||
47 | } | ||
48 | return location_naive(workspace_root, &span); | ||
49 | } | ||
50 | |||
51 | /// Converts a Rust span to a LSP location | 43 | /// Converts a Rust span to a LSP location |
52 | fn location_naive(workspace_root: &Path, span: &DiagnosticSpan) -> lsp_types::Location { | 44 | fn location(workspace_root: &Path, span: &DiagnosticSpan) -> lsp_types::Location { |
53 | let file_name = workspace_root.join(&span.file_name); | 45 | let file_name = workspace_root.join(&span.file_name); |
54 | let uri = url_from_abs_path(&file_name); | 46 | let uri = url_from_abs_path(&file_name); |
55 | 47 | ||
@@ -62,7 +54,25 @@ fn location_naive(workspace_root: &Path, span: &DiagnosticSpan) -> lsp_types::Lo | |||
62 | lsp_types::Location { uri, range } | 54 | lsp_types::Location { uri, range } |
63 | } | 55 | } |
64 | 56 | ||
65 | /// Converts a secondary Rust span to a LSP related inflocation(ormation | 57 | /// Extracts a suitable "primary" location from a rustc diagnostic. |
58 | /// | ||
59 | /// This takes locations pointing into the standard library, or generally outside the current | ||
60 | /// workspace into account and tries to avoid those, in case macros are involved. | ||
61 | fn primary_location(workspace_root: &Path, span: &DiagnosticSpan) -> lsp_types::Location { | ||
62 | let span_stack = std::iter::successors(Some(span), |span| Some(&span.expansion.as_ref()?.span)); | ||
63 | for span in span_stack.clone() { | ||
64 | let abs_path = workspace_root.join(&span.file_name); | ||
65 | if !is_dummy_macro_file(&span.file_name) && abs_path.starts_with(workspace_root) { | ||
66 | return location(workspace_root, span); | ||
67 | } | ||
68 | } | ||
69 | |||
70 | // Fall back to the outermost macro invocation if no suitable span comes up. | ||
71 | let last_span = span_stack.last().unwrap(); | ||
72 | location(workspace_root, last_span) | ||
73 | } | ||
74 | |||
75 | /// Converts a secondary Rust span to a LSP related information | ||
66 | /// | 76 | /// |
67 | /// If the span is unlabelled this will return `None`. | 77 | /// If the span is unlabelled this will return `None`. |
68 | fn diagnostic_related_information( | 78 | fn diagnostic_related_information( |
@@ -231,7 +241,7 @@ pub(crate) fn map_rust_diagnostic_to_lsp( | |||
231 | primary_spans | 241 | primary_spans |
232 | .iter() | 242 | .iter() |
233 | .flat_map(|primary_span| { | 243 | .flat_map(|primary_span| { |
234 | let location = location(workspace_root, &primary_span); | 244 | let primary_location = primary_location(workspace_root, &primary_span); |
235 | 245 | ||
236 | let mut message = message.clone(); | 246 | let mut message = message.clone(); |
237 | if needs_primary_span_label { | 247 | if needs_primary_span_label { |
@@ -243,31 +253,47 @@ pub(crate) fn map_rust_diagnostic_to_lsp( | |||
243 | // Each primary diagnostic span may result in multiple LSP diagnostics. | 253 | // Each primary diagnostic span may result in multiple LSP diagnostics. |
244 | let mut diagnostics = Vec::new(); | 254 | let mut diagnostics = Vec::new(); |
245 | 255 | ||
246 | let mut related_macro_info = None; | 256 | let mut related_info_macro_calls = vec![]; |
247 | 257 | ||
248 | // If error occurs from macro expansion, add related info pointing to | 258 | // If error occurs from macro expansion, add related info pointing to |
249 | // where the error originated | 259 | // where the error originated |
250 | // Also, we would generate an additional diagnostic, so that exact place of macro | 260 | // Also, we would generate an additional diagnostic, so that exact place of macro |
251 | // will be highlighted in the error origin place. | 261 | // will be highlighted in the error origin place. |
252 | if !is_from_macro(&primary_span.file_name) && primary_span.expansion.is_some() { | 262 | let span_stack = std::iter::successors(Some(*primary_span), |span| { |
253 | let in_macro_location = location_naive(workspace_root, &primary_span); | 263 | Some(&span.expansion.as_ref()?.span) |
264 | }); | ||
265 | for (i, span) in span_stack.enumerate() { | ||
266 | if is_dummy_macro_file(&span.file_name) { | ||
267 | continue; | ||
268 | } | ||
254 | 269 | ||
255 | // Add related information for the main disagnostic. | 270 | // First span is the original diagnostic, others are macro call locations that |
256 | related_macro_info = Some(lsp_types::DiagnosticRelatedInformation { | 271 | // generated that code. |
257 | location: in_macro_location.clone(), | 272 | let is_in_macro_call = i != 0; |
258 | message: "Error originated from macro here".to_string(), | ||
259 | }); | ||
260 | 273 | ||
274 | let secondary_location = location(workspace_root, &span); | ||
275 | if secondary_location == primary_location { | ||
276 | continue; | ||
277 | } | ||
278 | related_info_macro_calls.push(lsp_types::DiagnosticRelatedInformation { | ||
279 | location: secondary_location.clone(), | ||
280 | message: if is_in_macro_call { | ||
281 | "Error originated from macro call here".to_string() | ||
282 | } else { | ||
283 | "Actual error occurred here".to_string() | ||
284 | }, | ||
285 | }); | ||
261 | // For the additional in-macro diagnostic we add the inverse message pointing to the error location in code. | 286 | // For the additional in-macro diagnostic we add the inverse message pointing to the error location in code. |
262 | let information_for_additional_diagnostic = | 287 | let information_for_additional_diagnostic = |
263 | vec![lsp_types::DiagnosticRelatedInformation { | 288 | vec![lsp_types::DiagnosticRelatedInformation { |
264 | location: location.clone(), | 289 | location: primary_location.clone(), |
265 | message: "Exact error occurred here".to_string(), | 290 | message: "Exact error occurred here".to_string(), |
266 | }]; | 291 | }]; |
267 | 292 | ||
268 | let diagnostic = lsp_types::Diagnostic { | 293 | let diagnostic = lsp_types::Diagnostic { |
269 | range: in_macro_location.range, | 294 | range: secondary_location.range, |
270 | severity, | 295 | // downgrade to hint if we're pointing at the macro |
296 | severity: Some(lsp_types::DiagnosticSeverity::Hint), | ||
271 | code: code.clone().map(lsp_types::NumberOrString::String), | 297 | code: code.clone().map(lsp_types::NumberOrString::String), |
272 | code_description: code_description.clone(), | 298 | code_description: code_description.clone(), |
273 | source: Some(source.clone()), | 299 | source: Some(source.clone()), |
@@ -276,9 +302,8 @@ pub(crate) fn map_rust_diagnostic_to_lsp( | |||
276 | tags: if tags.is_empty() { None } else { Some(tags.clone()) }, | 302 | tags: if tags.is_empty() { None } else { Some(tags.clone()) }, |
277 | data: None, | 303 | data: None, |
278 | }; | 304 | }; |
279 | |||
280 | diagnostics.push(MappedRustDiagnostic { | 305 | diagnostics.push(MappedRustDiagnostic { |
281 | url: in_macro_location.uri, | 306 | url: secondary_location.uri, |
282 | diagnostic, | 307 | diagnostic, |
283 | fixes: Vec::new(), | 308 | fixes: Vec::new(), |
284 | }); | 309 | }); |
@@ -286,23 +311,25 @@ pub(crate) fn map_rust_diagnostic_to_lsp( | |||
286 | 311 | ||
287 | // Emit the primary diagnostic. | 312 | // Emit the primary diagnostic. |
288 | diagnostics.push(MappedRustDiagnostic { | 313 | diagnostics.push(MappedRustDiagnostic { |
289 | url: location.uri.clone(), | 314 | url: primary_location.uri.clone(), |
290 | diagnostic: lsp_types::Diagnostic { | 315 | diagnostic: lsp_types::Diagnostic { |
291 | range: location.range, | 316 | range: primary_location.range, |
292 | severity, | 317 | severity, |
293 | code: code.clone().map(lsp_types::NumberOrString::String), | 318 | code: code.clone().map(lsp_types::NumberOrString::String), |
294 | code_description: code_description.clone(), | 319 | code_description: code_description.clone(), |
295 | source: Some(source.clone()), | 320 | source: Some(source.clone()), |
296 | message, | 321 | message, |
297 | related_information: if subdiagnostics.is_empty() { | 322 | related_information: { |
298 | None | 323 | let info = related_info_macro_calls |
299 | } else { | ||
300 | let mut related = subdiagnostics | ||
301 | .iter() | 324 | .iter() |
302 | .map(|sub| sub.related.clone()) | 325 | .cloned() |
326 | .chain(subdiagnostics.iter().map(|sub| sub.related.clone())) | ||
303 | .collect::<Vec<_>>(); | 327 | .collect::<Vec<_>>(); |
304 | related.extend(related_macro_info); | 328 | if info.is_empty() { |
305 | Some(related) | 329 | None |
330 | } else { | ||
331 | Some(info) | ||
332 | } | ||
306 | }, | 333 | }, |
307 | tags: if tags.is_empty() { None } else { Some(tags.clone()) }, | 334 | tags: if tags.is_empty() { None } else { Some(tags.clone()) }, |
308 | data: None, | 335 | data: None, |
@@ -314,7 +341,7 @@ pub(crate) fn map_rust_diagnostic_to_lsp( | |||
314 | // This is useful because they will show up in the user's editor, unlike | 341 | // This is useful because they will show up in the user's editor, unlike |
315 | // `related_information`, which just produces hard-to-read links, at least in VS Code. | 342 | // `related_information`, which just produces hard-to-read links, at least in VS Code. |
316 | let back_ref = lsp_types::DiagnosticRelatedInformation { | 343 | let back_ref = lsp_types::DiagnosticRelatedInformation { |
317 | location, | 344 | location: primary_location, |
318 | message: "original diagnostic".to_string(), | 345 | message: "original diagnostic".to_string(), |
319 | }; | 346 | }; |
320 | for sub in &subdiagnostics { | 347 | for sub in &subdiagnostics { |
diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index 85e67554c..53d29ddfc 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs | |||
@@ -84,7 +84,8 @@ pub(crate) fn handle_analyzer_status( | |||
84 | 84 | ||
85 | pub(crate) fn handle_memory_usage(state: &mut GlobalState, _: ()) -> Result<String> { | 85 | pub(crate) fn handle_memory_usage(state: &mut GlobalState, _: ()) -> Result<String> { |
86 | let _p = profile::span("handle_memory_usage"); | 86 | let _p = profile::span("handle_memory_usage"); |
87 | let mem = state.analysis_host.per_query_memory_usage(); | 87 | let mut mem = state.analysis_host.per_query_memory_usage(); |
88 | mem.push(("Remaining".into(), profile::memory_usage().allocated)); | ||
88 | 89 | ||
89 | let mut out = String::new(); | 90 | let mut out = String::new(); |
90 | for (name, bytes) in mem { | 91 | for (name, bytes) in mem { |