aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_assists/Cargo.toml4
-rw-r--r--crates/ra_assists/src/assist_ctx.rs1
-rw-r--r--crates/ra_assists/src/assists/early_return.rs276
-rw-r--r--crates/ra_assists/src/assists/fill_match_arms.rs18
-rw-r--r--crates/ra_assists/src/assists/move_bounds.rs2
-rw-r--r--crates/ra_assists/src/assists/raw_string.rs110
-rw-r--r--crates/ra_assists/src/lib.rs2
-rw-r--r--crates/ra_batch/Cargo.toml2
-rw-r--r--crates/ra_batch/src/lib.rs18
-rw-r--r--crates/ra_cfg/src/lib.rs24
-rw-r--r--crates/ra_cli/Cargo.toml2
-rw-r--r--crates/ra_cli/src/analysis_bench.rs2
-rw-r--r--crates/ra_cli/src/analysis_stats.rs33
-rw-r--r--crates/ra_db/Cargo.toml2
-rw-r--r--crates/ra_db/src/input.rs6
-rw-r--r--crates/ra_db/src/lib.rs76
-rw-r--r--crates/ra_hir/Cargo.toml4
-rw-r--r--crates/ra_hir/src/adt.rs12
-rw-r--r--crates/ra_hir/src/attr.rs24
-rw-r--r--crates/ra_hir/src/code_model.rs44
-rw-r--r--crates/ra_hir/src/code_model/docs.rs20
-rw-r--r--crates/ra_hir/src/db.rs18
-rw-r--r--crates/ra_hir/src/debug.rs20
-rw-r--r--crates/ra_hir/src/from_source.rs10
-rw-r--r--crates/ra_hir/src/ids.rs45
-rw-r--r--crates/ra_hir/src/impl_block.rs35
-rw-r--r--crates/ra_hir/src/lib.rs3
-rw-r--r--crates/ra_hir/src/mock.rs42
-rw-r--r--crates/ra_hir/src/nameres/collector.rs77
-rw-r--r--crates/ra_hir/src/nameres/mod_resolution.rs228
-rw-r--r--crates/ra_hir/src/nameres/raw.rs53
-rw-r--r--crates/ra_hir/src/nameres/tests.rs13
-rw-r--r--crates/ra_hir/src/nameres/tests/incremental.rs2
-rw-r--r--crates/ra_hir/src/nameres/tests/macros.rs29
-rw-r--r--crates/ra_hir/src/nameres/tests/mod_resolution.rs214
-rw-r--r--crates/ra_hir/src/resolve.rs32
-rw-r--r--crates/ra_hir/src/source_binder.rs2
-rw-r--r--crates/ra_hir/src/ty.rs24
-rw-r--r--crates/ra_hir/src/ty/infer.rs1164
-rw-r--r--crates/ra_hir/src/ty/infer/coerce.rs336
-rw-r--r--crates/ra_hir/src/ty/infer/expr.rs658
-rw-r--r--crates/ra_hir/src/ty/infer/pat.rs180
-rw-r--r--crates/ra_hir/src/ty/infer/unify.rs23
-rw-r--r--crates/ra_hir/src/ty/lower.rs39
-rw-r--r--crates/ra_hir/src/ty/tests.rs22
-rw-r--r--crates/ra_hir/src/ty/traits.rs2
-rw-r--r--crates/ra_hir/src/ty/traits/chalk.rs61
-rw-r--r--crates/ra_hir/src/type_alias.rs18
-rw-r--r--crates/ra_hir/src/util.rs12
-rw-r--r--crates/ra_ide_api/Cargo.toml3
-rw-r--r--crates/ra_ide_api/src/change.rs2
-rw-r--r--crates/ra_ide_api/src/completion/complete_dot.rs185
-rw-r--r--crates/ra_ide_api/src/completion/complete_macro_in_item_position.rs62
-rw-r--r--crates/ra_ide_api/src/completion/complete_path.rs166
-rw-r--r--crates/ra_ide_api/src/completion/complete_postfix.rs67
-rw-r--r--crates/ra_ide_api/src/completion/complete_scope.rs679
-rw-r--r--crates/ra_ide_api/src/completion/completion_context.rs19
-rw-r--r--crates/ra_ide_api/src/completion/completion_item.rs4
-rw-r--r--crates/ra_ide_api/src/completion/presentation.rs389
-rw-r--r--crates/ra_ide_api/src/db.rs25
-rw-r--r--crates/ra_ide_api/src/diagnostics.rs13
-rw-r--r--crates/ra_ide_api/src/display/navigation_target.rs6
-rw-r--r--crates/ra_ide_api/src/extend_selection.rs3
-rw-r--r--crates/ra_ide_api/src/goto_definition.rs12
-rw-r--r--crates/ra_ide_api/src/hover.rs11
-rw-r--r--crates/ra_ide_api/src/lib.rs14
-rw-r--r--crates/ra_ide_api/src/marks.rs1
-rw-r--r--crates/ra_ide_api/src/name_ref_kind.rs98
-rw-r--r--crates/ra_ide_api/src/references.rs524
-rw-r--r--crates/ra_ide_api/src/references/classify.rs179
-rw-r--r--crates/ra_ide_api/src/references/name_definition.rs113
-rw-r--r--crates/ra_ide_api/src/references/rename.rs469
-rw-r--r--crates/ra_ide_api/src/references/search_scope.rs92
-rw-r--r--crates/ra_ide_api/src/snapshots/highlighting.html2
-rw-r--r--crates/ra_ide_api/src/symbol_index.rs2
-rw-r--r--crates/ra_ide_api/src/syntax_highlighting.rs17
-rw-r--r--crates/ra_lsp_server/Cargo.toml6
-rw-r--r--crates/ra_lsp_server/src/conv.rs106
-rw-r--r--crates/ra_lsp_server/src/lib.rs2
-rw-r--r--crates/ra_lsp_server/src/main_loop.rs24
-rw-r--r--crates/ra_lsp_server/src/main_loop/handlers.rs22
-rw-r--r--crates/ra_lsp_server/src/world.rs9
-rw-r--r--crates/ra_mbe/Cargo.toml1
-rw-r--r--crates/ra_parser/src/grammar/expressions.rs11
-rw-r--r--crates/ra_project_model/Cargo.toml2
-rw-r--r--crates/ra_project_model/src/lib.rs44
-rw-r--r--crates/ra_syntax/Cargo.toml3
-rw-r--r--crates/ra_syntax/src/ast/edit.rs119
-rw-r--r--crates/ra_syntax/src/ast/make.rs14
-rw-r--r--crates/ra_syntax/src/grammar.ron2
-rw-r--r--crates/ra_syntax/test_data/parser/inline/ok/0146_as_precedence.rs3
-rw-r--r--crates/ra_syntax/test_data/parser/inline/ok/0146_as_precedence.txt43
-rw-r--r--crates/ra_syntax/test_data/parser/ok/0058_unary_expr_precedence.rs7
-rw-r--r--crates/ra_syntax/test_data/parser/ok/0058_unary_expr_precedence.txt97
-rw-r--r--crates/ra_tools/Cargo.toml15
-rw-r--r--crates/ra_tools/src/bin/pre-commit.rs31
-rw-r--r--crates/ra_tools/src/boilerplate_gen.rs348
-rw-r--r--crates/ra_tools/src/help.rs47
-rw-r--r--crates/ra_tools/src/lib.rs332
-rw-r--r--crates/ra_tools/src/main.rs218
-rw-r--r--crates/ra_tools/tests/cli.rs45
-rw-r--r--crates/ra_tools/tests/docs.rs63
-rw-r--r--crates/ra_tools/tests/main.rs2
-rw-r--r--crates/ra_tt/Cargo.toml4
-rw-r--r--crates/ra_vfs_glob/Cargo.toml2
105 files changed, 4847 insertions, 3976 deletions
diff --git a/crates/ra_assists/Cargo.toml b/crates/ra_assists/Cargo.toml
index 02966bbda..beebccbd9 100644
--- a/crates/ra_assists/Cargo.toml
+++ b/crates/ra_assists/Cargo.toml
@@ -6,11 +6,9 @@ authors = ["rust-analyzer developers"]
6 6
7[dependencies] 7[dependencies]
8format-buf = "1.0.0" 8format-buf = "1.0.0"
9once_cell = "1.0.1"
10join_to_string = "0.1.3" 9join_to_string = "0.1.3"
11itertools = "0.8.0" 10itertools = "0.8.0"
12arrayvec = "0.4.10" 11rustc_lexer = "0.1.0"
13rustc-hash = "1.0.1"
14 12
15ra_syntax = { path = "../ra_syntax" } 13ra_syntax = { path = "../ra_syntax" }
16ra_text_edit = { path = "../ra_text_edit" } 14ra_text_edit = { path = "../ra_text_edit" }
diff --git a/crates/ra_assists/src/assist_ctx.rs b/crates/ra_assists/src/assist_ctx.rs
index 189cad7d0..e270c5d60 100644
--- a/crates/ra_assists/src/assist_ctx.rs
+++ b/crates/ra_assists/src/assist_ctx.rs
@@ -138,6 +138,7 @@ impl AssistBuilder {
138 138
139 /// Replaces specified `node` of text with a given string, reindenting the 139 /// Replaces specified `node` of text with a given string, reindenting the
140 /// string to maintain `node`'s existing indent. 140 /// string to maintain `node`'s existing indent.
141 // FIXME: remove in favor of ra_syntax::edit::IndentLevel::increase_indent
141 pub(crate) fn replace_node_and_indent( 142 pub(crate) fn replace_node_and_indent(
142 &mut self, 143 &mut self,
143 node: &SyntaxNode, 144 node: &SyntaxNode,
diff --git a/crates/ra_assists/src/assists/early_return.rs b/crates/ra_assists/src/assists/early_return.rs
new file mode 100644
index 000000000..8c975714c
--- /dev/null
+++ b/crates/ra_assists/src/assists/early_return.rs
@@ -0,0 +1,276 @@
1//! FIXME: write short doc here
2
3use crate::{
4 assist_ctx::{Assist, AssistCtx},
5 AssistId,
6};
7use hir::db::HirDatabase;
8use ra_syntax::{
9 algo::replace_children,
10 ast::edit::IndentLevel,
11 ast::make,
12 ast::Block,
13 ast::ContinueExpr,
14 ast::IfExpr,
15 ast::ReturnExpr,
16 AstNode,
17 SyntaxKind::{FN_DEF, LOOP_EXPR, L_CURLY, R_CURLY, WHILE_EXPR, WHITESPACE},
18};
19use std::ops::RangeInclusive;
20
21pub(crate) fn convert_to_guarded_return(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
22 let if_expr: IfExpr = ctx.node_at_offset()?;
23 let expr = if_expr.condition()?.expr()?;
24 let then_block = if_expr.then_branch()?.block()?;
25 if if_expr.else_branch().is_some() {
26 return None;
27 }
28
29 let parent_block = if_expr.syntax().parent()?.ancestors().find_map(Block::cast)?;
30
31 if parent_block.expr()? != if_expr.clone().into() {
32 return None;
33 }
34
35 // check for early return and continue
36 let first_in_then_block = then_block.syntax().first_child()?.clone();
37 if ReturnExpr::can_cast(first_in_then_block.kind())
38 || ContinueExpr::can_cast(first_in_then_block.kind())
39 || first_in_then_block
40 .children()
41 .any(|x| ReturnExpr::can_cast(x.kind()) || ContinueExpr::can_cast(x.kind()))
42 {
43 return None;
44 }
45
46 let parent_container = parent_block.syntax().parent()?.parent()?;
47
48 let early_expression = match parent_container.kind() {
49 WHILE_EXPR | LOOP_EXPR => Some("continue;"),
50 FN_DEF => Some("return;"),
51 _ => None,
52 }?;
53
54 if then_block.syntax().first_child_or_token().map(|t| t.kind() == L_CURLY).is_none() {
55 return None;
56 }
57
58 then_block.syntax().last_child_or_token().filter(|t| t.kind() == R_CURLY)?;
59 let cursor_position = ctx.frange.range.start();
60
61 ctx.add_action(AssistId("convert_to_guarded_return"), "convert to guarded return", |edit| {
62 let if_indent_level = IndentLevel::from_node(&if_expr.syntax());
63 let new_if_expr =
64 if_indent_level.increase_indent(make::if_expression(&expr, early_expression));
65 let then_block_items = IndentLevel::from(1).decrease_indent(then_block.clone());
66 let end_of_then = then_block_items.syntax().last_child_or_token().unwrap();
67 let end_of_then =
68 if end_of_then.prev_sibling_or_token().map(|n| n.kind()) == Some(WHITESPACE) {
69 end_of_then.prev_sibling_or_token().unwrap()
70 } else {
71 end_of_then
72 };
73 let mut new_if_and_then_statements = new_if_expr.syntax().children_with_tokens().chain(
74 then_block_items
75 .syntax()
76 .children_with_tokens()
77 .skip(1)
78 .take_while(|i| *i != end_of_then),
79 );
80 let new_block = replace_children(
81 &parent_block.syntax(),
82 RangeInclusive::new(
83 if_expr.clone().syntax().clone().into(),
84 if_expr.syntax().clone().into(),
85 ),
86 &mut new_if_and_then_statements,
87 );
88 edit.target(if_expr.syntax().text_range());
89 edit.replace_ast(parent_block, Block::cast(new_block).unwrap());
90 edit.set_cursor(cursor_position);
91 });
92 ctx.build()
93}
94
95#[cfg(test)]
96mod tests {
97 use super::*;
98 use crate::helpers::{check_assist, check_assist_not_applicable};
99
100 #[test]
101 fn convert_inside_fn() {
102 check_assist(
103 convert_to_guarded_return,
104 r#"
105 fn main() {
106 bar();
107 if<|> true {
108 foo();
109
110 //comment
111 bar();
112 }
113 }
114 "#,
115 r#"
116 fn main() {
117 bar();
118 if<|> !true {
119 return;
120 }
121 foo();
122
123 //comment
124 bar();
125 }
126 "#,
127 );
128 }
129
130 #[test]
131 fn convert_inside_while() {
132 check_assist(
133 convert_to_guarded_return,
134 r#"
135 fn main() {
136 while true {
137 if<|> true {
138 foo();
139 bar();
140 }
141 }
142 }
143 "#,
144 r#"
145 fn main() {
146 while true {
147 if<|> !true {
148 continue;
149 }
150 foo();
151 bar();
152 }
153 }
154 "#,
155 );
156 }
157
158 #[test]
159 fn convert_inside_loop() {
160 check_assist(
161 convert_to_guarded_return,
162 r#"
163 fn main() {
164 loop {
165 if<|> true {
166 foo();
167 bar();
168 }
169 }
170 }
171 "#,
172 r#"
173 fn main() {
174 loop {
175 if<|> !true {
176 continue;
177 }
178 foo();
179 bar();
180 }
181 }
182 "#,
183 );
184 }
185
186 #[test]
187 fn ignore_already_converted_if() {
188 check_assist_not_applicable(
189 convert_to_guarded_return,
190 r#"
191 fn main() {
192 if<|> true {
193 return;
194 }
195 }
196 "#,
197 );
198 }
199
200 #[test]
201 fn ignore_already_converted_loop() {
202 check_assist_not_applicable(
203 convert_to_guarded_return,
204 r#"
205 fn main() {
206 loop {
207 if<|> true {
208 continue;
209 }
210 }
211 }
212 "#,
213 );
214 }
215
216 #[test]
217 fn ignore_return() {
218 check_assist_not_applicable(
219 convert_to_guarded_return,
220 r#"
221 fn main() {
222 if<|> true {
223 return
224 }
225 }
226 "#,
227 );
228 }
229
230 #[test]
231 fn ignore_else_branch() {
232 check_assist_not_applicable(
233 convert_to_guarded_return,
234 r#"
235 fn main() {
236 if<|> true {
237 foo();
238 } else {
239 bar()
240 }
241 }
242 "#,
243 );
244 }
245
246 #[test]
247 fn ignore_statements_aftert_if() {
248 check_assist_not_applicable(
249 convert_to_guarded_return,
250 r#"
251 fn main() {
252 if<|> true {
253 foo();
254 }
255 bar();
256 }
257 "#,
258 );
259 }
260
261 #[test]
262 fn ignore_statements_inside_if() {
263 check_assist_not_applicable(
264 convert_to_guarded_return,
265 r#"
266 fn main() {
267 if false {
268 if<|> true {
269 foo();
270 }
271 }
272 }
273 "#,
274 );
275 }
276}
diff --git a/crates/ra_assists/src/assists/fill_match_arms.rs b/crates/ra_assists/src/assists/fill_match_arms.rs
index 7335cce09..e3f30b5de 100644
--- a/crates/ra_assists/src/assists/fill_match_arms.rs
+++ b/crates/ra_assists/src/assists/fill_match_arms.rs
@@ -3,7 +3,7 @@
3use std::iter; 3use std::iter;
4 4
5use hir::{db::HirDatabase, Adt, HasSource}; 5use hir::{db::HirDatabase, Adt, HasSource};
6use ra_syntax::ast::{self, make, AstNode, NameOwner}; 6use ra_syntax::ast::{self, edit::IndentLevel, make, AstNode, NameOwner};
7 7
8use crate::{Assist, AssistCtx, AssistId}; 8use crate::{Assist, AssistCtx, AssistId};
9 9
@@ -30,15 +30,19 @@ pub(crate) fn fill_match_arms(mut ctx: AssistCtx<impl HirDatabase>) -> Option<As
30 let variant_list = enum_def.variant_list()?; 30 let variant_list = enum_def.variant_list()?;
31 31
32 ctx.add_action(AssistId("fill_match_arms"), "fill match arms", |edit| { 32 ctx.add_action(AssistId("fill_match_arms"), "fill match arms", |edit| {
33 let variants = variant_list.variants(); 33 let indent_level = IndentLevel::from_node(match_arm_list.syntax());
34 let arms = variants 34
35 .filter_map(build_pat) 35 let new_arm_list = {
36 .map(|pat| make::match_arm(iter::once(pat), make::expr_unit())); 36 let variants = variant_list.variants();
37 let new_arm_list = make::match_arm_list(arms); 37 let arms = variants
38 .filter_map(build_pat)
39 .map(|pat| make::match_arm(iter::once(pat), make::expr_unit()));
40 indent_level.increase_indent(make::match_arm_list(arms))
41 };
38 42
39 edit.target(match_expr.syntax().text_range()); 43 edit.target(match_expr.syntax().text_range());
40 edit.set_cursor(expr.syntax().text_range().start()); 44 edit.set_cursor(expr.syntax().text_range().start());
41 edit.replace_node_and_indent(match_arm_list.syntax(), new_arm_list.syntax().text()); 45 edit.replace_ast(match_arm_list, new_arm_list);
42 }); 46 });
43 47
44 ctx.build() 48 ctx.build()
diff --git a/crates/ra_assists/src/assists/move_bounds.rs b/crates/ra_assists/src/assists/move_bounds.rs
index f791d22b0..d2444b6b9 100644
--- a/crates/ra_assists/src/assists/move_bounds.rs
+++ b/crates/ra_assists/src/assists/move_bounds.rs
@@ -18,7 +18,7 @@ pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx<impl HirDatabase>)
18 } 18 }
19 19
20 let parent = type_param_list.syntax().parent()?; 20 let parent = type_param_list.syntax().parent()?;
21 if parent.children_with_tokens().find(|it| it.kind() == WHERE_CLAUSE).is_some() { 21 if parent.children_with_tokens().any(|it| it.kind() == WHERE_CLAUSE) {
22 return None; 22 return None;
23 } 23 }
24 24
diff --git a/crates/ra_assists/src/assists/raw_string.rs b/crates/ra_assists/src/assists/raw_string.rs
index 388ee7e97..2d2e31e51 100644
--- a/crates/ra_assists/src/assists/raw_string.rs
+++ b/crates/ra_assists/src/assists/raw_string.rs
@@ -2,6 +2,7 @@
2 2
3use hir::db::HirDatabase; 3use hir::db::HirDatabase;
4use ra_syntax::{ast::AstNode, ast::Literal, TextRange, TextUnit}; 4use ra_syntax::{ast::AstNode, ast::Literal, TextRange, TextUnit};
5use rustc_lexer;
5 6
6use crate::{Assist, AssistCtx, AssistId}; 7use crate::{Assist, AssistCtx, AssistId};
7 8
@@ -10,13 +11,51 @@ pub(crate) fn make_raw_string(mut ctx: AssistCtx<impl HirDatabase>) -> Option<As
10 if literal.token().kind() != ra_syntax::SyntaxKind::STRING { 11 if literal.token().kind() != ra_syntax::SyntaxKind::STRING {
11 return None; 12 return None;
12 } 13 }
14 let token = literal.token();
15 let text = token.text().as_str();
16 let usual_string_range = find_usual_string_range(text)?;
17 let start_of_inside = usual_string_range.start().to_usize() + 1;
18 let end_of_inside = usual_string_range.end().to_usize();
19 let inside_str = &text[start_of_inside..end_of_inside];
20 let mut unescaped = String::with_capacity(inside_str.len());
21 let mut error = Ok(());
22 rustc_lexer::unescape::unescape_str(
23 inside_str,
24 &mut |_, unescaped_char| match unescaped_char {
25 Ok(c) => unescaped.push(c),
26 Err(_) => error = Err(()),
27 },
28 );
29 if error.is_err() {
30 return None;
31 }
13 ctx.add_action(AssistId("make_raw_string"), "make raw string", |edit| { 32 ctx.add_action(AssistId("make_raw_string"), "make raw string", |edit| {
14 edit.target(literal.syntax().text_range()); 33 edit.target(literal.syntax().text_range());
15 edit.insert(literal.syntax().text_range().start(), "r"); 34 let max_hash_streak = count_hashes(&unescaped);
35 let mut hashes = String::with_capacity(max_hash_streak + 1);
36 for _ in 0..hashes.capacity() {
37 hashes.push('#');
38 }
39 edit.replace(
40 literal.syntax().text_range(),
41 format!("r{}\"{}\"{}", hashes, unescaped, hashes),
42 );
16 }); 43 });
17 ctx.build() 44 ctx.build()
18} 45}
19 46
47fn count_hashes(s: &str) -> usize {
48 let mut max_hash_streak = 0usize;
49 for idx in s.match_indices("\"#").map(|(i, _)| i) {
50 let (_, sub) = s.split_at(idx + 1);
51 let nb_hash = sub.chars().take_while(|c| *c == '#').count();
52 if nb_hash > max_hash_streak {
53 max_hash_streak = nb_hash;
54 }
55 }
56 max_hash_streak
57}
58
20fn find_usual_string_range(s: &str) -> Option<TextRange> { 59fn find_usual_string_range(s: &str) -> Option<TextRange> {
21 Some(TextRange::from_to( 60 Some(TextRange::from_to(
22 TextUnit::from(s.find('"')? as u32), 61 TextUnit::from(s.find('"')? as u32),
@@ -94,10 +133,10 @@ mod test {
94 make_raw_string, 133 make_raw_string,
95 r#" 134 r#"
96 fn f() { 135 fn f() {
97 let s = <|>"random string"; 136 let s = <|>"random\nstring";
98 } 137 }
99 "#, 138 "#,
100 r#""random string""#, 139 r#""random\nstring""#,
101 ); 140 );
102 } 141 }
103 142
@@ -107,44 +146,69 @@ mod test {
107 make_raw_string, 146 make_raw_string,
108 r#" 147 r#"
109 fn f() { 148 fn f() {
110 let s = <|>"random string"; 149 let s = <|>"random\nstring";
111 } 150 }
112 "#, 151 "#,
113 r#" 152 r##"
114 fn f() { 153 fn f() {
115 let s = <|>r"random string"; 154 let s = <|>r#"random
155string"#;
116 } 156 }
117 "#, 157 "##,
118 ) 158 )
119 } 159 }
120 160
121 #[test] 161 #[test]
122 fn make_raw_string_with_escaped_works() { 162 fn make_raw_string_hashes_inside_works() {
123 check_assist( 163 check_assist(
124 make_raw_string, 164 make_raw_string,
125 r#" 165 r###"
126 fn f() { 166 fn f() {
127 let s = <|>"random\nstring"; 167 let s = <|>"#random##\nstring";
128 } 168 }
129 "#, 169 "###,
130 r#" 170 r####"
131 fn f() { 171 fn f() {
132 let s = <|>r"random\nstring"; 172 let s = <|>r#"#random##
173string"#;
133 } 174 }
134 "#, 175 "####,
135 ) 176 )
136 } 177 }
137 178
138 #[test] 179 #[test]
139 fn make_raw_string_not_works() { 180 fn make_raw_string_closing_hashes_inside_works() {
140 check_assist_not_applicable( 181 check_assist(
182 make_raw_string,
183 r###"
184 fn f() {
185 let s = <|>"#random\"##\nstring";
186 }
187 "###,
188 r####"
189 fn f() {
190 let s = <|>r###"#random"##
191string"###;
192 }
193 "####,
194 )
195 }
196
197 #[test]
198 fn make_raw_string_nothing_to_unescape_works() {
199 check_assist(
141 make_raw_string, 200 make_raw_string,
142 r#" 201 r#"
143 fn f() { 202 fn f() {
144 let s = <|>r"random string"; 203 let s = <|>"random string";
145 } 204 }
146 "#, 205 "#,
147 ); 206 r##"
207 fn f() {
208 let s = <|>r#"random string"#;
209 }
210 "##,
211 )
148 } 212 }
149 213
150 #[test] 214 #[test]
@@ -369,4 +433,14 @@ mod test {
369 "#, 433 "#,
370 ); 434 );
371 } 435 }
436
437 #[test]
438 fn count_hashes_test() {
439 assert_eq!(0, count_hashes("abc"));
440 assert_eq!(0, count_hashes("###"));
441 assert_eq!(1, count_hashes("\"#abc"));
442 assert_eq!(0, count_hashes("#abc"));
443 assert_eq!(2, count_hashes("#ab\"##c"));
444 assert_eq!(4, count_hashes("#ab\"##\"####c"));
445 }
372} 446}
diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs
index d2376c475..ab77b46a9 100644
--- a/crates/ra_assists/src/lib.rs
+++ b/crates/ra_assists/src/lib.rs
@@ -108,6 +108,7 @@ mod assists {
108 mod add_missing_impl_members; 108 mod add_missing_impl_members;
109 mod move_guard; 109 mod move_guard;
110 mod move_bounds; 110 mod move_bounds;
111 mod early_return;
111 112
112 pub(crate) fn all<DB: HirDatabase>() -> &'static [fn(AssistCtx<DB>) -> Option<Assist>] { 113 pub(crate) fn all<DB: HirDatabase>() -> &'static [fn(AssistCtx<DB>) -> Option<Assist>] {
113 &[ 114 &[
@@ -135,6 +136,7 @@ mod assists {
135 raw_string::make_raw_string, 136 raw_string::make_raw_string,
136 raw_string::make_usual_string, 137 raw_string::make_usual_string,
137 raw_string::remove_hash, 138 raw_string::remove_hash,
139 early_return::convert_to_guarded_return,
138 ] 140 ]
139 } 141 }
140} 142}
diff --git a/crates/ra_batch/Cargo.toml b/crates/ra_batch/Cargo.toml
index 62850746f..c85da7a2c 100644
--- a/crates/ra_batch/Cargo.toml
+++ b/crates/ra_batch/Cargo.toml
@@ -9,7 +9,7 @@ log = "0.4.5"
9rustc-hash = "1.0" 9rustc-hash = "1.0"
10crossbeam-channel = "0.3.5" 10crossbeam-channel = "0.3.5"
11 11
12ra_vfs = "0.4.0" 12ra_vfs = "0.5.0"
13ra_vfs_glob = { path = "../ra_vfs_glob" } 13ra_vfs_glob = { path = "../ra_vfs_glob" }
14ra_db = { path = "../ra_db" } 14ra_db = { path = "../ra_db" }
15ra_ide_api = { path = "../ra_ide_api" } 15ra_ide_api = { path = "../ra_ide_api" }
diff --git a/crates/ra_batch/src/lib.rs b/crates/ra_batch/src/lib.rs
index a5fc2a23e..df49eb13d 100644
--- a/crates/ra_batch/src/lib.rs
+++ b/crates/ra_batch/src/lib.rs
@@ -43,8 +43,12 @@ pub fn load_cargo(root: &Path) -> Result<(AnalysisHost, FxHashMap<SourceRootId,
43 ); 43 );
44 44
45 // FIXME: cfg options? 45 // FIXME: cfg options?
46 let default_cfg_options = 46 let default_cfg_options = {
47 get_rustc_cfg_options().atom("test".into()).atom("debug_assertion".into()); 47 let mut opts = get_rustc_cfg_options();
48 opts.insert_atom("test".into());
49 opts.insert_atom("debug_assertion".into());
50 opts
51 };
48 52
49 let (crate_graph, _crate_names) = 53 let (crate_graph, _crate_names) =
50 ws.to_crate_graph(&default_cfg_options, &mut |path: &Path| { 54 ws.to_crate_graph(&default_cfg_options, &mut |path: &Path| {
@@ -137,14 +141,8 @@ mod tests {
137 #[test] 141 #[test]
138 fn test_loading_rust_analyzer() { 142 fn test_loading_rust_analyzer() {
139 let path = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap().parent().unwrap(); 143 let path = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap().parent().unwrap();
140 let (host, roots) = load_cargo(path).unwrap(); 144 let (host, _roots) = load_cargo(path).unwrap();
141 let mut n_crates = 0; 145 let n_crates = Crate::all(host.raw_database()).len();
142 for (root, _) in roots {
143 for _krate in Crate::source_root_crates(host.raw_database(), root) {
144 n_crates += 1;
145 }
146 }
147
148 // RA has quite a few crates, but the exact count doesn't matter 146 // RA has quite a few crates, but the exact count doesn't matter
149 assert!(n_crates > 20); 147 assert!(n_crates > 20);
150 } 148 }
diff --git a/crates/ra_cfg/src/lib.rs b/crates/ra_cfg/src/lib.rs
index e1c92fbba..1bee3eb99 100644
--- a/crates/ra_cfg/src/lib.rs
+++ b/crates/ra_cfg/src/lib.rs
@@ -36,26 +36,20 @@ impl CfgOptions {
36 self.check(&parse_cfg(attr)) 36 self.check(&parse_cfg(attr))
37 } 37 }
38 38
39 pub fn atom(mut self, name: SmolStr) -> CfgOptions { 39 pub fn insert_atom(&mut self, key: SmolStr) {
40 self.atoms.insert(name); 40 self.atoms.insert(key);
41 self
42 } 41 }
43 42
44 pub fn key_value(mut self, key: SmolStr, value: SmolStr) -> CfgOptions { 43 pub fn remove_atom(&mut self, name: &str) {
45 self.key_values.insert((key, value)); 44 self.atoms.remove(name);
46 self
47 } 45 }
48 46
49 /// Shortcut to set features 47 pub fn insert_key_value(&mut self, key: SmolStr, value: SmolStr) {
50 pub fn features(mut self, iter: impl IntoIterator<Item = SmolStr>) -> CfgOptions { 48 self.key_values.insert((key, value));
51 for feat in iter {
52 self = self.key_value("feature".into(), feat);
53 }
54 self
55 } 49 }
56 50
57 pub fn remove_atom(mut self, name: &SmolStr) -> CfgOptions { 51 /// Shortcut to set features
58 self.atoms.remove(name); 52 pub fn insert_features(&mut self, iter: impl IntoIterator<Item = SmolStr>) {
59 self 53 iter.into_iter().for_each(|feat| self.insert_key_value("feature".into(), feat));
60 } 54 }
61} 55}
diff --git a/crates/ra_cli/Cargo.toml b/crates/ra_cli/Cargo.toml
index 67e727a88..fcd102e8b 100644
--- a/crates/ra_cli/Cargo.toml
+++ b/crates/ra_cli/Cargo.toml
@@ -8,7 +8,7 @@ publish = false
8[dependencies] 8[dependencies]
9pico-args = "0.3.0" 9pico-args = "0.3.0"
10flexi_logger = "0.14.0" 10flexi_logger = "0.14.0"
11indicatif = "0.11.0" 11indicatif = "0.12.0"
12 12
13ra_syntax = { path = "../ra_syntax" } 13ra_syntax = { path = "../ra_syntax" }
14ra_ide_api = { path = "../ra_ide_api" } 14ra_ide_api = { path = "../ra_ide_api" }
diff --git a/crates/ra_cli/src/analysis_bench.rs b/crates/ra_cli/src/analysis_bench.rs
index 727f1e62b..8bbe5d9e8 100644
--- a/crates/ra_cli/src/analysis_bench.rs
+++ b/crates/ra_cli/src/analysis_bench.rs
@@ -8,7 +8,7 @@ use std::{
8 8
9use ra_db::{ 9use ra_db::{
10 salsa::{Database, Durability}, 10 salsa::{Database, Durability},
11 FileId, SourceDatabase, 11 FileId, SourceDatabaseExt,
12}; 12};
13use ra_ide_api::{Analysis, AnalysisChange, AnalysisHost, FilePosition, LineCol}; 13use ra_ide_api::{Analysis, AnalysisChange, AnalysisHost, FilePosition, LineCol};
14 14
diff --git a/crates/ra_cli/src/analysis_stats.rs b/crates/ra_cli/src/analysis_stats.rs
index a8a110bd9..cda5cafb8 100644
--- a/crates/ra_cli/src/analysis_stats.rs
+++ b/crates/ra_cli/src/analysis_stats.rs
@@ -2,7 +2,7 @@
2 2
3use std::{collections::HashSet, fmt::Write, path::Path, time::Instant}; 3use std::{collections::HashSet, fmt::Write, path::Path, time::Instant};
4 4
5use ra_db::SourceDatabase; 5use ra_db::SourceDatabaseExt;
6use ra_hir::{AssocItem, Crate, HasBodySource, HasSource, HirDisplay, ModuleDef, Ty, TypeWalk}; 6use ra_hir::{AssocItem, Crate, HasBodySource, HasSource, HirDisplay, ModuleDef, Ty, TypeWalk};
7use ra_syntax::AstNode; 7use ra_syntax::AstNode;
8 8
@@ -22,16 +22,29 @@ pub fn run(
22 let mut num_crates = 0; 22 let mut num_crates = 0;
23 let mut visited_modules = HashSet::new(); 23 let mut visited_modules = HashSet::new();
24 let mut visit_queue = Vec::new(); 24 let mut visit_queue = Vec::new();
25 for (source_root_id, project_root) in roots { 25
26 if project_root.is_member() { 26 let members = roots
27 for krate in Crate::source_root_crates(db, source_root_id) { 27 .into_iter()
28 num_crates += 1; 28 .filter_map(
29 let module = 29 |(source_root_id, project_root)| {
30 krate.root_module(db).expect("crate in source root without root module"); 30 if project_root.is_member() {
31 visit_queue.push(module); 31 Some(source_root_id)
32 } 32 } else {
33 None
34 }
35 },
36 )
37 .collect::<HashSet<_>>();
38
39 for krate in Crate::all(db) {
40 let module = krate.root_module(db).expect("crate without root module");
41 let file_id = module.definition_source(db).file_id;
42 if members.contains(&db.file_source_root(file_id.original_file(db))) {
43 num_crates += 1;
44 visit_queue.push(module);
33 } 45 }
34 } 46 }
47
35 println!("Crates in this dir: {}", num_crates); 48 println!("Crates in this dir: {}", num_crates);
36 let mut num_decls = 0; 49 let mut num_decls = 0;
37 let mut funcs = Vec::new(); 50 let mut funcs = Vec::new();
@@ -130,7 +143,7 @@ pub fn run(
130 ); 143 );
131 bar.println(format!( 144 bar.println(format!(
132 "{} {}:{}-{}:{}: Expected {}, got {}", 145 "{} {}:{}-{}:{}: Expected {}, got {}",
133 path.display(), 146 path,
134 start.line + 1, 147 start.line + 1,
135 start.col_utf16, 148 start.col_utf16,
136 end.line + 1, 149 end.line + 1,
diff --git a/crates/ra_db/Cargo.toml b/crates/ra_db/Cargo.toml
index c141f1a88..3394ae8ce 100644
--- a/crates/ra_db/Cargo.toml
+++ b/crates/ra_db/Cargo.toml
@@ -6,7 +6,7 @@ authors = ["rust-analyzer developers"]
6 6
7[dependencies] 7[dependencies]
8salsa = "0.13.0" 8salsa = "0.13.0"
9relative-path = "0.4.0" 9relative-path = "1.0.0"
10rustc-hash = "1.0" 10rustc-hash = "1.0"
11 11
12ra_syntax = { path = "../ra_syntax" } 12ra_syntax = { path = "../ra_syntax" }
diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs
index 23148096c..eafa95921 100644
--- a/crates/ra_db/src/input.rs
+++ b/crates/ra_db/src/input.rs
@@ -48,9 +48,6 @@ impl SourceRoot {
48 pub fn new_library() -> SourceRoot { 48 pub fn new_library() -> SourceRoot {
49 SourceRoot { is_library: true, ..SourceRoot::new() } 49 SourceRoot { is_library: true, ..SourceRoot::new() }
50 } 50 }
51 pub fn file_by_relative_path(&self, path: &RelativePath) -> Option<FileId> {
52 self.files.get(path).copied()
53 }
54 pub fn insert_file(&mut self, path: RelativePathBuf, file_id: FileId) { 51 pub fn insert_file(&mut self, path: RelativePathBuf, file_id: FileId) {
55 self.files.insert(path, file_id); 52 self.files.insert(path, file_id);
56 } 53 }
@@ -60,6 +57,9 @@ impl SourceRoot {
60 pub fn walk(&self) -> impl Iterator<Item = FileId> + '_ { 57 pub fn walk(&self) -> impl Iterator<Item = FileId> + '_ {
61 self.files.values().copied() 58 self.files.values().copied()
62 } 59 }
60 pub fn file_by_relative_path(&self, path: &RelativePath) -> Option<FileId> {
61 self.files.get(path).copied()
62 }
63} 63}
64 64
65/// `CrateGraph` is a bit of information which turns a set of text files into a 65/// `CrateGraph` is a bit of information which turns a set of text files into a
diff --git a/crates/ra_db/src/lib.rs b/crates/ra_db/src/lib.rs
index 603daed37..fc5d6d396 100644
--- a/crates/ra_db/src/lib.rs
+++ b/crates/ra_db/src/lib.rs
@@ -6,7 +6,7 @@ use std::{panic, sync::Arc};
6 6
7use ra_prof::profile; 7use ra_prof::profile;
8use ra_syntax::{ast, Parse, SourceFile, TextRange, TextUnit}; 8use ra_syntax::{ast, Parse, SourceFile, TextRange, TextUnit};
9use relative_path::RelativePathBuf; 9use relative_path::{RelativePath, RelativePathBuf};
10 10
11pub use crate::{ 11pub use crate::{
12 cancellation::Canceled, 12 cancellation::Canceled,
@@ -64,16 +64,39 @@ pub struct FileRange {
64 64
65pub const DEFAULT_LRU_CAP: usize = 128; 65pub const DEFAULT_LRU_CAP: usize = 128;
66 66
67pub trait FileLoader {
68 /// Text of the file.
69 fn file_text(&self, file_id: FileId) -> Arc<String>;
70 fn resolve_relative_path(&self, anchor: FileId, relative_path: &RelativePath)
71 -> Option<FileId>;
72 fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>>;
73}
74
67/// Database which stores all significant input facts: source code and project 75/// Database which stores all significant input facts: source code and project
68/// model. Everything else in rust-analyzer is derived from these queries. 76/// model. Everything else in rust-analyzer is derived from these queries.
69#[salsa::query_group(SourceDatabaseStorage)] 77#[salsa::query_group(SourceDatabaseStorage)]
70pub trait SourceDatabase: CheckCanceled + std::fmt::Debug { 78pub trait SourceDatabase: CheckCanceled + FileLoader + std::fmt::Debug {
71 /// Text of the file.
72 #[salsa::input]
73 fn file_text(&self, file_id: FileId) -> Arc<String>;
74 // Parses the file into the syntax tree. 79 // Parses the file into the syntax tree.
75 #[salsa::invoke(parse_query)] 80 #[salsa::invoke(parse_query)]
76 fn parse(&self, file_id: FileId) -> Parse<ast::SourceFile>; 81 fn parse(&self, file_id: FileId) -> Parse<ast::SourceFile>;
82
83 /// The crate graph.
84 #[salsa::input]
85 fn crate_graph(&self) -> Arc<CrateGraph>;
86}
87
88fn parse_query(db: &impl SourceDatabase, file_id: FileId) -> Parse<ast::SourceFile> {
89 let _p = profile("parse_query");
90 let text = db.file_text(file_id);
91 SourceFile::parse(&*text)
92}
93
94/// We don't want to give HIR knowledge of source roots, hence we extract these
95/// methods into a separate DB.
96#[salsa::query_group(SourceDatabaseExtStorage)]
97pub trait SourceDatabaseExt: SourceDatabase {
98 #[salsa::input]
99 fn file_text(&self, file_id: FileId) -> Arc<String>;
77 /// Path to a file, relative to the root of its source root. 100 /// Path to a file, relative to the root of its source root.
78 #[salsa::input] 101 #[salsa::input]
79 fn file_relative_path(&self, file_id: FileId) -> RelativePathBuf; 102 fn file_relative_path(&self, file_id: FileId) -> RelativePathBuf;
@@ -83,21 +106,48 @@ pub trait SourceDatabase: CheckCanceled + std::fmt::Debug {
83 /// Contents of the source root. 106 /// Contents of the source root.
84 #[salsa::input] 107 #[salsa::input]
85 fn source_root(&self, id: SourceRootId) -> Arc<SourceRoot>; 108 fn source_root(&self, id: SourceRootId) -> Arc<SourceRoot>;
109
86 fn source_root_crates(&self, id: SourceRootId) -> Arc<Vec<CrateId>>; 110 fn source_root_crates(&self, id: SourceRootId) -> Arc<Vec<CrateId>>;
87 /// The crate graph.
88 #[salsa::input]
89 fn crate_graph(&self) -> Arc<CrateGraph>;
90} 111}
91 112
92fn source_root_crates(db: &impl SourceDatabase, id: SourceRootId) -> Arc<Vec<CrateId>> { 113fn source_root_crates(
114 db: &(impl SourceDatabaseExt + SourceDatabase),
115 id: SourceRootId,
116) -> Arc<Vec<CrateId>> {
93 let root = db.source_root(id); 117 let root = db.source_root(id);
94 let graph = db.crate_graph(); 118 let graph = db.crate_graph();
95 let res = root.walk().filter_map(|it| graph.crate_id_for_crate_root(it)).collect::<Vec<_>>(); 119 let res = root.walk().filter_map(|it| graph.crate_id_for_crate_root(it)).collect::<Vec<_>>();
96 Arc::new(res) 120 Arc::new(res)
97} 121}
98 122
99fn parse_query(db: &impl SourceDatabase, file_id: FileId) -> Parse<ast::SourceFile> { 123/// Silly workaround for cyclic deps between the traits
100 let _p = profile("parse_query"); 124pub struct FileLoaderDelegate<T>(pub T);
101 let text = db.file_text(file_id); 125
102 SourceFile::parse(&*text) 126impl<T: SourceDatabaseExt> FileLoader for FileLoaderDelegate<&'_ T> {
127 fn file_text(&self, file_id: FileId) -> Arc<String> {
128 SourceDatabaseExt::file_text(self.0, file_id)
129 }
130 fn resolve_relative_path(
131 &self,
132 anchor: FileId,
133 relative_path: &RelativePath,
134 ) -> Option<FileId> {
135 let path = {
136 let mut path = self.0.file_relative_path(anchor);
137 // Workaround for relative path API: turn `lib.rs` into ``.
138 if !path.pop() {
139 path = RelativePathBuf::default();
140 }
141 path.push(relative_path);
142 path.normalize()
143 };
144 let source_root = self.0.file_source_root(anchor);
145 let source_root = self.0.source_root(source_root);
146 source_root.file_by_relative_path(&path)
147 }
148
149 fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> {
150 let source_root = self.0.file_source_root(file_id);
151 self.0.source_root_crates(source_root)
152 }
103} 153}
diff --git a/crates/ra_hir/Cargo.toml b/crates/ra_hir/Cargo.toml
index cc117f84d..edf1fa49b 100644
--- a/crates/ra_hir/Cargo.toml
+++ b/crates/ra_hir/Cargo.toml
@@ -5,9 +5,9 @@ version = "0.1.0"
5authors = ["rust-analyzer developers"] 5authors = ["rust-analyzer developers"]
6 6
7[dependencies] 7[dependencies]
8arrayvec = "0.4.10" 8arrayvec = "0.5.1"
9log = "0.4.5" 9log = "0.4.5"
10relative-path = "0.4.0" 10relative-path = "1.0.0"
11rustc-hash = "1.0" 11rustc-hash = "1.0"
12parking_lot = "0.9.0" 12parking_lot = "0.9.0"
13ena = "0.13" 13ena = "0.13"
diff --git a/crates/ra_hir/src/adt.rs b/crates/ra_hir/src/adt.rs
index 99d286215..3e9cd3c63 100644
--- a/crates/ra_hir/src/adt.rs
+++ b/crates/ra_hir/src/adt.rs
@@ -9,7 +9,7 @@ use ra_syntax::ast::{self, NameOwner, StructKind, TypeAscriptionOwner};
9use crate::{ 9use crate::{
10 db::{AstDatabase, DefDatabase, HirDatabase}, 10 db::{AstDatabase, DefDatabase, HirDatabase},
11 type_ref::TypeRef, 11 type_ref::TypeRef,
12 AsName, Enum, EnumVariant, FieldSource, HasSource, Name, Source, Struct, StructField, 12 AsName, Enum, EnumVariant, FieldSource, HasSource, Module, Name, Source, Struct, StructField,
13}; 13};
14 14
15impl Struct { 15impl Struct {
@@ -170,12 +170,20 @@ impl VariantDef {
170 } 170 }
171 } 171 }
172 172
173 pub(crate) fn field(self, db: &impl HirDatabase, name: &Name) -> Option<StructField> { 173 pub fn field(self, db: &impl HirDatabase, name: &Name) -> Option<StructField> {
174 match self { 174 match self {
175 VariantDef::Struct(it) => it.field(db, name), 175 VariantDef::Struct(it) => it.field(db, name),
176 VariantDef::EnumVariant(it) => it.field(db, name), 176 VariantDef::EnumVariant(it) => it.field(db, name),
177 } 177 }
178 } 178 }
179
180 pub fn module(self, db: &impl HirDatabase) -> Module {
181 match self {
182 VariantDef::Struct(it) => it.module(db),
183 VariantDef::EnumVariant(it) => it.module(db),
184 }
185 }
186
179 pub(crate) fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> { 187 pub(crate) fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> {
180 match self { 188 match self {
181 VariantDef::Struct(it) => it.variant_data(db), 189 VariantDef::Struct(it) => it.variant_data(db),
diff --git a/crates/ra_hir/src/attr.rs b/crates/ra_hir/src/attr.rs
index f67e80bfd..bd159a566 100644
--- a/crates/ra_hir/src/attr.rs
+++ b/crates/ra_hir/src/attr.rs
@@ -63,14 +63,24 @@ impl Attr {
63 self.path.as_ident().map_or(false, |s| s.to_string() == name) 63 self.path.as_ident().map_or(false, |s| s.to_string() == name)
64 } 64 }
65 65
66 // FIXME: handle cfg_attr :-)
66 pub(crate) fn as_cfg(&self) -> Option<&Subtree> { 67 pub(crate) fn as_cfg(&self) -> Option<&Subtree> {
67 if self.is_simple_atom("cfg") { 68 if !self.is_simple_atom("cfg") {
68 match &self.input { 69 return None;
69 Some(AttrInput::TokenTree(subtree)) => Some(subtree), 70 }
70 _ => None, 71 match &self.input {
71 } 72 Some(AttrInput::TokenTree(subtree)) => Some(subtree),
72 } else { 73 _ => None,
73 None 74 }
75 }
76
77 pub(crate) fn as_path(&self) -> Option<&SmolStr> {
78 if !self.is_simple_atom("path") {
79 return None;
80 }
81 match &self.input {
82 Some(AttrInput::Literal(it)) => Some(it),
83 _ => None,
74 } 84 }
75 } 85 }
76 86
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index e3a7e8e3c..8eb3c577d 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -5,7 +5,7 @@ pub(crate) mod docs;
5 5
6use std::sync::Arc; 6use std::sync::Arc;
7 7
8use ra_db::{CrateId, Edition, FileId, SourceRootId}; 8use ra_db::{CrateId, Edition, FileId};
9use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner}; 9use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner};
10 10
11use crate::{ 11use crate::{
@@ -24,7 +24,7 @@ use crate::{
24 U8, USIZE, 24 U8, USIZE,
25 }, 25 },
26 nameres::{CrateModuleId, ImportId, ModuleScope, Namespace}, 26 nameres::{CrateModuleId, ImportId, ModuleScope, Namespace},
27 resolve::{Resolver, TypeNs}, 27 resolve::{Resolver, Scope, TypeNs},
28 traits::TraitData, 28 traits::TraitData,
29 ty::{ 29 ty::{
30 primitive::{FloatBitness, FloatTy, IntBitness, IntTy, Signedness}, 30 primitive::{FloatBitness, FloatTy, IntBitness, IntTy, Signedness},
@@ -76,10 +76,8 @@ impl Crate {
76 crate_graph.edition(self.crate_id) 76 crate_graph.edition(self.crate_id)
77 } 77 }
78 78
79 // FIXME: should this be in source_binder? 79 pub fn all(db: &impl DefDatabase) -> Vec<Crate> {
80 pub fn source_root_crates(db: &impl DefDatabase, source_root: SourceRootId) -> Vec<Crate> { 80 db.crate_graph().iter().map(|crate_id| Crate { crate_id }).collect()
81 let crate_ids = db.source_root_crates(source_root);
82 crate_ids.iter().map(|&crate_id| Crate { crate_id }).collect()
83 } 81 }
84} 82}
85 83
@@ -465,7 +463,7 @@ impl Enum {
465 // ...and add generic params, if present 463 // ...and add generic params, if present
466 let p = self.generic_params(db); 464 let p = self.generic_params(db);
467 let r = if !p.params.is_empty() { r.push_generic_params_scope(p) } else { r }; 465 let r = if !p.params.is_empty() { r.push_generic_params_scope(p) } else { r };
468 r 466 r.push_scope(Scope::AdtScope(self.into()))
469 } 467 }
470} 468}
471 469
@@ -569,6 +567,14 @@ impl DefWithBody {
569 DefWithBody::Static(s) => s.krate(db), 567 DefWithBody::Static(s) => s.krate(db),
570 } 568 }
571 } 569 }
570
571 pub fn module(self, db: &impl HirDatabase) -> Module {
572 match self {
573 DefWithBody::Const(c) => c.module(db),
574 DefWithBody::Function(f) => f.module(db),
575 DefWithBody::Static(s) => s.module(db),
576 }
577 }
572} 578}
573 579
574pub trait HasBody: Copy { 580pub trait HasBody: Copy {
@@ -789,6 +795,20 @@ impl Const {
789 ImplBlock::containing(module_impls, self.into()) 795 ImplBlock::containing(module_impls, self.into())
790 } 796 }
791 797
798 pub fn parent_trait(self, db: &impl DefDatabase) -> Option<Trait> {
799 db.trait_items_index(self.module(db)).get_parent_trait(self.into())
800 }
801
802 pub fn container(self, db: &impl DefDatabase) -> Option<Container> {
803 if let Some(impl_block) = self.impl_block(db) {
804 Some(impl_block.into())
805 } else if let Some(trait_) = self.parent_trait(db) {
806 Some(trait_.into())
807 } else {
808 None
809 }
810 }
811
792 // FIXME: move to a more general type for 'body-having' items 812 // FIXME: move to a more general type for 'body-having' items
793 /// Builds a resolver for code inside this item. 813 /// Builds a resolver for code inside this item.
794 pub(crate) fn resolver(self, db: &impl HirDatabase) -> Resolver { 814 pub(crate) fn resolver(self, db: &impl HirDatabase) -> Resolver {
@@ -1075,3 +1095,13 @@ impl From<AssocItem> for crate::generics::GenericDef {
1075 } 1095 }
1076 } 1096 }
1077} 1097}
1098
1099impl AssocItem {
1100 pub fn module(self, db: &impl DefDatabase) -> Module {
1101 match self {
1102 AssocItem::Function(f) => f.module(db),
1103 AssocItem::Const(c) => c.module(db),
1104 AssocItem::TypeAlias(t) => t.module(db),
1105 }
1106 }
1107}
diff --git a/crates/ra_hir/src/code_model/docs.rs b/crates/ra_hir/src/code_model/docs.rs
index 9675e397f..8533b4f5e 100644
--- a/crates/ra_hir/src/code_model/docs.rs
+++ b/crates/ra_hir/src/code_model/docs.rs
@@ -6,21 +6,19 @@ use ra_syntax::ast;
6 6
7use crate::{ 7use crate::{
8 db::{AstDatabase, DefDatabase, HirDatabase}, 8 db::{AstDatabase, DefDatabase, HirDatabase},
9 Const, Enum, EnumVariant, FieldSource, Function, HasSource, MacroDef, Module, Static, Struct, 9 Adt, Const, Enum, EnumVariant, FieldSource, Function, HasSource, MacroDef, Module, Static,
10 StructField, Trait, TypeAlias, Union, 10 Struct, StructField, Trait, TypeAlias, Union,
11}; 11};
12 12
13#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] 13#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
14pub enum DocDef { 14pub enum DocDef {
15 Module(Module), 15 Module(Module),
16 StructField(StructField), 16 StructField(StructField),
17 Struct(Struct), 17 Adt(Adt),
18 Enum(Enum),
19 EnumVariant(EnumVariant), 18 EnumVariant(EnumVariant),
20 Static(Static), 19 Static(Static),
21 Const(Const), 20 Const(Const),
22 Function(Function), 21 Function(Function),
23 Union(Union),
24 Trait(Trait), 22 Trait(Trait),
25 TypeAlias(TypeAlias), 23 TypeAlias(TypeAlias),
26 MacroDef(MacroDef), 24 MacroDef(MacroDef),
@@ -29,13 +27,11 @@ pub enum DocDef {
29impl_froms!( 27impl_froms!(
30 DocDef: Module, 28 DocDef: Module,
31 StructField, 29 StructField,
32 Struct, 30 Adt(Struct, Enum, Union),
33 Enum,
34 EnumVariant, 31 EnumVariant,
35 Static, 32 Static,
36 Const, 33 Const,
37 Function, 34 Function,
38 Union,
39 Trait, 35 Trait,
40 TypeAlias, 36 TypeAlias,
41 MacroDef 37 MacroDef
@@ -79,13 +75,15 @@ pub(crate) fn documentation_query(
79 FieldSource::Named(named) => docs_from_ast(&named), 75 FieldSource::Named(named) => docs_from_ast(&named),
80 FieldSource::Pos(..) => None, 76 FieldSource::Pos(..) => None,
81 }, 77 },
82 DocDef::Struct(it) => docs_from_ast(&it.source(db).ast), 78 DocDef::Adt(it) => match it {
83 DocDef::Enum(it) => docs_from_ast(&it.source(db).ast), 79 Adt::Struct(it) => docs_from_ast(&it.source(db).ast),
80 Adt::Enum(it) => docs_from_ast(&it.source(db).ast),
81 Adt::Union(it) => docs_from_ast(&it.source(db).ast),
82 },
84 DocDef::EnumVariant(it) => docs_from_ast(&it.source(db).ast), 83 DocDef::EnumVariant(it) => docs_from_ast(&it.source(db).ast),
85 DocDef::Static(it) => docs_from_ast(&it.source(db).ast), 84 DocDef::Static(it) => docs_from_ast(&it.source(db).ast),
86 DocDef::Const(it) => docs_from_ast(&it.source(db).ast), 85 DocDef::Const(it) => docs_from_ast(&it.source(db).ast),
87 DocDef::Function(it) => docs_from_ast(&it.source(db).ast), 86 DocDef::Function(it) => docs_from_ast(&it.source(db).ast),
88 DocDef::Union(it) => docs_from_ast(&it.source(db).ast),
89 DocDef::Trait(it) => docs_from_ast(&it.source(db).ast), 87 DocDef::Trait(it) => docs_from_ast(&it.source(db).ast),
90 DocDef::TypeAlias(it) => docs_from_ast(&it.source(db).ast), 88 DocDef::TypeAlias(it) => docs_from_ast(&it.source(db).ast),
91 DocDef::MacroDef(it) => docs_from_ast(&it.source(db).ast), 89 DocDef::MacroDef(it) => docs_from_ast(&it.source(db).ast),
diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs
index 73d7d6fb6..489a3b19c 100644
--- a/crates/ra_hir/src/db.rs
+++ b/crates/ra_hir/src/db.rs
@@ -111,37 +111,37 @@ pub trait DefDatabase: InternDatabase + HirDebugDatabase {
111 #[salsa::invoke(CrateDefMap::crate_def_map_query)] 111 #[salsa::invoke(CrateDefMap::crate_def_map_query)]
112 fn crate_def_map(&self, krate: Crate) -> Arc<CrateDefMap>; 112 fn crate_def_map(&self, krate: Crate) -> Arc<CrateDefMap>;
113 113
114 #[salsa::invoke(crate::impl_block::impls_in_module_with_source_map_query)] 114 #[salsa::invoke(ModuleImplBlocks::impls_in_module_with_source_map_query)]
115 fn impls_in_module_with_source_map( 115 fn impls_in_module_with_source_map(
116 &self, 116 &self,
117 module: Module, 117 module: Module,
118 ) -> (Arc<ModuleImplBlocks>, Arc<ImplSourceMap>); 118 ) -> (Arc<ModuleImplBlocks>, Arc<ImplSourceMap>);
119 119
120 #[salsa::invoke(crate::impl_block::impls_in_module)] 120 #[salsa::invoke(ModuleImplBlocks::impls_in_module_query)]
121 fn impls_in_module(&self, module: Module) -> Arc<ModuleImplBlocks>; 121 fn impls_in_module(&self, module: Module) -> Arc<ModuleImplBlocks>;
122 122
123 #[salsa::invoke(crate::generics::GenericParams::generic_params_query)] 123 #[salsa::invoke(crate::generics::GenericParams::generic_params_query)]
124 fn generic_params(&self, def: GenericDef) -> Arc<GenericParams>; 124 fn generic_params(&self, def: GenericDef) -> Arc<GenericParams>;
125 125
126 #[salsa::invoke(crate::FnData::fn_data_query)] 126 #[salsa::invoke(FnData::fn_data_query)]
127 fn fn_data(&self, func: Function) -> Arc<FnData>; 127 fn fn_data(&self, func: Function) -> Arc<FnData>;
128 128
129 #[salsa::invoke(crate::type_alias::type_alias_data_query)] 129 #[salsa::invoke(TypeAliasData::type_alias_data_query)]
130 fn type_alias_data(&self, typ: TypeAlias) -> Arc<TypeAliasData>; 130 fn type_alias_data(&self, typ: TypeAlias) -> Arc<TypeAliasData>;
131 131
132 #[salsa::invoke(crate::ConstData::const_data_query)] 132 #[salsa::invoke(ConstData::const_data_query)]
133 fn const_data(&self, konst: Const) -> Arc<ConstData>; 133 fn const_data(&self, konst: Const) -> Arc<ConstData>;
134 134
135 #[salsa::invoke(crate::ConstData::static_data_query)] 135 #[salsa::invoke(ConstData::static_data_query)]
136 fn static_data(&self, konst: Static) -> Arc<ConstData>; 136 fn static_data(&self, konst: Static) -> Arc<ConstData>;
137 137
138 #[salsa::invoke(crate::lang_item::LangItems::module_lang_items_query)] 138 #[salsa::invoke(LangItems::module_lang_items_query)]
139 fn module_lang_items(&self, module: Module) -> Option<Arc<LangItems>>; 139 fn module_lang_items(&self, module: Module) -> Option<Arc<LangItems>>;
140 140
141 #[salsa::invoke(crate::lang_item::LangItems::crate_lang_items_query)] 141 #[salsa::invoke(LangItems::crate_lang_items_query)]
142 fn crate_lang_items(&self, krate: Crate) -> Arc<LangItems>; 142 fn crate_lang_items(&self, krate: Crate) -> Arc<LangItems>;
143 143
144 #[salsa::invoke(crate::lang_item::LangItems::lang_item_query)] 144 #[salsa::invoke(LangItems::lang_item_query)]
145 fn lang_item(&self, start_crate: Crate, item: SmolStr) -> Option<LangItemTarget>; 145 fn lang_item(&self, start_crate: Crate, item: SmolStr) -> Option<LangItemTarget>;
146 146
147 #[salsa::invoke(crate::code_model::docs::documentation_query)] 147 #[salsa::invoke(crate::code_model::docs::documentation_query)]
diff --git a/crates/ra_hir/src/debug.rs b/crates/ra_hir/src/debug.rs
index 87f3180c3..48b69000b 100644
--- a/crates/ra_hir/src/debug.rs
+++ b/crates/ra_hir/src/debug.rs
@@ -22,7 +22,7 @@ use std::fmt;
22 22
23use ra_db::{CrateId, FileId}; 23use ra_db::{CrateId, FileId};
24 24
25use crate::{db::HirDatabase, Crate, Module, Name}; 25use crate::{db::HirDatabase, Crate, HirFileId, Module, Name};
26 26
27impl Crate { 27impl Crate {
28 pub fn debug(self, db: &impl HirDebugDatabase) -> impl fmt::Debug + '_ { 28 pub fn debug(self, db: &impl HirDebugDatabase) -> impl fmt::Debug + '_ {
@@ -36,6 +36,12 @@ impl Module {
36 } 36 }
37} 37}
38 38
39impl HirFileId {
40 pub fn debug(self, db: &impl HirDebugDatabase) -> impl fmt::Debug + '_ {
41 debug_fn(move |fmt| db.debug_hir_file_id(self, fmt))
42 }
43}
44
39pub trait HirDebugHelper: HirDatabase { 45pub trait HirDebugHelper: HirDatabase {
40 fn crate_name(&self, _krate: CrateId) -> Option<String> { 46 fn crate_name(&self, _krate: CrateId) -> Option<String> {
41 None 47 None
@@ -48,6 +54,7 @@ pub trait HirDebugHelper: HirDatabase {
48pub trait HirDebugDatabase { 54pub trait HirDebugDatabase {
49 fn debug_crate(&self, krate: Crate, fmt: &mut fmt::Formatter<'_>) -> fmt::Result; 55 fn debug_crate(&self, krate: Crate, fmt: &mut fmt::Formatter<'_>) -> fmt::Result;
50 fn debug_module(&self, module: Module, fmt: &mut fmt::Formatter<'_>) -> fmt::Result; 56 fn debug_module(&self, module: Module, fmt: &mut fmt::Formatter<'_>) -> fmt::Result;
57 fn debug_hir_file_id(&self, file_id: HirFileId, fmt: &mut fmt::Formatter<'_>) -> fmt::Result;
51} 58}
52 59
53impl<DB: HirDebugHelper> HirDebugDatabase for DB { 60impl<DB: HirDebugHelper> HirDebugDatabase for DB {
@@ -62,12 +69,19 @@ impl<DB: HirDebugHelper> HirDebugDatabase for DB {
62 69
63 fn debug_module(&self, module: Module, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { 70 fn debug_module(&self, module: Module, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
64 let file_id = module.definition_source(self).file_id.original_file(self); 71 let file_id = module.definition_source(self).file_id.original_file(self);
65 let path = self.file_path(file_id); 72 let path = self.file_path(file_id).unwrap_or_else(|| "N/A".to_string());
66 fmt.debug_struct("Module") 73 fmt.debug_struct("Module")
67 .field("name", &module.name(self).unwrap_or_else(Name::missing)) 74 .field("name", &module.name(self).unwrap_or_else(Name::missing))
68 .field("path", &path.unwrap_or_else(|| "N/A".to_string())) 75 .field("path", &path)
69 .finish() 76 .finish()
70 } 77 }
78
79 fn debug_hir_file_id(&self, file_id: HirFileId, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
80 let original = file_id.original_file(self);
81 let path = self.file_path(original).unwrap_or_else(|| "N/A".to_string());
82 let is_macro = file_id != original.into();
83 fmt.debug_struct("HirFileId").field("path", &path).field("macro", &is_macro).finish()
84 }
71} 85}
72 86
73fn debug_fn(f: impl Fn(&mut fmt::Formatter<'_>) -> fmt::Result) -> impl fmt::Debug { 87fn debug_fn(f: impl Fn(&mut fmt::Formatter<'_>) -> fmt::Result) -> impl fmt::Debug {
diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs
index a012f33f7..f80d8eb5f 100644
--- a/crates/ra_hir/src/from_source.rs
+++ b/crates/ra_hir/src/from_source.rs
@@ -189,14 +189,14 @@ impl Module {
189 ModuleSource::SourceFile(_) => None, 189 ModuleSource::SourceFile(_) => None,
190 }; 190 };
191 191
192 let source_root_id = db.file_source_root(src.file_id.original_file(db)); 192 db.relevant_crates(src.file_id.original_file(db))
193 db.source_root_crates(source_root_id).iter().map(|&crate_id| Crate { crate_id }).find_map( 193 .iter()
194 |krate| { 194 .map(|&crate_id| Crate { crate_id })
195 .find_map(|krate| {
195 let def_map = db.crate_def_map(krate); 196 let def_map = db.crate_def_map(krate);
196 let module_id = def_map.find_module_by_source(src.file_id, decl_id)?; 197 let module_id = def_map.find_module_by_source(src.file_id, decl_id)?;
197 Some(Module { krate, module_id }) 198 Some(Module { krate, module_id })
198 }, 199 })
199 )
200 } 200 }
201} 201}
202 202
diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs
index a3b65cc79..499dcafea 100644
--- a/crates/ra_hir/src/ids.rs
+++ b/crates/ra_hir/src/ids.rs
@@ -50,16 +50,6 @@ impl HirFileId {
50 } 50 }
51 } 51 }
52 52
53 /// XXX: this is a temporary function, which should go away when we implement the
54 /// nameresolution+macro expansion combo. Prefer using `original_file` if
55 /// possible.
56 pub fn as_original_file(self) -> FileId {
57 match self.0 {
58 HirFileIdRepr::File(file_id) => file_id,
59 HirFileIdRepr::Macro(_r) => panic!("macro generated file: {:?}", self),
60 }
61 }
62
63 /// Get the crate which the macro lives in, if it is a macro file. 53 /// Get the crate which the macro lives in, if it is a macro file.
64 pub(crate) fn macro_crate(self, db: &impl AstDatabase) -> Option<Crate> { 54 pub(crate) fn macro_crate(self, db: &impl AstDatabase) -> Option<Crate> {
65 match self.0 { 55 match self.0 {
@@ -95,11 +85,7 @@ impl HirFileId {
95 // Note: 85 // Note:
96 // The final goal we would like to make all parse_macro success, 86 // The final goal we would like to make all parse_macro success,
97 // such that the following log will not call anyway. 87 // such that the following log will not call anyway.
98 log::warn!( 88 log::warn!("fail on macro_parse: (reason: {})", err,);
99 "fail on macro_parse: (reason: {}) {}",
100 err,
101 macro_call_id.debug_dump(db)
102 );
103 }) 89 })
104 .ok()?; 90 .ok()?;
105 match macro_file.macro_file_kind { 91 match macro_file.macro_file_kind {
@@ -377,35 +363,6 @@ impl AstItemDef<ast::TypeAliasDef> for TypeAliasId {
377 } 363 }
378} 364}
379 365
380impl MacroCallId {
381 pub fn debug_dump(self, db: &impl AstDatabase) -> String {
382 let loc = self.loc(db);
383 let node = loc.ast_id.to_node(db);
384 let syntax_str = {
385 let mut res = String::new();
386 node.syntax().text().for_each_chunk(|chunk| {
387 if !res.is_empty() {
388 res.push(' ')
389 }
390 res.push_str(chunk)
391 });
392 res
393 };
394
395 // dump the file name
396 let file_id: HirFileId = self.loc(db).ast_id.file_id();
397 let original = file_id.original_file(db);
398 let macro_rules = db.macro_def(loc.def);
399
400 format!(
401 "macro call [file: {:?}] : {}\nhas rules: {}",
402 db.file_relative_path(original),
403 syntax_str,
404 macro_rules.is_some()
405 )
406 }
407}
408
409/// This exists just for Chalk, because Chalk just has a single `StructId` where 366/// This exists just for Chalk, because Chalk just has a single `StructId` where
410/// we have different kinds of ADTs, primitive types and special type 367/// we have different kinds of ADTs, primitive types and special type
411/// constructors like tuples and function pointers. 368/// constructors like tuples and function pointers.
diff --git a/crates/ra_hir/src/impl_block.rs b/crates/ra_hir/src/impl_block.rs
index 55dfc393b..33ef87563 100644
--- a/crates/ra_hir/src/impl_block.rs
+++ b/crates/ra_hir/src/impl_block.rs
@@ -176,6 +176,25 @@ pub struct ModuleImplBlocks {
176} 176}
177 177
178impl ModuleImplBlocks { 178impl ModuleImplBlocks {
179 pub(crate) fn impls_in_module_with_source_map_query(
180 db: &(impl DefDatabase + AstDatabase),
181 module: Module,
182 ) -> (Arc<ModuleImplBlocks>, Arc<ImplSourceMap>) {
183 let mut source_map = ImplSourceMap::default();
184 let crate_graph = db.crate_graph();
185 let cfg_options = crate_graph.cfg_options(module.krate.crate_id());
186
187 let result = ModuleImplBlocks::collect(db, cfg_options, module, &mut source_map);
188 (Arc::new(result), Arc::new(source_map))
189 }
190
191 pub(crate) fn impls_in_module_query(
192 db: &impl DefDatabase,
193 module: Module,
194 ) -> Arc<ModuleImplBlocks> {
195 db.impls_in_module_with_source_map(module).0
196 }
197
179 fn collect( 198 fn collect(
180 db: &(impl DefDatabase + AstDatabase), 199 db: &(impl DefDatabase + AstDatabase),
181 cfg_options: &CfgOptions, 200 cfg_options: &CfgOptions,
@@ -264,19 +283,3 @@ impl ModuleImplBlocks {
264 } 283 }
265 } 284 }
266} 285}
267
268pub(crate) fn impls_in_module_with_source_map_query(
269 db: &(impl DefDatabase + AstDatabase),
270 module: Module,
271) -> (Arc<ModuleImplBlocks>, Arc<ImplSourceMap>) {
272 let mut source_map = ImplSourceMap::default();
273 let crate_graph = db.crate_graph();
274 let cfg_options = crate_graph.cfg_options(module.krate.crate_id());
275
276 let result = ModuleImplBlocks::collect(db, cfg_options, module, &mut source_map);
277 (Arc::new(result), Arc::new(source_map))
278}
279
280pub(crate) fn impls_in_module(db: &impl DefDatabase, module: Module) -> Arc<ModuleImplBlocks> {
281 db.impls_in_module_with_source_map(module).0
282}
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs
index 4340e9d34..ca261e8f5 100644
--- a/crates/ra_hir/src/lib.rs
+++ b/crates/ra_hir/src/lib.rs
@@ -51,6 +51,7 @@ mod lang_item;
51mod generics; 51mod generics;
52mod resolve; 52mod resolve;
53pub mod diagnostics; 53pub mod diagnostics;
54mod util;
54 55
55mod code_model; 56mod code_model;
56 57
@@ -71,7 +72,7 @@ pub use self::{
71 either::Either, 72 either::Either,
72 expr::ExprScopes, 73 expr::ExprScopes,
73 from_source::FromSource, 74 from_source::FromSource,
74 generics::{GenericParam, GenericParams, HasGenericParams}, 75 generics::{GenericDef, GenericParam, GenericParams, HasGenericParams},
75 ids::{HirFileId, MacroCallId, MacroCallLoc, MacroDefId, MacroFile}, 76 ids::{HirFileId, MacroCallId, MacroCallLoc, MacroDefId, MacroFile},
76 impl_block::ImplBlock, 77 impl_block::ImplBlock,
77 name::Name, 78 name::Name,
diff --git a/crates/ra_hir/src/mock.rs b/crates/ra_hir/src/mock.rs
index f750986b8..0b278deb3 100644
--- a/crates/ra_hir/src/mock.rs
+++ b/crates/ra_hir/src/mock.rs
@@ -5,10 +5,10 @@ use std::{panic, sync::Arc};
5use parking_lot::Mutex; 5use parking_lot::Mutex;
6use ra_cfg::CfgOptions; 6use ra_cfg::CfgOptions;
7use ra_db::{ 7use ra_db::{
8 salsa, CrateGraph, CrateId, Edition, FileId, FilePosition, SourceDatabase, SourceRoot, 8 salsa, CrateGraph, CrateId, Edition, FileId, FileLoader, FileLoaderDelegate, FilePosition,
9 SourceRootId, 9 SourceDatabase, SourceDatabaseExt, SourceRoot, SourceRootId,
10}; 10};
11use relative_path::RelativePathBuf; 11use relative_path::{RelativePath, RelativePathBuf};
12use rustc_hash::FxHashMap; 12use rustc_hash::FxHashMap;
13use test_utils::{extract_offset, parse_fixture, CURSOR_MARKER}; 13use test_utils::{extract_offset, parse_fixture, CURSOR_MARKER};
14 14
@@ -17,6 +17,7 @@ use crate::{db, debug::HirDebugHelper, diagnostics::DiagnosticSink};
17pub const WORKSPACE: SourceRootId = SourceRootId(0); 17pub const WORKSPACE: SourceRootId = SourceRootId(0);
18 18
19#[salsa::database( 19#[salsa::database(
20 ra_db::SourceDatabaseExtStorage,
20 ra_db::SourceDatabaseStorage, 21 ra_db::SourceDatabaseStorage,
21 db::InternDatabaseStorage, 22 db::InternDatabaseStorage,
22 db::AstDatabaseStorage, 23 db::AstDatabaseStorage,
@@ -34,6 +35,22 @@ pub struct MockDatabase {
34 35
35impl panic::RefUnwindSafe for MockDatabase {} 36impl panic::RefUnwindSafe for MockDatabase {}
36 37
38impl FileLoader for MockDatabase {
39 fn file_text(&self, file_id: FileId) -> Arc<String> {
40 FileLoaderDelegate(self).file_text(file_id)
41 }
42 fn resolve_relative_path(
43 &self,
44 anchor: FileId,
45 relative_path: &RelativePath,
46 ) -> Option<FileId> {
47 FileLoaderDelegate(self).resolve_relative_path(anchor, relative_path)
48 }
49 fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> {
50 FileLoaderDelegate(self).relevant_crates(file_id)
51 }
52}
53
37impl HirDebugHelper for MockDatabase { 54impl HirDebugHelper for MockDatabase {
38 fn crate_name(&self, krate: CrateId) -> Option<String> { 55 fn crate_name(&self, krate: CrateId) -> Option<String> {
39 self.crate_names.get(&krate).cloned() 56 self.crate_names.get(&krate).cloned()
@@ -278,7 +295,10 @@ macro_rules! crate_graph {
278 $crate_path:literal, 295 $crate_path:literal,
279 $($edition:literal,)? 296 $($edition:literal,)?
280 [$($dep:literal),*] 297 [$($dep:literal),*]
281 $(,$cfg:expr)? 298 $(, cfg = {
299 $($key:literal $(= $value:literal)?),*
300 $(,)?
301 })?
282 ), 302 ),
283 )*) => {{ 303 )*) => {{
284 let mut res = $crate::mock::CrateGraphFixture::default(); 304 let mut res = $crate::mock::CrateGraphFixture::default();
@@ -286,7 +306,19 @@ macro_rules! crate_graph {
286 #[allow(unused_mut, unused_assignments)] 306 #[allow(unused_mut, unused_assignments)]
287 let mut edition = ra_db::Edition::Edition2018; 307 let mut edition = ra_db::Edition::Edition2018;
288 $(edition = ra_db::Edition::from_string($edition);)? 308 $(edition = ra_db::Edition::from_string($edition);)?
289 let cfg_options = { ::ra_cfg::CfgOptions::default() $(; $cfg)? }; 309 let cfg_options = {
310 #[allow(unused_mut)]
311 let mut cfg = ::ra_cfg::CfgOptions::default();
312 $(
313 $(
314 if 0 == 0 $(+ { drop($value); 1})? {
315 cfg.insert_atom($key.into());
316 }
317 $(cfg.insert_key_value($key.into(), $value.into());)?
318 )*
319 )?
320 cfg
321 };
290 res.0.push(( 322 res.0.push((
291 $crate_name.to_string(), 323 $crate_name.to_string(),
292 ($crate_path.to_string(), edition, cfg_options, vec![$($dep.to_string()),*]) 324 ($crate_path.to_string(), edition, cfg_options, vec![$($dep.to_string()),*])
diff --git a/crates/ra_hir/src/nameres/collector.rs b/crates/ra_hir/src/nameres/collector.rs
index cef2dc9d2..b5fe16bfa 100644
--- a/crates/ra_hir/src/nameres/collector.rs
+++ b/crates/ra_hir/src/nameres/collector.rs
@@ -7,14 +7,13 @@ use rustc_hash::FxHashMap;
7use test_utils::tested_by; 7use test_utils::tested_by;
8 8
9use crate::{ 9use crate::{
10 attr::Attr,
10 db::DefDatabase, 11 db::DefDatabase,
11 ids::{AstItemDef, LocationCtx, MacroCallId, MacroCallLoc, MacroDefId, MacroFileKind}, 12 ids::{AstItemDef, LocationCtx, MacroCallId, MacroCallLoc, MacroDefId, MacroFileKind},
12 name::MACRO_RULES, 13 name::MACRO_RULES,
13 nameres::{ 14 nameres::{
14 diagnostics::DefDiagnostic, 15 diagnostics::DefDiagnostic, mod_resolution::ModDir, raw, Crate, CrateDefMap, CrateModuleId,
15 mod_resolution::{resolve_submodule, ParentModule}, 16 ModuleData, ModuleDef, PerNs, ReachedFixedPoint, Resolution, ResolveMode,
16 raw, Crate, CrateDefMap, CrateModuleId, ModuleData, ModuleDef, PerNs, ReachedFixedPoint,
17 Resolution, ResolveMode,
18 }, 17 },
19 Adt, AstId, Const, Enum, Function, HirFileId, MacroDef, Module, Name, Path, PathKind, Static, 18 Adt, AstId, Const, Enum, Function, HirFileId, MacroDef, Module, Name, Path, PathKind, Static,
20 Struct, Trait, TypeAlias, Union, 19 Struct, Trait, TypeAlias, Union,
@@ -45,6 +44,7 @@ pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> C
45 glob_imports: FxHashMap::default(), 44 glob_imports: FxHashMap::default(),
46 unresolved_imports: Vec::new(), 45 unresolved_imports: Vec::new(),
47 unexpanded_macros: Vec::new(), 46 unexpanded_macros: Vec::new(),
47 mod_dirs: FxHashMap::default(),
48 macro_stack_monitor: MacroStackMonitor::default(), 48 macro_stack_monitor: MacroStackMonitor::default(),
49 cfg_options, 49 cfg_options,
50 }; 50 };
@@ -87,6 +87,7 @@ struct DefCollector<'a, DB> {
87 glob_imports: FxHashMap<CrateModuleId, Vec<(CrateModuleId, raw::ImportId)>>, 87 glob_imports: FxHashMap<CrateModuleId, Vec<(CrateModuleId, raw::ImportId)>>,
88 unresolved_imports: Vec<(CrateModuleId, raw::ImportId, raw::ImportData)>, 88 unresolved_imports: Vec<(CrateModuleId, raw::ImportId, raw::ImportData)>,
89 unexpanded_macros: Vec<(CrateModuleId, AstId<ast::MacroCall>, Path)>, 89 unexpanded_macros: Vec<(CrateModuleId, AstId<ast::MacroCall>, Path)>,
90 mod_dirs: FxHashMap<CrateModuleId, ModDir>,
90 91
91 /// Some macro use `$tt:tt which mean we have to handle the macro perfectly 92 /// Some macro use `$tt:tt which mean we have to handle the macro perfectly
92 /// To prevent stack overflow, we add a deep counter here for prevent that. 93 /// To prevent stack overflow, we add a deep counter here for prevent that.
@@ -107,11 +108,10 @@ where
107 self.def_map.modules[module_id].definition = Some(file_id); 108 self.def_map.modules[module_id].definition = Some(file_id);
108 ModCollector { 109 ModCollector {
109 def_collector: &mut *self, 110 def_collector: &mut *self,
110 attr_path: None,
111 module_id, 111 module_id,
112 file_id: file_id.into(), 112 file_id: file_id.into(),
113 raw_items: &raw_items, 113 raw_items: &raw_items,
114 parent_module: None, 114 mod_dir: ModDir::root(),
115 } 115 }
116 .collect(raw_items.items()); 116 .collect(raw_items.items());
117 117
@@ -481,13 +481,13 @@ where
481 if !self.macro_stack_monitor.is_poison(macro_def_id) { 481 if !self.macro_stack_monitor.is_poison(macro_def_id) {
482 let file_id: HirFileId = macro_call_id.as_file(MacroFileKind::Items); 482 let file_id: HirFileId = macro_call_id.as_file(MacroFileKind::Items);
483 let raw_items = self.db.raw_items(file_id); 483 let raw_items = self.db.raw_items(file_id);
484 let mod_dir = self.mod_dirs[&module_id].clone();
484 ModCollector { 485 ModCollector {
485 def_collector: &mut *self, 486 def_collector: &mut *self,
486 file_id, 487 file_id,
487 attr_path: None,
488 module_id, 488 module_id,
489 raw_items: &raw_items, 489 raw_items: &raw_items,
490 parent_module: None, 490 mod_dir,
491 } 491 }
492 .collect(raw_items.items()); 492 .collect(raw_items.items());
493 } else { 493 } else {
@@ -508,9 +508,8 @@ struct ModCollector<'a, D> {
508 def_collector: D, 508 def_collector: D,
509 module_id: CrateModuleId, 509 module_id: CrateModuleId,
510 file_id: HirFileId, 510 file_id: HirFileId,
511 attr_path: Option<&'a SmolStr>,
512 raw_items: &'a raw::RawItems, 511 raw_items: &'a raw::RawItems,
513 parent_module: Option<ParentModule<'a>>, 512 mod_dir: ModDir,
514} 513}
515 514
516impl<DB> ModCollector<'_, &'_ mut DefCollector<'_, DB>> 515impl<DB> ModCollector<'_, &'_ mut DefCollector<'_, DB>>
@@ -518,6 +517,10 @@ where
518 DB: DefDatabase, 517 DB: DefDatabase,
519{ 518{
520 fn collect(&mut self, items: &[raw::RawItem]) { 519 fn collect(&mut self, items: &[raw::RawItem]) {
520 // Note: don't assert that inserted value is fresh: it's simply not true
521 // for macros.
522 self.def_collector.mod_dirs.insert(self.module_id, self.mod_dir.clone());
523
521 // Prelude module is always considered to be `#[macro_use]`. 524 // Prelude module is always considered to be `#[macro_use]`.
522 if let Some(prelude_module) = self.def_collector.def_map.prelude { 525 if let Some(prelude_module) = self.def_collector.def_map.prelude {
523 if prelude_module.krate != self.def_collector.def_map.krate { 526 if prelude_module.krate != self.def_collector.def_map.krate {
@@ -530,7 +533,7 @@ where
530 // `#[macro_use] extern crate` is hoisted to imports macros before collecting 533 // `#[macro_use] extern crate` is hoisted to imports macros before collecting
531 // any other items. 534 // any other items.
532 for item in items { 535 for item in items {
533 if self.is_cfg_enabled(&item.attrs) { 536 if self.is_cfg_enabled(item.attrs()) {
534 if let raw::RawItemKind::Import(import_id) = item.kind { 537 if let raw::RawItemKind::Import(import_id) = item.kind {
535 let import = self.raw_items[import_id].clone(); 538 let import = self.raw_items[import_id].clone();
536 if import.is_extern_crate && import.is_macro_use { 539 if import.is_extern_crate && import.is_macro_use {
@@ -541,9 +544,11 @@ where
541 } 544 }
542 545
543 for item in items { 546 for item in items {
544 if self.is_cfg_enabled(&item.attrs) { 547 if self.is_cfg_enabled(item.attrs()) {
545 match item.kind { 548 match item.kind {
546 raw::RawItemKind::Module(m) => self.collect_module(&self.raw_items[m]), 549 raw::RawItemKind::Module(m) => {
550 self.collect_module(&self.raw_items[m], item.attrs())
551 }
547 raw::RawItemKind::Import(import_id) => self 552 raw::RawItemKind::Import(import_id) => self
548 .def_collector 553 .def_collector
549 .unresolved_imports 554 .unresolved_imports
@@ -555,53 +560,48 @@ where
555 } 560 }
556 } 561 }
557 562
558 fn collect_module(&mut self, module: &raw::ModuleData) { 563 fn collect_module(&mut self, module: &raw::ModuleData, attrs: &[Attr]) {
564 let path_attr = self.path_attr(attrs);
565 let is_macro_use = self.is_macro_use(attrs);
559 match module { 566 match module {
560 // inline module, just recurse 567 // inline module, just recurse
561 raw::ModuleData::Definition { name, items, ast_id, attr_path, is_macro_use } => { 568 raw::ModuleData::Definition { name, items, ast_id } => {
562 let module_id = 569 let module_id =
563 self.push_child_module(name.clone(), ast_id.with_file_id(self.file_id), None); 570 self.push_child_module(name.clone(), ast_id.with_file_id(self.file_id), None);
564 let parent_module = ParentModule { name, attr_path: attr_path.as_ref() };
565 571
566 ModCollector { 572 ModCollector {
567 def_collector: &mut *self.def_collector, 573 def_collector: &mut *self.def_collector,
568 module_id, 574 module_id,
569 attr_path: attr_path.as_ref(),
570 file_id: self.file_id, 575 file_id: self.file_id,
571 raw_items: self.raw_items, 576 raw_items: self.raw_items,
572 parent_module: Some(parent_module), 577 mod_dir: self.mod_dir.descend_into_definition(name, path_attr),
573 } 578 }
574 .collect(&*items); 579 .collect(&*items);
575 if *is_macro_use { 580 if is_macro_use {
576 self.import_all_legacy_macros(module_id); 581 self.import_all_legacy_macros(module_id);
577 } 582 }
578 } 583 }
579 // out of line module, resolve, parse and recurse 584 // out of line module, resolve, parse and recurse
580 raw::ModuleData::Declaration { name, ast_id, attr_path, is_macro_use } => { 585 raw::ModuleData::Declaration { name, ast_id } => {
581 let ast_id = ast_id.with_file_id(self.file_id); 586 let ast_id = ast_id.with_file_id(self.file_id);
582 let is_root = self.def_collector.def_map.modules[self.module_id].parent.is_none(); 587 match self.mod_dir.resolve_declaration(
583 match resolve_submodule(
584 self.def_collector.db, 588 self.def_collector.db,
585 self.file_id, 589 self.file_id,
586 self.attr_path,
587 name, 590 name,
588 is_root, 591 path_attr,
589 attr_path.as_ref(),
590 self.parent_module,
591 ) { 592 ) {
592 Ok(file_id) => { 593 Ok((file_id, mod_dir)) => {
593 let module_id = self.push_child_module(name.clone(), ast_id, Some(file_id)); 594 let module_id = self.push_child_module(name.clone(), ast_id, Some(file_id));
594 let raw_items = self.def_collector.db.raw_items(file_id.into()); 595 let raw_items = self.def_collector.db.raw_items(file_id.into());
595 ModCollector { 596 ModCollector {
596 def_collector: &mut *self.def_collector, 597 def_collector: &mut *self.def_collector,
597 module_id, 598 module_id,
598 attr_path: attr_path.as_ref(),
599 file_id: file_id.into(), 599 file_id: file_id.into(),
600 raw_items: &raw_items, 600 raw_items: &raw_items,
601 parent_module: None, 601 mod_dir,
602 } 602 }
603 .collect(raw_items.items()); 603 .collect(raw_items.items());
604 if *is_macro_use { 604 if is_macro_use {
605 self.import_all_legacy_macros(module_id); 605 self.import_all_legacy_macros(module_id);
606 } 606 }
607 } 607 }
@@ -714,12 +714,16 @@ where
714 } 714 }
715 } 715 }
716 716
717 fn is_cfg_enabled(&self, attrs: &raw::Attrs) -> bool { 717 fn is_cfg_enabled(&self, attrs: &[Attr]) -> bool {
718 attrs.as_ref().map_or(true, |attrs| { 718 attrs.iter().all(|attr| attr.is_cfg_enabled(&self.def_collector.cfg_options) != Some(false))
719 attrs 719 }
720 .iter() 720
721 .all(|attr| attr.is_cfg_enabled(&self.def_collector.cfg_options) != Some(false)) 721 fn path_attr<'a>(&self, attrs: &'a [Attr]) -> Option<&'a SmolStr> {
722 }) 722 attrs.iter().find_map(|attr| attr.as_path())
723 }
724
725 fn is_macro_use<'a>(&self, attrs: &'a [Attr]) -> bool {
726 attrs.iter().any(|attr| attr.is_simple_atom("macro_use"))
723 } 727 }
724} 728}
725 729
@@ -747,6 +751,7 @@ mod tests {
747 glob_imports: FxHashMap::default(), 751 glob_imports: FxHashMap::default(),
748 unresolved_imports: Vec::new(), 752 unresolved_imports: Vec::new(),
749 unexpanded_macros: Vec::new(), 753 unexpanded_macros: Vec::new(),
754 mod_dirs: FxHashMap::default(),
750 macro_stack_monitor: monitor, 755 macro_stack_monitor: monitor,
751 cfg_options: &CfgOptions::default(), 756 cfg_options: &CfgOptions::default(),
752 }; 757 };
diff --git a/crates/ra_hir/src/nameres/mod_resolution.rs b/crates/ra_hir/src/nameres/mod_resolution.rs
index 3aa32bd66..e8b808514 100644
--- a/crates/ra_hir/src/nameres/mod_resolution.rs
+++ b/crates/ra_hir/src/nameres/mod_resolution.rs
@@ -1,187 +1,87 @@
1//! This module resolves `mod foo;` declaration to file. 1//! This module resolves `mod foo;` declaration to file.
2 2use ra_db::FileId;
3use std::{borrow::Cow, sync::Arc};
4
5use ra_db::{FileId, SourceRoot};
6use ra_syntax::SmolStr; 3use ra_syntax::SmolStr;
7use relative_path::RelativePathBuf; 4use relative_path::RelativePathBuf;
8 5
9use crate::{db::DefDatabase, HirFileId, Name}; 6use crate::{db::DefDatabase, HirFileId, Name};
10 7
11#[derive(Clone, Copy)] 8#[derive(Clone, Debug)]
12pub(super) struct ParentModule<'a> { 9pub(super) struct ModDir {
13 pub(super) name: &'a Name, 10 /// `.` for `mod.rs`, `lib.rs`
14 pub(super) attr_path: Option<&'a SmolStr>, 11 /// `./foo` for `foo.rs`
12 /// `./foo/bar` for `mod bar { mod x; }` nested in `foo.rs`
13 path: RelativePathBuf,
14 /// inside `./foo.rs`, mods with `#[path]` should *not* be relative to `./foo/`
15 root_non_dir_owner: bool,
15} 16}
16 17
17impl<'a> ParentModule<'a> { 18impl ModDir {
18 fn attribute_path(&self) -> Option<&SmolStr> { 19 pub(super) fn root() -> ModDir {
19 self.attr_path.filter(|p| !p.is_empty()) 20 ModDir { path: RelativePathBuf::default(), root_non_dir_owner: false }
20 } 21 }
21}
22 22
23pub(super) fn resolve_submodule( 23 pub(super) fn descend_into_definition(
24 db: &impl DefDatabase, 24 &self,
25 file_id: HirFileId, 25 name: &Name,
26 mod_attr_path: Option<&SmolStr>, 26 attr_path: Option<&SmolStr>,
27 name: &Name, 27 ) -> ModDir {
28 is_root: bool, 28 let mut path = self.path.clone();
29 attr_path: Option<&SmolStr>, 29 match attr_to_path(attr_path) {
30 parent_module: Option<ParentModule<'_>>, 30 None => path.push(&name.to_string()),
31) -> Result<FileId, RelativePathBuf> { 31 Some(attr_path) => {
32 let file_id = file_id.original_file(db); 32 if self.root_non_dir_owner {
33 let source_root_id = db.file_source_root(file_id); 33 // Workaround for relative path API: turn `lib.rs` into ``.
34 let path = db.file_relative_path(file_id); 34 if !path.pop() {
35 let root = RelativePathBuf::default(); 35 path = RelativePathBuf::default();
36 let dir_path = path.parent().unwrap_or(&root); 36 }
37 let mod_name = path.file_stem().unwrap_or("unknown");
38
39 let resolve_mode = match (attr_path.filter(|p| !p.is_empty()), parent_module) {
40 (Some(file_path), Some(parent_module)) => {
41 let file_path = normalize_attribute_path(file_path);
42 match parent_module.attribute_path() {
43 Some(parent_module_attr_path) => {
44 let path = dir_path
45 .join(format!(
46 "{}/{}",
47 normalize_attribute_path(parent_module_attr_path),
48 file_path
49 ))
50 .normalize();
51 ResolutionMode::InlineModuleWithAttributePath(
52 InsideInlineModuleMode::WithAttributePath(path),
53 )
54 }
55 None => {
56 let path =
57 dir_path.join(format!("{}/{}", parent_module.name, file_path)).normalize();
58 ResolutionMode::InsideInlineModule(InsideInlineModuleMode::WithAttributePath(
59 path,
60 ))
61 } 37 }
38 path.push(attr_path);
62 } 39 }
63 } 40 }
64 (None, Some(parent_module)) => match parent_module.attribute_path() { 41 ModDir { path, root_non_dir_owner: false }
65 Some(parent_module_attr_path) => { 42 }
66 let path = dir_path.join(format!( 43
67 "{}/{}.rs", 44 pub(super) fn resolve_declaration(
68 normalize_attribute_path(parent_module_attr_path), 45 &self,
69 name 46 db: &impl DefDatabase,
70 )); 47 file_id: HirFileId,
71 ResolutionMode::InlineModuleWithAttributePath(InsideInlineModuleMode::File(path)) 48 name: &Name,
49 attr_path: Option<&SmolStr>,
50 ) -> Result<(FileId, ModDir), RelativePathBuf> {
51 let empty_path = RelativePathBuf::default();
52 let file_id = file_id.original_file(db);
53
54 let mut candidate_files = Vec::new();
55 match attr_to_path(attr_path) {
56 Some(attr_path) => {
57 let base = if self.root_non_dir_owner {
58 self.path.parent().unwrap_or(&empty_path)
59 } else {
60 &self.path
61 };
62 candidate_files.push(base.join(attr_path))
72 } 63 }
73 None => { 64 None => {
74 let path = dir_path.join(format!("{}/{}.rs", parent_module.name, name)); 65 candidate_files.push(self.path.join(&format!("{}.rs", name)));
75 ResolutionMode::InsideInlineModule(InsideInlineModuleMode::File(path)) 66 candidate_files.push(self.path.join(&format!("{}/mod.rs", name)));
76 }
77 },
78 (Some(file_path), None) => {
79 let file_path = normalize_attribute_path(file_path);
80 let path = dir_path.join(file_path.as_ref()).normalize();
81 ResolutionMode::OutOfLine(OutOfLineMode::WithAttributePath(path))
82 }
83 (None, None) => {
84 let is_dir_owner = is_root || mod_name == "mod" || mod_attr_path.is_some();
85 if is_dir_owner {
86 let file_mod = dir_path.join(format!("{}.rs", name));
87 let dir_mod = dir_path.join(format!("{}/mod.rs", name));
88 ResolutionMode::OutOfLine(OutOfLineMode::RootOrModRs {
89 file: file_mod,
90 directory: dir_mod,
91 })
92 } else {
93 let path = dir_path.join(format!("{}/{}.rs", mod_name, name));
94 ResolutionMode::OutOfLine(OutOfLineMode::FileInDirectory(path))
95 } 67 }
96 } 68 };
97 }; 69
98 70 for candidate in candidate_files.iter() {
99 resolve_mode.resolve(db.source_root(source_root_id)) 71 if let Some(file_id) = db.resolve_relative_path(file_id, candidate) {
100} 72 let mut root_non_dir_owner = false;
101 73 let mut mod_path = RelativePathBuf::new();
102fn normalize_attribute_path(file_path: &SmolStr) -> Cow<str> { 74 if !(candidate.ends_with("mod.rs") || attr_path.is_some()) {
103 let current_dir = "./"; 75 root_non_dir_owner = true;
104 let windows_path_separator = r#"\"#; 76 mod_path.push(&name.to_string());
105 let current_dir_normalize = if file_path.starts_with(current_dir) {
106 &file_path[current_dir.len()..]
107 } else {
108 file_path.as_str()
109 };
110 if current_dir_normalize.contains(windows_path_separator) {
111 Cow::Owned(current_dir_normalize.replace(windows_path_separator, "/"))
112 } else {
113 Cow::Borrowed(current_dir_normalize)
114 }
115}
116
117enum OutOfLineMode {
118 RootOrModRs { file: RelativePathBuf, directory: RelativePathBuf },
119 FileInDirectory(RelativePathBuf),
120 WithAttributePath(RelativePathBuf),
121}
122
123impl OutOfLineMode {
124 pub fn resolve(&self, source_root: Arc<SourceRoot>) -> Result<FileId, RelativePathBuf> {
125 match self {
126 OutOfLineMode::RootOrModRs { file, directory } => {
127 match source_root.file_by_relative_path(file) {
128 None => resolve_simple_path(source_root, directory).map_err(|_| file.clone()),
129 file_id => resolve_find_result(file_id, file),
130 } 77 }
78 return Ok((file_id, ModDir { path: mod_path, root_non_dir_owner }));
131 } 79 }
132 OutOfLineMode::FileInDirectory(path) => resolve_simple_path(source_root, path),
133 OutOfLineMode::WithAttributePath(path) => resolve_simple_path(source_root, path),
134 } 80 }
81 Err(candidate_files.remove(0))
135 } 82 }
136} 83}
137 84
138enum InsideInlineModuleMode { 85fn attr_to_path(attr: Option<&SmolStr>) -> Option<RelativePathBuf> {
139 File(RelativePathBuf), 86 attr.and_then(|it| RelativePathBuf::from_path(&it.replace("\\", "/")).ok())
140 WithAttributePath(RelativePathBuf),
141}
142
143impl InsideInlineModuleMode {
144 pub fn resolve(&self, source_root: Arc<SourceRoot>) -> Result<FileId, RelativePathBuf> {
145 match self {
146 InsideInlineModuleMode::File(path) => resolve_simple_path(source_root, path),
147 InsideInlineModuleMode::WithAttributePath(path) => {
148 resolve_simple_path(source_root, path)
149 }
150 }
151 }
152}
153
154enum ResolutionMode {
155 OutOfLine(OutOfLineMode),
156 InsideInlineModule(InsideInlineModuleMode),
157 InlineModuleWithAttributePath(InsideInlineModuleMode),
158}
159
160impl ResolutionMode {
161 pub fn resolve(&self, source_root: Arc<SourceRoot>) -> Result<FileId, RelativePathBuf> {
162 use self::ResolutionMode::*;
163
164 match self {
165 OutOfLine(mode) => mode.resolve(source_root),
166 InsideInlineModule(mode) => mode.resolve(source_root),
167 InlineModuleWithAttributePath(mode) => mode.resolve(source_root),
168 }
169 }
170}
171
172fn resolve_simple_path(
173 source_root: Arc<SourceRoot>,
174 path: &RelativePathBuf,
175) -> Result<FileId, RelativePathBuf> {
176 resolve_find_result(source_root.file_by_relative_path(path), path)
177}
178
179fn resolve_find_result(
180 file_id: Option<FileId>,
181 path: &RelativePathBuf,
182) -> Result<FileId, RelativePathBuf> {
183 match file_id {
184 Some(file_id) => Ok(file_id.clone()),
185 None => Err(path.clone()),
186 }
187} 87}
diff --git a/crates/ra_hir/src/nameres/raw.rs b/crates/ra_hir/src/nameres/raw.rs
index 623b343c4..57f2929c3 100644
--- a/crates/ra_hir/src/nameres/raw.rs
+++ b/crates/ra_hir/src/nameres/raw.rs
@@ -5,7 +5,7 @@ use std::{ops::Index, sync::Arc};
5use ra_arena::{impl_arena_id, map::ArenaMap, Arena, RawId}; 5use ra_arena::{impl_arena_id, map::ArenaMap, Arena, RawId};
6use ra_syntax::{ 6use ra_syntax::{
7 ast::{self, AttrsOwner, NameOwner}, 7 ast::{self, AttrsOwner, NameOwner},
8 AstNode, AstPtr, SmolStr, SourceFile, 8 AstNode, AstPtr, SourceFile,
9}; 9};
10use test_utils::tested_by; 10use test_utils::tested_by;
11 11
@@ -121,14 +121,20 @@ impl Index<Macro> for RawItems {
121} 121}
122 122
123// Avoid heap allocation on items without attributes. 123// Avoid heap allocation on items without attributes.
124pub(super) type Attrs = Option<Arc<[Attr]>>; 124type Attrs = Option<Arc<[Attr]>>;
125 125
126#[derive(Debug, PartialEq, Eq, Clone)] 126#[derive(Debug, PartialEq, Eq, Clone)]
127pub(super) struct RawItem { 127pub(super) struct RawItem {
128 pub(super) attrs: Attrs, 128 attrs: Attrs,
129 pub(super) kind: RawItemKind, 129 pub(super) kind: RawItemKind,
130} 130}
131 131
132impl RawItem {
133 pub(super) fn attrs(&self) -> &[Attr] {
134 self.attrs.as_ref().map_or(&[], |it| &*it)
135 }
136}
137
132#[derive(Debug, PartialEq, Eq, Clone, Copy)] 138#[derive(Debug, PartialEq, Eq, Clone, Copy)]
133pub(super) enum RawItemKind { 139pub(super) enum RawItemKind {
134 Module(Module), 140 Module(Module),
@@ -143,19 +149,8 @@ impl_arena_id!(Module);
143 149
144#[derive(Debug, PartialEq, Eq)] 150#[derive(Debug, PartialEq, Eq)]
145pub(super) enum ModuleData { 151pub(super) enum ModuleData {
146 Declaration { 152 Declaration { name: Name, ast_id: FileAstId<ast::Module> },
147 name: Name, 153 Definition { name: Name, ast_id: FileAstId<ast::Module>, items: Vec<RawItem> },
148 ast_id: FileAstId<ast::Module>,
149 attr_path: Option<SmolStr>,
150 is_macro_use: bool,
151 },
152 Definition {
153 name: Name,
154 ast_id: FileAstId<ast::Module>,
155 items: Vec<RawItem>,
156 attr_path: Option<SmolStr>,
157 is_macro_use: bool,
158 },
159} 154}
160 155
161#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 156#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -286,28 +281,17 @@ impl<DB: AstDatabase> RawItemsCollector<&DB> {
286 let attrs = self.parse_attrs(&module); 281 let attrs = self.parse_attrs(&module);
287 282
288 let ast_id = self.source_ast_id_map.ast_id(&module); 283 let ast_id = self.source_ast_id_map.ast_id(&module);
289 // FIXME: cfg_attr
290 let is_macro_use = module.has_atom_attr("macro_use");
291 if module.has_semi() { 284 if module.has_semi() {
292 let attr_path = extract_mod_path_attribute(&module); 285 let item = self.raw_items.modules.alloc(ModuleData::Declaration { name, ast_id });
293 let item = self.raw_items.modules.alloc(ModuleData::Declaration {
294 name,
295 ast_id,
296 attr_path,
297 is_macro_use,
298 });
299 self.push_item(current_module, attrs, RawItemKind::Module(item)); 286 self.push_item(current_module, attrs, RawItemKind::Module(item));
300 return; 287 return;
301 } 288 }
302 289
303 if let Some(item_list) = module.item_list() { 290 if let Some(item_list) = module.item_list() {
304 let attr_path = extract_mod_path_attribute(&module);
305 let item = self.raw_items.modules.alloc(ModuleData::Definition { 291 let item = self.raw_items.modules.alloc(ModuleData::Definition {
306 name, 292 name,
307 ast_id, 293 ast_id,
308 items: Vec::new(), 294 items: Vec::new(),
309 attr_path,
310 is_macro_use,
311 }); 295 });
312 self.process_module(Some(item), item_list); 296 self.process_module(Some(item), item_list);
313 self.push_item(current_module, attrs, RawItemKind::Module(item)); 297 self.push_item(current_module, attrs, RawItemKind::Module(item));
@@ -417,16 +401,3 @@ impl<DB: AstDatabase> RawItemsCollector<&DB> {
417 Attr::from_attrs_owner(self.file_id, item, self.db) 401 Attr::from_attrs_owner(self.file_id, item, self.db)
418 } 402 }
419} 403}
420
421fn extract_mod_path_attribute(module: &ast::Module) -> Option<SmolStr> {
422 module.attrs().into_iter().find_map(|attr| {
423 attr.as_simple_key_value().and_then(|(name, value)| {
424 let is_path = name == "path";
425 if is_path {
426 Some(value)
427 } else {
428 None
429 }
430 })
431 })
432}
diff --git a/crates/ra_hir/src/nameres/tests.rs b/crates/ra_hir/src/nameres/tests.rs
index 34dd79574..8c6b40aaf 100644
--- a/crates/ra_hir/src/nameres/tests.rs
+++ b/crates/ra_hir/src/nameres/tests.rs
@@ -7,7 +7,6 @@ mod mod_resolution;
7use std::sync::Arc; 7use std::sync::Arc;
8 8
9use insta::assert_snapshot; 9use insta::assert_snapshot;
10use ra_cfg::CfgOptions;
11use ra_db::SourceDatabase; 10use ra_db::SourceDatabase;
12use test_utils::covers; 11use test_utils::covers;
13 12
@@ -561,12 +560,12 @@ fn cfg_test() {
561 "#, 560 "#,
562 crate_graph! { 561 crate_graph! {
563 "main": ("/main.rs", ["std"]), 562 "main": ("/main.rs", ["std"]),
564 "std": ("/lib.rs", [], CfgOptions::default() 563 "std": ("/lib.rs", [], cfg = {
565 .atom("test".into()) 564 "test",
566 .key_value("feature".into(), "foo".into()) 565 "feature" = "foo",
567 .key_value("feature".into(), "bar".into()) 566 "feature" = "bar",
568 .key_value("opt".into(), "42".into()) 567 "opt" = "42",
569 ), 568 }),
570 }, 569 },
571 ); 570 );
572 571
diff --git a/crates/ra_hir/src/nameres/tests/incremental.rs b/crates/ra_hir/src/nameres/tests/incremental.rs
index c41862a0b..af9c39760 100644
--- a/crates/ra_hir/src/nameres/tests/incremental.rs
+++ b/crates/ra_hir/src/nameres/tests/incremental.rs
@@ -2,7 +2,7 @@ use super::*;
2 2
3use std::sync::Arc; 3use std::sync::Arc;
4 4
5use ra_db::SourceDatabase; 5use ra_db::{SourceDatabase, SourceDatabaseExt};
6 6
7fn check_def_map_is_not_recomputed(initial: &str, file_change: &str) { 7fn check_def_map_is_not_recomputed(initial: &str, file_change: &str) {
8 let (mut db, pos) = MockDatabase::with_position(initial); 8 let (mut db, pos) = MockDatabase::with_position(initial);
diff --git a/crates/ra_hir/src/nameres/tests/macros.rs b/crates/ra_hir/src/nameres/tests/macros.rs
index e4b408394..4f52ad2c5 100644
--- a/crates/ra_hir/src/nameres/tests/macros.rs
+++ b/crates/ra_hir/src/nameres/tests/macros.rs
@@ -38,21 +38,34 @@ fn macro_rules_can_define_modules() {
38 } 38 }
39 m!(n1); 39 m!(n1);
40 40
41 mod m {
42 m!(n3)
43 }
44
41 //- /n1.rs 45 //- /n1.rs
42 m!(n2) 46 m!(n2)
43 //- /n1/n2.rs 47 //- /n1/n2.rs
44 struct X; 48 struct X;
49 //- /m/n3.rs
50 struct Y;
45 ", 51 ",
46 ); 52 );
47 assert_snapshot!(map, @r###" 53 assert_snapshot!(map, @r###"
48 â‹®crate 54 crate
49 â‹®n1: t 55 m: t
50 â‹® 56 n1: t
51 â‹®crate::n1 57
52 â‹®n2: t 58 crate::m
53 â‹® 59 n3: t
54 â‹®crate::n1::n2 60
55 â‹®X: t v 61 crate::m::n3
62 Y: t v
63
64 crate::n1
65 n2: t
66
67 crate::n1::n2
68 X: t v
56 "###); 69 "###);
57} 70}
58 71
diff --git a/crates/ra_hir/src/nameres/tests/mod_resolution.rs b/crates/ra_hir/src/nameres/tests/mod_resolution.rs
index e3e6f1e95..f569aacdc 100644
--- a/crates/ra_hir/src/nameres/tests/mod_resolution.rs
+++ b/crates/ra_hir/src/nameres/tests/mod_resolution.rs
@@ -26,6 +26,33 @@ fn name_res_works_for_broken_modules() {
26} 26}
27 27
28#[test] 28#[test]
29fn nested_module_resolution() {
30 let map = def_map(
31 "
32 //- /lib.rs
33 mod n1;
34
35 //- /n1.rs
36 mod n2;
37
38 //- /n1/n2.rs
39 struct X;
40 ",
41 );
42
43 assert_snapshot!(map, @r###"
44 â‹®crate
45 â‹®n1: t
46 â‹®
47 â‹®crate::n1
48 â‹®n2: t
49 â‹®
50 â‹®crate::n1::n2
51 â‹®X: t v
52 "###);
53}
54
55#[test]
29fn module_resolution_works_for_non_standard_filenames() { 56fn module_resolution_works_for_non_standard_filenames() {
30 let map = def_map_with_crate_graph( 57 let map = def_map_with_crate_graph(
31 " 58 "
@@ -53,18 +80,15 @@ fn module_resolution_works_for_non_standard_filenames() {
53 80
54#[test] 81#[test]
55fn module_resolution_works_for_raw_modules() { 82fn module_resolution_works_for_raw_modules() {
56 let map = def_map_with_crate_graph( 83 let map = def_map(
57 " 84 "
58 //- /library.rs 85 //- /lib.rs
59 mod r#async; 86 mod r#async;
60 use self::r#async::Bar; 87 use self::r#async::Bar;
61 88
62 //- /async.rs 89 //- /async.rs
63 pub struct Bar; 90 pub struct Bar;
64 ", 91 ",
65 crate_graph! {
66 "library": ("/library.rs", []),
67 },
68 ); 92 );
69 93
70 assert_snapshot!(map, @r###" 94 assert_snapshot!(map, @r###"
@@ -79,9 +103,9 @@ fn module_resolution_works_for_raw_modules() {
79 103
80#[test] 104#[test]
81fn module_resolution_decl_path() { 105fn module_resolution_decl_path() {
82 let map = def_map_with_crate_graph( 106 let map = def_map(
83 r###" 107 r###"
84 //- /library.rs 108 //- /lib.rs
85 #[path = "bar/baz/foo.rs"] 109 #[path = "bar/baz/foo.rs"]
86 mod foo; 110 mod foo;
87 use self::foo::Bar; 111 use self::foo::Bar;
@@ -89,9 +113,6 @@ fn module_resolution_decl_path() {
89 //- /bar/baz/foo.rs 113 //- /bar/baz/foo.rs
90 pub struct Bar; 114 pub struct Bar;
91 "###, 115 "###,
92 crate_graph! {
93 "library": ("/library.rs", []),
94 },
95 ); 116 );
96 117
97 assert_snapshot!(map, @r###" 118 assert_snapshot!(map, @r###"
@@ -106,7 +127,7 @@ fn module_resolution_decl_path() {
106 127
107#[test] 128#[test]
108fn module_resolution_module_with_path_in_mod_rs() { 129fn module_resolution_module_with_path_in_mod_rs() {
109 let map = def_map_with_crate_graph( 130 let map = def_map(
110 r###" 131 r###"
111 //- /main.rs 132 //- /main.rs
112 mod foo; 133 mod foo;
@@ -120,9 +141,6 @@ fn module_resolution_module_with_path_in_mod_rs() {
120 //- /foo/baz.rs 141 //- /foo/baz.rs
121 pub struct Baz; 142 pub struct Baz;
122 "###, 143 "###,
123 crate_graph! {
124 "main": ("/main.rs", []),
125 },
126 ); 144 );
127 145
128 assert_snapshot!(map, @r###" 146 assert_snapshot!(map, @r###"
@@ -140,7 +158,7 @@ fn module_resolution_module_with_path_in_mod_rs() {
140 158
141#[test] 159#[test]
142fn module_resolution_module_with_path_non_crate_root() { 160fn module_resolution_module_with_path_non_crate_root() {
143 let map = def_map_with_crate_graph( 161 let map = def_map(
144 r###" 162 r###"
145 //- /main.rs 163 //- /main.rs
146 mod foo; 164 mod foo;
@@ -154,9 +172,6 @@ fn module_resolution_module_with_path_non_crate_root() {
154 //- /baz.rs 172 //- /baz.rs
155 pub struct Baz; 173 pub struct Baz;
156 "###, 174 "###,
157 crate_graph! {
158 "main": ("/main.rs", []),
159 },
160 ); 175 );
161 176
162 assert_snapshot!(map, @r###" 177 assert_snapshot!(map, @r###"
@@ -174,7 +189,7 @@ fn module_resolution_module_with_path_non_crate_root() {
174 189
175#[test] 190#[test]
176fn module_resolution_module_decl_path_super() { 191fn module_resolution_module_decl_path_super() {
177 let map = def_map_with_crate_graph( 192 let map = def_map(
178 r###" 193 r###"
179 //- /main.rs 194 //- /main.rs
180 #[path = "bar/baz/module.rs"] 195 #[path = "bar/baz/module.rs"]
@@ -184,9 +199,6 @@ fn module_resolution_module_decl_path_super() {
184 //- /bar/baz/module.rs 199 //- /bar/baz/module.rs
185 use super::Baz; 200 use super::Baz;
186 "###, 201 "###,
187 crate_graph! {
188 "main": ("/main.rs", []),
189 },
190 ); 202 );
191 203
192 assert_snapshot!(map, @r###" 204 assert_snapshot!(map, @r###"
@@ -201,7 +213,7 @@ fn module_resolution_module_decl_path_super() {
201 213
202#[test] 214#[test]
203fn module_resolution_explicit_path_mod_rs() { 215fn module_resolution_explicit_path_mod_rs() {
204 let map = def_map_with_crate_graph( 216 let map = def_map(
205 r###" 217 r###"
206 //- /main.rs 218 //- /main.rs
207 #[path = "module/mod.rs"] 219 #[path = "module/mod.rs"]
@@ -210,9 +222,6 @@ fn module_resolution_explicit_path_mod_rs() {
210 //- /module/mod.rs 222 //- /module/mod.rs
211 pub struct Baz; 223 pub struct Baz;
212 "###, 224 "###,
213 crate_graph! {
214 "main": ("/main.rs", []),
215 },
216 ); 225 );
217 226
218 assert_snapshot!(map, @r###" 227 assert_snapshot!(map, @r###"
@@ -226,7 +235,7 @@ fn module_resolution_explicit_path_mod_rs() {
226 235
227#[test] 236#[test]
228fn module_resolution_relative_path() { 237fn module_resolution_relative_path() {
229 let map = def_map_with_crate_graph( 238 let map = def_map(
230 r###" 239 r###"
231 //- /main.rs 240 //- /main.rs
232 mod foo; 241 mod foo;
@@ -238,9 +247,6 @@ fn module_resolution_relative_path() {
238 //- /sub.rs 247 //- /sub.rs
239 pub struct Baz; 248 pub struct Baz;
240 "###, 249 "###,
241 crate_graph! {
242 "main": ("/main.rs", []),
243 },
244 ); 250 );
245 251
246 assert_snapshot!(map, @r###" 252 assert_snapshot!(map, @r###"
@@ -257,7 +263,7 @@ fn module_resolution_relative_path() {
257 263
258#[test] 264#[test]
259fn module_resolution_relative_path_2() { 265fn module_resolution_relative_path_2() {
260 let map = def_map_with_crate_graph( 266 let map = def_map(
261 r###" 267 r###"
262 //- /main.rs 268 //- /main.rs
263 mod foo; 269 mod foo;
@@ -269,9 +275,6 @@ fn module_resolution_relative_path_2() {
269 //- /sub.rs 275 //- /sub.rs
270 pub struct Baz; 276 pub struct Baz;
271 "###, 277 "###,
272 crate_graph! {
273 "main": ("/main.rs", []),
274 },
275 ); 278 );
276 279
277 assert_snapshot!(map, @r###" 280 assert_snapshot!(map, @r###"
@@ -288,7 +291,7 @@ fn module_resolution_relative_path_2() {
288 291
289#[test] 292#[test]
290fn module_resolution_explicit_path_mod_rs_2() { 293fn module_resolution_explicit_path_mod_rs_2() {
291 let map = def_map_with_crate_graph( 294 let map = def_map(
292 r###" 295 r###"
293 //- /main.rs 296 //- /main.rs
294 #[path = "module/bar/mod.rs"] 297 #[path = "module/bar/mod.rs"]
@@ -297,9 +300,6 @@ fn module_resolution_explicit_path_mod_rs_2() {
297 //- /module/bar/mod.rs 300 //- /module/bar/mod.rs
298 pub struct Baz; 301 pub struct Baz;
299 "###, 302 "###,
300 crate_graph! {
301 "main": ("/main.rs", []),
302 },
303 ); 303 );
304 304
305 assert_snapshot!(map, @r###" 305 assert_snapshot!(map, @r###"
@@ -313,7 +313,7 @@ fn module_resolution_explicit_path_mod_rs_2() {
313 313
314#[test] 314#[test]
315fn module_resolution_explicit_path_mod_rs_with_win_separator() { 315fn module_resolution_explicit_path_mod_rs_with_win_separator() {
316 let map = def_map_with_crate_graph( 316 let map = def_map(
317 r###" 317 r###"
318 //- /main.rs 318 //- /main.rs
319 #[path = "module\bar\mod.rs"] 319 #[path = "module\bar\mod.rs"]
@@ -322,9 +322,6 @@ fn module_resolution_explicit_path_mod_rs_with_win_separator() {
322 //- /module/bar/mod.rs 322 //- /module/bar/mod.rs
323 pub struct Baz; 323 pub struct Baz;
324 "###, 324 "###,
325 crate_graph! {
326 "main": ("/main.rs", []),
327 },
328 ); 325 );
329 326
330 assert_snapshot!(map, @r###" 327 assert_snapshot!(map, @r###"
@@ -338,7 +335,7 @@ fn module_resolution_explicit_path_mod_rs_with_win_separator() {
338 335
339#[test] 336#[test]
340fn module_resolution_decl_inside_inline_module_with_path_attribute() { 337fn module_resolution_decl_inside_inline_module_with_path_attribute() {
341 let map = def_map_with_crate_graph( 338 let map = def_map(
342 r###" 339 r###"
343 //- /main.rs 340 //- /main.rs
344 #[path = "models"] 341 #[path = "models"]
@@ -349,9 +346,6 @@ fn module_resolution_decl_inside_inline_module_with_path_attribute() {
349 //- /models/bar.rs 346 //- /models/bar.rs
350 pub struct Baz; 347 pub struct Baz;
351 "###, 348 "###,
352 crate_graph! {
353 "main": ("/main.rs", []),
354 },
355 ); 349 );
356 350
357 assert_snapshot!(map, @r###" 351 assert_snapshot!(map, @r###"
@@ -368,7 +362,7 @@ fn module_resolution_decl_inside_inline_module_with_path_attribute() {
368 362
369#[test] 363#[test]
370fn module_resolution_decl_inside_inline_module() { 364fn module_resolution_decl_inside_inline_module() {
371 let map = def_map_with_crate_graph( 365 let map = def_map(
372 r###" 366 r###"
373 //- /main.rs 367 //- /main.rs
374 mod foo { 368 mod foo {
@@ -378,9 +372,6 @@ fn module_resolution_decl_inside_inline_module() {
378 //- /foo/bar.rs 372 //- /foo/bar.rs
379 pub struct Baz; 373 pub struct Baz;
380 "###, 374 "###,
381 crate_graph! {
382 "main": ("/main.rs", []),
383 },
384 ); 375 );
385 376
386 assert_snapshot!(map, @r###" 377 assert_snapshot!(map, @r###"
@@ -397,7 +388,7 @@ fn module_resolution_decl_inside_inline_module() {
397 388
398#[test] 389#[test]
399fn module_resolution_decl_inside_inline_module_2_with_path_attribute() { 390fn module_resolution_decl_inside_inline_module_2_with_path_attribute() {
400 let map = def_map_with_crate_graph( 391 let map = def_map(
401 r###" 392 r###"
402 //- /main.rs 393 //- /main.rs
403 #[path = "models/db"] 394 #[path = "models/db"]
@@ -408,9 +399,6 @@ fn module_resolution_decl_inside_inline_module_2_with_path_attribute() {
408 //- /models/db/bar.rs 399 //- /models/db/bar.rs
409 pub struct Baz; 400 pub struct Baz;
410 "###, 401 "###,
411 crate_graph! {
412 "main": ("/main.rs", []),
413 },
414 ); 402 );
415 403
416 assert_snapshot!(map, @r###" 404 assert_snapshot!(map, @r###"
@@ -427,7 +415,7 @@ fn module_resolution_decl_inside_inline_module_2_with_path_attribute() {
427 415
428#[test] 416#[test]
429fn module_resolution_decl_inside_inline_module_3() { 417fn module_resolution_decl_inside_inline_module_3() {
430 let map = def_map_with_crate_graph( 418 let map = def_map(
431 r###" 419 r###"
432 //- /main.rs 420 //- /main.rs
433 #[path = "models/db"] 421 #[path = "models/db"]
@@ -439,9 +427,6 @@ fn module_resolution_decl_inside_inline_module_3() {
439 //- /models/db/users.rs 427 //- /models/db/users.rs
440 pub struct Baz; 428 pub struct Baz;
441 "###, 429 "###,
442 crate_graph! {
443 "main": ("/main.rs", []),
444 },
445 ); 430 );
446 431
447 assert_snapshot!(map, @r###" 432 assert_snapshot!(map, @r###"
@@ -458,7 +443,7 @@ fn module_resolution_decl_inside_inline_module_3() {
458 443
459#[test] 444#[test]
460fn module_resolution_decl_inside_inline_module_empty_path() { 445fn module_resolution_decl_inside_inline_module_empty_path() {
461 let map = def_map_with_crate_graph( 446 let map = def_map(
462 r###" 447 r###"
463 //- /main.rs 448 //- /main.rs
464 #[path = ""] 449 #[path = ""]
@@ -467,12 +452,9 @@ fn module_resolution_decl_inside_inline_module_empty_path() {
467 mod bar; 452 mod bar;
468 } 453 }
469 454
470 //- /foo/users.rs 455 //- /users.rs
471 pub struct Baz; 456 pub struct Baz;
472 "###, 457 "###,
473 crate_graph! {
474 "main": ("/main.rs", []),
475 },
476 ); 458 );
477 459
478 assert_snapshot!(map, @r###" 460 assert_snapshot!(map, @r###"
@@ -489,32 +471,25 @@ fn module_resolution_decl_inside_inline_module_empty_path() {
489 471
490#[test] 472#[test]
491fn module_resolution_decl_empty_path() { 473fn module_resolution_decl_empty_path() {
492 let map = def_map_with_crate_graph( 474 let map = def_map(
493 r###" 475 r###"
494 //- /main.rs 476 //- /main.rs
495 #[path = ""] 477 #[path = ""] // Should try to read `/` (a directory)
496 mod foo; 478 mod foo;
497 479
498 //- /foo.rs 480 //- /foo.rs
499 pub struct Baz; 481 pub struct Baz;
500 "###, 482 "###,
501 crate_graph! {
502 "main": ("/main.rs", []),
503 },
504 ); 483 );
505 484
506 assert_snapshot!(map, @r###" 485 assert_snapshot!(map, @r###"
507 â‹®crate 486 â‹®crate
508 â‹®foo: t
509 â‹®
510 â‹®crate::foo
511 â‹®Baz: t v
512 "###); 487 "###);
513} 488}
514 489
515#[test] 490#[test]
516fn module_resolution_decl_inside_inline_module_relative_path() { 491fn module_resolution_decl_inside_inline_module_relative_path() {
517 let map = def_map_with_crate_graph( 492 let map = def_map(
518 r###" 493 r###"
519 //- /main.rs 494 //- /main.rs
520 #[path = "./models"] 495 #[path = "./models"]
@@ -525,9 +500,6 @@ fn module_resolution_decl_inside_inline_module_relative_path() {
525 //- /models/bar.rs 500 //- /models/bar.rs
526 pub struct Baz; 501 pub struct Baz;
527 "###, 502 "###,
528 crate_graph! {
529 "main": ("/main.rs", []),
530 },
531 ); 503 );
532 504
533 assert_snapshot!(map, @r###" 505 assert_snapshot!(map, @r###"
@@ -544,7 +516,7 @@ fn module_resolution_decl_inside_inline_module_relative_path() {
544 516
545#[test] 517#[test]
546fn module_resolution_decl_inside_inline_module_in_crate_root() { 518fn module_resolution_decl_inside_inline_module_in_crate_root() {
547 let map = def_map_with_crate_graph( 519 let map = def_map(
548 r###" 520 r###"
549 //- /main.rs 521 //- /main.rs
550 mod foo { 522 mod foo {
@@ -556,9 +528,6 @@ fn module_resolution_decl_inside_inline_module_in_crate_root() {
556 //- /foo/baz.rs 528 //- /foo/baz.rs
557 pub struct Baz; 529 pub struct Baz;
558 "###, 530 "###,
559 crate_graph! {
560 "main": ("/main.rs", []),
561 },
562 ); 531 );
563 532
564 assert_snapshot!(map, @r###" 533 assert_snapshot!(map, @r###"
@@ -576,7 +545,7 @@ fn module_resolution_decl_inside_inline_module_in_crate_root() {
576 545
577#[test] 546#[test]
578fn module_resolution_decl_inside_inline_module_in_mod_rs() { 547fn module_resolution_decl_inside_inline_module_in_mod_rs() {
579 let map = def_map_with_crate_graph( 548 let map = def_map(
580 r###" 549 r###"
581 //- /main.rs 550 //- /main.rs
582 mod foo; 551 mod foo;
@@ -591,9 +560,6 @@ fn module_resolution_decl_inside_inline_module_in_mod_rs() {
591 //- /foo/bar/qwe.rs 560 //- /foo/bar/qwe.rs
592 pub struct Baz; 561 pub struct Baz;
593 "###, 562 "###,
594 crate_graph! {
595 "main": ("/main.rs", []),
596 },
597 ); 563 );
598 564
599 assert_snapshot!(map, @r###" 565 assert_snapshot!(map, @r###"
@@ -614,7 +580,7 @@ fn module_resolution_decl_inside_inline_module_in_mod_rs() {
614 580
615#[test] 581#[test]
616fn module_resolution_decl_inside_inline_module_in_non_crate_root() { 582fn module_resolution_decl_inside_inline_module_in_non_crate_root() {
617 let map = def_map_with_crate_graph( 583 let map = def_map(
618 r###" 584 r###"
619 //- /main.rs 585 //- /main.rs
620 mod foo; 586 mod foo;
@@ -626,12 +592,9 @@ fn module_resolution_decl_inside_inline_module_in_non_crate_root() {
626 } 592 }
627 use self::bar::baz::Baz; 593 use self::bar::baz::Baz;
628 594
629 //- /bar/qwe.rs 595 //- /foo/bar/qwe.rs
630 pub struct Baz; 596 pub struct Baz;
631 "###, 597 "###,
632 crate_graph! {
633 "main": ("/main.rs", []),
634 },
635 ); 598 );
636 599
637 assert_snapshot!(map, @r###" 600 assert_snapshot!(map, @r###"
@@ -652,7 +615,7 @@ fn module_resolution_decl_inside_inline_module_in_non_crate_root() {
652 615
653#[test] 616#[test]
654fn module_resolution_decl_inside_inline_module_in_non_crate_root_2() { 617fn module_resolution_decl_inside_inline_module_in_non_crate_root_2() {
655 let map = def_map_with_crate_graph( 618 let map = def_map(
656 r###" 619 r###"
657 //- /main.rs 620 //- /main.rs
658 mod foo; 621 mod foo;
@@ -667,9 +630,6 @@ fn module_resolution_decl_inside_inline_module_in_non_crate_root_2() {
667 //- /bar/baz.rs 630 //- /bar/baz.rs
668 pub struct Baz; 631 pub struct Baz;
669 "###, 632 "###,
670 crate_graph! {
671 "main": ("/main.rs", []),
672 },
673 ); 633 );
674 634
675 assert_snapshot!(map, @r###" 635 assert_snapshot!(map, @r###"
@@ -709,7 +669,7 @@ fn unresolved_module_diagnostics() {
709 669
710#[test] 670#[test]
711fn module_resolution_decl_inside_module_in_non_crate_root_2() { 671fn module_resolution_decl_inside_module_in_non_crate_root_2() {
712 let map = def_map_with_crate_graph( 672 let map = def_map(
713 r###" 673 r###"
714 //- /main.rs 674 //- /main.rs
715 #[path="module/m2.rs"] 675 #[path="module/m2.rs"]
@@ -721,9 +681,6 @@ fn module_resolution_decl_inside_module_in_non_crate_root_2() {
721 //- /module/submod.rs 681 //- /module/submod.rs
722 pub struct Baz; 682 pub struct Baz;
723 "###, 683 "###,
724 crate_graph! {
725 "main": ("/main.rs", []),
726 },
727 ); 684 );
728 685
729 assert_snapshot!(map, @r###" 686 assert_snapshot!(map, @r###"
@@ -737,3 +694,66 @@ fn module_resolution_decl_inside_module_in_non_crate_root_2() {
737 â‹®Baz: t v 694 â‹®Baz: t v
738 "###); 695 "###);
739} 696}
697
698#[test]
699fn nested_out_of_line_module() {
700 let map = def_map(
701 r###"
702 //- /lib.rs
703 mod a {
704 mod b {
705 mod c;
706 }
707 }
708
709 //- /a/b/c.rs
710 struct X;
711 "###,
712 );
713
714 assert_snapshot!(map, @r###"
715 crate
716 a: t
717
718 crate::a
719 b: t
720
721 crate::a::b
722 c: t
723
724 crate::a::b::c
725 X: t v
726 "###);
727}
728
729#[test]
730fn nested_out_of_line_module_with_path() {
731 let map = def_map(
732 r###"
733 //- /lib.rs
734 mod a {
735 #[path = "d/e"]
736 mod b {
737 mod c;
738 }
739 }
740
741 //- /a/d/e/c.rs
742 struct X;
743 "###,
744 );
745
746 assert_snapshot!(map, @r###"
747 crate
748 a: t
749
750 crate::a
751 b: t
752
753 crate::a::b
754 c: t
755
756 crate::a::b::c
757 X: t v
758 "###);
759}
diff --git a/crates/ra_hir/src/resolve.rs b/crates/ra_hir/src/resolve.rs
index 39f8e1d8a..3c797c0c3 100644
--- a/crates/ra_hir/src/resolve.rs
+++ b/crates/ra_hir/src/resolve.rs
@@ -43,8 +43,10 @@ pub(crate) enum Scope {
43 ModuleScope(ModuleItemMap), 43 ModuleScope(ModuleItemMap),
44 /// Brings the generic parameters of an item into scope 44 /// Brings the generic parameters of an item into scope
45 GenericParams(Arc<GenericParams>), 45 GenericParams(Arc<GenericParams>),
46 /// Brings `Self` into scope 46 /// Brings `Self` in `impl` block into scope
47 ImplBlockScope(ImplBlock), 47 ImplBlockScope(ImplBlock),
48 /// Brings `Self` in enum, struct and union definitions into scope
49 AdtScope(Adt),
48 /// Local bindings 50 /// Local bindings
49 ExprScope(ExprScope), 51 ExprScope(ExprScope),
50} 52}
@@ -54,6 +56,7 @@ pub enum TypeNs {
54 SelfType(ImplBlock), 56 SelfType(ImplBlock),
55 GenericParam(u32), 57 GenericParam(u32),
56 Adt(Adt), 58 Adt(Adt),
59 AdtSelfType(Adt),
57 EnumVariant(EnumVariant), 60 EnumVariant(EnumVariant),
58 TypeAlias(TypeAlias), 61 TypeAlias(TypeAlias),
59 BuiltinType(BuiltinType), 62 BuiltinType(BuiltinType),
@@ -151,6 +154,12 @@ impl Resolver {
151 return Some((TypeNs::SelfType(*impl_), idx)); 154 return Some((TypeNs::SelfType(*impl_), idx));
152 } 155 }
153 } 156 }
157 Scope::AdtScope(adt) => {
158 if first_name == &SELF_TYPE {
159 let idx = if path.segments.len() == 1 { None } else { Some(1) };
160 return Some((TypeNs::AdtSelfType(*adt), idx));
161 }
162 }
154 Scope::ModuleScope(m) => { 163 Scope::ModuleScope(m) => {
155 let (module_def, idx) = m.crate_def_map.resolve_path(db, m.module_id, path); 164 let (module_def, idx) = m.crate_def_map.resolve_path(db, m.module_id, path);
156 let res = match module_def.take_types()? { 165 let res = match module_def.take_types()? {
@@ -200,7 +209,10 @@ impl Resolver {
200 let skip_to_mod = path.kind != PathKind::Plain && !path.is_self(); 209 let skip_to_mod = path.kind != PathKind::Plain && !path.is_self();
201 for scope in self.scopes.iter().rev() { 210 for scope in self.scopes.iter().rev() {
202 match scope { 211 match scope {
203 Scope::ExprScope(_) | Scope::GenericParams(_) | Scope::ImplBlockScope(_) 212 Scope::AdtScope(_)
213 | Scope::ExprScope(_)
214 | Scope::GenericParams(_)
215 | Scope::ImplBlockScope(_)
204 if skip_to_mod => 216 if skip_to_mod =>
205 { 217 {
206 continue 218 continue
@@ -233,7 +245,13 @@ impl Resolver {
233 return Some(ResolveValueResult::Partial(ty, 1)); 245 return Some(ResolveValueResult::Partial(ty, 1));
234 } 246 }
235 } 247 }
236 Scope::ImplBlockScope(_) => continue, 248 Scope::AdtScope(adt) if n_segments > 1 => {
249 if first_name == &SELF_TYPE {
250 let ty = TypeNs::AdtSelfType(*adt);
251 return Some(ResolveValueResult::Partial(ty, 1));
252 }
253 }
254 Scope::ImplBlockScope(_) | Scope::AdtScope(_) => continue,
237 255
238 Scope::ModuleScope(m) => { 256 Scope::ModuleScope(m) => {
239 let (module_def, idx) = m.crate_def_map.resolve_path(db, m.module_id, path); 257 let (module_def, idx) = m.crate_def_map.resolve_path(db, m.module_id, path);
@@ -389,7 +407,8 @@ pub enum ScopeDef {
389 ModuleDef(ModuleDef), 407 ModuleDef(ModuleDef),
390 MacroDef(MacroDef), 408 MacroDef(MacroDef),
391 GenericParam(u32), 409 GenericParam(u32),
392 SelfType(ImplBlock), 410 ImplSelfType(ImplBlock),
411 AdtSelfType(Adt),
393 LocalBinding(PatId), 412 LocalBinding(PatId),
394 Unknown, 413 Unknown,
395} 414}
@@ -437,7 +456,10 @@ impl Scope {
437 } 456 }
438 } 457 }
439 Scope::ImplBlockScope(i) => { 458 Scope::ImplBlockScope(i) => {
440 f(SELF_TYPE, ScopeDef::SelfType(*i)); 459 f(SELF_TYPE, ScopeDef::ImplSelfType(*i));
460 }
461 Scope::AdtScope(i) => {
462 f(SELF_TYPE, ScopeDef::AdtSelfType(*i));
441 } 463 }
442 Scope::ExprScope(e) => { 464 Scope::ExprScope(e) => {
443 e.expr_scopes.entries(e.scope_id).iter().for_each(|e| { 465 e.expr_scopes.entries(e.scope_id).iter().for_each(|e| {
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs
index 088335e66..a907d6a9f 100644
--- a/crates/ra_hir/src/source_binder.rs
+++ b/crates/ra_hir/src/source_binder.rs
@@ -216,7 +216,7 @@ impl SourceAnalyzer {
216 let types = self.resolver.resolve_path_in_type_ns_fully(db, &path).map(|ty| match ty { 216 let types = self.resolver.resolve_path_in_type_ns_fully(db, &path).map(|ty| match ty {
217 TypeNs::SelfType(it) => PathResolution::SelfType(it), 217 TypeNs::SelfType(it) => PathResolution::SelfType(it),
218 TypeNs::GenericParam(it) => PathResolution::GenericParam(it), 218 TypeNs::GenericParam(it) => PathResolution::GenericParam(it),
219 TypeNs::Adt(it) => PathResolution::Def(it.into()), 219 TypeNs::AdtSelfType(it) | TypeNs::Adt(it) => PathResolution::Def(it.into()),
220 TypeNs::EnumVariant(it) => PathResolution::Def(it.into()), 220 TypeNs::EnumVariant(it) => PathResolution::Def(it.into()),
221 TypeNs::TypeAlias(it) => PathResolution::Def(it.into()), 221 TypeNs::TypeAlias(it) => PathResolution::Def(it.into()),
222 TypeNs::BuiltinType(it) => PathResolution::Def(it.into()), 222 TypeNs::BuiltinType(it) => PathResolution::Def(it.into()),
diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs
index d161735e8..cc9746f6d 100644
--- a/crates/ra_hir/src/ty.rs
+++ b/crates/ra_hir/src/ty.rs
@@ -17,8 +17,8 @@ use std::sync::Arc;
17use std::{fmt, iter, mem}; 17use std::{fmt, iter, mem};
18 18
19use crate::{ 19use crate::{
20 db::HirDatabase, expr::ExprId, type_ref::Mutability, Adt, Crate, DefWithBody, GenericParams, 20 db::HirDatabase, expr::ExprId, type_ref::Mutability, util::make_mut_slice, Adt, Crate,
21 HasGenericParams, Name, Trait, TypeAlias, 21 DefWithBody, GenericParams, HasGenericParams, Name, Trait, TypeAlias,
22}; 22};
23use display::{HirDisplay, HirFormatter}; 23use display::{HirDisplay, HirFormatter};
24 24
@@ -308,12 +308,9 @@ impl Substs {
308 } 308 }
309 309
310 pub fn walk_mut(&mut self, f: &mut impl FnMut(&mut Ty)) { 310 pub fn walk_mut(&mut self, f: &mut impl FnMut(&mut Ty)) {
311 // Without an Arc::make_mut_slice, we can't avoid the clone here: 311 for t in make_mut_slice(&mut self.0) {
312 let mut v: Vec<_> = self.0.iter().cloned().collect();
313 for t in &mut v {
314 t.walk_mut(f); 312 t.walk_mut(f);
315 } 313 }
316 self.0 = v.into();
317 } 314 }
318 315
319 pub fn as_single(&self) -> &Ty { 316 pub fn as_single(&self) -> &Ty {
@@ -330,8 +327,7 @@ impl Substs {
330 .params_including_parent() 327 .params_including_parent()
331 .into_iter() 328 .into_iter()
332 .map(|p| Ty::Param { idx: p.idx, name: p.name.clone() }) 329 .map(|p| Ty::Param { idx: p.idx, name: p.name.clone() })
333 .collect::<Vec<_>>() 330 .collect(),
334 .into(),
335 ) 331 )
336 } 332 }
337 333
@@ -342,8 +338,7 @@ impl Substs {
342 .params_including_parent() 338 .params_including_parent()
343 .into_iter() 339 .into_iter()
344 .map(|p| Ty::Bound(p.idx)) 340 .map(|p| Ty::Bound(p.idx))
345 .collect::<Vec<_>>() 341 .collect(),
346 .into(),
347 ) 342 )
348 } 343 }
349 344
@@ -541,12 +536,9 @@ impl TypeWalk for FnSig {
541 } 536 }
542 537
543 fn walk_mut(&mut self, f: &mut impl FnMut(&mut Ty)) { 538 fn walk_mut(&mut self, f: &mut impl FnMut(&mut Ty)) {
544 // Without an Arc::make_mut_slice, we can't avoid the clone here: 539 for t in make_mut_slice(&mut self.params_and_return) {
545 let mut v: Vec<_> = self.params_and_return.iter().cloned().collect();
546 for t in &mut v {
547 t.walk_mut(f); 540 t.walk_mut(f);
548 } 541 }
549 self.params_and_return = v.into();
550 } 542 }
551} 543}
552 544
@@ -756,11 +748,9 @@ impl TypeWalk for Ty {
756 p_ty.parameters.walk_mut(f); 748 p_ty.parameters.walk_mut(f);
757 } 749 }
758 Ty::Dyn(predicates) | Ty::Opaque(predicates) => { 750 Ty::Dyn(predicates) | Ty::Opaque(predicates) => {
759 let mut v: Vec<_> = predicates.iter().cloned().collect(); 751 for p in make_mut_slice(predicates) {
760 for p in &mut v {
761 p.walk_mut(f); 752 p.walk_mut(f);
762 } 753 }
763 *predicates = v.into();
764 } 754 }
765 Ty::Param { .. } | Ty::Bound(_) | Ty::Infer(_) | Ty::Unknown => {} 755 Ty::Param { .. } | Ty::Bound(_) | Ty::Infer(_) | Ty::Unknown => {}
766 } 756 }
diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs
index ca9aefc42..ebaff998e 100644
--- a/crates/ra_hir/src/ty/infer.rs
+++ b/crates/ra_hir/src/ty/infer.rs
@@ -14,7 +14,6 @@
14//! the `ena` crate, which is extracted from rustc. 14//! the `ena` crate, which is extracted from rustc.
15 15
16use std::borrow::Cow; 16use std::borrow::Cow;
17use std::iter::{repeat, repeat_with};
18use std::mem; 17use std::mem;
19use std::ops::Index; 18use std::ops::Index;
20use std::sync::Arc; 19use std::sync::Arc;
@@ -27,33 +26,39 @@ use ra_prof::profile;
27use test_utils::tested_by; 26use test_utils::tested_by;
28 27
29use super::{ 28use super::{
30 autoderef, lower, method_resolution, op, primitive, 29 lower, primitive,
31 traits::{Guidance, Obligation, ProjectionPredicate, Solution}, 30 traits::{Guidance, Obligation, ProjectionPredicate, Solution},
32 ApplicationTy, CallableDef, InEnvironment, ProjectionTy, Substs, TraitEnvironment, TraitRef, 31 ApplicationTy, InEnvironment, ProjectionTy, Substs, TraitEnvironment, TraitRef, Ty, TypableDef,
33 Ty, TypableDef, TypeCtor, TypeWalk, 32 TypeCtor, TypeWalk,
34}; 33};
35use crate::{ 34use crate::{
36 adt::VariantDef, 35 adt::VariantDef,
37 code_model::TypeAlias, 36 code_model::TypeAlias,
38 db::HirDatabase, 37 db::HirDatabase,
39 diagnostics::DiagnosticSink, 38 diagnostics::DiagnosticSink,
40 expr::{ 39 expr::{BindingAnnotation, Body, ExprId, PatId},
41 self, Array, BinaryOp, BindingAnnotation, Body, Expr, ExprId, Literal, Pat, PatId,
42 RecordFieldPat, Statement, UnaryOp,
43 },
44 generics::{GenericParams, HasGenericParams},
45 lang_item::LangItemTarget,
46 name, 40 name,
47 nameres::Namespace, 41 path::known,
48 path::{known, GenericArg, GenericArgs},
49 resolve::{Resolver, TypeNs}, 42 resolve::{Resolver, TypeNs},
50 ty::infer::diagnostics::InferenceDiagnostic, 43 ty::infer::diagnostics::InferenceDiagnostic,
51 type_ref::{Mutability, TypeRef}, 44 type_ref::{Mutability, TypeRef},
52 Adt, AssocItem, ConstData, DefWithBody, FnData, Function, HasBody, Name, Path, StructField, 45 Adt, AssocItem, ConstData, DefWithBody, FnData, Function, HasBody, Path, StructField,
53}; 46};
54 47
48macro_rules! ty_app {
49 ($ctor:pat, $param:pat) => {
50 crate::ty::Ty::Apply(crate::ty::ApplicationTy { ctor: $ctor, parameters: $param })
51 };
52 ($ctor:pat) => {
53 ty_app!($ctor, _)
54 };
55}
56
55mod unify; 57mod unify;
56mod path; 58mod path;
59mod expr;
60mod pat;
61mod coerce;
57 62
58/// The entry point of type inference. 63/// The entry point of type inference.
59pub fn infer_query(db: &impl HirDatabase, def: DefWithBody) -> Arc<InferenceResult> { 64pub fn infer_query(db: &impl HirDatabase, def: DefWithBody) -> Arc<InferenceResult> {
@@ -197,15 +202,6 @@ struct InferenceContext<'a, D: HirDatabase> {
197 coerce_unsized_map: FxHashMap<(TypeCtor, TypeCtor), usize>, 202 coerce_unsized_map: FxHashMap<(TypeCtor, TypeCtor), usize>,
198} 203}
199 204
200macro_rules! ty_app {
201 ($ctor:pat, $param:pat) => {
202 Ty::Apply(ApplicationTy { ctor: $ctor, parameters: $param })
203 };
204 ($ctor:pat) => {
205 ty_app!($ctor, _)
206 };
207}
208
209impl<'a, D: HirDatabase> InferenceContext<'a, D> { 205impl<'a, D: HirDatabase> InferenceContext<'a, D> {
210 fn new(db: &'a D, body: Arc<Body>, resolver: Resolver) -> Self { 206 fn new(db: &'a D, body: Arc<Body>, resolver: Resolver) -> Self {
211 InferenceContext { 207 InferenceContext {
@@ -221,45 +217,6 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
221 } 217 }
222 } 218 }
223 219
224 fn init_coerce_unsized_map(
225 db: &'a D,
226 resolver: &Resolver,
227 ) -> FxHashMap<(TypeCtor, TypeCtor), usize> {
228 let krate = resolver.krate().unwrap();
229 let impls = match db.lang_item(krate, "coerce_unsized".into()) {
230 Some(LangItemTarget::Trait(trait_)) => db.impls_for_trait(krate, trait_),
231 _ => return FxHashMap::default(),
232 };
233
234 impls
235 .iter()
236 .filter_map(|impl_block| {
237 // `CoerseUnsized` has one generic parameter for the target type.
238 let trait_ref = impl_block.target_trait_ref(db)?;
239 let cur_from_ty = trait_ref.substs.0.get(0)?;
240 let cur_to_ty = trait_ref.substs.0.get(1)?;
241
242 match (&cur_from_ty, cur_to_ty) {
243 (ty_app!(ctor1, st1), ty_app!(ctor2, st2)) => {
244 // FIXME: We return the first non-equal bound as the type parameter to coerce to unsized type.
245 // This works for smart-pointer-like coercion, which covers all impls from std.
246 st1.iter().zip(st2.iter()).enumerate().find_map(|(i, (ty1, ty2))| {
247 match (ty1, ty2) {
248 (Ty::Param { idx: p1, .. }, Ty::Param { idx: p2, .. })
249 if p1 != p2 =>
250 {
251 Some(((*ctor1, *ctor2), i))
252 }
253 _ => None,
254 }
255 })
256 }
257 _ => None,
258 }
259 })
260 .collect()
261 }
262
263 fn resolve_all(mut self) -> InferenceResult { 220 fn resolve_all(mut self) -> InferenceResult {
264 // FIXME resolve obligations as well (use Guidance if necessary) 221 // FIXME resolve obligations as well (use Guidance if necessary)
265 let mut result = mem::replace(&mut self.result, InferenceResult::default()); 222 let mut result = mem::replace(&mut self.result, InferenceResult::default());
@@ -457,7 +414,9 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
457 // recursive type 414 // recursive type
458 return tv.fallback_value(); 415 return tv.fallback_value();
459 } 416 }
460 if let Some(known_ty) = self.var_unification_table.probe_value(inner).known() { 417 if let Some(known_ty) =
418 self.var_unification_table.inlined_probe_value(inner).known()
419 {
461 // known_ty may contain other variables that are known by now 420 // known_ty may contain other variables that are known by now
462 tv_stack.push(inner); 421 tv_stack.push(inner);
463 let result = self.resolve_ty_as_possible(tv_stack, known_ty.clone()); 422 let result = self.resolve_ty_as_possible(tv_stack, known_ty.clone());
@@ -485,7 +444,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
485 match &*ty { 444 match &*ty {
486 Ty::Infer(tv) => { 445 Ty::Infer(tv) => {
487 let inner = tv.to_inner(); 446 let inner = tv.to_inner();
488 match self.var_unification_table.probe_value(inner).known() { 447 match self.var_unification_table.inlined_probe_value(inner).known() {
489 Some(known_ty) => { 448 Some(known_ty) => {
490 // The known_ty can't be a type var itself 449 // The known_ty can't be a type var itself
491 ty = Cow::Owned(known_ty.clone()); 450 ty = Cow::Owned(known_ty.clone());
@@ -533,7 +492,9 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
533 // recursive type 492 // recursive type
534 return tv.fallback_value(); 493 return tv.fallback_value();
535 } 494 }
536 if let Some(known_ty) = self.var_unification_table.probe_value(inner).known() { 495 if let Some(known_ty) =
496 self.var_unification_table.inlined_probe_value(inner).known()
497 {
537 // known_ty may contain other variables that are known by now 498 // known_ty may contain other variables that are known by now
538 tv_stack.push(inner); 499 tv_stack.push(inner);
539 let result = self.resolve_ty_completely(tv_stack, known_ty.clone()); 500 let result = self.resolve_ty_completely(tv_stack, known_ty.clone());
@@ -559,6 +520,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
559 match resolver.resolve_path_in_type_ns_fully(self.db, &path) { 520 match resolver.resolve_path_in_type_ns_fully(self.db, &path) {
560 Some(TypeNs::Adt(Adt::Struct(it))) => it.into(), 521 Some(TypeNs::Adt(Adt::Struct(it))) => it.into(),
561 Some(TypeNs::Adt(Adt::Union(it))) => it.into(), 522 Some(TypeNs::Adt(Adt::Union(it))) => it.into(),
523 Some(TypeNs::AdtSelfType(adt)) => adt.into(),
562 Some(TypeNs::EnumVariant(it)) => it.into(), 524 Some(TypeNs::EnumVariant(it)) => it.into(),
563 Some(TypeNs::TypeAlias(it)) => it.into(), 525 Some(TypeNs::TypeAlias(it)) => it.into(),
564 526
@@ -594,1080 +556,6 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
594 } 556 }
595 } 557 }
596 558
597 fn infer_tuple_struct_pat(
598 &mut self,
599 path: Option<&Path>,
600 subpats: &[PatId],
601 expected: &Ty,
602 default_bm: BindingMode,
603 ) -> Ty {
604 let (ty, def) = self.resolve_variant(path);
605
606 self.unify(&ty, expected);
607
608 let substs = ty.substs().unwrap_or_else(Substs::empty);
609
610 for (i, &subpat) in subpats.iter().enumerate() {
611 let expected_ty = def
612 .and_then(|d| d.field(self.db, &Name::new_tuple_field(i)))
613 .map_or(Ty::Unknown, |field| field.ty(self.db))
614 .subst(&substs);
615 let expected_ty = self.normalize_associated_types_in(expected_ty);
616 self.infer_pat(subpat, &expected_ty, default_bm);
617 }
618
619 ty
620 }
621
622 fn infer_record_pat(
623 &mut self,
624 path: Option<&Path>,
625 subpats: &[RecordFieldPat],
626 expected: &Ty,
627 default_bm: BindingMode,
628 id: PatId,
629 ) -> Ty {
630 let (ty, def) = self.resolve_variant(path);
631 if let Some(variant) = def {
632 self.write_variant_resolution(id.into(), variant);
633 }
634
635 self.unify(&ty, expected);
636
637 let substs = ty.substs().unwrap_or_else(Substs::empty);
638
639 for subpat in subpats {
640 let matching_field = def.and_then(|it| it.field(self.db, &subpat.name));
641 let expected_ty =
642 matching_field.map_or(Ty::Unknown, |field| field.ty(self.db)).subst(&substs);
643 let expected_ty = self.normalize_associated_types_in(expected_ty);
644 self.infer_pat(subpat.pat, &expected_ty, default_bm);
645 }
646
647 ty
648 }
649
650 fn infer_pat(&mut self, pat: PatId, mut expected: &Ty, mut default_bm: BindingMode) -> Ty {
651 let body = Arc::clone(&self.body); // avoid borrow checker problem
652
653 let is_non_ref_pat = match &body[pat] {
654 Pat::Tuple(..)
655 | Pat::TupleStruct { .. }
656 | Pat::Record { .. }
657 | Pat::Range { .. }
658 | Pat::Slice { .. } => true,
659 // FIXME: Path/Lit might actually evaluate to ref, but inference is unimplemented.
660 Pat::Path(..) | Pat::Lit(..) => true,
661 Pat::Wild | Pat::Bind { .. } | Pat::Ref { .. } | Pat::Missing => false,
662 };
663 if is_non_ref_pat {
664 while let Some((inner, mutability)) = expected.as_reference() {
665 expected = inner;
666 default_bm = match default_bm {
667 BindingMode::Move => BindingMode::Ref(mutability),
668 BindingMode::Ref(Mutability::Shared) => BindingMode::Ref(Mutability::Shared),
669 BindingMode::Ref(Mutability::Mut) => BindingMode::Ref(mutability),
670 }
671 }
672 } else if let Pat::Ref { .. } = &body[pat] {
673 tested_by!(match_ergonomics_ref);
674 // When you encounter a `&pat` pattern, reset to Move.
675 // This is so that `w` is by value: `let (_, &w) = &(1, &2);`
676 default_bm = BindingMode::Move;
677 }
678
679 // Lose mutability.
680 let default_bm = default_bm;
681 let expected = expected;
682
683 let ty = match &body[pat] {
684 Pat::Tuple(ref args) => {
685 let expectations = match expected.as_tuple() {
686 Some(parameters) => &*parameters.0,
687 _ => &[],
688 };
689 let expectations_iter = expectations.iter().chain(repeat(&Ty::Unknown));
690
691 let inner_tys = args
692 .iter()
693 .zip(expectations_iter)
694 .map(|(&pat, ty)| self.infer_pat(pat, ty, default_bm))
695 .collect();
696
697 Ty::apply(TypeCtor::Tuple { cardinality: args.len() as u16 }, Substs(inner_tys))
698 }
699 Pat::Ref { pat, mutability } => {
700 let expectation = match expected.as_reference() {
701 Some((inner_ty, exp_mut)) => {
702 if *mutability != exp_mut {
703 // FIXME: emit type error?
704 }
705 inner_ty
706 }
707 _ => &Ty::Unknown,
708 };
709 let subty = self.infer_pat(*pat, expectation, default_bm);
710 Ty::apply_one(TypeCtor::Ref(*mutability), subty)
711 }
712 Pat::TupleStruct { path: p, args: subpats } => {
713 self.infer_tuple_struct_pat(p.as_ref(), subpats, expected, default_bm)
714 }
715 Pat::Record { path: p, args: fields } => {
716 self.infer_record_pat(p.as_ref(), fields, expected, default_bm, pat)
717 }
718 Pat::Path(path) => {
719 // FIXME use correct resolver for the surrounding expression
720 let resolver = self.resolver.clone();
721 self.infer_path(&resolver, &path, pat.into()).unwrap_or(Ty::Unknown)
722 }
723 Pat::Bind { mode, name: _, subpat } => {
724 let mode = if mode == &BindingAnnotation::Unannotated {
725 default_bm
726 } else {
727 BindingMode::convert(*mode)
728 };
729 let inner_ty = if let Some(subpat) = subpat {
730 self.infer_pat(*subpat, expected, default_bm)
731 } else {
732 expected.clone()
733 };
734 let inner_ty = self.insert_type_vars_shallow(inner_ty);
735
736 let bound_ty = match mode {
737 BindingMode::Ref(mutability) => {
738 Ty::apply_one(TypeCtor::Ref(mutability), inner_ty.clone())
739 }
740 BindingMode::Move => inner_ty.clone(),
741 };
742 let bound_ty = self.resolve_ty_as_possible(&mut vec![], bound_ty);
743 self.write_pat_ty(pat, bound_ty);
744 return inner_ty;
745 }
746 _ => Ty::Unknown,
747 };
748 // use a new type variable if we got Ty::Unknown here
749 let ty = self.insert_type_vars_shallow(ty);
750 self.unify(&ty, expected);
751 let ty = self.resolve_ty_as_possible(&mut vec![], ty);
752 self.write_pat_ty(pat, ty.clone());
753 ty
754 }
755
756 fn substs_for_method_call(
757 &mut self,
758 def_generics: Option<Arc<GenericParams>>,
759 generic_args: Option<&GenericArgs>,
760 receiver_ty: &Ty,
761 ) -> Substs {
762 let (parent_param_count, param_count) =
763 def_generics.as_ref().map_or((0, 0), |g| (g.count_parent_params(), g.params.len()));
764 let mut substs = Vec::with_capacity(parent_param_count + param_count);
765 // Parent arguments are unknown, except for the receiver type
766 if let Some(parent_generics) = def_generics.and_then(|p| p.parent_params.clone()) {
767 for param in &parent_generics.params {
768 if param.name == name::SELF_TYPE {
769 substs.push(receiver_ty.clone());
770 } else {
771 substs.push(Ty::Unknown);
772 }
773 }
774 }
775 // handle provided type arguments
776 if let Some(generic_args) = generic_args {
777 // if args are provided, it should be all of them, but we can't rely on that
778 for arg in generic_args.args.iter().take(param_count) {
779 match arg {
780 GenericArg::Type(type_ref) => {
781 let ty = self.make_ty(type_ref);
782 substs.push(ty);
783 }
784 }
785 }
786 };
787 let supplied_params = substs.len();
788 for _ in supplied_params..parent_param_count + param_count {
789 substs.push(Ty::Unknown);
790 }
791 assert_eq!(substs.len(), parent_param_count + param_count);
792 Substs(substs.into())
793 }
794
795 fn register_obligations_for_call(&mut self, callable_ty: &Ty) {
796 if let Ty::Apply(a_ty) = callable_ty {
797 if let TypeCtor::FnDef(def) = a_ty.ctor {
798 let generic_predicates = self.db.generic_predicates(def.into());
799 for predicate in generic_predicates.iter() {
800 let predicate = predicate.clone().subst(&a_ty.parameters);
801 if let Some(obligation) = Obligation::from_predicate(predicate) {
802 self.obligations.push(obligation);
803 }
804 }
805 // add obligation for trait implementation, if this is a trait method
806 match def {
807 CallableDef::Function(f) => {
808 if let Some(trait_) = f.parent_trait(self.db) {
809 // construct a TraitDef
810 let substs = a_ty.parameters.prefix(
811 trait_.generic_params(self.db).count_params_including_parent(),
812 );
813 self.obligations.push(Obligation::Trait(TraitRef { trait_, substs }));
814 }
815 }
816 CallableDef::Struct(_) | CallableDef::EnumVariant(_) => {}
817 }
818 }
819 }
820 }
821
822 fn infer_method_call(
823 &mut self,
824 tgt_expr: ExprId,
825 receiver: ExprId,
826 args: &[ExprId],
827 method_name: &Name,
828 generic_args: Option<&GenericArgs>,
829 ) -> Ty {
830 let receiver_ty = self.infer_expr(receiver, &Expectation::none());
831 let canonicalized_receiver = self.canonicalizer().canonicalize_ty(receiver_ty.clone());
832 let resolved = method_resolution::lookup_method(
833 &canonicalized_receiver.value,
834 self.db,
835 method_name,
836 &self.resolver,
837 );
838 let (derefed_receiver_ty, method_ty, def_generics) = match resolved {
839 Some((ty, func)) => {
840 let ty = canonicalized_receiver.decanonicalize_ty(ty);
841 self.write_method_resolution(tgt_expr, func);
842 (
843 ty,
844 self.db.type_for_def(func.into(), Namespace::Values),
845 Some(func.generic_params(self.db)),
846 )
847 }
848 None => (receiver_ty, Ty::Unknown, None),
849 };
850 let substs = self.substs_for_method_call(def_generics, generic_args, &derefed_receiver_ty);
851 let method_ty = method_ty.apply_substs(substs);
852 let method_ty = self.insert_type_vars(method_ty);
853 self.register_obligations_for_call(&method_ty);
854 let (expected_receiver_ty, param_tys, ret_ty) = match method_ty.callable_sig(self.db) {
855 Some(sig) => {
856 if !sig.params().is_empty() {
857 (sig.params()[0].clone(), sig.params()[1..].to_vec(), sig.ret().clone())
858 } else {
859 (Ty::Unknown, Vec::new(), sig.ret().clone())
860 }
861 }
862 None => (Ty::Unknown, Vec::new(), Ty::Unknown),
863 };
864 // Apply autoref so the below unification works correctly
865 // FIXME: return correct autorefs from lookup_method
866 let actual_receiver_ty = match expected_receiver_ty.as_reference() {
867 Some((_, mutability)) => Ty::apply_one(TypeCtor::Ref(mutability), derefed_receiver_ty),
868 _ => derefed_receiver_ty,
869 };
870 self.unify(&expected_receiver_ty, &actual_receiver_ty);
871
872 self.check_call_arguments(args, &param_tys);
873 let ret_ty = self.normalize_associated_types_in(ret_ty);
874 ret_ty
875 }
876
877 /// Infer type of expression with possibly implicit coerce to the expected type.
878 /// Return the type after possible coercion.
879 fn infer_expr_coerce(&mut self, expr: ExprId, expected: &Expectation) -> Ty {
880 let ty = self.infer_expr_inner(expr, &expected);
881 let ty = if !self.coerce(&ty, &expected.ty) {
882 self.result
883 .type_mismatches
884 .insert(expr, TypeMismatch { expected: expected.ty.clone(), actual: ty.clone() });
885 // Return actual type when type mismatch.
886 // This is needed for diagnostic when return type mismatch.
887 ty
888 } else if expected.ty == Ty::Unknown {
889 ty
890 } else {
891 expected.ty.clone()
892 };
893
894 self.resolve_ty_as_possible(&mut vec![], ty)
895 }
896
897 /// Merge two types from different branches, with possible implicit coerce.
898 ///
899 /// Note that it is only possible that one type are coerced to another.
900 /// Coercing both types to another least upper bound type is not possible in rustc,
901 /// which will simply result in "incompatible types" error.
902 fn coerce_merge_branch<'t>(&mut self, ty1: &Ty, ty2: &Ty) -> Ty {
903 if self.coerce(ty1, ty2) {
904 ty2.clone()
905 } else if self.coerce(ty2, ty1) {
906 ty1.clone()
907 } else {
908 tested_by!(coerce_merge_fail_fallback);
909 // For incompatible types, we use the latter one as result
910 // to be better recovery for `if` without `else`.
911 ty2.clone()
912 }
913 }
914
915 /// Unify two types, but may coerce the first one to the second one
916 /// using "implicit coercion rules" if needed.
917 ///
918 /// See: https://doc.rust-lang.org/nomicon/coercions.html
919 fn coerce(&mut self, from_ty: &Ty, to_ty: &Ty) -> bool {
920 let from_ty = self.resolve_ty_shallow(from_ty).into_owned();
921 let to_ty = self.resolve_ty_shallow(to_ty);
922 self.coerce_inner(from_ty, &to_ty)
923 }
924
925 fn coerce_inner(&mut self, mut from_ty: Ty, to_ty: &Ty) -> bool {
926 match (&from_ty, to_ty) {
927 // Never type will make type variable to fallback to Never Type instead of Unknown.
928 (ty_app!(TypeCtor::Never), Ty::Infer(InferTy::TypeVar(tv))) => {
929 let var = self.new_maybe_never_type_var();
930 self.var_unification_table.union_value(*tv, TypeVarValue::Known(var));
931 return true;
932 }
933 (ty_app!(TypeCtor::Never), _) => return true,
934
935 // Trivial cases, this should go after `never` check to
936 // avoid infer result type to be never
937 _ => {
938 if self.unify_inner_trivial(&from_ty, &to_ty) {
939 return true;
940 }
941 }
942 }
943
944 // Pointer weakening and function to pointer
945 match (&mut from_ty, to_ty) {
946 // `*mut T`, `&mut T, `&T`` -> `*const T`
947 // `&mut T` -> `&T`
948 // `&mut T` -> `*mut T`
949 (ty_app!(c1@TypeCtor::RawPtr(_)), ty_app!(c2@TypeCtor::RawPtr(Mutability::Shared)))
950 | (ty_app!(c1@TypeCtor::Ref(_)), ty_app!(c2@TypeCtor::RawPtr(Mutability::Shared)))
951 | (ty_app!(c1@TypeCtor::Ref(_)), ty_app!(c2@TypeCtor::Ref(Mutability::Shared)))
952 | (ty_app!(c1@TypeCtor::Ref(Mutability::Mut)), ty_app!(c2@TypeCtor::RawPtr(_))) => {
953 *c1 = *c2;
954 }
955
956 // Illegal mutablity conversion
957 (
958 ty_app!(TypeCtor::RawPtr(Mutability::Shared)),
959 ty_app!(TypeCtor::RawPtr(Mutability::Mut)),
960 )
961 | (
962 ty_app!(TypeCtor::Ref(Mutability::Shared)),
963 ty_app!(TypeCtor::Ref(Mutability::Mut)),
964 ) => return false,
965
966 // `{function_type}` -> `fn()`
967 (ty_app!(TypeCtor::FnDef(_)), ty_app!(TypeCtor::FnPtr { .. })) => {
968 match from_ty.callable_sig(self.db) {
969 None => return false,
970 Some(sig) => {
971 let num_args = sig.params_and_return.len() as u16 - 1;
972 from_ty =
973 Ty::apply(TypeCtor::FnPtr { num_args }, Substs(sig.params_and_return));
974 }
975 }
976 }
977
978 _ => {}
979 }
980
981 if let Some(ret) = self.try_coerce_unsized(&from_ty, &to_ty) {
982 return ret;
983 }
984
985 // Auto Deref if cannot coerce
986 match (&from_ty, to_ty) {
987 // FIXME: DerefMut
988 (ty_app!(TypeCtor::Ref(_), st1), ty_app!(TypeCtor::Ref(_), st2)) => {
989 self.unify_autoderef_behind_ref(&st1[0], &st2[0])
990 }
991
992 // Otherwise, normal unify
993 _ => self.unify(&from_ty, to_ty),
994 }
995 }
996
997 /// Coerce a type using `from_ty: CoerceUnsized<ty_ty>`
998 ///
999 /// See: https://doc.rust-lang.org/nightly/std/marker/trait.CoerceUnsized.html
1000 fn try_coerce_unsized(&mut self, from_ty: &Ty, to_ty: &Ty) -> Option<bool> {
1001 let (ctor1, st1, ctor2, st2) = match (from_ty, to_ty) {
1002 (ty_app!(ctor1, st1), ty_app!(ctor2, st2)) => (ctor1, st1, ctor2, st2),
1003 _ => return None,
1004 };
1005
1006 let coerce_generic_index = *self.coerce_unsized_map.get(&(*ctor1, *ctor2))?;
1007
1008 // Check `Unsize` first
1009 match self.check_unsize_and_coerce(
1010 st1.0.get(coerce_generic_index)?,
1011 st2.0.get(coerce_generic_index)?,
1012 0,
1013 ) {
1014 Some(true) => {}
1015 ret => return ret,
1016 }
1017
1018 let ret = st1
1019 .iter()
1020 .zip(st2.iter())
1021 .enumerate()
1022 .filter(|&(idx, _)| idx != coerce_generic_index)
1023 .all(|(_, (ty1, ty2))| self.unify(ty1, ty2));
1024
1025 Some(ret)
1026 }
1027
1028 /// Check if `from_ty: Unsize<to_ty>`, and coerce to `to_ty` if it holds.
1029 ///
1030 /// It should not be directly called. It is only used by `try_coerce_unsized`.
1031 ///
1032 /// See: https://doc.rust-lang.org/nightly/std/marker/trait.Unsize.html
1033 fn check_unsize_and_coerce(&mut self, from_ty: &Ty, to_ty: &Ty, depth: usize) -> Option<bool> {
1034 if depth > 1000 {
1035 panic!("Infinite recursion in coercion");
1036 }
1037
1038 match (&from_ty, &to_ty) {
1039 // `[T; N]` -> `[T]`
1040 (ty_app!(TypeCtor::Array, st1), ty_app!(TypeCtor::Slice, st2)) => {
1041 Some(self.unify(&st1[0], &st2[0]))
1042 }
1043
1044 // `T` -> `dyn Trait` when `T: Trait`
1045 (_, Ty::Dyn(_)) => {
1046 // FIXME: Check predicates
1047 Some(true)
1048 }
1049
1050 // `(..., T)` -> `(..., U)` when `T: Unsize<U>`
1051 (
1052 ty_app!(TypeCtor::Tuple { cardinality: len1 }, st1),
1053 ty_app!(TypeCtor::Tuple { cardinality: len2 }, st2),
1054 ) => {
1055 if len1 != len2 || *len1 == 0 {
1056 return None;
1057 }
1058
1059 match self.check_unsize_and_coerce(
1060 st1.last().unwrap(),
1061 st2.last().unwrap(),
1062 depth + 1,
1063 ) {
1064 Some(true) => {}
1065 ret => return ret,
1066 }
1067
1068 let ret = st1[..st1.len() - 1]
1069 .iter()
1070 .zip(&st2[..st2.len() - 1])
1071 .all(|(ty1, ty2)| self.unify(ty1, ty2));
1072
1073 Some(ret)
1074 }
1075
1076 // Foo<..., T, ...> is Unsize<Foo<..., U, ...>> if:
1077 // - T: Unsize<U>
1078 // - Foo is a struct
1079 // - Only the last field of Foo has a type involving T
1080 // - T is not part of the type of any other fields
1081 // - Bar<T>: Unsize<Bar<U>>, if the last field of Foo has type Bar<T>
1082 (
1083 ty_app!(TypeCtor::Adt(Adt::Struct(struct1)), st1),
1084 ty_app!(TypeCtor::Adt(Adt::Struct(struct2)), st2),
1085 ) if struct1 == struct2 => {
1086 let fields = struct1.fields(self.db);
1087 let (last_field, prev_fields) = fields.split_last()?;
1088
1089 // Get the generic parameter involved in the last field.
1090 let unsize_generic_index = {
1091 let mut index = None;
1092 let mut multiple_param = false;
1093 last_field.ty(self.db).walk(&mut |ty| match ty {
1094 &Ty::Param { idx, .. } => {
1095 if index.is_none() {
1096 index = Some(idx);
1097 } else if Some(idx) != index {
1098 multiple_param = true;
1099 }
1100 }
1101 _ => {}
1102 });
1103
1104 if multiple_param {
1105 return None;
1106 }
1107 index?
1108 };
1109
1110 // Check other fields do not involve it.
1111 let mut multiple_used = false;
1112 prev_fields.iter().for_each(|field| {
1113 field.ty(self.db).walk(&mut |ty| match ty {
1114 &Ty::Param { idx, .. } if idx == unsize_generic_index => {
1115 multiple_used = true
1116 }
1117 _ => {}
1118 })
1119 });
1120 if multiple_used {
1121 return None;
1122 }
1123
1124 let unsize_generic_index = unsize_generic_index as usize;
1125
1126 // Check `Unsize` first
1127 match self.check_unsize_and_coerce(
1128 st1.get(unsize_generic_index)?,
1129 st2.get(unsize_generic_index)?,
1130 depth + 1,
1131 ) {
1132 Some(true) => {}
1133 ret => return ret,
1134 }
1135
1136 // Then unify other parameters
1137 let ret = st1
1138 .iter()
1139 .zip(st2.iter())
1140 .enumerate()
1141 .filter(|&(idx, _)| idx != unsize_generic_index)
1142 .all(|(_, (ty1, ty2))| self.unify(ty1, ty2));
1143
1144 Some(ret)
1145 }
1146
1147 _ => None,
1148 }
1149 }
1150
1151 /// Unify `from_ty` to `to_ty` with optional auto Deref
1152 ///
1153 /// Note that the parameters are already stripped the outer reference.
1154 fn unify_autoderef_behind_ref(&mut self, from_ty: &Ty, to_ty: &Ty) -> bool {
1155 let canonicalized = self.canonicalizer().canonicalize_ty(from_ty.clone());
1156 let to_ty = self.resolve_ty_shallow(&to_ty);
1157 // FIXME: Auto DerefMut
1158 for derefed_ty in
1159 autoderef::autoderef(self.db, &self.resolver.clone(), canonicalized.value.clone())
1160 {
1161 let derefed_ty = canonicalized.decanonicalize_ty(derefed_ty.value);
1162 match (&*self.resolve_ty_shallow(&derefed_ty), &*to_ty) {
1163 // Stop when constructor matches.
1164 (ty_app!(from_ctor, st1), ty_app!(to_ctor, st2)) if from_ctor == to_ctor => {
1165 // It will not recurse to `coerce`.
1166 return self.unify_substs(st1, st2, 0);
1167 }
1168 _ => {}
1169 }
1170 }
1171
1172 false
1173 }
1174
1175 fn infer_expr(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty {
1176 let ty = self.infer_expr_inner(tgt_expr, expected);
1177 let could_unify = self.unify(&ty, &expected.ty);
1178 if !could_unify {
1179 self.result.type_mismatches.insert(
1180 tgt_expr,
1181 TypeMismatch { expected: expected.ty.clone(), actual: ty.clone() },
1182 );
1183 }
1184 let ty = self.resolve_ty_as_possible(&mut vec![], ty);
1185 ty
1186 }
1187
1188 fn infer_expr_inner(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty {
1189 let body = Arc::clone(&self.body); // avoid borrow checker problem
1190 let ty = match &body[tgt_expr] {
1191 Expr::Missing => Ty::Unknown,
1192 Expr::If { condition, then_branch, else_branch } => {
1193 // if let is desugared to match, so this is always simple if
1194 self.infer_expr(*condition, &Expectation::has_type(Ty::simple(TypeCtor::Bool)));
1195
1196 let then_ty = self.infer_expr_inner(*then_branch, &expected);
1197 let else_ty = match else_branch {
1198 Some(else_branch) => self.infer_expr_inner(*else_branch, &expected),
1199 None => Ty::unit(),
1200 };
1201
1202 self.coerce_merge_branch(&then_ty, &else_ty)
1203 }
1204 Expr::Block { statements, tail } => self.infer_block(statements, *tail, expected),
1205 Expr::TryBlock { body } => {
1206 let _inner = self.infer_expr(*body, expected);
1207 // FIXME should be std::result::Result<{inner}, _>
1208 Ty::Unknown
1209 }
1210 Expr::Loop { body } => {
1211 self.infer_expr(*body, &Expectation::has_type(Ty::unit()));
1212 // FIXME handle break with value
1213 Ty::simple(TypeCtor::Never)
1214 }
1215 Expr::While { condition, body } => {
1216 // while let is desugared to a match loop, so this is always simple while
1217 self.infer_expr(*condition, &Expectation::has_type(Ty::simple(TypeCtor::Bool)));
1218 self.infer_expr(*body, &Expectation::has_type(Ty::unit()));
1219 Ty::unit()
1220 }
1221 Expr::For { iterable, body, pat } => {
1222 let iterable_ty = self.infer_expr(*iterable, &Expectation::none());
1223
1224 let pat_ty = match self.resolve_into_iter_item() {
1225 Some(into_iter_item_alias) => {
1226 let pat_ty = self.new_type_var();
1227 let projection = ProjectionPredicate {
1228 ty: pat_ty.clone(),
1229 projection_ty: ProjectionTy {
1230 associated_ty: into_iter_item_alias,
1231 parameters: Substs::single(iterable_ty),
1232 },
1233 };
1234 self.obligations.push(Obligation::Projection(projection));
1235 self.resolve_ty_as_possible(&mut vec![], pat_ty)
1236 }
1237 None => Ty::Unknown,
1238 };
1239
1240 self.infer_pat(*pat, &pat_ty, BindingMode::default());
1241 self.infer_expr(*body, &Expectation::has_type(Ty::unit()));
1242 Ty::unit()
1243 }
1244 Expr::Lambda { body, args, arg_types } => {
1245 assert_eq!(args.len(), arg_types.len());
1246
1247 let mut sig_tys = Vec::new();
1248
1249 for (arg_pat, arg_type) in args.iter().zip(arg_types.iter()) {
1250 let expected = if let Some(type_ref) = arg_type {
1251 self.make_ty(type_ref)
1252 } else {
1253 Ty::Unknown
1254 };
1255 let arg_ty = self.infer_pat(*arg_pat, &expected, BindingMode::default());
1256 sig_tys.push(arg_ty);
1257 }
1258
1259 // add return type
1260 let ret_ty = self.new_type_var();
1261 sig_tys.push(ret_ty.clone());
1262 let sig_ty = Ty::apply(
1263 TypeCtor::FnPtr { num_args: sig_tys.len() as u16 - 1 },
1264 Substs(sig_tys.into()),
1265 );
1266 let closure_ty = Ty::apply_one(
1267 TypeCtor::Closure { def: self.body.owner(), expr: tgt_expr },
1268 sig_ty,
1269 );
1270
1271 // Eagerly try to relate the closure type with the expected
1272 // type, otherwise we often won't have enough information to
1273 // infer the body.
1274 self.coerce(&closure_ty, &expected.ty);
1275
1276 self.infer_expr(*body, &Expectation::has_type(ret_ty));
1277 closure_ty
1278 }
1279 Expr::Call { callee, args } => {
1280 let callee_ty = self.infer_expr(*callee, &Expectation::none());
1281 let (param_tys, ret_ty) = match callee_ty.callable_sig(self.db) {
1282 Some(sig) => (sig.params().to_vec(), sig.ret().clone()),
1283 None => {
1284 // Not callable
1285 // FIXME: report an error
1286 (Vec::new(), Ty::Unknown)
1287 }
1288 };
1289 self.register_obligations_for_call(&callee_ty);
1290 self.check_call_arguments(args, &param_tys);
1291 let ret_ty = self.normalize_associated_types_in(ret_ty);
1292 ret_ty
1293 }
1294 Expr::MethodCall { receiver, args, method_name, generic_args } => self
1295 .infer_method_call(tgt_expr, *receiver, &args, &method_name, generic_args.as_ref()),
1296 Expr::Match { expr, arms } => {
1297 let input_ty = self.infer_expr(*expr, &Expectation::none());
1298
1299 let mut result_ty = self.new_maybe_never_type_var();
1300
1301 for arm in arms {
1302 for &pat in &arm.pats {
1303 let _pat_ty = self.infer_pat(pat, &input_ty, BindingMode::default());
1304 }
1305 if let Some(guard_expr) = arm.guard {
1306 self.infer_expr(
1307 guard_expr,
1308 &Expectation::has_type(Ty::simple(TypeCtor::Bool)),
1309 );
1310 }
1311
1312 let arm_ty = self.infer_expr_inner(arm.expr, &expected);
1313 result_ty = self.coerce_merge_branch(&result_ty, &arm_ty);
1314 }
1315
1316 result_ty
1317 }
1318 Expr::Path(p) => {
1319 // FIXME this could be more efficient...
1320 let resolver = expr::resolver_for_expr(self.body.clone(), self.db, tgt_expr);
1321 self.infer_path(&resolver, p, tgt_expr.into()).unwrap_or(Ty::Unknown)
1322 }
1323 Expr::Continue => Ty::simple(TypeCtor::Never),
1324 Expr::Break { expr } => {
1325 if let Some(expr) = expr {
1326 // FIXME handle break with value
1327 self.infer_expr(*expr, &Expectation::none());
1328 }
1329 Ty::simple(TypeCtor::Never)
1330 }
1331 Expr::Return { expr } => {
1332 if let Some(expr) = expr {
1333 self.infer_expr(*expr, &Expectation::has_type(self.return_ty.clone()));
1334 }
1335 Ty::simple(TypeCtor::Never)
1336 }
1337 Expr::RecordLit { path, fields, spread } => {
1338 let (ty, def_id) = self.resolve_variant(path.as_ref());
1339 if let Some(variant) = def_id {
1340 self.write_variant_resolution(tgt_expr.into(), variant);
1341 }
1342
1343 self.unify(&ty, &expected.ty);
1344
1345 let substs = ty.substs().unwrap_or_else(Substs::empty);
1346 for (field_idx, field) in fields.iter().enumerate() {
1347 let field_ty = def_id
1348 .and_then(|it| match it.field(self.db, &field.name) {
1349 Some(field) => Some(field),
1350 None => {
1351 self.push_diagnostic(InferenceDiagnostic::NoSuchField {
1352 expr: tgt_expr,
1353 field: field_idx,
1354 });
1355 None
1356 }
1357 })
1358 .map_or(Ty::Unknown, |field| field.ty(self.db))
1359 .subst(&substs);
1360 self.infer_expr_coerce(field.expr, &Expectation::has_type(field_ty));
1361 }
1362 if let Some(expr) = spread {
1363 self.infer_expr(*expr, &Expectation::has_type(ty.clone()));
1364 }
1365 ty
1366 }
1367 Expr::Field { expr, name } => {
1368 let receiver_ty = self.infer_expr(*expr, &Expectation::none());
1369 let canonicalized = self.canonicalizer().canonicalize_ty(receiver_ty);
1370 let ty = autoderef::autoderef(
1371 self.db,
1372 &self.resolver.clone(),
1373 canonicalized.value.clone(),
1374 )
1375 .find_map(|derefed_ty| match canonicalized.decanonicalize_ty(derefed_ty.value) {
1376 Ty::Apply(a_ty) => match a_ty.ctor {
1377 TypeCtor::Tuple { .. } => name
1378 .as_tuple_index()
1379 .and_then(|idx| a_ty.parameters.0.get(idx).cloned()),
1380 TypeCtor::Adt(Adt::Struct(s)) => s.field(self.db, name).map(|field| {
1381 self.write_field_resolution(tgt_expr, field);
1382 field.ty(self.db).subst(&a_ty.parameters)
1383 }),
1384 _ => None,
1385 },
1386 _ => None,
1387 })
1388 .unwrap_or(Ty::Unknown);
1389 let ty = self.insert_type_vars(ty);
1390 self.normalize_associated_types_in(ty)
1391 }
1392 Expr::Await { expr } => {
1393 let inner_ty = self.infer_expr(*expr, &Expectation::none());
1394 let ty = match self.resolve_future_future_output() {
1395 Some(future_future_output_alias) => {
1396 let ty = self.new_type_var();
1397 let projection = ProjectionPredicate {
1398 ty: ty.clone(),
1399 projection_ty: ProjectionTy {
1400 associated_ty: future_future_output_alias,
1401 parameters: Substs::single(inner_ty),
1402 },
1403 };
1404 self.obligations.push(Obligation::Projection(projection));
1405 self.resolve_ty_as_possible(&mut vec![], ty)
1406 }
1407 None => Ty::Unknown,
1408 };
1409 ty
1410 }
1411 Expr::Try { expr } => {
1412 let inner_ty = self.infer_expr(*expr, &Expectation::none());
1413 let ty = match self.resolve_ops_try_ok() {
1414 Some(ops_try_ok_alias) => {
1415 let ty = self.new_type_var();
1416 let projection = ProjectionPredicate {
1417 ty: ty.clone(),
1418 projection_ty: ProjectionTy {
1419 associated_ty: ops_try_ok_alias,
1420 parameters: Substs::single(inner_ty),
1421 },
1422 };
1423 self.obligations.push(Obligation::Projection(projection));
1424 self.resolve_ty_as_possible(&mut vec![], ty)
1425 }
1426 None => Ty::Unknown,
1427 };
1428 ty
1429 }
1430 Expr::Cast { expr, type_ref } => {
1431 let _inner_ty = self.infer_expr(*expr, &Expectation::none());
1432 let cast_ty = self.make_ty(type_ref);
1433 // FIXME check the cast...
1434 cast_ty
1435 }
1436 Expr::Ref { expr, mutability } => {
1437 let expectation =
1438 if let Some((exp_inner, exp_mutability)) = &expected.ty.as_reference() {
1439 if *exp_mutability == Mutability::Mut && *mutability == Mutability::Shared {
1440 // FIXME: throw type error - expected mut reference but found shared ref,
1441 // which cannot be coerced
1442 }
1443 Expectation::has_type(Ty::clone(exp_inner))
1444 } else {
1445 Expectation::none()
1446 };
1447 // FIXME reference coercions etc.
1448 let inner_ty = self.infer_expr(*expr, &expectation);
1449 Ty::apply_one(TypeCtor::Ref(*mutability), inner_ty)
1450 }
1451 Expr::Box { expr } => {
1452 let inner_ty = self.infer_expr(*expr, &Expectation::none());
1453 if let Some(box_) = self.resolve_boxed_box() {
1454 Ty::apply_one(TypeCtor::Adt(box_), inner_ty)
1455 } else {
1456 Ty::Unknown
1457 }
1458 }
1459 Expr::UnaryOp { expr, op } => {
1460 let inner_ty = self.infer_expr(*expr, &Expectation::none());
1461 match op {
1462 UnaryOp::Deref => {
1463 let canonicalized = self.canonicalizer().canonicalize_ty(inner_ty);
1464 if let Some(derefed_ty) =
1465 autoderef::deref(self.db, &self.resolver, &canonicalized.value)
1466 {
1467 canonicalized.decanonicalize_ty(derefed_ty.value)
1468 } else {
1469 Ty::Unknown
1470 }
1471 }
1472 UnaryOp::Neg => {
1473 match &inner_ty {
1474 Ty::Apply(a_ty) => match a_ty.ctor {
1475 TypeCtor::Int(primitive::UncertainIntTy::Unknown)
1476 | TypeCtor::Int(primitive::UncertainIntTy::Known(
1477 primitive::IntTy {
1478 signedness: primitive::Signedness::Signed,
1479 ..
1480 },
1481 ))
1482 | TypeCtor::Float(..) => inner_ty,
1483 _ => Ty::Unknown,
1484 },
1485 Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => {
1486 inner_ty
1487 }
1488 // FIXME: resolve ops::Neg trait
1489 _ => Ty::Unknown,
1490 }
1491 }
1492 UnaryOp::Not => {
1493 match &inner_ty {
1494 Ty::Apply(a_ty) => match a_ty.ctor {
1495 TypeCtor::Bool | TypeCtor::Int(_) => inner_ty,
1496 _ => Ty::Unknown,
1497 },
1498 Ty::Infer(InferTy::IntVar(..)) => inner_ty,
1499 // FIXME: resolve ops::Not trait for inner_ty
1500 _ => Ty::Unknown,
1501 }
1502 }
1503 }
1504 }
1505 Expr::BinaryOp { lhs, rhs, op } => match op {
1506 Some(op) => {
1507 let lhs_expectation = match op {
1508 BinaryOp::LogicOp(..) => Expectation::has_type(Ty::simple(TypeCtor::Bool)),
1509 _ => Expectation::none(),
1510 };
1511 let lhs_ty = self.infer_expr(*lhs, &lhs_expectation);
1512 // FIXME: find implementation of trait corresponding to operation
1513 // symbol and resolve associated `Output` type
1514 let rhs_expectation = op::binary_op_rhs_expectation(*op, lhs_ty);
1515 let rhs_ty = self.infer_expr(*rhs, &Expectation::has_type(rhs_expectation));
1516
1517 // FIXME: similar as above, return ty is often associated trait type
1518 op::binary_op_return_ty(*op, rhs_ty)
1519 }
1520 _ => Ty::Unknown,
1521 },
1522 Expr::Index { base, index } => {
1523 let _base_ty = self.infer_expr(*base, &Expectation::none());
1524 let _index_ty = self.infer_expr(*index, &Expectation::none());
1525 // FIXME: use `std::ops::Index::Output` to figure out the real return type
1526 Ty::Unknown
1527 }
1528 Expr::Tuple { exprs } => {
1529 let mut tys = match &expected.ty {
1530 ty_app!(TypeCtor::Tuple { .. }, st) => st
1531 .iter()
1532 .cloned()
1533 .chain(repeat_with(|| self.new_type_var()))
1534 .take(exprs.len())
1535 .collect::<Vec<_>>(),
1536 _ => (0..exprs.len()).map(|_| self.new_type_var()).collect(),
1537 };
1538
1539 for (expr, ty) in exprs.iter().zip(tys.iter_mut()) {
1540 self.infer_expr_coerce(*expr, &Expectation::has_type(ty.clone()));
1541 }
1542
1543 Ty::apply(TypeCtor::Tuple { cardinality: tys.len() as u16 }, Substs(tys.into()))
1544 }
1545 Expr::Array(array) => {
1546 let elem_ty = match &expected.ty {
1547 ty_app!(TypeCtor::Array, st) | ty_app!(TypeCtor::Slice, st) => {
1548 st.as_single().clone()
1549 }
1550 _ => self.new_type_var(),
1551 };
1552
1553 match array {
1554 Array::ElementList(items) => {
1555 for expr in items.iter() {
1556 self.infer_expr_coerce(*expr, &Expectation::has_type(elem_ty.clone()));
1557 }
1558 }
1559 Array::Repeat { initializer, repeat } => {
1560 self.infer_expr_coerce(
1561 *initializer,
1562 &Expectation::has_type(elem_ty.clone()),
1563 );
1564 self.infer_expr(
1565 *repeat,
1566 &Expectation::has_type(Ty::simple(TypeCtor::Int(
1567 primitive::UncertainIntTy::Known(primitive::IntTy::usize()),
1568 ))),
1569 );
1570 }
1571 }
1572
1573 Ty::apply_one(TypeCtor::Array, elem_ty)
1574 }
1575 Expr::Literal(lit) => match lit {
1576 Literal::Bool(..) => Ty::simple(TypeCtor::Bool),
1577 Literal::String(..) => {
1578 Ty::apply_one(TypeCtor::Ref(Mutability::Shared), Ty::simple(TypeCtor::Str))
1579 }
1580 Literal::ByteString(..) => {
1581 let byte_type = Ty::simple(TypeCtor::Int(primitive::UncertainIntTy::Known(
1582 primitive::IntTy::u8(),
1583 )));
1584 let slice_type = Ty::apply_one(TypeCtor::Slice, byte_type);
1585 Ty::apply_one(TypeCtor::Ref(Mutability::Shared), slice_type)
1586 }
1587 Literal::Char(..) => Ty::simple(TypeCtor::Char),
1588 Literal::Int(_v, ty) => Ty::simple(TypeCtor::Int(*ty)),
1589 Literal::Float(_v, ty) => Ty::simple(TypeCtor::Float(*ty)),
1590 },
1591 };
1592 // use a new type variable if we got Ty::Unknown here
1593 let ty = self.insert_type_vars_shallow(ty);
1594 let ty = self.resolve_ty_as_possible(&mut vec![], ty);
1595 self.write_expr_ty(tgt_expr, ty.clone());
1596 ty
1597 }
1598
1599 fn infer_block(
1600 &mut self,
1601 statements: &[Statement],
1602 tail: Option<ExprId>,
1603 expected: &Expectation,
1604 ) -> Ty {
1605 let mut diverges = false;
1606 for stmt in statements {
1607 match stmt {
1608 Statement::Let { pat, type_ref, initializer } => {
1609 let decl_ty =
1610 type_ref.as_ref().map(|tr| self.make_ty(tr)).unwrap_or(Ty::Unknown);
1611
1612 // Always use the declared type when specified
1613 let mut ty = decl_ty.clone();
1614
1615 if let Some(expr) = initializer {
1616 let actual_ty =
1617 self.infer_expr_coerce(*expr, &Expectation::has_type(decl_ty.clone()));
1618 if decl_ty == Ty::Unknown {
1619 ty = actual_ty;
1620 }
1621 }
1622
1623 let ty = self.resolve_ty_as_possible(&mut vec![], ty);
1624 self.infer_pat(*pat, &ty, BindingMode::default());
1625 }
1626 Statement::Expr(expr) => {
1627 if let ty_app!(TypeCtor::Never) = self.infer_expr(*expr, &Expectation::none()) {
1628 diverges = true;
1629 }
1630 }
1631 }
1632 }
1633
1634 let ty = if let Some(expr) = tail {
1635 self.infer_expr_coerce(expr, expected)
1636 } else {
1637 self.coerce(&Ty::unit(), &expected.ty);
1638 Ty::unit()
1639 };
1640 if diverges {
1641 Ty::simple(TypeCtor::Never)
1642 } else {
1643 ty
1644 }
1645 }
1646
1647 fn check_call_arguments(&mut self, args: &[ExprId], param_tys: &[Ty]) {
1648 // Quoting https://github.com/rust-lang/rust/blob/6ef275e6c3cb1384ec78128eceeb4963ff788dca/src/librustc_typeck/check/mod.rs#L3325 --
1649 // We do this in a pretty awful way: first we type-check any arguments
1650 // that are not closures, then we type-check the closures. This is so
1651 // that we have more information about the types of arguments when we
1652 // type-check the functions. This isn't really the right way to do this.
1653 for &check_closures in &[false, true] {
1654 let param_iter = param_tys.iter().cloned().chain(repeat(Ty::Unknown));
1655 for (&arg, param_ty) in args.iter().zip(param_iter) {
1656 let is_closure = match &self.body[arg] {
1657 Expr::Lambda { .. } => true,
1658 _ => false,
1659 };
1660
1661 if is_closure != check_closures {
1662 continue;
1663 }
1664
1665 let param_ty = self.normalize_associated_types_in(param_ty);
1666 self.infer_expr_coerce(arg, &Expectation::has_type(param_ty.clone()));
1667 }
1668 }
1669 }
1670
1671 fn collect_const(&mut self, data: &ConstData) { 559 fn collect_const(&mut self, data: &ConstData) {
1672 self.return_ty = self.make_ty(data.type_ref()); 560 self.return_ty = self.make_ty(data.type_ref());
1673 } 561 }
diff --git a/crates/ra_hir/src/ty/infer/coerce.rs b/crates/ra_hir/src/ty/infer/coerce.rs
new file mode 100644
index 000000000..0429a9866
--- /dev/null
+++ b/crates/ra_hir/src/ty/infer/coerce.rs
@@ -0,0 +1,336 @@
1//! Coercion logic. Coercions are certain type conversions that can implicitly
2//! happen in certain places, e.g. weakening `&mut` to `&` or deref coercions
3//! like going from `&Vec<T>` to `&[T]`.
4//!
5//! See: https://doc.rust-lang.org/nomicon/coercions.html
6
7use rustc_hash::FxHashMap;
8
9use test_utils::tested_by;
10
11use super::{InferTy, InferenceContext, TypeVarValue};
12use crate::{
13 db::HirDatabase,
14 lang_item::LangItemTarget,
15 resolve::Resolver,
16 ty::{autoderef, Substs, Ty, TypeCtor, TypeWalk},
17 type_ref::Mutability,
18 Adt,
19};
20
21impl<'a, D: HirDatabase> InferenceContext<'a, D> {
22 /// Unify two types, but may coerce the first one to the second one
23 /// using "implicit coercion rules" if needed.
24 pub(super) fn coerce(&mut self, from_ty: &Ty, to_ty: &Ty) -> bool {
25 let from_ty = self.resolve_ty_shallow(from_ty).into_owned();
26 let to_ty = self.resolve_ty_shallow(to_ty);
27 self.coerce_inner(from_ty, &to_ty)
28 }
29
30 /// Merge two types from different branches, with possible implicit coerce.
31 ///
32 /// Note that it is only possible that one type are coerced to another.
33 /// Coercing both types to another least upper bound type is not possible in rustc,
34 /// which will simply result in "incompatible types" error.
35 pub(super) fn coerce_merge_branch<'t>(&mut self, ty1: &Ty, ty2: &Ty) -> Ty {
36 if self.coerce(ty1, ty2) {
37 ty2.clone()
38 } else if self.coerce(ty2, ty1) {
39 ty1.clone()
40 } else {
41 tested_by!(coerce_merge_fail_fallback);
42 // For incompatible types, we use the latter one as result
43 // to be better recovery for `if` without `else`.
44 ty2.clone()
45 }
46 }
47
48 pub(super) fn init_coerce_unsized_map(
49 db: &'a D,
50 resolver: &Resolver,
51 ) -> FxHashMap<(TypeCtor, TypeCtor), usize> {
52 let krate = resolver.krate().unwrap();
53 let impls = match db.lang_item(krate, "coerce_unsized".into()) {
54 Some(LangItemTarget::Trait(trait_)) => db.impls_for_trait(krate, trait_),
55 _ => return FxHashMap::default(),
56 };
57
58 impls
59 .iter()
60 .filter_map(|impl_block| {
61 // `CoerseUnsized` has one generic parameter for the target type.
62 let trait_ref = impl_block.target_trait_ref(db)?;
63 let cur_from_ty = trait_ref.substs.0.get(0)?;
64 let cur_to_ty = trait_ref.substs.0.get(1)?;
65
66 match (&cur_from_ty, cur_to_ty) {
67 (ty_app!(ctor1, st1), ty_app!(ctor2, st2)) => {
68 // FIXME: We return the first non-equal bound as the type parameter to coerce to unsized type.
69 // This works for smart-pointer-like coercion, which covers all impls from std.
70 st1.iter().zip(st2.iter()).enumerate().find_map(|(i, (ty1, ty2))| {
71 match (ty1, ty2) {
72 (Ty::Param { idx: p1, .. }, Ty::Param { idx: p2, .. })
73 if p1 != p2 =>
74 {
75 Some(((*ctor1, *ctor2), i))
76 }
77 _ => None,
78 }
79 })
80 }
81 _ => None,
82 }
83 })
84 .collect()
85 }
86
87 fn coerce_inner(&mut self, mut from_ty: Ty, to_ty: &Ty) -> bool {
88 match (&from_ty, to_ty) {
89 // Never type will make type variable to fallback to Never Type instead of Unknown.
90 (ty_app!(TypeCtor::Never), Ty::Infer(InferTy::TypeVar(tv))) => {
91 let var = self.new_maybe_never_type_var();
92 self.var_unification_table.union_value(*tv, TypeVarValue::Known(var));
93 return true;
94 }
95 (ty_app!(TypeCtor::Never), _) => return true,
96
97 // Trivial cases, this should go after `never` check to
98 // avoid infer result type to be never
99 _ => {
100 if self.unify_inner_trivial(&from_ty, &to_ty) {
101 return true;
102 }
103 }
104 }
105
106 // Pointer weakening and function to pointer
107 match (&mut from_ty, to_ty) {
108 // `*mut T`, `&mut T, `&T`` -> `*const T`
109 // `&mut T` -> `&T`
110 // `&mut T` -> `*mut T`
111 (ty_app!(c1@TypeCtor::RawPtr(_)), ty_app!(c2@TypeCtor::RawPtr(Mutability::Shared)))
112 | (ty_app!(c1@TypeCtor::Ref(_)), ty_app!(c2@TypeCtor::RawPtr(Mutability::Shared)))
113 | (ty_app!(c1@TypeCtor::Ref(_)), ty_app!(c2@TypeCtor::Ref(Mutability::Shared)))
114 | (ty_app!(c1@TypeCtor::Ref(Mutability::Mut)), ty_app!(c2@TypeCtor::RawPtr(_))) => {
115 *c1 = *c2;
116 }
117
118 // Illegal mutablity conversion
119 (
120 ty_app!(TypeCtor::RawPtr(Mutability::Shared)),
121 ty_app!(TypeCtor::RawPtr(Mutability::Mut)),
122 )
123 | (
124 ty_app!(TypeCtor::Ref(Mutability::Shared)),
125 ty_app!(TypeCtor::Ref(Mutability::Mut)),
126 ) => return false,
127
128 // `{function_type}` -> `fn()`
129 (ty_app!(TypeCtor::FnDef(_)), ty_app!(TypeCtor::FnPtr { .. })) => {
130 match from_ty.callable_sig(self.db) {
131 None => return false,
132 Some(sig) => {
133 let num_args = sig.params_and_return.len() as u16 - 1;
134 from_ty =
135 Ty::apply(TypeCtor::FnPtr { num_args }, Substs(sig.params_and_return));
136 }
137 }
138 }
139
140 _ => {}
141 }
142
143 if let Some(ret) = self.try_coerce_unsized(&from_ty, &to_ty) {
144 return ret;
145 }
146
147 // Auto Deref if cannot coerce
148 match (&from_ty, to_ty) {
149 // FIXME: DerefMut
150 (ty_app!(TypeCtor::Ref(_), st1), ty_app!(TypeCtor::Ref(_), st2)) => {
151 self.unify_autoderef_behind_ref(&st1[0], &st2[0])
152 }
153
154 // Otherwise, normal unify
155 _ => self.unify(&from_ty, to_ty),
156 }
157 }
158
159 /// Coerce a type using `from_ty: CoerceUnsized<ty_ty>`
160 ///
161 /// See: https://doc.rust-lang.org/nightly/std/marker/trait.CoerceUnsized.html
162 fn try_coerce_unsized(&mut self, from_ty: &Ty, to_ty: &Ty) -> Option<bool> {
163 let (ctor1, st1, ctor2, st2) = match (from_ty, to_ty) {
164 (ty_app!(ctor1, st1), ty_app!(ctor2, st2)) => (ctor1, st1, ctor2, st2),
165 _ => return None,
166 };
167
168 let coerce_generic_index = *self.coerce_unsized_map.get(&(*ctor1, *ctor2))?;
169
170 // Check `Unsize` first
171 match self.check_unsize_and_coerce(
172 st1.0.get(coerce_generic_index)?,
173 st2.0.get(coerce_generic_index)?,
174 0,
175 ) {
176 Some(true) => {}
177 ret => return ret,
178 }
179
180 let ret = st1
181 .iter()
182 .zip(st2.iter())
183 .enumerate()
184 .filter(|&(idx, _)| idx != coerce_generic_index)
185 .all(|(_, (ty1, ty2))| self.unify(ty1, ty2));
186
187 Some(ret)
188 }
189
190 /// Check if `from_ty: Unsize<to_ty>`, and coerce to `to_ty` if it holds.
191 ///
192 /// It should not be directly called. It is only used by `try_coerce_unsized`.
193 ///
194 /// See: https://doc.rust-lang.org/nightly/std/marker/trait.Unsize.html
195 fn check_unsize_and_coerce(&mut self, from_ty: &Ty, to_ty: &Ty, depth: usize) -> Option<bool> {
196 if depth > 1000 {
197 panic!("Infinite recursion in coercion");
198 }
199
200 match (&from_ty, &to_ty) {
201 // `[T; N]` -> `[T]`
202 (ty_app!(TypeCtor::Array, st1), ty_app!(TypeCtor::Slice, st2)) => {
203 Some(self.unify(&st1[0], &st2[0]))
204 }
205
206 // `T` -> `dyn Trait` when `T: Trait`
207 (_, Ty::Dyn(_)) => {
208 // FIXME: Check predicates
209 Some(true)
210 }
211
212 // `(..., T)` -> `(..., U)` when `T: Unsize<U>`
213 (
214 ty_app!(TypeCtor::Tuple { cardinality: len1 }, st1),
215 ty_app!(TypeCtor::Tuple { cardinality: len2 }, st2),
216 ) => {
217 if len1 != len2 || *len1 == 0 {
218 return None;
219 }
220
221 match self.check_unsize_and_coerce(
222 st1.last().unwrap(),
223 st2.last().unwrap(),
224 depth + 1,
225 ) {
226 Some(true) => {}
227 ret => return ret,
228 }
229
230 let ret = st1[..st1.len() - 1]
231 .iter()
232 .zip(&st2[..st2.len() - 1])
233 .all(|(ty1, ty2)| self.unify(ty1, ty2));
234
235 Some(ret)
236 }
237
238 // Foo<..., T, ...> is Unsize<Foo<..., U, ...>> if:
239 // - T: Unsize<U>
240 // - Foo is a struct
241 // - Only the last field of Foo has a type involving T
242 // - T is not part of the type of any other fields
243 // - Bar<T>: Unsize<Bar<U>>, if the last field of Foo has type Bar<T>
244 (
245 ty_app!(TypeCtor::Adt(Adt::Struct(struct1)), st1),
246 ty_app!(TypeCtor::Adt(Adt::Struct(struct2)), st2),
247 ) if struct1 == struct2 => {
248 let fields = struct1.fields(self.db);
249 let (last_field, prev_fields) = fields.split_last()?;
250
251 // Get the generic parameter involved in the last field.
252 let unsize_generic_index = {
253 let mut index = None;
254 let mut multiple_param = false;
255 last_field.ty(self.db).walk(&mut |ty| match ty {
256 &Ty::Param { idx, .. } => {
257 if index.is_none() {
258 index = Some(idx);
259 } else if Some(idx) != index {
260 multiple_param = true;
261 }
262 }
263 _ => {}
264 });
265
266 if multiple_param {
267 return None;
268 }
269 index?
270 };
271
272 // Check other fields do not involve it.
273 let mut multiple_used = false;
274 prev_fields.iter().for_each(|field| {
275 field.ty(self.db).walk(&mut |ty| match ty {
276 &Ty::Param { idx, .. } if idx == unsize_generic_index => {
277 multiple_used = true
278 }
279 _ => {}
280 })
281 });
282 if multiple_used {
283 return None;
284 }
285
286 let unsize_generic_index = unsize_generic_index as usize;
287
288 // Check `Unsize` first
289 match self.check_unsize_and_coerce(
290 st1.get(unsize_generic_index)?,
291 st2.get(unsize_generic_index)?,
292 depth + 1,
293 ) {
294 Some(true) => {}
295 ret => return ret,
296 }
297
298 // Then unify other parameters
299 let ret = st1
300 .iter()
301 .zip(st2.iter())
302 .enumerate()
303 .filter(|&(idx, _)| idx != unsize_generic_index)
304 .all(|(_, (ty1, ty2))| self.unify(ty1, ty2));
305
306 Some(ret)
307 }
308
309 _ => None,
310 }
311 }
312
313 /// Unify `from_ty` to `to_ty` with optional auto Deref
314 ///
315 /// Note that the parameters are already stripped the outer reference.
316 fn unify_autoderef_behind_ref(&mut self, from_ty: &Ty, to_ty: &Ty) -> bool {
317 let canonicalized = self.canonicalizer().canonicalize_ty(from_ty.clone());
318 let to_ty = self.resolve_ty_shallow(&to_ty);
319 // FIXME: Auto DerefMut
320 for derefed_ty in
321 autoderef::autoderef(self.db, &self.resolver.clone(), canonicalized.value.clone())
322 {
323 let derefed_ty = canonicalized.decanonicalize_ty(derefed_ty.value);
324 match (&*self.resolve_ty_shallow(&derefed_ty), &*to_ty) {
325 // Stop when constructor matches.
326 (ty_app!(from_ctor, st1), ty_app!(to_ctor, st2)) if from_ctor == to_ctor => {
327 // It will not recurse to `coerce`.
328 return self.unify_substs(st1, st2, 0);
329 }
330 _ => {}
331 }
332 }
333
334 false
335 }
336}
diff --git a/crates/ra_hir/src/ty/infer/expr.rs b/crates/ra_hir/src/ty/infer/expr.rs
new file mode 100644
index 000000000..f8807c742
--- /dev/null
+++ b/crates/ra_hir/src/ty/infer/expr.rs
@@ -0,0 +1,658 @@
1//! Type inference for expressions.
2
3use std::iter::{repeat, repeat_with};
4use std::sync::Arc;
5
6use super::{BindingMode, Expectation, InferenceContext, InferenceDiagnostic, TypeMismatch};
7use crate::{
8 db::HirDatabase,
9 expr::{self, Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp},
10 generics::{GenericParams, HasGenericParams},
11 name,
12 nameres::Namespace,
13 path::{GenericArg, GenericArgs},
14 ty::{
15 autoderef, method_resolution, op, primitive, CallableDef, InferTy, Mutability, Obligation,
16 ProjectionPredicate, ProjectionTy, Substs, TraitRef, Ty, TypeCtor, TypeWalk,
17 },
18 Adt, Name,
19};
20
21impl<'a, D: HirDatabase> InferenceContext<'a, D> {
22 pub(super) fn infer_expr(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty {
23 let ty = self.infer_expr_inner(tgt_expr, expected);
24 let could_unify = self.unify(&ty, &expected.ty);
25 if !could_unify {
26 self.result.type_mismatches.insert(
27 tgt_expr,
28 TypeMismatch { expected: expected.ty.clone(), actual: ty.clone() },
29 );
30 }
31 let ty = self.resolve_ty_as_possible(&mut vec![], ty);
32 ty
33 }
34
35 /// Infer type of expression with possibly implicit coerce to the expected type.
36 /// Return the type after possible coercion.
37 fn infer_expr_coerce(&mut self, expr: ExprId, expected: &Expectation) -> Ty {
38 let ty = self.infer_expr_inner(expr, &expected);
39 let ty = if !self.coerce(&ty, &expected.ty) {
40 self.result
41 .type_mismatches
42 .insert(expr, TypeMismatch { expected: expected.ty.clone(), actual: ty.clone() });
43 // Return actual type when type mismatch.
44 // This is needed for diagnostic when return type mismatch.
45 ty
46 } else if expected.ty == Ty::Unknown {
47 ty
48 } else {
49 expected.ty.clone()
50 };
51
52 self.resolve_ty_as_possible(&mut vec![], ty)
53 }
54
55 fn infer_expr_inner(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty {
56 let body = Arc::clone(&self.body); // avoid borrow checker problem
57 let ty = match &body[tgt_expr] {
58 Expr::Missing => Ty::Unknown,
59 Expr::If { condition, then_branch, else_branch } => {
60 // if let is desugared to match, so this is always simple if
61 self.infer_expr(*condition, &Expectation::has_type(Ty::simple(TypeCtor::Bool)));
62
63 let then_ty = self.infer_expr_inner(*then_branch, &expected);
64 let else_ty = match else_branch {
65 Some(else_branch) => self.infer_expr_inner(*else_branch, &expected),
66 None => Ty::unit(),
67 };
68
69 self.coerce_merge_branch(&then_ty, &else_ty)
70 }
71 Expr::Block { statements, tail } => self.infer_block(statements, *tail, expected),
72 Expr::TryBlock { body } => {
73 let _inner = self.infer_expr(*body, expected);
74 // FIXME should be std::result::Result<{inner}, _>
75 Ty::Unknown
76 }
77 Expr::Loop { body } => {
78 self.infer_expr(*body, &Expectation::has_type(Ty::unit()));
79 // FIXME handle break with value
80 Ty::simple(TypeCtor::Never)
81 }
82 Expr::While { condition, body } => {
83 // while let is desugared to a match loop, so this is always simple while
84 self.infer_expr(*condition, &Expectation::has_type(Ty::simple(TypeCtor::Bool)));
85 self.infer_expr(*body, &Expectation::has_type(Ty::unit()));
86 Ty::unit()
87 }
88 Expr::For { iterable, body, pat } => {
89 let iterable_ty = self.infer_expr(*iterable, &Expectation::none());
90
91 let pat_ty = match self.resolve_into_iter_item() {
92 Some(into_iter_item_alias) => {
93 let pat_ty = self.new_type_var();
94 let projection = ProjectionPredicate {
95 ty: pat_ty.clone(),
96 projection_ty: ProjectionTy {
97 associated_ty: into_iter_item_alias,
98 parameters: Substs::single(iterable_ty),
99 },
100 };
101 self.obligations.push(Obligation::Projection(projection));
102 self.resolve_ty_as_possible(&mut vec![], pat_ty)
103 }
104 None => Ty::Unknown,
105 };
106
107 self.infer_pat(*pat, &pat_ty, BindingMode::default());
108 self.infer_expr(*body, &Expectation::has_type(Ty::unit()));
109 Ty::unit()
110 }
111 Expr::Lambda { body, args, arg_types } => {
112 assert_eq!(args.len(), arg_types.len());
113
114 let mut sig_tys = Vec::new();
115
116 for (arg_pat, arg_type) in args.iter().zip(arg_types.iter()) {
117 let expected = if let Some(type_ref) = arg_type {
118 self.make_ty(type_ref)
119 } else {
120 Ty::Unknown
121 };
122 let arg_ty = self.infer_pat(*arg_pat, &expected, BindingMode::default());
123 sig_tys.push(arg_ty);
124 }
125
126 // add return type
127 let ret_ty = self.new_type_var();
128 sig_tys.push(ret_ty.clone());
129 let sig_ty = Ty::apply(
130 TypeCtor::FnPtr { num_args: sig_tys.len() as u16 - 1 },
131 Substs(sig_tys.into()),
132 );
133 let closure_ty = Ty::apply_one(
134 TypeCtor::Closure { def: self.body.owner(), expr: tgt_expr },
135 sig_ty,
136 );
137
138 // Eagerly try to relate the closure type with the expected
139 // type, otherwise we often won't have enough information to
140 // infer the body.
141 self.coerce(&closure_ty, &expected.ty);
142
143 self.infer_expr(*body, &Expectation::has_type(ret_ty));
144 closure_ty
145 }
146 Expr::Call { callee, args } => {
147 let callee_ty = self.infer_expr(*callee, &Expectation::none());
148 let (param_tys, ret_ty) = match callee_ty.callable_sig(self.db) {
149 Some(sig) => (sig.params().to_vec(), sig.ret().clone()),
150 None => {
151 // Not callable
152 // FIXME: report an error
153 (Vec::new(), Ty::Unknown)
154 }
155 };
156 self.register_obligations_for_call(&callee_ty);
157 self.check_call_arguments(args, &param_tys);
158 let ret_ty = self.normalize_associated_types_in(ret_ty);
159 ret_ty
160 }
161 Expr::MethodCall { receiver, args, method_name, generic_args } => self
162 .infer_method_call(tgt_expr, *receiver, &args, &method_name, generic_args.as_ref()),
163 Expr::Match { expr, arms } => {
164 let input_ty = self.infer_expr(*expr, &Expectation::none());
165
166 let mut result_ty = self.new_maybe_never_type_var();
167
168 for arm in arms {
169 for &pat in &arm.pats {
170 let _pat_ty = self.infer_pat(pat, &input_ty, BindingMode::default());
171 }
172 if let Some(guard_expr) = arm.guard {
173 self.infer_expr(
174 guard_expr,
175 &Expectation::has_type(Ty::simple(TypeCtor::Bool)),
176 );
177 }
178
179 let arm_ty = self.infer_expr_inner(arm.expr, &expected);
180 result_ty = self.coerce_merge_branch(&result_ty, &arm_ty);
181 }
182
183 result_ty
184 }
185 Expr::Path(p) => {
186 // FIXME this could be more efficient...
187 let resolver = expr::resolver_for_expr(self.body.clone(), self.db, tgt_expr);
188 self.infer_path(&resolver, p, tgt_expr.into()).unwrap_or(Ty::Unknown)
189 }
190 Expr::Continue => Ty::simple(TypeCtor::Never),
191 Expr::Break { expr } => {
192 if let Some(expr) = expr {
193 // FIXME handle break with value
194 self.infer_expr(*expr, &Expectation::none());
195 }
196 Ty::simple(TypeCtor::Never)
197 }
198 Expr::Return { expr } => {
199 if let Some(expr) = expr {
200 self.infer_expr(*expr, &Expectation::has_type(self.return_ty.clone()));
201 }
202 Ty::simple(TypeCtor::Never)
203 }
204 Expr::RecordLit { path, fields, spread } => {
205 let (ty, def_id) = self.resolve_variant(path.as_ref());
206 if let Some(variant) = def_id {
207 self.write_variant_resolution(tgt_expr.into(), variant);
208 }
209
210 self.unify(&ty, &expected.ty);
211
212 let substs = ty.substs().unwrap_or_else(Substs::empty);
213 for (field_idx, field) in fields.iter().enumerate() {
214 let field_ty = def_id
215 .and_then(|it| match it.field(self.db, &field.name) {
216 Some(field) => Some(field),
217 None => {
218 self.push_diagnostic(InferenceDiagnostic::NoSuchField {
219 expr: tgt_expr,
220 field: field_idx,
221 });
222 None
223 }
224 })
225 .map_or(Ty::Unknown, |field| field.ty(self.db))
226 .subst(&substs);
227 self.infer_expr_coerce(field.expr, &Expectation::has_type(field_ty));
228 }
229 if let Some(expr) = spread {
230 self.infer_expr(*expr, &Expectation::has_type(ty.clone()));
231 }
232 ty
233 }
234 Expr::Field { expr, name } => {
235 let receiver_ty = self.infer_expr(*expr, &Expectation::none());
236 let canonicalized = self.canonicalizer().canonicalize_ty(receiver_ty);
237 let ty = autoderef::autoderef(
238 self.db,
239 &self.resolver.clone(),
240 canonicalized.value.clone(),
241 )
242 .find_map(|derefed_ty| match canonicalized.decanonicalize_ty(derefed_ty.value) {
243 Ty::Apply(a_ty) => match a_ty.ctor {
244 TypeCtor::Tuple { .. } => name
245 .as_tuple_index()
246 .and_then(|idx| a_ty.parameters.0.get(idx).cloned()),
247 TypeCtor::Adt(Adt::Struct(s)) => s.field(self.db, name).map(|field| {
248 self.write_field_resolution(tgt_expr, field);
249 field.ty(self.db).subst(&a_ty.parameters)
250 }),
251 _ => None,
252 },
253 _ => None,
254 })
255 .unwrap_or(Ty::Unknown);
256 let ty = self.insert_type_vars(ty);
257 self.normalize_associated_types_in(ty)
258 }
259 Expr::Await { expr } => {
260 let inner_ty = self.infer_expr(*expr, &Expectation::none());
261 let ty = match self.resolve_future_future_output() {
262 Some(future_future_output_alias) => {
263 let ty = self.new_type_var();
264 let projection = ProjectionPredicate {
265 ty: ty.clone(),
266 projection_ty: ProjectionTy {
267 associated_ty: future_future_output_alias,
268 parameters: Substs::single(inner_ty),
269 },
270 };
271 self.obligations.push(Obligation::Projection(projection));
272 self.resolve_ty_as_possible(&mut vec![], ty)
273 }
274 None => Ty::Unknown,
275 };
276 ty
277 }
278 Expr::Try { expr } => {
279 let inner_ty = self.infer_expr(*expr, &Expectation::none());
280 let ty = match self.resolve_ops_try_ok() {
281 Some(ops_try_ok_alias) => {
282 let ty = self.new_type_var();
283 let projection = ProjectionPredicate {
284 ty: ty.clone(),
285 projection_ty: ProjectionTy {
286 associated_ty: ops_try_ok_alias,
287 parameters: Substs::single(inner_ty),
288 },
289 };
290 self.obligations.push(Obligation::Projection(projection));
291 self.resolve_ty_as_possible(&mut vec![], ty)
292 }
293 None => Ty::Unknown,
294 };
295 ty
296 }
297 Expr::Cast { expr, type_ref } => {
298 let _inner_ty = self.infer_expr(*expr, &Expectation::none());
299 let cast_ty = self.make_ty(type_ref);
300 // FIXME check the cast...
301 cast_ty
302 }
303 Expr::Ref { expr, mutability } => {
304 let expectation =
305 if let Some((exp_inner, exp_mutability)) = &expected.ty.as_reference() {
306 if *exp_mutability == Mutability::Mut && *mutability == Mutability::Shared {
307 // FIXME: throw type error - expected mut reference but found shared ref,
308 // which cannot be coerced
309 }
310 Expectation::has_type(Ty::clone(exp_inner))
311 } else {
312 Expectation::none()
313 };
314 // FIXME reference coercions etc.
315 let inner_ty = self.infer_expr(*expr, &expectation);
316 Ty::apply_one(TypeCtor::Ref(*mutability), inner_ty)
317 }
318 Expr::Box { expr } => {
319 let inner_ty = self.infer_expr(*expr, &Expectation::none());
320 if let Some(box_) = self.resolve_boxed_box() {
321 Ty::apply_one(TypeCtor::Adt(box_), inner_ty)
322 } else {
323 Ty::Unknown
324 }
325 }
326 Expr::UnaryOp { expr, op } => {
327 let inner_ty = self.infer_expr(*expr, &Expectation::none());
328 match op {
329 UnaryOp::Deref => {
330 let canonicalized = self.canonicalizer().canonicalize_ty(inner_ty);
331 if let Some(derefed_ty) =
332 autoderef::deref(self.db, &self.resolver, &canonicalized.value)
333 {
334 canonicalized.decanonicalize_ty(derefed_ty.value)
335 } else {
336 Ty::Unknown
337 }
338 }
339 UnaryOp::Neg => {
340 match &inner_ty {
341 Ty::Apply(a_ty) => match a_ty.ctor {
342 TypeCtor::Int(primitive::UncertainIntTy::Unknown)
343 | TypeCtor::Int(primitive::UncertainIntTy::Known(
344 primitive::IntTy {
345 signedness: primitive::Signedness::Signed,
346 ..
347 },
348 ))
349 | TypeCtor::Float(..) => inner_ty,
350 _ => Ty::Unknown,
351 },
352 Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => {
353 inner_ty
354 }
355 // FIXME: resolve ops::Neg trait
356 _ => Ty::Unknown,
357 }
358 }
359 UnaryOp::Not => {
360 match &inner_ty {
361 Ty::Apply(a_ty) => match a_ty.ctor {
362 TypeCtor::Bool | TypeCtor::Int(_) => inner_ty,
363 _ => Ty::Unknown,
364 },
365 Ty::Infer(InferTy::IntVar(..)) => inner_ty,
366 // FIXME: resolve ops::Not trait for inner_ty
367 _ => Ty::Unknown,
368 }
369 }
370 }
371 }
372 Expr::BinaryOp { lhs, rhs, op } => match op {
373 Some(op) => {
374 let lhs_expectation = match op {
375 BinaryOp::LogicOp(..) => Expectation::has_type(Ty::simple(TypeCtor::Bool)),
376 _ => Expectation::none(),
377 };
378 let lhs_ty = self.infer_expr(*lhs, &lhs_expectation);
379 // FIXME: find implementation of trait corresponding to operation
380 // symbol and resolve associated `Output` type
381 let rhs_expectation = op::binary_op_rhs_expectation(*op, lhs_ty);
382 let rhs_ty = self.infer_expr(*rhs, &Expectation::has_type(rhs_expectation));
383
384 // FIXME: similar as above, return ty is often associated trait type
385 op::binary_op_return_ty(*op, rhs_ty)
386 }
387 _ => Ty::Unknown,
388 },
389 Expr::Index { base, index } => {
390 let _base_ty = self.infer_expr(*base, &Expectation::none());
391 let _index_ty = self.infer_expr(*index, &Expectation::none());
392 // FIXME: use `std::ops::Index::Output` to figure out the real return type
393 Ty::Unknown
394 }
395 Expr::Tuple { exprs } => {
396 let mut tys = match &expected.ty {
397 ty_app!(TypeCtor::Tuple { .. }, st) => st
398 .iter()
399 .cloned()
400 .chain(repeat_with(|| self.new_type_var()))
401 .take(exprs.len())
402 .collect::<Vec<_>>(),
403 _ => (0..exprs.len()).map(|_| self.new_type_var()).collect(),
404 };
405
406 for (expr, ty) in exprs.iter().zip(tys.iter_mut()) {
407 self.infer_expr_coerce(*expr, &Expectation::has_type(ty.clone()));
408 }
409
410 Ty::apply(TypeCtor::Tuple { cardinality: tys.len() as u16 }, Substs(tys.into()))
411 }
412 Expr::Array(array) => {
413 let elem_ty = match &expected.ty {
414 ty_app!(TypeCtor::Array, st) | ty_app!(TypeCtor::Slice, st) => {
415 st.as_single().clone()
416 }
417 _ => self.new_type_var(),
418 };
419
420 match array {
421 Array::ElementList(items) => {
422 for expr in items.iter() {
423 self.infer_expr_coerce(*expr, &Expectation::has_type(elem_ty.clone()));
424 }
425 }
426 Array::Repeat { initializer, repeat } => {
427 self.infer_expr_coerce(
428 *initializer,
429 &Expectation::has_type(elem_ty.clone()),
430 );
431 self.infer_expr(
432 *repeat,
433 &Expectation::has_type(Ty::simple(TypeCtor::Int(
434 primitive::UncertainIntTy::Known(primitive::IntTy::usize()),
435 ))),
436 );
437 }
438 }
439
440 Ty::apply_one(TypeCtor::Array, elem_ty)
441 }
442 Expr::Literal(lit) => match lit {
443 Literal::Bool(..) => Ty::simple(TypeCtor::Bool),
444 Literal::String(..) => {
445 Ty::apply_one(TypeCtor::Ref(Mutability::Shared), Ty::simple(TypeCtor::Str))
446 }
447 Literal::ByteString(..) => {
448 let byte_type = Ty::simple(TypeCtor::Int(primitive::UncertainIntTy::Known(
449 primitive::IntTy::u8(),
450 )));
451 let slice_type = Ty::apply_one(TypeCtor::Slice, byte_type);
452 Ty::apply_one(TypeCtor::Ref(Mutability::Shared), slice_type)
453 }
454 Literal::Char(..) => Ty::simple(TypeCtor::Char),
455 Literal::Int(_v, ty) => Ty::simple(TypeCtor::Int(*ty)),
456 Literal::Float(_v, ty) => Ty::simple(TypeCtor::Float(*ty)),
457 },
458 };
459 // use a new type variable if we got Ty::Unknown here
460 let ty = self.insert_type_vars_shallow(ty);
461 let ty = self.resolve_ty_as_possible(&mut vec![], ty);
462 self.write_expr_ty(tgt_expr, ty.clone());
463 ty
464 }
465
466 fn infer_block(
467 &mut self,
468 statements: &[Statement],
469 tail: Option<ExprId>,
470 expected: &Expectation,
471 ) -> Ty {
472 let mut diverges = false;
473 for stmt in statements {
474 match stmt {
475 Statement::Let { pat, type_ref, initializer } => {
476 let decl_ty =
477 type_ref.as_ref().map(|tr| self.make_ty(tr)).unwrap_or(Ty::Unknown);
478
479 // Always use the declared type when specified
480 let mut ty = decl_ty.clone();
481
482 if let Some(expr) = initializer {
483 let actual_ty =
484 self.infer_expr_coerce(*expr, &Expectation::has_type(decl_ty.clone()));
485 if decl_ty == Ty::Unknown {
486 ty = actual_ty;
487 }
488 }
489
490 let ty = self.resolve_ty_as_possible(&mut vec![], ty);
491 self.infer_pat(*pat, &ty, BindingMode::default());
492 }
493 Statement::Expr(expr) => {
494 if let ty_app!(TypeCtor::Never) = self.infer_expr(*expr, &Expectation::none()) {
495 diverges = true;
496 }
497 }
498 }
499 }
500
501 let ty = if let Some(expr) = tail {
502 self.infer_expr_coerce(expr, expected)
503 } else {
504 self.coerce(&Ty::unit(), &expected.ty);
505 Ty::unit()
506 };
507 if diverges {
508 Ty::simple(TypeCtor::Never)
509 } else {
510 ty
511 }
512 }
513
514 fn infer_method_call(
515 &mut self,
516 tgt_expr: ExprId,
517 receiver: ExprId,
518 args: &[ExprId],
519 method_name: &Name,
520 generic_args: Option<&GenericArgs>,
521 ) -> Ty {
522 let receiver_ty = self.infer_expr(receiver, &Expectation::none());
523 let canonicalized_receiver = self.canonicalizer().canonicalize_ty(receiver_ty.clone());
524 let resolved = method_resolution::lookup_method(
525 &canonicalized_receiver.value,
526 self.db,
527 method_name,
528 &self.resolver,
529 );
530 let (derefed_receiver_ty, method_ty, def_generics) = match resolved {
531 Some((ty, func)) => {
532 let ty = canonicalized_receiver.decanonicalize_ty(ty);
533 self.write_method_resolution(tgt_expr, func);
534 (
535 ty,
536 self.db.type_for_def(func.into(), Namespace::Values),
537 Some(func.generic_params(self.db)),
538 )
539 }
540 None => (receiver_ty, Ty::Unknown, None),
541 };
542 let substs = self.substs_for_method_call(def_generics, generic_args, &derefed_receiver_ty);
543 let method_ty = method_ty.apply_substs(substs);
544 let method_ty = self.insert_type_vars(method_ty);
545 self.register_obligations_for_call(&method_ty);
546 let (expected_receiver_ty, param_tys, ret_ty) = match method_ty.callable_sig(self.db) {
547 Some(sig) => {
548 if !sig.params().is_empty() {
549 (sig.params()[0].clone(), sig.params()[1..].to_vec(), sig.ret().clone())
550 } else {
551 (Ty::Unknown, Vec::new(), sig.ret().clone())
552 }
553 }
554 None => (Ty::Unknown, Vec::new(), Ty::Unknown),
555 };
556 // Apply autoref so the below unification works correctly
557 // FIXME: return correct autorefs from lookup_method
558 let actual_receiver_ty = match expected_receiver_ty.as_reference() {
559 Some((_, mutability)) => Ty::apply_one(TypeCtor::Ref(mutability), derefed_receiver_ty),
560 _ => derefed_receiver_ty,
561 };
562 self.unify(&expected_receiver_ty, &actual_receiver_ty);
563
564 self.check_call_arguments(args, &param_tys);
565 let ret_ty = self.normalize_associated_types_in(ret_ty);
566 ret_ty
567 }
568
569 fn check_call_arguments(&mut self, args: &[ExprId], param_tys: &[Ty]) {
570 // Quoting https://github.com/rust-lang/rust/blob/6ef275e6c3cb1384ec78128eceeb4963ff788dca/src/librustc_typeck/check/mod.rs#L3325 --
571 // We do this in a pretty awful way: first we type-check any arguments
572 // that are not closures, then we type-check the closures. This is so
573 // that we have more information about the types of arguments when we
574 // type-check the functions. This isn't really the right way to do this.
575 for &check_closures in &[false, true] {
576 let param_iter = param_tys.iter().cloned().chain(repeat(Ty::Unknown));
577 for (&arg, param_ty) in args.iter().zip(param_iter) {
578 let is_closure = match &self.body[arg] {
579 Expr::Lambda { .. } => true,
580 _ => false,
581 };
582
583 if is_closure != check_closures {
584 continue;
585 }
586
587 let param_ty = self.normalize_associated_types_in(param_ty);
588 self.infer_expr_coerce(arg, &Expectation::has_type(param_ty.clone()));
589 }
590 }
591 }
592
593 fn substs_for_method_call(
594 &mut self,
595 def_generics: Option<Arc<GenericParams>>,
596 generic_args: Option<&GenericArgs>,
597 receiver_ty: &Ty,
598 ) -> Substs {
599 let (parent_param_count, param_count) =
600 def_generics.as_ref().map_or((0, 0), |g| (g.count_parent_params(), g.params.len()));
601 let mut substs = Vec::with_capacity(parent_param_count + param_count);
602 // Parent arguments are unknown, except for the receiver type
603 if let Some(parent_generics) = def_generics.and_then(|p| p.parent_params.clone()) {
604 for param in &parent_generics.params {
605 if param.name == name::SELF_TYPE {
606 substs.push(receiver_ty.clone());
607 } else {
608 substs.push(Ty::Unknown);
609 }
610 }
611 }
612 // handle provided type arguments
613 if let Some(generic_args) = generic_args {
614 // if args are provided, it should be all of them, but we can't rely on that
615 for arg in generic_args.args.iter().take(param_count) {
616 match arg {
617 GenericArg::Type(type_ref) => {
618 let ty = self.make_ty(type_ref);
619 substs.push(ty);
620 }
621 }
622 }
623 };
624 let supplied_params = substs.len();
625 for _ in supplied_params..parent_param_count + param_count {
626 substs.push(Ty::Unknown);
627 }
628 assert_eq!(substs.len(), parent_param_count + param_count);
629 Substs(substs.into())
630 }
631
632 fn register_obligations_for_call(&mut self, callable_ty: &Ty) {
633 if let Ty::Apply(a_ty) = callable_ty {
634 if let TypeCtor::FnDef(def) = a_ty.ctor {
635 let generic_predicates = self.db.generic_predicates(def.into());
636 for predicate in generic_predicates.iter() {
637 let predicate = predicate.clone().subst(&a_ty.parameters);
638 if let Some(obligation) = Obligation::from_predicate(predicate) {
639 self.obligations.push(obligation);
640 }
641 }
642 // add obligation for trait implementation, if this is a trait method
643 match def {
644 CallableDef::Function(f) => {
645 if let Some(trait_) = f.parent_trait(self.db) {
646 // construct a TraitDef
647 let substs = a_ty.parameters.prefix(
648 trait_.generic_params(self.db).count_params_including_parent(),
649 );
650 self.obligations.push(Obligation::Trait(TraitRef { trait_, substs }));
651 }
652 }
653 CallableDef::Struct(_) | CallableDef::EnumVariant(_) => {}
654 }
655 }
656 }
657 }
658}
diff --git a/crates/ra_hir/src/ty/infer/pat.rs b/crates/ra_hir/src/ty/infer/pat.rs
new file mode 100644
index 000000000..c125ddfbc
--- /dev/null
+++ b/crates/ra_hir/src/ty/infer/pat.rs
@@ -0,0 +1,180 @@
1//! Type inference for patterns.
2
3use std::iter::repeat;
4use std::sync::Arc;
5
6use test_utils::tested_by;
7
8use super::{BindingMode, InferenceContext};
9use crate::{
10 db::HirDatabase,
11 expr::{BindingAnnotation, Pat, PatId, RecordFieldPat},
12 ty::{Mutability, Substs, Ty, TypeCtor, TypeWalk},
13 Name, Path,
14};
15
16impl<'a, D: HirDatabase> InferenceContext<'a, D> {
17 fn infer_tuple_struct_pat(
18 &mut self,
19 path: Option<&Path>,
20 subpats: &[PatId],
21 expected: &Ty,
22 default_bm: BindingMode,
23 ) -> Ty {
24 let (ty, def) = self.resolve_variant(path);
25
26 self.unify(&ty, expected);
27
28 let substs = ty.substs().unwrap_or_else(Substs::empty);
29
30 for (i, &subpat) in subpats.iter().enumerate() {
31 let expected_ty = def
32 .and_then(|d| d.field(self.db, &Name::new_tuple_field(i)))
33 .map_or(Ty::Unknown, |field| field.ty(self.db))
34 .subst(&substs);
35 let expected_ty = self.normalize_associated_types_in(expected_ty);
36 self.infer_pat(subpat, &expected_ty, default_bm);
37 }
38
39 ty
40 }
41
42 fn infer_record_pat(
43 &mut self,
44 path: Option<&Path>,
45 subpats: &[RecordFieldPat],
46 expected: &Ty,
47 default_bm: BindingMode,
48 id: PatId,
49 ) -> Ty {
50 let (ty, def) = self.resolve_variant(path);
51 if let Some(variant) = def {
52 self.write_variant_resolution(id.into(), variant);
53 }
54
55 self.unify(&ty, expected);
56
57 let substs = ty.substs().unwrap_or_else(Substs::empty);
58
59 for subpat in subpats {
60 let matching_field = def.and_then(|it| it.field(self.db, &subpat.name));
61 let expected_ty =
62 matching_field.map_or(Ty::Unknown, |field| field.ty(self.db)).subst(&substs);
63 let expected_ty = self.normalize_associated_types_in(expected_ty);
64 self.infer_pat(subpat.pat, &expected_ty, default_bm);
65 }
66
67 ty
68 }
69
70 pub(super) fn infer_pat(
71 &mut self,
72 pat: PatId,
73 mut expected: &Ty,
74 mut default_bm: BindingMode,
75 ) -> Ty {
76 let body = Arc::clone(&self.body); // avoid borrow checker problem
77
78 let is_non_ref_pat = match &body[pat] {
79 Pat::Tuple(..)
80 | Pat::TupleStruct { .. }
81 | Pat::Record { .. }
82 | Pat::Range { .. }
83 | Pat::Slice { .. } => true,
84 // FIXME: Path/Lit might actually evaluate to ref, but inference is unimplemented.
85 Pat::Path(..) | Pat::Lit(..) => true,
86 Pat::Wild | Pat::Bind { .. } | Pat::Ref { .. } | Pat::Missing => false,
87 };
88 if is_non_ref_pat {
89 while let Some((inner, mutability)) = expected.as_reference() {
90 expected = inner;
91 default_bm = match default_bm {
92 BindingMode::Move => BindingMode::Ref(mutability),
93 BindingMode::Ref(Mutability::Shared) => BindingMode::Ref(Mutability::Shared),
94 BindingMode::Ref(Mutability::Mut) => BindingMode::Ref(mutability),
95 }
96 }
97 } else if let Pat::Ref { .. } = &body[pat] {
98 tested_by!(match_ergonomics_ref);
99 // When you encounter a `&pat` pattern, reset to Move.
100 // This is so that `w` is by value: `let (_, &w) = &(1, &2);`
101 default_bm = BindingMode::Move;
102 }
103
104 // Lose mutability.
105 let default_bm = default_bm;
106 let expected = expected;
107
108 let ty = match &body[pat] {
109 Pat::Tuple(ref args) => {
110 let expectations = match expected.as_tuple() {
111 Some(parameters) => &*parameters.0,
112 _ => &[],
113 };
114 let expectations_iter = expectations.iter().chain(repeat(&Ty::Unknown));
115
116 let inner_tys = args
117 .iter()
118 .zip(expectations_iter)
119 .map(|(&pat, ty)| self.infer_pat(pat, ty, default_bm))
120 .collect();
121
122 Ty::apply(TypeCtor::Tuple { cardinality: args.len() as u16 }, Substs(inner_tys))
123 }
124 Pat::Ref { pat, mutability } => {
125 let expectation = match expected.as_reference() {
126 Some((inner_ty, exp_mut)) => {
127 if *mutability != exp_mut {
128 // FIXME: emit type error?
129 }
130 inner_ty
131 }
132 _ => &Ty::Unknown,
133 };
134 let subty = self.infer_pat(*pat, expectation, default_bm);
135 Ty::apply_one(TypeCtor::Ref(*mutability), subty)
136 }
137 Pat::TupleStruct { path: p, args: subpats } => {
138 self.infer_tuple_struct_pat(p.as_ref(), subpats, expected, default_bm)
139 }
140 Pat::Record { path: p, args: fields } => {
141 self.infer_record_pat(p.as_ref(), fields, expected, default_bm, pat)
142 }
143 Pat::Path(path) => {
144 // FIXME use correct resolver for the surrounding expression
145 let resolver = self.resolver.clone();
146 self.infer_path(&resolver, &path, pat.into()).unwrap_or(Ty::Unknown)
147 }
148 Pat::Bind { mode, name: _, subpat } => {
149 let mode = if mode == &BindingAnnotation::Unannotated {
150 default_bm
151 } else {
152 BindingMode::convert(*mode)
153 };
154 let inner_ty = if let Some(subpat) = subpat {
155 self.infer_pat(*subpat, expected, default_bm)
156 } else {
157 expected.clone()
158 };
159 let inner_ty = self.insert_type_vars_shallow(inner_ty);
160
161 let bound_ty = match mode {
162 BindingMode::Ref(mutability) => {
163 Ty::apply_one(TypeCtor::Ref(mutability), inner_ty.clone())
164 }
165 BindingMode::Move => inner_ty.clone(),
166 };
167 let bound_ty = self.resolve_ty_as_possible(&mut vec![], bound_ty);
168 self.write_pat_ty(pat, bound_ty);
169 return inner_ty;
170 }
171 _ => Ty::Unknown,
172 };
173 // use a new type variable if we got Ty::Unknown here
174 let ty = self.insert_type_vars_shallow(ty);
175 self.unify(&ty, expected);
176 let ty = self.resolve_ty_as_possible(&mut vec![], ty);
177 self.write_pat_ty(pat, ty.clone());
178 ty
179 }
180}
diff --git a/crates/ra_hir/src/ty/infer/unify.rs b/crates/ra_hir/src/ty/infer/unify.rs
index d161aa6b3..ca33cc7f8 100644
--- a/crates/ra_hir/src/ty/infer/unify.rs
+++ b/crates/ra_hir/src/ty/infer/unify.rs
@@ -6,6 +6,7 @@ use crate::ty::{
6 Canonical, InEnvironment, InferTy, ProjectionPredicate, ProjectionTy, Substs, TraitRef, Ty, 6 Canonical, InEnvironment, InferTy, ProjectionPredicate, ProjectionTy, Substs, TraitRef, Ty,
7 TypeWalk, 7 TypeWalk,
8}; 8};
9use crate::util::make_mut_slice;
9 10
10impl<'a, D: HirDatabase> InferenceContext<'a, D> { 11impl<'a, D: HirDatabase> InferenceContext<'a, D> {
11 pub(super) fn canonicalizer<'b>(&'b mut self) -> Canonicalizer<'a, 'b, D> 12 pub(super) fn canonicalizer<'b>(&'b mut self) -> Canonicalizer<'a, 'b, D>
@@ -53,7 +54,9 @@ where
53 // recursive type 54 // recursive type
54 return tv.fallback_value(); 55 return tv.fallback_value();
55 } 56 }
56 if let Some(known_ty) = self.ctx.var_unification_table.probe_value(inner).known() { 57 if let Some(known_ty) =
58 self.ctx.var_unification_table.inlined_probe_value(inner).known()
59 {
57 self.var_stack.push(inner); 60 self.var_stack.push(inner);
58 let result = self.do_canonicalize_ty(known_ty.clone()); 61 let result = self.do_canonicalize_ty(known_ty.clone());
59 self.var_stack.pop(); 62 self.var_stack.pop();
@@ -74,10 +77,11 @@ where
74 }) 77 })
75 } 78 }
76 79
77 fn do_canonicalize_trait_ref(&mut self, trait_ref: TraitRef) -> TraitRef { 80 fn do_canonicalize_trait_ref(&mut self, mut trait_ref: TraitRef) -> TraitRef {
78 let substs = 81 for ty in make_mut_slice(&mut trait_ref.substs.0) {
79 trait_ref.substs.iter().map(|ty| self.do_canonicalize_ty(ty.clone())).collect(); 82 *ty = self.do_canonicalize_ty(ty.clone());
80 TraitRef { trait_: trait_ref.trait_, substs: Substs(substs) } 83 }
84 trait_ref
81 } 85 }
82 86
83 fn into_canonicalized<T>(self, result: T) -> Canonicalized<T> { 87 fn into_canonicalized<T>(self, result: T) -> Canonicalized<T> {
@@ -87,10 +91,11 @@ where
87 } 91 }
88 } 92 }
89 93
90 fn do_canonicalize_projection_ty(&mut self, projection_ty: ProjectionTy) -> ProjectionTy { 94 fn do_canonicalize_projection_ty(&mut self, mut projection_ty: ProjectionTy) -> ProjectionTy {
91 let params = 95 for ty in make_mut_slice(&mut projection_ty.parameters.0) {
92 projection_ty.parameters.iter().map(|ty| self.do_canonicalize_ty(ty.clone())).collect(); 96 *ty = self.do_canonicalize_ty(ty.clone());
93 ProjectionTy { associated_ty: projection_ty.associated_ty, parameters: Substs(params) } 97 }
98 projection_ty
94 } 99 }
95 100
96 fn do_canonicalize_projection_predicate( 101 fn do_canonicalize_projection_predicate(
diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs
index 4b67c82e7..366556134 100644
--- a/crates/ra_hir/src/ty/lower.rs
+++ b/crates/ra_hir/src/ty/lower.rs
@@ -22,6 +22,7 @@ use crate::{
22 resolve::{Resolver, TypeNs}, 22 resolve::{Resolver, TypeNs},
23 ty::Adt, 23 ty::Adt,
24 type_ref::{TypeBound, TypeRef}, 24 type_ref::{TypeBound, TypeRef},
25 util::make_mut_slice,
25 BuiltinType, Const, Enum, EnumVariant, Function, ModuleDef, Path, Static, Struct, StructField, 26 BuiltinType, Const, Enum, EnumVariant, Function, ModuleDef, Path, Static, Struct, StructField,
26 Trait, TypeAlias, Union, 27 Trait, TypeAlias, Union,
27}; 28};
@@ -31,11 +32,11 @@ impl Ty {
31 match type_ref { 32 match type_ref {
32 TypeRef::Never => Ty::simple(TypeCtor::Never), 33 TypeRef::Never => Ty::simple(TypeCtor::Never),
33 TypeRef::Tuple(inner) => { 34 TypeRef::Tuple(inner) => {
34 let inner_tys = 35 let inner_tys: Arc<[Ty]> =
35 inner.iter().map(|tr| Ty::from_hir(db, resolver, tr)).collect::<Vec<_>>(); 36 inner.iter().map(|tr| Ty::from_hir(db, resolver, tr)).collect();
36 Ty::apply( 37 Ty::apply(
37 TypeCtor::Tuple { cardinality: inner_tys.len() as u16 }, 38 TypeCtor::Tuple { cardinality: inner_tys.len() as u16 },
38 Substs(inner_tys.into()), 39 Substs(inner_tys),
39 ) 40 )
40 } 41 }
41 TypeRef::Path(path) => Ty::from_hir_path(db, resolver, path), 42 TypeRef::Path(path) => Ty::from_hir_path(db, resolver, path),
@@ -57,9 +58,7 @@ impl Ty {
57 } 58 }
58 TypeRef::Placeholder => Ty::Unknown, 59 TypeRef::Placeholder => Ty::Unknown,
59 TypeRef::Fn(params) => { 60 TypeRef::Fn(params) => {
60 let inner_tys = 61 let sig = Substs(params.iter().map(|tr| Ty::from_hir(db, resolver, tr)).collect());
61 params.iter().map(|tr| Ty::from_hir(db, resolver, tr)).collect::<Vec<_>>();
62 let sig = Substs(inner_tys.into());
63 Ty::apply(TypeCtor::FnPtr { num_args: sig.len() as u16 - 1 }, sig) 62 Ty::apply(TypeCtor::FnPtr { num_args: sig.len() as u16 - 1 }, sig)
64 } 63 }
65 TypeRef::DynTrait(bounds) => { 64 TypeRef::DynTrait(bounds) => {
@@ -69,8 +68,8 @@ impl Ty {
69 .flat_map(|b| { 68 .flat_map(|b| {
70 GenericPredicate::from_type_bound(db, resolver, b, self_ty.clone()) 69 GenericPredicate::from_type_bound(db, resolver, b, self_ty.clone())
71 }) 70 })
72 .collect::<Vec<_>>(); 71 .collect();
73 Ty::Dyn(predicates.into()) 72 Ty::Dyn(predicates)
74 } 73 }
75 TypeRef::ImplTrait(bounds) => { 74 TypeRef::ImplTrait(bounds) => {
76 let self_ty = Ty::Bound(0); 75 let self_ty = Ty::Bound(0);
@@ -79,8 +78,8 @@ impl Ty {
79 .flat_map(|b| { 78 .flat_map(|b| {
80 GenericPredicate::from_type_bound(db, resolver, b, self_ty.clone()) 79 GenericPredicate::from_type_bound(db, resolver, b, self_ty.clone())
81 }) 80 })
82 .collect::<Vec<_>>(); 81 .collect();
83 Ty::Opaque(predicates.into()) 82 Ty::Opaque(predicates)
84 } 83 }
85 TypeRef::Error => Ty::Unknown, 84 TypeRef::Error => Ty::Unknown,
86 } 85 }
@@ -175,6 +174,7 @@ impl Ty {
175 Ty::Param { idx, name } 174 Ty::Param { idx, name }
176 } 175 }
177 TypeNs::SelfType(impl_block) => impl_block.target_ty(db), 176 TypeNs::SelfType(impl_block) => impl_block.target_ty(db),
177 TypeNs::AdtSelfType(adt) => adt.ty(db),
178 178
179 TypeNs::Adt(it) => Ty::from_hir_path_inner(db, resolver, resolved_segment, it.into()), 179 TypeNs::Adt(it) => Ty::from_hir_path_inner(db, resolver, resolved_segment, it.into()),
180 TypeNs::BuiltinType(it) => { 180 TypeNs::BuiltinType(it) => {
@@ -391,10 +391,7 @@ impl TraitRef {
391 ) -> Self { 391 ) -> Self {
392 let mut substs = TraitRef::substs_from_path(db, resolver, segment, resolved); 392 let mut substs = TraitRef::substs_from_path(db, resolver, segment, resolved);
393 if let Some(self_ty) = explicit_self_ty { 393 if let Some(self_ty) = explicit_self_ty {
394 // FIXME this could be nicer 394 make_mut_slice(&mut substs.0)[0] = self_ty;
395 let mut substs_vec = substs.0.to_vec();
396 substs_vec[0] = self_ty;
397 substs.0 = substs_vec.into();
398 } 395 }
399 TraitRef { trait_: resolved, substs } 396 TraitRef { trait_: resolved, substs }
400 } 397 }
@@ -557,13 +554,12 @@ pub(crate) fn generic_predicates_for_param_query(
557 param_idx: u32, 554 param_idx: u32,
558) -> Arc<[GenericPredicate]> { 555) -> Arc<[GenericPredicate]> {
559 let resolver = def.resolver(db); 556 let resolver = def.resolver(db);
560 let predicates = resolver 557 resolver
561 .where_predicates_in_scope() 558 .where_predicates_in_scope()
562 // we have to filter out all other predicates *first*, before attempting to lower them 559 // we have to filter out all other predicates *first*, before attempting to lower them
563 .filter(|pred| Ty::from_hir_only_param(db, &resolver, &pred.type_ref) == Some(param_idx)) 560 .filter(|pred| Ty::from_hir_only_param(db, &resolver, &pred.type_ref) == Some(param_idx))
564 .flat_map(|pred| GenericPredicate::from_where_predicate(db, &resolver, pred)) 561 .flat_map(|pred| GenericPredicate::from_where_predicate(db, &resolver, pred))
565 .collect::<Vec<_>>(); 562 .collect()
566 predicates.into()
567} 563}
568 564
569pub(crate) fn trait_env( 565pub(crate) fn trait_env(
@@ -584,11 +580,10 @@ pub(crate) fn generic_predicates_query(
584 def: GenericDef, 580 def: GenericDef,
585) -> Arc<[GenericPredicate]> { 581) -> Arc<[GenericPredicate]> {
586 let resolver = def.resolver(db); 582 let resolver = def.resolver(db);
587 let predicates = resolver 583 resolver
588 .where_predicates_in_scope() 584 .where_predicates_in_scope()
589 .flat_map(|pred| GenericPredicate::from_where_predicate(db, &resolver, pred)) 585 .flat_map(|pred| GenericPredicate::from_where_predicate(db, &resolver, pred))
590 .collect::<Vec<_>>(); 586 .collect()
591 predicates.into()
592} 587}
593 588
594/// Resolve the default type params from generics 589/// Resolve the default type params from generics
@@ -602,9 +597,9 @@ pub(crate) fn generic_defaults_query(db: &impl HirDatabase, def: GenericDef) ->
602 .map(|p| { 597 .map(|p| {
603 p.default.as_ref().map_or(Ty::Unknown, |path| Ty::from_hir_path(db, &resolver, path)) 598 p.default.as_ref().map_or(Ty::Unknown, |path| Ty::from_hir_path(db, &resolver, path))
604 }) 599 })
605 .collect::<Vec<_>>(); 600 .collect();
606 601
607 Substs(defaults.into()) 602 Substs(defaults)
608} 603}
609 604
610fn fn_sig_for_fn(db: &impl HirDatabase, def: Function) -> FnSig { 605fn fn_sig_for_fn(db: &impl HirDatabase, def: Function) -> FnSig {
diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs
index 25dad81eb..c12326643 100644
--- a/crates/ra_hir/src/ty/tests.rs
+++ b/crates/ra_hir/src/ty/tests.rs
@@ -3,7 +3,6 @@ use std::sync::Arc;
3 3
4use insta::assert_snapshot; 4use insta::assert_snapshot;
5 5
6use ra_cfg::CfgOptions;
7use ra_db::{salsa::Database, FilePosition, SourceDatabase}; 6use ra_db::{salsa::Database, FilePosition, SourceDatabase};
8use ra_syntax::{ 7use ra_syntax::{
9 algo, 8 algo,
@@ -62,7 +61,7 @@ impl S {
62"#, 61"#,
63 ); 62 );
64 db.set_crate_graph_from_fixture(crate_graph! { 63 db.set_crate_graph_from_fixture(crate_graph! {
65 "main": ("/main.rs", ["foo"], CfgOptions::default().atom("test".into())), 64 "main": ("/main.rs", ["foo"], cfg = { "test" }),
66 "foo": ("/foo.rs", []), 65 "foo": ("/foo.rs", []),
67 }); 66 });
68 assert_eq!("(i32, {unknown}, i32, {unknown})", type_at_pos(&db, pos)); 67 assert_eq!("(i32, {unknown}, i32, {unknown})", type_at_pos(&db, pos));
@@ -135,6 +134,25 @@ mod boxed {
135} 134}
136 135
137#[test] 136#[test]
137fn infer_adt_self() {
138 let (db, pos) = MockDatabase::with_position(
139 r#"
140//- /main.rs
141enum Nat { Succ(Self), Demo(Nat), Zero }
142
143fn test() {
144 let foo: Nat = Nat::Zero;
145 if let Nat::Succ(x) = foo {
146 x<|>
147 }
148}
149
150"#,
151 );
152 assert_eq!("Nat", type_at_pos(&db, pos));
153}
154
155#[test]
138fn infer_try() { 156fn infer_try() {
139 let (mut db, pos) = MockDatabase::with_position( 157 let (mut db, pos) = MockDatabase::with_position(
140 r#" 158 r#"
diff --git a/crates/ra_hir/src/ty/traits.rs b/crates/ra_hir/src/ty/traits.rs
index b0f67ae50..0cb5c3798 100644
--- a/crates/ra_hir/src/ty/traits.rs
+++ b/crates/ra_hir/src/ty/traits.rs
@@ -89,7 +89,7 @@ pub(crate) fn impls_for_trait_query(
89 } 89 }
90 let crate_impl_blocks = db.impls_in_crate(krate); 90 let crate_impl_blocks = db.impls_in_crate(krate);
91 impls.extend(crate_impl_blocks.lookup_impl_blocks_for_trait(trait_)); 91 impls.extend(crate_impl_blocks.lookup_impl_blocks_for_trait(trait_));
92 impls.into_iter().collect::<Vec<_>>().into() 92 impls.into_iter().collect()
93} 93}
94 94
95/// A set of clauses that we assume to be true. E.g. if we are inside this function: 95/// A set of clauses that we assume to be true. E.g. if we are inside this function:
diff --git a/crates/ra_hir/src/ty/traits/chalk.rs b/crates/ra_hir/src/ty/traits/chalk.rs
index 2642a54bf..00aaf65d9 100644
--- a/crates/ra_hir/src/ty/traits/chalk.rs
+++ b/crates/ra_hir/src/ty/traits/chalk.rs
@@ -126,8 +126,7 @@ impl ToChalk for Substs {
126 chalk_ir::Parameter(chalk_ir::ParameterKind::Ty(ty)) => from_chalk(db, ty), 126 chalk_ir::Parameter(chalk_ir::ParameterKind::Ty(ty)) => from_chalk(db, ty),
127 chalk_ir::Parameter(chalk_ir::ParameterKind::Lifetime(_)) => unimplemented!(), 127 chalk_ir::Parameter(chalk_ir::ParameterKind::Lifetime(_)) => unimplemented!(),
128 }) 128 })
129 .collect::<Vec<_>>() 129 .collect();
130 .into();
131 Substs(tys) 130 Substs(tys)
132 } 131 }
133} 132}
@@ -491,15 +490,16 @@ pub(crate) fn trait_datum_query(
491 }, 490 },
492 associated_ty_ids: Vec::new(), 491 associated_ty_ids: Vec::new(),
493 where_clauses: Vec::new(), 492 where_clauses: Vec::new(),
494 flags: chalk_rust_ir::TraitFlags {
495 non_enumerable: true,
496 auto: false,
497 marker: false,
498 upstream: true,
499 fundamental: false,
500 },
501 }; 493 };
502 return Arc::new(TraitDatum { binders: make_binders(trait_datum_bound, 1) }); 494
495 let flags = chalk_rust_ir::TraitFlags {
496 auto: false,
497 marker: false,
498 upstream: true,
499 fundamental: false,
500 non_enumerable: true,
501 };
502 return Arc::new(TraitDatum { binders: make_binders(trait_datum_bound, 1), flags });
503 } 503 }
504 let trait_: Trait = from_chalk(db, trait_id); 504 let trait_: Trait = from_chalk(db, trait_id);
505 debug!("trait {:?} = {:?}", trait_id, trait_.name(db)); 505 debug!("trait {:?} = {:?}", trait_id, trait_.name(db));
@@ -525,8 +525,9 @@ pub(crate) fn trait_datum_query(
525 .map(|type_alias| type_alias.to_chalk(db)) 525 .map(|type_alias| type_alias.to_chalk(db))
526 .collect(); 526 .collect();
527 let trait_datum_bound = 527 let trait_datum_bound =
528 chalk_rust_ir::TraitDatumBound { trait_ref, where_clauses, flags, associated_ty_ids }; 528 chalk_rust_ir::TraitDatumBound { trait_ref, where_clauses, associated_ty_ids };
529 let trait_datum = TraitDatum { binders: make_binders(trait_datum_bound, bound_vars.len()) }; 529 let trait_datum =
530 TraitDatum { binders: make_binders(trait_datum_bound, bound_vars.len()), flags };
530 Arc::new(trait_datum) 531 Arc::new(trait_datum)
531} 532}
532 533
@@ -632,18 +633,20 @@ fn impl_block_datum(
632 }) 633 })
633 .collect(); 634 .collect();
634 635
635 let impl_datum_bound = chalk_rust_ir::ImplDatumBound { 636 let polarity = if negative {
636 trait_ref: if negative { 637 chalk_rust_ir::Polarity::Negative
637 chalk_rust_ir::PolarizedTraitRef::Negative(trait_ref) 638 } else {
638 } else { 639 chalk_rust_ir::Polarity::Positive
639 chalk_rust_ir::PolarizedTraitRef::Positive(trait_ref)
640 },
641 where_clauses,
642 associated_ty_values,
643 impl_type,
644 }; 640 };
641
642 let impl_datum_bound =
643 chalk_rust_ir::ImplDatumBound { trait_ref, where_clauses, associated_ty_values };
645 debug!("impl_datum: {:?}", impl_datum_bound); 644 debug!("impl_datum: {:?}", impl_datum_bound);
646 let impl_datum = ImplDatum { binders: make_binders(impl_datum_bound, bound_vars.len()) }; 645 let impl_datum = ImplDatum {
646 binders: make_binders(impl_datum_bound, bound_vars.len()),
647 impl_type,
648 polarity,
649 };
647 Arc::new(impl_datum) 650 Arc::new(impl_datum)
648} 651}
649 652
@@ -653,12 +656,15 @@ fn invalid_impl_datum() -> Arc<ImplDatum> {
653 parameters: vec![chalk_ir::Ty::BoundVar(0).cast()], 656 parameters: vec![chalk_ir::Ty::BoundVar(0).cast()],
654 }; 657 };
655 let impl_datum_bound = chalk_rust_ir::ImplDatumBound { 658 let impl_datum_bound = chalk_rust_ir::ImplDatumBound {
656 trait_ref: chalk_rust_ir::PolarizedTraitRef::Positive(trait_ref), 659 trait_ref,
657 where_clauses: Vec::new(), 660 where_clauses: Vec::new(),
658 associated_ty_values: Vec::new(), 661 associated_ty_values: Vec::new(),
662 };
663 let impl_datum = ImplDatum {
664 binders: make_binders(impl_datum_bound, 1),
659 impl_type: chalk_rust_ir::ImplType::External, 665 impl_type: chalk_rust_ir::ImplType::External,
666 polarity: chalk_rust_ir::Polarity::Positive,
660 }; 667 };
661 let impl_datum = ImplDatum { binders: make_binders(impl_datum_bound, 1) };
662 Arc::new(impl_datum) 668 Arc::new(impl_datum)
663} 669}
664 670
@@ -713,12 +719,15 @@ fn closure_fn_trait_impl_datum(
713 let impl_type = chalk_rust_ir::ImplType::External; 719 let impl_type = chalk_rust_ir::ImplType::External;
714 720
715 let impl_datum_bound = chalk_rust_ir::ImplDatumBound { 721 let impl_datum_bound = chalk_rust_ir::ImplDatumBound {
716 trait_ref: chalk_rust_ir::PolarizedTraitRef::Positive(trait_ref.to_chalk(db)), 722 trait_ref: trait_ref.to_chalk(db),
717 where_clauses: Vec::new(), 723 where_clauses: Vec::new(),
718 associated_ty_values: vec![output_ty_value], 724 associated_ty_values: vec![output_ty_value],
725 };
726 let impl_datum = ImplDatum {
727 binders: make_binders(impl_datum_bound, num_args as usize + 1),
719 impl_type, 728 impl_type,
729 polarity: chalk_rust_ir::Polarity::Positive,
720 }; 730 };
721 let impl_datum = ImplDatum { binders: make_binders(impl_datum_bound, num_args as usize + 1) };
722 Some(Arc::new(impl_datum)) 731 Some(Arc::new(impl_datum))
723} 732}
724 733
diff --git a/crates/ra_hir/src/type_alias.rs b/crates/ra_hir/src/type_alias.rs
index 3b38c4740..674a46102 100644
--- a/crates/ra_hir/src/type_alias.rs
+++ b/crates/ra_hir/src/type_alias.rs
@@ -17,12 +17,14 @@ pub struct TypeAliasData {
17 pub(crate) type_ref: Option<TypeRef>, 17 pub(crate) type_ref: Option<TypeRef>,
18} 18}
19 19
20pub(crate) fn type_alias_data_query( 20impl TypeAliasData {
21 db: &(impl DefDatabase + AstDatabase), 21 pub(crate) fn type_alias_data_query(
22 typ: TypeAlias, 22 db: &(impl DefDatabase + AstDatabase),
23) -> Arc<TypeAliasData> { 23 typ: TypeAlias,
24 let node = typ.source(db).ast; 24 ) -> Arc<TypeAliasData> {
25 let name = node.name().map_or_else(Name::missing, |n| n.as_name()); 25 let node = typ.source(db).ast;
26 let type_ref = node.type_ref().map(TypeRef::from_ast); 26 let name = node.name().map_or_else(Name::missing, |n| n.as_name());
27 Arc::new(TypeAliasData { name, type_ref }) 27 let type_ref = node.type_ref().map(TypeRef::from_ast);
28 Arc::new(TypeAliasData { name, type_ref })
29 }
28} 30}
diff --git a/crates/ra_hir/src/util.rs b/crates/ra_hir/src/util.rs
new file mode 100644
index 000000000..0095ee45d
--- /dev/null
+++ b/crates/ra_hir/src/util.rs
@@ -0,0 +1,12 @@
1//! Internal utility functions.
2
3use std::sync::Arc;
4
5/// Helper for mutating `Arc<[T]>` (i.e. `Arc::make_mut` for Arc slices).
6/// The underlying values are cloned if there are other strong references.
7pub(crate) fn make_mut_slice<T: Clone>(a: &mut Arc<[T]>) -> &mut [T] {
8 if Arc::get_mut(a).is_none() {
9 *a = a.iter().cloned().collect();
10 }
11 Arc::get_mut(a).unwrap()
12}
diff --git a/crates/ra_ide_api/Cargo.toml b/crates/ra_ide_api/Cargo.toml
index f919a2d61..f9bf0c686 100644
--- a/crates/ra_ide_api/Cargo.toml
+++ b/crates/ra_ide_api/Cargo.toml
@@ -12,13 +12,14 @@ format-buf = "1.0.0"
12itertools = "0.8.0" 12itertools = "0.8.0"
13join_to_string = "0.1.3" 13join_to_string = "0.1.3"
14log = "0.4.5" 14log = "0.4.5"
15relative-path = "0.4.0" 15relative-path = "1.0.0"
16rayon = "1.0.2" 16rayon = "1.0.2"
17fst = { version = "0.3.1", default-features = false } 17fst = { version = "0.3.1", default-features = false }
18rustc-hash = "1.0" 18rustc-hash = "1.0"
19unicase = "2.2.0" 19unicase = "2.2.0"
20superslice = "1.0.0" 20superslice = "1.0.0"
21rand = { version = "0.7.0", features = ["small_rng"] } 21rand = { version = "0.7.0", features = ["small_rng"] }
22once_cell = "1.2.0"
22 23
23ra_syntax = { path = "../ra_syntax" } 24ra_syntax = { path = "../ra_syntax" }
24ra_text_edit = { path = "../ra_text_edit" } 25ra_text_edit = { path = "../ra_text_edit" }
diff --git a/crates/ra_ide_api/src/change.rs b/crates/ra_ide_api/src/change.rs
index 09913787b..050249c0e 100644
--- a/crates/ra_ide_api/src/change.rs
+++ b/crates/ra_ide_api/src/change.rs
@@ -4,7 +4,7 @@ use std::{fmt, sync::Arc, time};
4 4
5use ra_db::{ 5use ra_db::{
6 salsa::{Database, Durability, SweepStrategy}, 6 salsa::{Database, Durability, SweepStrategy},
7 CrateGraph, CrateId, FileId, SourceDatabase, SourceRoot, SourceRootId, 7 CrateGraph, CrateId, FileId, SourceDatabase, SourceDatabaseExt, SourceRoot, SourceRootId,
8}; 8};
9use ra_prof::{memory_usage, profile, Bytes}; 9use ra_prof::{memory_usage, profile, Bytes};
10use ra_syntax::SourceFile; 10use ra_syntax::SourceFile;
diff --git a/crates/ra_ide_api/src/completion/complete_dot.rs b/crates/ra_ide_api/src/completion/complete_dot.rs
index 28c8324d0..b4df6ee2a 100644
--- a/crates/ra_ide_api/src/completion/complete_dot.rs
+++ b/crates/ra_ide_api/src/completion/complete_dot.rs
@@ -119,27 +119,28 @@ mod tests {
119 ", 119 ",
120 ), 120 ),
121 @r###" 121 @r###"
122 â‹®[ 122 [
123 â‹® CompletionItem { 123 CompletionItem {
124 â‹® label: "foo", 124 label: "foo()",
125 â‹® source_range: [187; 187), 125 source_range: [187; 187),
126 â‹® delete: [187; 187), 126 delete: [187; 187),
127 â‹® insert: "foo()$0", 127 insert: "foo()$0",
128 â‹® kind: Method, 128 kind: Method,
129 â‹® detail: "fn foo(self)", 129 lookup: "foo",
130 â‹® }, 130 detail: "fn foo(self)",
131 â‹® CompletionItem { 131 },
132 â‹® label: "the_field", 132 CompletionItem {
133 â‹® source_range: [187; 187), 133 label: "the_field",
134 â‹® delete: [187; 187), 134 source_range: [187; 187),
135 â‹® insert: "the_field", 135 delete: [187; 187),
136 â‹® kind: Field, 136 insert: "the_field",
137 â‹® detail: "(u32,)", 137 kind: Field,
138 â‹® documentation: Documentation( 138 detail: "(u32,)",
139 â‹® "This is the_field", 139 documentation: Documentation(
140 â‹® ), 140 "This is the_field",
141 â‹® }, 141 ),
142 â‹®] 142 },
143 ]
143 "### 144 "###
144 ); 145 );
145 } 146 }
@@ -158,24 +159,25 @@ mod tests {
158 ", 159 ",
159 ), 160 ),
160 @r###" 161 @r###"
161 â‹®[ 162 [
162 â‹® CompletionItem { 163 CompletionItem {
163 â‹® label: "foo", 164 label: "foo()",
164 â‹® source_range: [126; 126), 165 source_range: [126; 126),
165 â‹® delete: [126; 126), 166 delete: [126; 126),
166 â‹® insert: "foo()$0", 167 insert: "foo()$0",
167 â‹® kind: Method, 168 kind: Method,
168 â‹® detail: "fn foo(&self)", 169 lookup: "foo",
169 â‹® }, 170 detail: "fn foo(&self)",
170 â‹® CompletionItem { 171 },
171 â‹® label: "the_field", 172 CompletionItem {
172 â‹® source_range: [126; 126), 173 label: "the_field",
173 â‹® delete: [126; 126), 174 source_range: [126; 126),
174 â‹® insert: "the_field", 175 delete: [126; 126),
175 â‹® kind: Field, 176 insert: "the_field",
176 â‹® detail: "(u32, i32)", 177 kind: Field,
177 â‹® }, 178 detail: "(u32, i32)",
178 â‹®] 179 },
180 ]
179 "### 181 "###
180 ); 182 );
181 } 183 }
@@ -210,16 +212,17 @@ mod tests {
210 ", 212 ",
211 ), 213 ),
212 @r###" 214 @r###"
213 â‹®[ 215 [
214 â‹® CompletionItem { 216 CompletionItem {
215 â‹® label: "the_method", 217 label: "the_method()",
216 â‹® source_range: [144; 144), 218 source_range: [144; 144),
217 â‹® delete: [144; 144), 219 delete: [144; 144),
218 â‹® insert: "the_method()$0", 220 insert: "the_method()$0",
219 â‹® kind: Method, 221 kind: Method,
220 â‹® detail: "fn the_method(&self)", 222 lookup: "the_method",
221 â‹® }, 223 detail: "fn the_method(&self)",
222 â‹®] 224 },
225 ]
223 "### 226 "###
224 ); 227 );
225 } 228 }
@@ -238,16 +241,17 @@ mod tests {
238 ", 241 ",
239 ), 242 ),
240 @r###" 243 @r###"
241 â‹®[ 244 [
242 â‹® CompletionItem { 245 CompletionItem {
243 â‹® label: "the_method", 246 label: "the_method()",
244 â‹® source_range: [151; 151), 247 source_range: [151; 151),
245 â‹® delete: [151; 151), 248 delete: [151; 151),
246 â‹® insert: "the_method()$0", 249 insert: "the_method()$0",
247 â‹® kind: Method, 250 kind: Method,
248 â‹® detail: "fn the_method(&self)", 251 lookup: "the_method",
249 â‹® }, 252 detail: "fn the_method(&self)",
250 â‹®] 253 },
254 ]
251 "### 255 "###
252 ); 256 );
253 } 257 }
@@ -266,16 +270,17 @@ mod tests {
266 ", 270 ",
267 ), 271 ),
268 @r###" 272 @r###"
269 â‹®[ 273 [
270 â‹® CompletionItem { 274 CompletionItem {
271 â‹® label: "the_method", 275 label: "the_method()",
272 â‹® source_range: [155; 155), 276 source_range: [155; 155),
273 â‹® delete: [155; 155), 277 delete: [155; 155),
274 â‹® insert: "the_method()$0", 278 insert: "the_method()$0",
275 â‹® kind: Method, 279 kind: Method,
276 â‹® detail: "fn the_method(&self)", 280 lookup: "the_method",
277 â‹® }, 281 detail: "fn the_method(&self)",
278 â‹®] 282 },
283 ]
279 "### 284 "###
280 ); 285 );
281 } 286 }
@@ -317,16 +322,17 @@ mod tests {
317 ", 322 ",
318 ), 323 ),
319 @r###" 324 @r###"
320 â‹®[ 325 [
321 â‹® CompletionItem { 326 CompletionItem {
322 â‹® label: "the_method", 327 label: "the_method()",
323 â‹® source_range: [249; 249), 328 source_range: [249; 249),
324 â‹® delete: [249; 249), 329 delete: [249; 249),
325 â‹® insert: "the_method()$0", 330 insert: "the_method()$0",
326 â‹® kind: Method, 331 kind: Method,
327 â‹® detail: "fn the_method(&self)", 332 lookup: "the_method",
328 â‹® }, 333 detail: "fn the_method(&self)",
329 â‹®] 334 },
335 ]
330 "### 336 "###
331 ); 337 );
332 } 338 }
@@ -386,16 +392,17 @@ mod tests {
386 ", 392 ",
387 ), 393 ),
388 @r###" 394 @r###"
389 â‹®[ 395 [
390 â‹® CompletionItem { 396 CompletionItem {
391 â‹® label: "blah", 397 label: "blah()",
392 â‹® source_range: [299; 300), 398 source_range: [299; 300),
393 â‹® delete: [299; 300), 399 delete: [299; 300),
394 â‹® insert: "blah()$0", 400 insert: "blah()$0",
395 â‹® kind: Method, 401 kind: Method,
396 â‹® detail: "pub fn blah(&self)", 402 lookup: "blah",
397 â‹® }, 403 detail: "pub fn blah(&self)",
398 â‹®] 404 },
405 ]
399 "### 406 "###
400 ); 407 );
401 } 408 }
diff --git a/crates/ra_ide_api/src/completion/complete_macro_in_item_position.rs b/crates/ra_ide_api/src/completion/complete_macro_in_item_position.rs
index d808b2357..09f743c66 100644
--- a/crates/ra_ide_api/src/completion/complete_macro_in_item_position.rs
+++ b/crates/ra_ide_api/src/completion/complete_macro_in_item_position.rs
@@ -56,6 +56,16 @@ mod tests {
56 do_reference_completion( 56 do_reference_completion(
57 " 57 "
58 //- /main.rs 58 //- /main.rs
59 /// Creates a [`Vec`] containing the arguments.
60 ///
61 /// - Create a [`Vec`] containing a given list of elements:
62 ///
63 /// ```
64 /// let v = vec![1, 2, 3];
65 /// assert_eq!(v[0], 1);
66 /// assert_eq!(v[1], 2);
67 /// assert_eq!(v[2], 3);
68 /// ```
59 macro_rules! vec { 69 macro_rules! vec {
60 () => {} 70 () => {}
61 } 71 }
@@ -68,13 +78,61 @@ mod tests {
68 @r##"[ 78 @r##"[
69 CompletionItem { 79 CompletionItem {
70 label: "vec!", 80 label: "vec!",
71 source_range: [46; 46), 81 source_range: [280; 280),
72 delete: [46; 46), 82 delete: [280; 280),
73 insert: "vec![$0]", 83 insert: "vec![$0]",
74 kind: Macro, 84 kind: Macro,
75 detail: "macro_rules! vec", 85 detail: "macro_rules! vec",
86 documentation: Documentation(
87 "Creates a [`Vec`] containing the arguments.\n\n- Create a [`Vec`] containing a given list of elements:\n\n```\nlet v = vec![1, 2, 3];\nassert_eq!(v[0], 1);\nassert_eq!(v[1], 2);\nassert_eq!(v[2], 3);\n```",
88 ),
76 }, 89 },
77]"## 90]"##
78 ); 91 );
79 } 92 }
93
94 #[test]
95 fn completes_macros_braces_guessing() {
96 assert_debug_snapshot!(
97 do_reference_completion(
98 "
99 //- /main.rs
100 /// Foo
101 ///
102 /// Not call `fooo!()` `fooo!()`, or `_foo![]` `_foo![]`.
103 /// Call as `let _=foo! { hello world };`
104 macro_rules! foo {
105 () => {}
106 }
107
108 fn main() {
109 <|>
110 }
111 "
112 ),
113 @r###"[
114 CompletionItem {
115 label: "foo!",
116 source_range: [163; 163),
117 delete: [163; 163),
118 insert: "foo! {$0}",
119 kind: Macro,
120 detail: "macro_rules! foo",
121 documentation: Documentation(
122 "Foo\n\nNot call `fooo!()` `fooo!()`, or `_foo![]` `_foo![]`.\nCall as `let _=foo! { hello world };`",
123 ),
124 },
125 CompletionItem {
126 label: "main()",
127 source_range: [163; 163),
128 delete: [163; 163),
129 insert: "main()$0",
130 kind: Function,
131 lookup: "main",
132 detail: "fn main()",
133 },
134]
135 "###
136 );
137 }
80} 138}
diff --git a/crates/ra_ide_api/src/completion/complete_path.rs b/crates/ra_ide_api/src/completion/complete_path.rs
index e01197fe4..23dece73c 100644
--- a/crates/ra_ide_api/src/completion/complete_path.rs
+++ b/crates/ra_ide_api/src/completion/complete_path.rs
@@ -375,19 +375,22 @@ mod tests {
375 fn foo() { let _ = S::<|> } 375 fn foo() { let _ = S::<|> }
376 " 376 "
377 ), 377 ),
378 @r###"[ 378 @r###"
379 CompletionItem { 379 [
380 label: "m", 380 CompletionItem {
381 source_range: [100; 100), 381 label: "m()",
382 delete: [100; 100), 382 source_range: [100; 100),
383 insert: "m()$0", 383 delete: [100; 100),
384 kind: Function, 384 insert: "m()$0",
385 detail: "fn m()", 385 kind: Function,
386 documentation: Documentation( 386 lookup: "m",
387 "An associated method", 387 detail: "fn m()",
388 ), 388 documentation: Documentation(
389 }, 389 "An associated method",
390]"### 390 ),
391 },
392 ]
393 "###
391 ); 394 );
392 } 395 }
393 396
@@ -474,19 +477,22 @@ mod tests {
474 fn foo() { let _ = S::<|> } 477 fn foo() { let _ = S::<|> }
475 " 478 "
476 ), 479 ),
477 @r###"[ 480 @r###"
478 CompletionItem { 481 [
479 label: "m", 482 CompletionItem {
480 source_range: [100; 100), 483 label: "m()",
481 delete: [100; 100), 484 source_range: [100; 100),
482 insert: "m()$0", 485 delete: [100; 100),
483 kind: Function, 486 insert: "m()$0",
484 detail: "fn m()", 487 kind: Function,
485 documentation: Documentation( 488 lookup: "m",
486 "An associated method", 489 detail: "fn m()",
487 ), 490 documentation: Documentation(
488 }, 491 "An associated method",
489]"### 492 ),
493 },
494 ]
495 "###
490 ); 496 );
491 } 497 }
492 498
@@ -507,19 +513,22 @@ mod tests {
507 fn foo() { let _ = U::<|> } 513 fn foo() { let _ = U::<|> }
508 " 514 "
509 ), 515 ),
510 @r###"[ 516 @r###"
511 CompletionItem { 517 [
512 label: "m", 518 CompletionItem {
513 source_range: [101; 101), 519 label: "m()",
514 delete: [101; 101), 520 source_range: [101; 101),
515 insert: "m()$0", 521 delete: [101; 101),
516 kind: Function, 522 insert: "m()$0",
517 detail: "fn m()", 523 kind: Function,
518 documentation: Documentation( 524 lookup: "m",
519 "An associated method", 525 detail: "fn m()",
520 ), 526 documentation: Documentation(
521 }, 527 "An associated method",
522]"### 528 ),
529 },
530 ]
531 "###
523 ); 532 );
524 } 533 }
525 534
@@ -564,24 +573,28 @@ mod tests {
564 } 573 }
565 " 574 "
566 ), 575 ),
567 @r###"[ 576 @r###"
568 CompletionItem { 577 [
569 label: "bar", 578 CompletionItem {
570 source_range: [185; 185), 579 label: "bar()",
571 delete: [185; 185), 580 source_range: [185; 185),
572 insert: "bar()$0", 581 delete: [185; 185),
573 kind: Function, 582 insert: "bar()$0",
574 detail: "fn bar()", 583 kind: Function,
575 }, 584 lookup: "bar",
576 CompletionItem { 585 detail: "fn bar()",
577 label: "foo", 586 },
578 source_range: [185; 185), 587 CompletionItem {
579 delete: [185; 185), 588 label: "foo()",
580 insert: "foo()$0", 589 source_range: [185; 185),
581 kind: Function, 590 delete: [185; 185),
582 detail: "fn foo()", 591 insert: "foo()$0",
583 }, 592 kind: Function,
584]"### 593 lookup: "foo",
594 detail: "fn foo()",
595 },
596 ]
597 "###
585 ); 598 );
586 } 599 }
587 600
@@ -600,24 +613,27 @@ mod tests {
600 } 613 }
601 " 614 "
602 ), 615 ),
603 @r###"[ 616 @r###"
604 CompletionItem { 617 [
605 label: "foo!", 618 CompletionItem {
606 source_range: [179; 179), 619 label: "foo!",
607 delete: [179; 179), 620 source_range: [179; 179),
608 insert: "foo!($0)", 621 delete: [179; 179),
609 kind: Macro, 622 insert: "foo!($0)",
610 detail: "#[macro_export]\nmacro_rules! foo", 623 kind: Macro,
611 }, 624 detail: "#[macro_export]\nmacro_rules! foo",
612 CompletionItem { 625 },
613 label: "main", 626 CompletionItem {
614 source_range: [179; 179), 627 label: "main()",
615 delete: [179; 179), 628 source_range: [179; 179),
616 insert: "main()$0", 629 delete: [179; 179),
617 kind: Function, 630 insert: "main()$0",
618 detail: "fn main()", 631 kind: Function,
619 }, 632 lookup: "main",
620]"### 633 detail: "fn main()",
634 },
635 ]
636 "###
621 ); 637 );
622 } 638 }
623} 639}
diff --git a/crates/ra_ide_api/src/completion/complete_postfix.rs b/crates/ra_ide_api/src/completion/complete_postfix.rs
index 445a02676..555cecb73 100644
--- a/crates/ra_ide_api/src/completion/complete_postfix.rs
+++ b/crates/ra_ide_api/src/completion/complete_postfix.rs
@@ -8,7 +8,7 @@ use crate::{
8 CompletionItem, 8 CompletionItem,
9}; 9};
10use hir::{Ty, TypeCtor}; 10use hir::{Ty, TypeCtor};
11use ra_syntax::{ast::AstNode, TextRange}; 11use ra_syntax::{ast::AstNode, TextRange, TextUnit};
12use ra_text_edit::TextEditBuilder; 12use ra_text_edit::TextEditBuilder;
13 13
14fn postfix_snippet(ctx: &CompletionContext, label: &str, detail: &str, snippet: &str) -> Builder { 14fn postfix_snippet(ctx: &CompletionContext, label: &str, detail: &str, snippet: &str) -> Builder {
@@ -42,7 +42,13 @@ fn is_bool_or_unknown(ty: Option<Ty>) -> bool {
42 42
43pub(super) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) { 43pub(super) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
44 if let Some(dot_receiver) = &ctx.dot_receiver { 44 if let Some(dot_receiver) = &ctx.dot_receiver {
45 let receiver_text = dot_receiver.syntax().text().to_string(); 45 let receiver_text = if ctx.dot_receiver_is_ambiguous_float_literal {
46 let text = dot_receiver.syntax().text();
47 let without_dot = ..text.len() - TextUnit::of_char('.');
48 text.slice(without_dot).to_string()
49 } else {
50 dot_receiver.syntax().text().to_string()
51 };
46 let receiver_ty = ctx.analyzer.type_of(ctx.db, &dot_receiver); 52 let receiver_ty = ctx.analyzer.type_of(ctx.db, &dot_receiver);
47 if is_bool_or_unknown(receiver_ty) { 53 if is_bool_or_unknown(receiver_ty) {
48 postfix_snippet(ctx, "if", "if expr {}", &format!("if {} {{$0}}", receiver_text)) 54 postfix_snippet(ctx, "if", "if expr {}", &format!("if {} {{$0}}", receiver_text))
@@ -209,4 +215,61 @@ mod tests {
209]"### 215]"###
210 ); 216 );
211 } 217 }
218
219 #[test]
220 fn postfix_completion_works_for_ambiguous_float_literal() {
221 assert_debug_snapshot!(
222 do_postfix_completion(
223 r#"
224 fn main() {
225 42.<|>
226 }
227 "#,
228 ),
229 @r###"[
230 CompletionItem {
231 label: "box",
232 source_range: [52; 52),
233 delete: [49; 52),
234 insert: "Box::new(42)",
235 detail: "Box::new(expr)",
236 },
237 CompletionItem {
238 label: "dbg",
239 source_range: [52; 52),
240 delete: [49; 52),
241 insert: "dbg!(42)",
242 detail: "dbg!(expr)",
243 },
244 CompletionItem {
245 label: "match",
246 source_range: [52; 52),
247 delete: [49; 52),
248 insert: "match 42 {\n ${1:_} => {$0\\},\n}",
249 detail: "match expr {}",
250 },
251 CompletionItem {
252 label: "not",
253 source_range: [52; 52),
254 delete: [49; 52),
255 insert: "!42",
256 detail: "!expr",
257 },
258 CompletionItem {
259 label: "ref",
260 source_range: [52; 52),
261 delete: [49; 52),
262 insert: "&42",
263 detail: "&expr",
264 },
265 CompletionItem {
266 label: "refm",
267 source_range: [52; 52),
268 delete: [49; 52),
269 insert: "&mut 42",
270 detail: "&mut expr",
271 },
272]"###
273 );
274 }
212} 275}
diff --git a/crates/ra_ide_api/src/completion/complete_scope.rs b/crates/ra_ide_api/src/completion/complete_scope.rs
index 515a6285c..4e56de3f5 100644
--- a/crates/ra_ide_api/src/completion/complete_scope.rs
+++ b/crates/ra_ide_api/src/completion/complete_scope.rs
@@ -145,32 +145,35 @@ mod tests {
145 } 145 }
146 " 146 "
147 ), 147 ),
148 @r###"[ 148 @r###"
149 CompletionItem { 149 [
150 label: "quux", 150 CompletionItem {
151 source_range: [91; 91), 151 label: "quux(…)",
152 delete: [91; 91), 152 source_range: [91; 91),
153 insert: "quux($0)", 153 delete: [91; 91),
154 kind: Function, 154 insert: "quux($0)",
155 detail: "fn quux(x: i32)", 155 kind: Function,
156 }, 156 lookup: "quux",
157 CompletionItem { 157 detail: "fn quux(x: i32)",
158 label: "x", 158 },
159 source_range: [91; 91), 159 CompletionItem {
160 delete: [91; 91), 160 label: "x",
161 insert: "x", 161 source_range: [91; 91),
162 kind: Binding, 162 delete: [91; 91),
163 detail: "i32", 163 insert: "x",
164 }, 164 kind: Binding,
165 CompletionItem { 165 detail: "i32",
166 label: "y", 166 },
167 source_range: [91; 91), 167 CompletionItem {
168 delete: [91; 91), 168 label: "y",
169 insert: "y", 169 source_range: [91; 91),
170 kind: Binding, 170 delete: [91; 91),
171 detail: "i32", 171 insert: "y",
172 }, 172 kind: Binding,
173]"### 173 detail: "i32",
174 },
175 ]
176 "###
174 ); 177 );
175 } 178 }
176 179
@@ -190,31 +193,34 @@ mod tests {
190 } 193 }
191 " 194 "
192 ), 195 ),
193 @r###"[ 196 @r###"
194 CompletionItem { 197 [
195 label: "a", 198 CompletionItem {
196 source_range: [242; 242), 199 label: "a",
197 delete: [242; 242), 200 source_range: [242; 242),
198 insert: "a", 201 delete: [242; 242),
199 kind: Binding, 202 insert: "a",
200 }, 203 kind: Binding,
201 CompletionItem { 204 },
202 label: "b", 205 CompletionItem {
203 source_range: [242; 242), 206 label: "b",
204 delete: [242; 242), 207 source_range: [242; 242),
205 insert: "b", 208 delete: [242; 242),
206 kind: Binding, 209 insert: "b",
207 detail: "i32", 210 kind: Binding,
208 }, 211 detail: "i32",
209 CompletionItem { 212 },
210 label: "quux", 213 CompletionItem {
211 source_range: [242; 242), 214 label: "quux()",
212 delete: [242; 242), 215 source_range: [242; 242),
213 insert: "quux()$0", 216 delete: [242; 242),
214 kind: Function, 217 insert: "quux()$0",
215 detail: "fn quux()", 218 kind: Function,
216 }, 219 lookup: "quux",
217]"### 220 detail: "fn quux()",
221 },
222 ]
223 "###
218 ); 224 );
219 } 225 }
220 226
@@ -230,23 +236,26 @@ mod tests {
230 } 236 }
231 " 237 "
232 ), 238 ),
233 @r###"[ 239 @r###"
234 CompletionItem { 240 [
235 label: "quux", 241 CompletionItem {
236 source_range: [95; 95), 242 label: "quux()",
237 delete: [95; 95), 243 source_range: [95; 95),
238 insert: "quux()$0", 244 delete: [95; 95),
239 kind: Function, 245 insert: "quux()$0",
240 detail: "fn quux()", 246 kind: Function,
241 }, 247 lookup: "quux",
242 CompletionItem { 248 detail: "fn quux()",
243 label: "x", 249 },
244 source_range: [95; 95), 250 CompletionItem {
245 delete: [95; 95), 251 label: "x",
246 insert: "x", 252 source_range: [95; 95),
247 kind: Binding, 253 delete: [95; 95),
248 }, 254 insert: "x",
249]"### 255 kind: Binding,
256 },
257 ]
258 "###
250 ); 259 );
251 } 260 }
252 261
@@ -260,23 +269,26 @@ mod tests {
260 } 269 }
261 " 270 "
262 ), 271 ),
263 @r###"[ 272 @r###"
264 CompletionItem { 273 [
265 label: "T", 274 CompletionItem {
266 source_range: [52; 52), 275 label: "T",
267 delete: [52; 52), 276 source_range: [52; 52),
268 insert: "T", 277 delete: [52; 52),
269 kind: TypeParam, 278 insert: "T",
270 }, 279 kind: TypeParam,
271 CompletionItem { 280 },
272 label: "quux", 281 CompletionItem {
273 source_range: [52; 52), 282 label: "quux()",
274 delete: [52; 52), 283 source_range: [52; 52),
275 insert: "quux()$0", 284 delete: [52; 52),
276 kind: Function, 285 insert: "quux()$0",
277 detail: "fn quux<T>()", 286 kind: Function,
278 }, 287 lookup: "quux",
279]"### 288 detail: "fn quux<T>()",
289 },
290 ]
291 "###
280 ); 292 );
281 } 293 }
282 294
@@ -290,22 +302,56 @@ mod tests {
290 } 302 }
291 " 303 "
292 ), 304 ),
293 @r###"[ 305 @r###"
294 CompletionItem { 306 [
295 label: "T", 307 CompletionItem {
296 source_range: [54; 54), 308 label: "T",
297 delete: [54; 54), 309 source_range: [54; 54),
298 insert: "T", 310 delete: [54; 54),
299 kind: TypeParam, 311 insert: "T",
300 }, 312 kind: TypeParam,
301 CompletionItem { 313 },
302 label: "X", 314 CompletionItem {
303 source_range: [54; 54), 315 label: "X<…>",
304 delete: [54; 54), 316 source_range: [54; 54),
305 insert: "X", 317 delete: [54; 54),
306 kind: Struct, 318 insert: "X<$0>",
307 }, 319 kind: Struct,
308]"### 320 lookup: "X",
321 },
322 ]
323 "###
324 );
325 }
326
327 #[test]
328 fn completes_self_in_enum() {
329 assert_debug_snapshot!(
330 do_reference_completion(
331 r"
332 enum X {
333 Y(<|>)
334 }
335 "
336 ),
337 @r###"
338 [
339 CompletionItem {
340 label: "Self",
341 source_range: [48; 48),
342 delete: [48; 48),
343 insert: "Self",
344 kind: TypeParam,
345 },
346 CompletionItem {
347 label: "X",
348 source_range: [48; 48),
349 delete: [48; 48),
350 insert: "X",
351 kind: Enum,
352 },
353 ]
354 "###
309 ); 355 );
310 } 356 }
311 357
@@ -321,30 +367,33 @@ mod tests {
321 } 367 }
322 " 368 "
323 ), 369 ),
324 @r###"[ 370 @r###"
325 CompletionItem { 371 [
326 label: "Baz", 372 CompletionItem {
327 source_range: [105; 105), 373 label: "Baz",
328 delete: [105; 105), 374 source_range: [105; 105),
329 insert: "Baz", 375 delete: [105; 105),
330 kind: Enum, 376 insert: "Baz",
331 }, 377 kind: Enum,
332 CompletionItem { 378 },
333 label: "Foo", 379 CompletionItem {
334 source_range: [105; 105), 380 label: "Foo",
335 delete: [105; 105), 381 source_range: [105; 105),
336 insert: "Foo", 382 delete: [105; 105),
337 kind: Struct, 383 insert: "Foo",
338 }, 384 kind: Struct,
339 CompletionItem { 385 },
340 label: "quux", 386 CompletionItem {
341 source_range: [105; 105), 387 label: "quux()",
342 delete: [105; 105), 388 source_range: [105; 105),
343 insert: "quux()$0", 389 delete: [105; 105),
344 kind: Function, 390 insert: "quux()$0",
345 detail: "fn quux()", 391 kind: Function,
346 }, 392 lookup: "quux",
347]"### 393 detail: "fn quux()",
394 },
395 ]
396 "###
348 ); 397 );
349 } 398 }
350 399
@@ -384,23 +433,26 @@ mod tests {
384 } 433 }
385 " 434 "
386 ), 435 ),
387 @r###"[ 436 @r###"
388 CompletionItem { 437 [
389 label: "Bar", 438 CompletionItem {
390 source_range: [117; 117), 439 label: "Bar",
391 delete: [117; 117), 440 source_range: [117; 117),
392 insert: "Bar", 441 delete: [117; 117),
393 kind: Struct, 442 insert: "Bar",
394 }, 443 kind: Struct,
395 CompletionItem { 444 },
396 label: "quux", 445 CompletionItem {
397 source_range: [117; 117), 446 label: "quux()",
398 delete: [117; 117), 447 source_range: [117; 117),
399 insert: "quux()$0", 448 delete: [117; 117),
400 kind: Function, 449 insert: "quux()$0",
401 detail: "fn quux()", 450 kind: Function,
402 }, 451 lookup: "quux",
403]"### 452 detail: "fn quux()",
453 },
454 ]
455 "###
404 ); 456 );
405 } 457 }
406 458
@@ -413,23 +465,26 @@ mod tests {
413 fn x() -> <|> 465 fn x() -> <|>
414 " 466 "
415 ), 467 ),
416 @r###"[ 468 @r###"
417 CompletionItem { 469 [
418 label: "Foo", 470 CompletionItem {
419 source_range: [55; 55), 471 label: "Foo",
420 delete: [55; 55), 472 source_range: [55; 55),
421 insert: "Foo", 473 delete: [55; 55),
422 kind: Struct, 474 insert: "Foo",
423 }, 475 kind: Struct,
424 CompletionItem { 476 },
425 label: "x", 477 CompletionItem {
426 source_range: [55; 55), 478 label: "x()",
427 delete: [55; 55), 479 source_range: [55; 55),
428 insert: "x()$0", 480 delete: [55; 55),
429 kind: Function, 481 insert: "x()$0",
430 detail: "fn x()", 482 kind: Function,
431 }, 483 lookup: "x",
432]"### 484 detail: "fn x()",
485 },
486 ]
487 "###
433 ); 488 );
434 } 489 }
435 490
@@ -447,24 +502,27 @@ mod tests {
447 } 502 }
448 " 503 "
449 ), 504 ),
450 @r###"[ 505 @r###"
451 CompletionItem { 506 [
452 label: "bar", 507 CompletionItem {
453 source_range: [146; 146), 508 label: "bar",
454 delete: [146; 146), 509 source_range: [146; 146),
455 insert: "bar", 510 delete: [146; 146),
456 kind: Binding, 511 insert: "bar",
457 detail: "i32", 512 kind: Binding,
458 }, 513 detail: "i32",
459 CompletionItem { 514 },
460 label: "foo", 515 CompletionItem {
461 source_range: [146; 146), 516 label: "foo()",
462 delete: [146; 146), 517 source_range: [146; 146),
463 insert: "foo()$0", 518 delete: [146; 146),
464 kind: Function, 519 insert: "foo()$0",
465 detail: "fn foo()", 520 kind: Function,
466 }, 521 lookup: "foo",
467]"### 522 detail: "fn foo()",
523 },
524 ]
525 "###
468 ); 526 );
469 } 527 }
470 528
@@ -509,30 +567,33 @@ mod tests {
509 } 567 }
510 " 568 "
511 ), 569 ),
512 @r#"[ 570 @r###"
513 CompletionItem { 571 [
514 label: "Option", 572 CompletionItem {
515 source_range: [18; 18), 573 label: "Option",
516 delete: [18; 18), 574 source_range: [18; 18),
517 insert: "Option", 575 delete: [18; 18),
518 kind: Struct, 576 insert: "Option",
519 }, 577 kind: Struct,
520 CompletionItem { 578 },
521 label: "foo", 579 CompletionItem {
522 source_range: [18; 18), 580 label: "foo()",
523 delete: [18; 18), 581 source_range: [18; 18),
524 insert: "foo()$0", 582 delete: [18; 18),
525 kind: Function, 583 insert: "foo()$0",
526 detail: "fn foo()", 584 kind: Function,
527 }, 585 lookup: "foo",
528 CompletionItem { 586 detail: "fn foo()",
529 label: "std", 587 },
530 source_range: [18; 18), 588 CompletionItem {
531 delete: [18; 18), 589 label: "std",
532 insert: "std", 590 source_range: [18; 18),
533 kind: Module, 591 delete: [18; 18),
534 }, 592 insert: "std",
535]"# 593 kind: Module,
594 },
595 ]
596 "###
536 ); 597 );
537 } 598 }
538 599
@@ -569,54 +630,57 @@ mod tests {
569 } 630 }
570 " 631 "
571 ), 632 ),
572 @r##"[ 633 @r###"
573 CompletionItem { 634 [
574 label: "bar!", 635 CompletionItem {
575 source_range: [252; 252), 636 label: "bar!",
576 delete: [252; 252), 637 source_range: [252; 252),
577 insert: "bar!($0)", 638 delete: [252; 252),
578 kind: Macro, 639 insert: "bar!($0)",
579 detail: "macro_rules! bar", 640 kind: Macro,
580 }, 641 detail: "macro_rules! bar",
581 CompletionItem { 642 },
582 label: "baz!", 643 CompletionItem {
583 source_range: [252; 252), 644 label: "baz!",
584 delete: [252; 252), 645 source_range: [252; 252),
585 insert: "baz!($0)", 646 delete: [252; 252),
586 kind: Macro, 647 insert: "baz!($0)",
587 detail: "#[macro_export]\nmacro_rules! baz", 648 kind: Macro,
588 }, 649 detail: "#[macro_export]\nmacro_rules! baz",
589 CompletionItem { 650 },
590 label: "foo!", 651 CompletionItem {
591 source_range: [252; 252), 652 label: "foo!",
592 delete: [252; 252), 653 source_range: [252; 252),
593 insert: "foo!($0)", 654 delete: [252; 252),
594 kind: Macro, 655 insert: "foo!($0)",
595 detail: "macro_rules! foo", 656 kind: Macro,
596 }, 657 detail: "macro_rules! foo",
597 CompletionItem { 658 },
598 label: "m1", 659 CompletionItem {
599 source_range: [252; 252), 660 label: "m1",
600 delete: [252; 252), 661 source_range: [252; 252),
601 insert: "m1", 662 delete: [252; 252),
602 kind: Module, 663 insert: "m1",
603 }, 664 kind: Module,
604 CompletionItem { 665 },
605 label: "m2", 666 CompletionItem {
606 source_range: [252; 252), 667 label: "m2",
607 delete: [252; 252), 668 source_range: [252; 252),
608 insert: "m2", 669 delete: [252; 252),
609 kind: Module, 670 insert: "m2",
610 }, 671 kind: Module,
611 CompletionItem { 672 },
612 label: "main", 673 CompletionItem {
613 source_range: [252; 252), 674 label: "main()",
614 delete: [252; 252), 675 source_range: [252; 252),
615 insert: "main()$0", 676 delete: [252; 252),
616 kind: Function, 677 insert: "main()$0",
617 detail: "fn main()", 678 kind: Function,
618 }, 679 lookup: "main",
619]"## 680 detail: "fn main()",
681 },
682 ]
683 "###
620 ); 684 );
621 } 685 }
622 686
@@ -635,24 +699,27 @@ mod tests {
635 } 699 }
636 " 700 "
637 ), 701 ),
638 @r##"[ 702 @r###"
639 CompletionItem { 703 [
640 label: "foo", 704 CompletionItem {
641 source_range: [49; 49), 705 label: "foo!",
642 delete: [49; 49), 706 source_range: [49; 49),
643 insert: "foo()$0", 707 delete: [49; 49),
644 kind: Function, 708 insert: "foo!($0)",
645 detail: "fn foo()", 709 kind: Macro,
646 }, 710 detail: "macro_rules! foo",
647 CompletionItem { 711 },
648 label: "foo!", 712 CompletionItem {
649 source_range: [49; 49), 713 label: "foo()",
650 delete: [49; 49), 714 source_range: [49; 49),
651 insert: "foo!($0)", 715 delete: [49; 49),
652 kind: Macro, 716 insert: "foo()$0",
653 detail: "macro_rules! foo", 717 kind: Function,
654 }, 718 lookup: "foo",
655]"## 719 detail: "fn foo()",
720 },
721 ]
722 "###
656 ); 723 );
657 } 724 }
658 725
@@ -671,24 +738,27 @@ mod tests {
671 } 738 }
672 " 739 "
673 ), 740 ),
674 @r##"[ 741 @r###"
675 CompletionItem { 742 [
676 label: "foo!", 743 CompletionItem {
677 source_range: [57; 57), 744 label: "foo!",
678 delete: [57; 57), 745 source_range: [57; 57),
679 insert: "foo!($0)", 746 delete: [57; 57),
680 kind: Macro, 747 insert: "foo!($0)",
681 detail: "macro_rules! foo", 748 kind: Macro,
682 }, 749 detail: "macro_rules! foo",
683 CompletionItem { 750 },
684 label: "main", 751 CompletionItem {
685 source_range: [57; 57), 752 label: "main()",
686 delete: [57; 57), 753 source_range: [57; 57),
687 insert: "main()$0", 754 delete: [57; 57),
688 kind: Function, 755 insert: "main()$0",
689 detail: "fn main()", 756 kind: Function,
690 }, 757 lookup: "main",
691]"## 758 detail: "fn main()",
759 },
760 ]
761 "###
692 ); 762 );
693 } 763 }
694 764
@@ -707,24 +777,27 @@ mod tests {
707 } 777 }
708 " 778 "
709 ), 779 ),
710 @r##"[ 780 @r###"
711 CompletionItem { 781 [
712 label: "foo!", 782 CompletionItem {
713 source_range: [50; 50), 783 label: "foo!",
714 delete: [50; 50), 784 source_range: [50; 50),
715 insert: "foo!($0)", 785 delete: [50; 50),
716 kind: Macro, 786 insert: "foo!($0)",
717 detail: "macro_rules! foo", 787 kind: Macro,
718 }, 788 detail: "macro_rules! foo",
719 CompletionItem { 789 },
720 label: "main", 790 CompletionItem {
721 source_range: [50; 50), 791 label: "main()",
722 delete: [50; 50), 792 source_range: [50; 50),
723 insert: "main()$0", 793 delete: [50; 50),
724 kind: Function, 794 insert: "main()$0",
725 detail: "fn main()", 795 kind: Function,
726 }, 796 lookup: "main",
727]"## 797 detail: "fn main()",
798 },
799 ]
800 "###
728 ); 801 );
729 } 802 }
730} 803}
diff --git a/crates/ra_ide_api/src/completion/completion_context.rs b/crates/ra_ide_api/src/completion/completion_context.rs
index e9ad06965..64cbc0f98 100644
--- a/crates/ra_ide_api/src/completion/completion_context.rs
+++ b/crates/ra_ide_api/src/completion/completion_context.rs
@@ -38,8 +38,11 @@ pub(crate) struct CompletionContext<'a> {
38 pub(super) is_new_item: bool, 38 pub(super) is_new_item: bool,
39 /// The receiver if this is a field or method access, i.e. writing something.<|> 39 /// The receiver if this is a field or method access, i.e. writing something.<|>
40 pub(super) dot_receiver: Option<ast::Expr>, 40 pub(super) dot_receiver: Option<ast::Expr>,
41 pub(super) dot_receiver_is_ambiguous_float_literal: bool,
41 /// If this is a call (method or function) in particular, i.e. the () are already there. 42 /// If this is a call (method or function) in particular, i.e. the () are already there.
42 pub(super) is_call: bool, 43 pub(super) is_call: bool,
44 pub(super) is_path_type: bool,
45 pub(super) has_type_args: bool,
43} 46}
44 47
45impl<'a> CompletionContext<'a> { 48impl<'a> CompletionContext<'a> {
@@ -76,6 +79,9 @@ impl<'a> CompletionContext<'a> {
76 is_new_item: false, 79 is_new_item: false,
77 dot_receiver: None, 80 dot_receiver: None,
78 is_call: false, 81 is_call: false,
82 is_path_type: false,
83 has_type_args: false,
84 dot_receiver_is_ambiguous_float_literal: false,
79 }; 85 };
80 ctx.fill(&original_parse, position.offset); 86 ctx.fill(&original_parse, position.offset);
81 Some(ctx) 87 Some(ctx)
@@ -176,6 +182,9 @@ impl<'a> CompletionContext<'a> {
176 .and_then(|it| it.syntax().parent().and_then(ast::CallExpr::cast)) 182 .and_then(|it| it.syntax().parent().and_then(ast::CallExpr::cast))
177 .is_some(); 183 .is_some();
178 184
185 self.is_path_type = path.syntax().parent().and_then(ast::PathType::cast).is_some();
186 self.has_type_args = segment.type_arg_list().is_some();
187
179 if let Some(mut path) = hir::Path::from_ast(path.clone()) { 188 if let Some(mut path) = hir::Path::from_ast(path.clone()) {
180 if !path.is_ident() { 189 if !path.is_ident() {
181 path.segments.pop().unwrap(); 190 path.segments.pop().unwrap();
@@ -228,6 +237,16 @@ impl<'a> CompletionContext<'a> {
228 .expr() 237 .expr()
229 .map(|e| e.syntax().text_range()) 238 .map(|e| e.syntax().text_range())
230 .and_then(|r| find_node_with_range(original_file.syntax(), r)); 239 .and_then(|r| find_node_with_range(original_file.syntax(), r));
240 self.dot_receiver_is_ambiguous_float_literal = if let Some(ast::Expr::Literal(l)) =
241 &self.dot_receiver
242 {
243 match l.kind() {
244 ast::LiteralKind::FloatNumber { suffix: _ } => l.token().text().ends_with('.'),
245 _ => false,
246 }
247 } else {
248 false
249 }
231 } 250 }
232 if let Some(method_call_expr) = ast::MethodCallExpr::cast(parent) { 251 if let Some(method_call_expr) = ast::MethodCallExpr::cast(parent) {
233 // As above 252 // As above
diff --git a/crates/ra_ide_api/src/completion/completion_item.rs b/crates/ra_ide_api/src/completion/completion_item.rs
index b1f0390ec..3e6933bc1 100644
--- a/crates/ra_ide_api/src/completion/completion_item.rs
+++ b/crates/ra_ide_api/src/completion/completion_item.rs
@@ -216,6 +216,10 @@ impl Builder {
216 self.lookup = Some(lookup.into()); 216 self.lookup = Some(lookup.into());
217 self 217 self
218 } 218 }
219 pub(crate) fn label(mut self, label: impl Into<String>) -> Builder {
220 self.label = label.into();
221 self
222 }
219 pub(crate) fn insert_text(mut self, insert_text: impl Into<String>) -> Builder { 223 pub(crate) fn insert_text(mut self, insert_text: impl Into<String>) -> Builder {
220 self.insert_text = Some(insert_text.into()); 224 self.insert_text = Some(insert_text.into());
221 self 225 self
diff --git a/crates/ra_ide_api/src/completion/presentation.rs b/crates/ra_ide_api/src/completion/presentation.rs
index 48028a2f9..aed4ce6d4 100644
--- a/crates/ra_ide_api/src/completion/presentation.rs
+++ b/crates/ra_ide_api/src/completion/presentation.rs
@@ -1,12 +1,12 @@
1//! This modules takes care of rendering various definitions as completion items. 1//! This modules takes care of rendering various definitions as completion items.
2 2
3use hir::{Docs, HasSource, HirDisplay, ScopeDef, Ty, TypeWalk}; 3use hir::{db::HirDatabase, Docs, HasSource, HirDisplay, ScopeDef, Ty, TypeWalk};
4use join_to_string::join; 4use join_to_string::join;
5use ra_syntax::ast::NameOwner; 5use ra_syntax::ast::NameOwner;
6use test_utils::tested_by; 6use test_utils::tested_by;
7 7
8use crate::completion::{ 8use crate::completion::{
9 CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions, 9 db, CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions,
10}; 10};
11 11
12use crate::display::{const_label, function_label, macro_label, type_label}; 12use crate::display::{const_label, function_label, macro_label, type_label};
@@ -44,54 +44,56 @@ impl Completions {
44 ) { 44 ) {
45 use hir::ModuleDef::*; 45 use hir::ModuleDef::*;
46 46
47 let mut completion_kind = CompletionKind::Reference; 47 let completion_kind = match resolution {
48 let (kind, docs) = match resolution { 48 ScopeDef::ModuleDef(BuiltinType(..)) => CompletionKind::BuiltinType,
49 ScopeDef::ModuleDef(Module(it)) => (CompletionItemKind::Module, it.docs(ctx.db)), 49 _ => CompletionKind::Reference,
50 };
51
52 let kind = match resolution {
53 ScopeDef::ModuleDef(Module(..)) => CompletionItemKind::Module,
50 ScopeDef::ModuleDef(Function(func)) => { 54 ScopeDef::ModuleDef(Function(func)) => {
51 return self.add_function_with_name(ctx, Some(local_name), *func); 55 return self.add_function_with_name(ctx, Some(local_name), *func);
52 } 56 }
53 ScopeDef::ModuleDef(Adt(hir::Adt::Struct(it))) => { 57 ScopeDef::ModuleDef(Adt(hir::Adt::Struct(_))) => CompletionItemKind::Struct,
54 (CompletionItemKind::Struct, it.docs(ctx.db)) 58 // FIXME: add CompletionItemKind::Union
55 } 59 ScopeDef::ModuleDef(Adt(hir::Adt::Union(_))) => CompletionItemKind::Struct,
56 ScopeDef::ModuleDef(Adt(hir::Adt::Union(it))) => { 60 ScopeDef::ModuleDef(Adt(hir::Adt::Enum(_))) => CompletionItemKind::Enum,
57 (CompletionItemKind::Struct, it.docs(ctx.db)) 61
58 } 62 ScopeDef::ModuleDef(EnumVariant(..)) => CompletionItemKind::EnumVariant,
59 ScopeDef::ModuleDef(Adt(hir::Adt::Enum(it))) => { 63 ScopeDef::ModuleDef(Const(..)) => CompletionItemKind::Const,
60 (CompletionItemKind::Enum, it.docs(ctx.db)) 64 ScopeDef::ModuleDef(Static(..)) => CompletionItemKind::Static,
61 } 65 ScopeDef::ModuleDef(Trait(..)) => CompletionItemKind::Trait,
62 ScopeDef::ModuleDef(EnumVariant(it)) => { 66 ScopeDef::ModuleDef(TypeAlias(..)) => CompletionItemKind::TypeAlias,
63 (CompletionItemKind::EnumVariant, it.docs(ctx.db)) 67 ScopeDef::ModuleDef(BuiltinType(..)) => CompletionItemKind::BuiltinType,
64 } 68 ScopeDef::GenericParam(..) => CompletionItemKind::TypeParam,
65 ScopeDef::ModuleDef(Const(it)) => (CompletionItemKind::Const, it.docs(ctx.db)), 69 ScopeDef::LocalBinding(..) => CompletionItemKind::Binding,
66 ScopeDef::ModuleDef(Static(it)) => (CompletionItemKind::Static, it.docs(ctx.db)), 70 // (does this need its own kind?)
67 ScopeDef::ModuleDef(Trait(it)) => (CompletionItemKind::Trait, it.docs(ctx.db)), 71 ScopeDef::AdtSelfType(..) | ScopeDef::ImplSelfType(..) => CompletionItemKind::TypeParam,
68 ScopeDef::ModuleDef(TypeAlias(it)) => (CompletionItemKind::TypeAlias, it.docs(ctx.db)),
69 ScopeDef::ModuleDef(BuiltinType(..)) => {
70 completion_kind = CompletionKind::BuiltinType;
71 (CompletionItemKind::BuiltinType, None)
72 }
73 ScopeDef::GenericParam(..) => (CompletionItemKind::TypeParam, None),
74 ScopeDef::LocalBinding(..) => (CompletionItemKind::Binding, None),
75 ScopeDef::SelfType(..) => (
76 CompletionItemKind::TypeParam, // (does this need its own kind?)
77 None,
78 ),
79 ScopeDef::MacroDef(mac) => { 72 ScopeDef::MacroDef(mac) => {
80 self.add_macro(ctx, Some(local_name), *mac); 73 return self.add_macro(ctx, Some(local_name), *mac);
81 return;
82 } 74 }
83 ScopeDef::Unknown => { 75 ScopeDef::Unknown => {
84 self.add(CompletionItem::new( 76 return self.add(CompletionItem::new(
85 CompletionKind::Reference, 77 CompletionKind::Reference,
86 ctx.source_range(), 78 ctx.source_range(),
87 local_name, 79 local_name,
88 )); 80 ));
89 return;
90 } 81 }
91 }; 82 };
92 83
84 let docs = match resolution {
85 ScopeDef::ModuleDef(Module(it)) => it.docs(ctx.db),
86 ScopeDef::ModuleDef(Adt(it)) => it.docs(ctx.db),
87 ScopeDef::ModuleDef(EnumVariant(it)) => it.docs(ctx.db),
88 ScopeDef::ModuleDef(Const(it)) => it.docs(ctx.db),
89 ScopeDef::ModuleDef(Static(it)) => it.docs(ctx.db),
90 ScopeDef::ModuleDef(Trait(it)) => it.docs(ctx.db),
91 ScopeDef::ModuleDef(TypeAlias(it)) => it.docs(ctx.db),
92 _ => None,
93 };
94
93 let mut completion_item = 95 let mut completion_item =
94 CompletionItem::new(completion_kind, ctx.source_range(), local_name); 96 CompletionItem::new(completion_kind, ctx.source_range(), local_name.clone());
95 if let ScopeDef::LocalBinding(pat_id) = resolution { 97 if let ScopeDef::LocalBinding(pat_id) = resolution {
96 let ty = ctx 98 let ty = ctx
97 .analyzer 99 .analyzer
@@ -100,6 +102,28 @@ impl Completions {
100 .map(|t| t.display(ctx.db).to_string()); 102 .map(|t| t.display(ctx.db).to_string());
101 completion_item = completion_item.set_detail(ty); 103 completion_item = completion_item.set_detail(ty);
102 }; 104 };
105
106 // If not an import, add parenthesis automatically.
107 if ctx.is_path_type
108 && !ctx.has_type_args
109 && ctx.db.feature_flags.get("completion.insertion.add-call-parenthesis")
110 {
111 let generic_def: Option<hir::GenericDef> = match resolution {
112 ScopeDef::ModuleDef(Adt(it)) => Some((*it).into()),
113 ScopeDef::ModuleDef(TypeAlias(it)) => Some((*it).into()),
114 _ => None,
115 };
116 if let Some(def) = generic_def {
117 if has_non_default_type_params(def, ctx.db) {
118 tested_by!(inserts_angle_brackets_for_generics);
119 completion_item = completion_item
120 .lookup_by(local_name.clone())
121 .label(format!("{}<…>", local_name))
122 .insert_snippet(format!("{}<$0>", local_name));
123 }
124 }
125 }
126
103 completion_item.kind(kind).set_documentation(docs).add_to(self) 127 completion_item.kind(kind).set_documentation(docs).add_to(self)
104 } 128 }
105 129
@@ -107,6 +131,33 @@ impl Completions {
107 self.add_function_with_name(ctx, None, func) 131 self.add_function_with_name(ctx, None, func)
108 } 132 }
109 133
134 fn guess_macro_braces(&self, macro_name: &str, docs: &str) -> &'static str {
135 let mut votes = [0, 0, 0];
136 for (idx, s) in docs.match_indices(&macro_name) {
137 let (before, after) = (&docs[..idx], &docs[idx + s.len()..]);
138 // Ensure to match the full word
139 if after.starts_with("!")
140 && before
141 .chars()
142 .rev()
143 .next()
144 .map_or(true, |c| c != '_' && !c.is_ascii_alphanumeric())
145 {
146 // It may have spaces before the braces like `foo! {}`
147 match after[1..].chars().find(|&c| !c.is_whitespace()) {
148 Some('{') => votes[0] += 1,
149 Some('[') => votes[1] += 1,
150 Some('(') => votes[2] += 1,
151 _ => {}
152 }
153 }
154 }
155
156 // Insert a space before `{}`.
157 // We prefer the last one when some votes equal.
158 *votes.iter().zip(&[" {$0}", "[$0]", "($0)"]).max_by_key(|&(&vote, _)| vote).unwrap().1
159 }
160
110 pub(crate) fn add_macro( 161 pub(crate) fn add_macro(
111 &mut self, 162 &mut self,
112 ctx: &CompletionContext, 163 ctx: &CompletionContext,
@@ -117,10 +168,9 @@ impl Completions {
117 if let Some(name) = name { 168 if let Some(name) = name {
118 let detail = macro_label(&ast_node); 169 let detail = macro_label(&ast_node);
119 170
120 let macro_braces_to_insert = match name.as_str() { 171 let docs = macro_.docs(ctx.db);
121 "vec" => "[$0]", 172 let macro_braces_to_insert =
122 _ => "($0)", 173 self.guess_macro_braces(&name, docs.as_ref().map_or("", |s| s.as_str()));
123 };
124 let macro_declaration = name + "!"; 174 let macro_declaration = name + "!";
125 175
126 let builder = CompletionItem::new( 176 let builder = CompletionItem::new(
@@ -129,7 +179,7 @@ impl Completions {
129 &macro_declaration, 179 &macro_declaration,
130 ) 180 )
131 .kind(CompletionItemKind::Macro) 181 .kind(CompletionItemKind::Macro)
132 .set_documentation(macro_.docs(ctx.db)) 182 .set_documentation(docs)
133 .detail(detail) 183 .detail(detail)
134 .insert_snippet(macro_declaration + macro_braces_to_insert); 184 .insert_snippet(macro_declaration + macro_braces_to_insert);
135 185
@@ -148,28 +198,31 @@ impl Completions {
148 let ast_node = func.source(ctx.db).ast; 198 let ast_node = func.source(ctx.db).ast;
149 let detail = function_label(&ast_node); 199 let detail = function_label(&ast_node);
150 200
151 let mut builder = CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name) 201 let mut builder =
152 .kind(if data.has_self_param() { 202 CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.clone())
153 CompletionItemKind::Method 203 .kind(if data.has_self_param() {
154 } else { 204 CompletionItemKind::Method
155 CompletionItemKind::Function 205 } else {
156 }) 206 CompletionItemKind::Function
157 .set_documentation(func.docs(ctx.db)) 207 })
158 .detail(detail); 208 .set_documentation(func.docs(ctx.db))
159 // If not an import, add parenthesis automatically. 209 .detail(detail);
210
211 // Add `<>` for generic types
160 if ctx.use_item_syntax.is_none() 212 if ctx.use_item_syntax.is_none()
161 && !ctx.is_call 213 && !ctx.is_call
162 && ctx.db.feature_flags.get("completion.insertion.add-call-parenthesis") 214 && ctx.db.feature_flags.get("completion.insertion.add-call-parenthesis")
163 { 215 {
164 tested_by!(inserts_parens_for_function_calls); 216 tested_by!(inserts_parens_for_function_calls);
165 let snippet = 217 let (snippet, label) =
166 if data.params().is_empty() || data.has_self_param() && data.params().len() == 1 { 218 if data.params().is_empty() || data.has_self_param() && data.params().len() == 1 {
167 format!("{}()$0", data.name()) 219 (format!("{}()$0", data.name()), format!("{}()", name))
168 } else { 220 } else {
169 format!("{}($0)", data.name()) 221 (format!("{}($0)", data.name()), format!("{}(…)", name))
170 }; 222 };
171 builder = builder.insert_snippet(snippet); 223 builder = builder.lookup_by(name.clone()).label(label).insert_snippet(snippet);
172 } 224 }
225
173 self.add(builder) 226 self.add(builder)
174 } 227 }
175 228
@@ -213,7 +266,6 @@ impl Completions {
213 .separator(", ") 266 .separator(", ")
214 .surround_with("(", ")") 267 .surround_with("(", ")")
215 .to_string(); 268 .to_string();
216
217 CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.to_string()) 269 CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.to_string())
218 .kind(CompletionItemKind::EnumVariant) 270 .kind(CompletionItemKind::EnumVariant)
219 .set_documentation(variant.docs(ctx.db)) 271 .set_documentation(variant.docs(ctx.db))
@@ -222,6 +274,11 @@ impl Completions {
222 } 274 }
223} 275}
224 276
277fn has_non_default_type_params(def: hir::GenericDef, db: &db::RootDatabase) -> bool {
278 let subst = db.generic_defaults(def);
279 subst.iter().any(|ty| ty == &Ty::Unknown)
280}
281
225#[cfg(test)] 282#[cfg(test)]
226mod tests { 283mod tests {
227 use crate::completion::{do_completion, CompletionItem, CompletionKind}; 284 use crate::completion::{do_completion, CompletionItem, CompletionKind};
@@ -242,24 +299,28 @@ mod tests {
242 fn main() { no_<|> } 299 fn main() { no_<|> }
243 " 300 "
244 ), 301 ),
245 @r###"[ 302 @r###"
246 CompletionItem { 303 [
247 label: "main", 304 CompletionItem {
248 source_range: [61; 64), 305 label: "main()",
249 delete: [61; 64), 306 source_range: [61; 64),
250 insert: "main()$0", 307 delete: [61; 64),
251 kind: Function, 308 insert: "main()$0",
252 detail: "fn main()", 309 kind: Function,
253 }, 310 lookup: "main",
254 CompletionItem { 311 detail: "fn main()",
255 label: "no_args", 312 },
256 source_range: [61; 64), 313 CompletionItem {
257 delete: [61; 64), 314 label: "no_args()",
258 insert: "no_args()$0", 315 source_range: [61; 64),
259 kind: Function, 316 delete: [61; 64),
260 detail: "fn no_args()", 317 insert: "no_args()$0",
261 }, 318 kind: Function,
262]"### 319 lookup: "no_args",
320 detail: "fn no_args()",
321 },
322 ]
323 "###
263 ); 324 );
264 assert_debug_snapshot!( 325 assert_debug_snapshot!(
265 do_reference_completion( 326 do_reference_completion(
@@ -268,24 +329,28 @@ mod tests {
268 fn main() { with_<|> } 329 fn main() { with_<|> }
269 " 330 "
270 ), 331 ),
271 @r###"[ 332 @r###"
272 CompletionItem { 333 [
273 label: "main", 334 CompletionItem {
274 source_range: [80; 85), 335 label: "main()",
275 delete: [80; 85), 336 source_range: [80; 85),
276 insert: "main()$0", 337 delete: [80; 85),
277 kind: Function, 338 insert: "main()$0",
278 detail: "fn main()", 339 kind: Function,
279 }, 340 lookup: "main",
280 CompletionItem { 341 detail: "fn main()",
281 label: "with_args", 342 },
282 source_range: [80; 85), 343 CompletionItem {
283 delete: [80; 85), 344 label: "with_args(…)",
284 insert: "with_args($0)", 345 source_range: [80; 85),
285 kind: Function, 346 delete: [80; 85),
286 detail: "fn with_args(x: i32, y: String)", 347 insert: "with_args($0)",
287 }, 348 kind: Function,
288]"### 349 lookup: "with_args",
350 detail: "fn with_args(x: i32, y: String)",
351 },
352 ]
353 "###
289 ); 354 );
290 assert_debug_snapshot!( 355 assert_debug_snapshot!(
291 do_reference_completion( 356 do_reference_completion(
@@ -299,16 +364,19 @@ mod tests {
299 } 364 }
300 " 365 "
301 ), 366 ),
302 @r###"[ 367 @r###"
303 CompletionItem { 368 [
304 label: "foo", 369 CompletionItem {
305 source_range: [163; 164), 370 label: "foo()",
306 delete: [163; 164), 371 source_range: [163; 164),
307 insert: "foo()$0", 372 delete: [163; 164),
308 kind: Method, 373 insert: "foo()$0",
309 detail: "fn foo(&self)", 374 kind: Method,
310 }, 375 lookup: "foo",
311]"### 376 detail: "fn foo(&self)",
377 },
378 ]
379 "###
312 ); 380 );
313 } 381 }
314 382
@@ -389,4 +457,123 @@ mod tests {
389]"# 457]"#
390 ); 458 );
391 } 459 }
460
461 #[test]
462 fn inserts_angle_brackets_for_generics() {
463 covers!(inserts_angle_brackets_for_generics);
464 assert_debug_snapshot!(
465 do_reference_completion(
466 r"
467 struct Vec<T> {}
468 fn foo(xs: Ve<|>)
469 "
470 ),
471 @r###"
472 [
473 CompletionItem {
474 label: "Vec<…>",
475 source_range: [61; 63),
476 delete: [61; 63),
477 insert: "Vec<$0>",
478 kind: Struct,
479 lookup: "Vec",
480 },
481 CompletionItem {
482 label: "foo(…)",
483 source_range: [61; 63),
484 delete: [61; 63),
485 insert: "foo($0)",
486 kind: Function,
487 lookup: "foo",
488 detail: "fn foo(xs: Ve)",
489 },
490 ]
491 "###
492 );
493 assert_debug_snapshot!(
494 do_reference_completion(
495 r"
496 type Vec<T> = (T,);
497 fn foo(xs: Ve<|>)
498 "
499 ),
500 @r###"
501 [
502 CompletionItem {
503 label: "Vec<…>",
504 source_range: [64; 66),
505 delete: [64; 66),
506 insert: "Vec<$0>",
507 kind: TypeAlias,
508 lookup: "Vec",
509 },
510 CompletionItem {
511 label: "foo(…)",
512 source_range: [64; 66),
513 delete: [64; 66),
514 insert: "foo($0)",
515 kind: Function,
516 lookup: "foo",
517 detail: "fn foo(xs: Ve)",
518 },
519 ]
520 "###
521 );
522 assert_debug_snapshot!(
523 do_reference_completion(
524 r"
525 struct Vec<T = i128> {}
526 fn foo(xs: Ve<|>)
527 "
528 ),
529 @r###"
530 [
531 CompletionItem {
532 label: "Vec",
533 source_range: [68; 70),
534 delete: [68; 70),
535 insert: "Vec",
536 kind: Struct,
537 },
538 CompletionItem {
539 label: "foo(…)",
540 source_range: [68; 70),
541 delete: [68; 70),
542 insert: "foo($0)",
543 kind: Function,
544 lookup: "foo",
545 detail: "fn foo(xs: Ve)",
546 },
547 ]
548 "###
549 );
550 assert_debug_snapshot!(
551 do_reference_completion(
552 r"
553 struct Vec<T> {}
554 fn foo(xs: Ve<|><i128>)
555 "
556 ),
557 @r###"
558 [
559 CompletionItem {
560 label: "Vec",
561 source_range: [61; 63),
562 delete: [61; 63),
563 insert: "Vec",
564 kind: Struct,
565 },
566 CompletionItem {
567 label: "foo(…)",
568 source_range: [61; 63),
569 delete: [61; 63),
570 insert: "foo($0)",
571 kind: Function,
572 lookup: "foo",
573 detail: "fn foo(xs: Ve<i128>)",
574 },
575 ]
576 "###
577 );
578 }
392} 579}
diff --git a/crates/ra_ide_api/src/db.rs b/crates/ra_ide_api/src/db.rs
index ea0714add..9146b647a 100644
--- a/crates/ra_ide_api/src/db.rs
+++ b/crates/ra_ide_api/src/db.rs
@@ -4,8 +4,10 @@ use std::sync::Arc;
4 4
5use ra_db::{ 5use ra_db::{
6 salsa::{self, Database, Durability}, 6 salsa::{self, Database, Durability},
7 Canceled, CheckCanceled, CrateId, FileId, SourceDatabase, SourceRootId, 7 Canceled, CheckCanceled, CrateId, FileId, FileLoader, FileLoaderDelegate, SourceDatabase,
8 SourceDatabaseExt, SourceRootId,
8}; 9};
10use relative_path::RelativePath;
9use rustc_hash::FxHashMap; 11use rustc_hash::FxHashMap;
10 12
11use crate::{ 13use crate::{
@@ -15,6 +17,7 @@ use crate::{
15 17
16#[salsa::database( 18#[salsa::database(
17 ra_db::SourceDatabaseStorage, 19 ra_db::SourceDatabaseStorage,
20 ra_db::SourceDatabaseExtStorage,
18 LineIndexDatabaseStorage, 21 LineIndexDatabaseStorage,
19 symbol_index::SymbolsDatabaseStorage, 22 symbol_index::SymbolsDatabaseStorage,
20 hir::db::InternDatabaseStorage, 23 hir::db::InternDatabaseStorage,
@@ -31,6 +34,22 @@ pub(crate) struct RootDatabase {
31 pub(crate) last_gc_check: crate::wasm_shims::Instant, 34 pub(crate) last_gc_check: crate::wasm_shims::Instant,
32} 35}
33 36
37impl FileLoader for RootDatabase {
38 fn file_text(&self, file_id: FileId) -> Arc<String> {
39 FileLoaderDelegate(self).file_text(file_id)
40 }
41 fn resolve_relative_path(
42 &self,
43 anchor: FileId,
44 relative_path: &RelativePath,
45 ) -> Option<FileId> {
46 FileLoaderDelegate(self).resolve_relative_path(anchor, relative_path)
47 }
48 fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> {
49 FileLoaderDelegate(self).relevant_crates(file_id)
50 }
51}
52
34impl hir::debug::HirDebugHelper for RootDatabase { 53impl hir::debug::HirDebugHelper for RootDatabase {
35 fn crate_name(&self, krate: CrateId) -> Option<String> { 54 fn crate_name(&self, krate: CrateId) -> Option<String> {
36 self.debug_data.crate_names.get(&krate).cloned() 55 self.debug_data.crate_names.get(&krate).cloned()
@@ -39,7 +58,7 @@ impl hir::debug::HirDebugHelper for RootDatabase {
39 let source_root_id = self.file_source_root(file_id); 58 let source_root_id = self.file_source_root(file_id);
40 let source_root_path = self.debug_data.root_paths.get(&source_root_id)?; 59 let source_root_path = self.debug_data.root_paths.get(&source_root_id)?;
41 let file_path = self.file_relative_path(file_id); 60 let file_path = self.file_relative_path(file_id);
42 Some(format!("{}/{}", source_root_path, file_path.display())) 61 Some(format!("{}/{}", source_root_path, file_path))
43 } 62 }
44} 63}
45 64
@@ -104,7 +123,7 @@ pub(crate) trait LineIndexDatabase: ra_db::SourceDatabase + CheckCanceled {
104 fn line_index(&self, file_id: FileId) -> Arc<LineIndex>; 123 fn line_index(&self, file_id: FileId) -> Arc<LineIndex>;
105} 124}
106 125
107fn line_index(db: &impl ra_db::SourceDatabase, file_id: FileId) -> Arc<LineIndex> { 126fn line_index(db: &impl LineIndexDatabase, file_id: FileId) -> Arc<LineIndex> {
108 let text = db.file_text(file_id); 127 let text = db.file_text(file_id);
109 Arc::new(LineIndex::new(&*text)) 128 Arc::new(LineIndex::new(&*text))
110} 129}
diff --git a/crates/ra_ide_api/src/diagnostics.rs b/crates/ra_ide_api/src/diagnostics.rs
index 0435188c8..8743a3a79 100644
--- a/crates/ra_ide_api/src/diagnostics.rs
+++ b/crates/ra_ide_api/src/diagnostics.rs
@@ -4,7 +4,7 @@ use std::cell::RefCell;
4 4
5use hir::diagnostics::{AstDiagnostic, Diagnostic as _, DiagnosticSink}; 5use hir::diagnostics::{AstDiagnostic, Diagnostic as _, DiagnosticSink};
6use itertools::Itertools; 6use itertools::Itertools;
7use ra_db::SourceDatabase; 7use ra_db::{SourceDatabase, SourceDatabaseExt};
8use ra_prof::profile; 8use ra_prof::profile;
9use ra_syntax::{ 9use ra_syntax::{
10 algo, 10 algo,
@@ -12,6 +12,7 @@ use ra_syntax::{
12 Location, SyntaxNode, TextRange, T, 12 Location, SyntaxNode, TextRange, T,
13}; 13};
14use ra_text_edit::{TextEdit, TextEditBuilder}; 14use ra_text_edit::{TextEdit, TextEditBuilder};
15use relative_path::RelativePath;
15 16
16use crate::{db::RootDatabase, Diagnostic, FileId, FileSystemEdit, SourceChange, SourceFileEdit}; 17use crate::{db::RootDatabase, Diagnostic, FileId, FileSystemEdit, SourceChange, SourceFileEdit};
17 18
@@ -47,8 +48,14 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic>
47 }) 48 })
48 }) 49 })
49 .on::<hir::diagnostics::UnresolvedModule, _>(|d| { 50 .on::<hir::diagnostics::UnresolvedModule, _>(|d| {
50 let source_root = db.file_source_root(d.source().file_id.original_file(db)); 51 let original_file = d.source().file_id.original_file(db);
51 let create_file = FileSystemEdit::CreateFile { source_root, path: d.candidate.clone() }; 52 let source_root = db.file_source_root(original_file);
53 let path = db
54 .file_relative_path(original_file)
55 .parent()
56 .unwrap_or_else(|| RelativePath::new(""))
57 .join(&d.candidate);
58 let create_file = FileSystemEdit::CreateFile { source_root, path };
52 let fix = SourceChange::file_system_edit("create module", create_file); 59 let fix = SourceChange::file_system_edit("create module", create_file);
53 res.borrow_mut().push(Diagnostic { 60 res.borrow_mut().push(Diagnostic {
54 range: d.highlight_range(), 61 range: d.highlight_range(),
diff --git a/crates/ra_ide_api/src/display/navigation_target.rs b/crates/ra_ide_api/src/display/navigation_target.rs
index d0b1a8a2a..5cb67fb95 100644
--- a/crates/ra_ide_api/src/display/navigation_target.rs
+++ b/crates/ra_ide_api/src/display/navigation_target.rs
@@ -119,7 +119,7 @@ impl NavigationTarget {
119 119
120 pub(crate) fn from_module(db: &RootDatabase, module: hir::Module) -> NavigationTarget { 120 pub(crate) fn from_module(db: &RootDatabase, module: hir::Module) -> NavigationTarget {
121 let src = module.definition_source(db); 121 let src = module.definition_source(db);
122 let file_id = src.file_id.as_original_file(); 122 let file_id = src.file_id.original_file(db);
123 let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default(); 123 let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default();
124 match src.ast { 124 match src.ast {
125 ModuleSource::SourceFile(node) => { 125 ModuleSource::SourceFile(node) => {
@@ -139,7 +139,7 @@ impl NavigationTarget {
139 pub(crate) fn from_module_to_decl(db: &RootDatabase, module: hir::Module) -> NavigationTarget { 139 pub(crate) fn from_module_to_decl(db: &RootDatabase, module: hir::Module) -> NavigationTarget {
140 let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default(); 140 let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default();
141 if let Some(src) = module.declaration_source(db) { 141 if let Some(src) = module.declaration_source(db) {
142 let file_id = src.file_id.as_original_file(); 142 let file_id = src.file_id.original_file(db);
143 return NavigationTarget::from_syntax( 143 return NavigationTarget::from_syntax(
144 file_id, 144 file_id,
145 name, 145 name,
@@ -213,7 +213,7 @@ impl NavigationTarget {
213 ) -> NavigationTarget { 213 ) -> NavigationTarget {
214 let src = impl_block.source(db); 214 let src = impl_block.source(db);
215 NavigationTarget::from_syntax( 215 NavigationTarget::from_syntax(
216 src.file_id.as_original_file(), 216 src.file_id.original_file(db),
217 "impl".into(), 217 "impl".into(),
218 None, 218 None,
219 src.ast.syntax(), 219 src.ast.syntax(),
diff --git a/crates/ra_ide_api/src/extend_selection.rs b/crates/ra_ide_api/src/extend_selection.rs
index 33fefb541..602757e92 100644
--- a/crates/ra_ide_api/src/extend_selection.rs
+++ b/crates/ra_ide_api/src/extend_selection.rs
@@ -32,6 +32,7 @@ fn try_extend_selection(root: &SyntaxNode, range: TextRange) -> Option<TextRange
32 PARAM_LIST, 32 PARAM_LIST,
33 ARG_LIST, 33 ARG_LIST,
34 ARRAY_EXPR, 34 ARRAY_EXPR,
35 TUPLE_EXPR,
35 ]; 36 ];
36 37
37 if range.is_empty() { 38 if range.is_empty() {
@@ -245,6 +246,8 @@ mod tests {
245 do_check(r#"const FOO: [usize; 2] = [ 22 , 33<|>];"#, &["33", ", 33"]); 246 do_check(r#"const FOO: [usize; 2] = [ 22 , 33<|>];"#, &["33", ", 33"]);
246 do_check(r#"const FOO: [usize; 2] = [ 22 , 33<|> ,];"#, &["33", ", 33"]); 247 do_check(r#"const FOO: [usize; 2] = [ 22 , 33<|> ,];"#, &["33", ", 33"]);
247 248
249 do_check(r#"fn main() { (1, 2<|>) }"#, &["2", ", 2", "(1, 2)"]);
250
248 do_check( 251 do_check(
249 r#" 252 r#"
250const FOO: [usize; 2] = [ 253const FOO: [usize; 2] = [
diff --git a/crates/ra_ide_api/src/goto_definition.rs b/crates/ra_ide_api/src/goto_definition.rs
index 41a88314f..1f3fa6c57 100644
--- a/crates/ra_ide_api/src/goto_definition.rs
+++ b/crates/ra_ide_api/src/goto_definition.rs
@@ -10,7 +10,7 @@ use ra_syntax::{
10use crate::{ 10use crate::{
11 db::RootDatabase, 11 db::RootDatabase,
12 display::ShortLabel, 12 display::ShortLabel,
13 name_ref_kind::{classify_name_ref, NameRefKind::*}, 13 references::{classify_name_ref, NameKind::*},
14 FilePosition, NavigationTarget, RangeInfo, 14 FilePosition, NavigationTarget, RangeInfo,
15}; 15};
16 16
@@ -54,13 +54,11 @@ pub(crate) fn reference_definition(
54) -> ReferenceResult { 54) -> ReferenceResult {
55 use self::ReferenceResult::*; 55 use self::ReferenceResult::*;
56 56
57 let analyzer = hir::SourceAnalyzer::new(db, file_id, name_ref.syntax(), None); 57 let name_kind = classify_name_ref(db, file_id, &name_ref).map(|d| d.kind);
58 58 match name_kind {
59 match classify_name_ref(db, &analyzer, name_ref) {
60 Some(Macro(mac)) => return Exact(NavigationTarget::from_macro_def(db, mac)), 59 Some(Macro(mac)) => return Exact(NavigationTarget::from_macro_def(db, mac)),
61 Some(FieldAccess(field)) => return Exact(NavigationTarget::from_field(db, field)), 60 Some(Field(field)) => return Exact(NavigationTarget::from_field(db, field)),
62 Some(AssocItem(assoc)) => return Exact(NavigationTarget::from_assoc_item(db, assoc)), 61 Some(AssocItem(assoc)) => return Exact(NavigationTarget::from_assoc_item(db, assoc)),
63 Some(Method(func)) => return Exact(NavigationTarget::from_def_source(db, func)),
64 Some(Def(def)) => match NavigationTarget::from_def(db, def) { 62 Some(Def(def)) => match NavigationTarget::from_def(db, def) {
65 Some(nav) => return Exact(nav), 63 Some(nav) => return Exact(nav),
66 None => return Approximate(vec![]), 64 None => return Approximate(vec![]),
@@ -70,7 +68,7 @@ pub(crate) fn reference_definition(
70 return Exact(NavigationTarget::from_adt_def(db, def_id)); 68 return Exact(NavigationTarget::from_adt_def(db, def_id));
71 } 69 }
72 } 70 }
73 Some(Pat(pat)) => return Exact(NavigationTarget::from_pat(db, file_id, pat)), 71 Some(Pat((_, pat))) => return Exact(NavigationTarget::from_pat(db, file_id, pat)),
74 Some(SelfParam(par)) => return Exact(NavigationTarget::from_self_param(file_id, par)), 72 Some(SelfParam(par)) => return Exact(NavigationTarget::from_self_param(file_id, par)),
75 Some(GenericParam(_)) => { 73 Some(GenericParam(_)) => {
76 // FIXME: go to the generic param def 74 // FIXME: go to the generic param def
diff --git a/crates/ra_ide_api/src/hover.rs b/crates/ra_ide_api/src/hover.rs
index 24b161c5c..ba328efa1 100644
--- a/crates/ra_ide_api/src/hover.rs
+++ b/crates/ra_ide_api/src/hover.rs
@@ -14,7 +14,7 @@ use crate::{
14 description_from_symbol, docs_from_symbol, macro_label, rust_code_markup, 14 description_from_symbol, docs_from_symbol, macro_label, rust_code_markup,
15 rust_code_markup_with_doc, ShortLabel, 15 rust_code_markup_with_doc, ShortLabel,
16 }, 16 },
17 name_ref_kind::{classify_name_ref, NameRefKind::*}, 17 references::{classify_name_ref, NameKind::*},
18 FilePosition, FileRange, RangeInfo, 18 FilePosition, FileRange, RangeInfo,
19}; 19};
20 20
@@ -99,17 +99,14 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn
99 99
100 let mut range = None; 100 let mut range = None;
101 if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(file.syntax(), position.offset) { 101 if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(file.syntax(), position.offset) {
102 let analyzer = hir::SourceAnalyzer::new(db, position.file_id, name_ref.syntax(), None);
103
104 let mut no_fallback = false; 102 let mut no_fallback = false;
105 103 let name_kind = classify_name_ref(db, position.file_id, &name_ref).map(|d| d.kind);
106 match classify_name_ref(db, &analyzer, &name_ref) { 104 match name_kind {
107 Some(Method(it)) => res.extend(from_def_source(db, it)),
108 Some(Macro(it)) => { 105 Some(Macro(it)) => {
109 let src = it.source(db); 106 let src = it.source(db);
110 res.extend(hover_text(src.ast.doc_comment_text(), Some(macro_label(&src.ast)))); 107 res.extend(hover_text(src.ast.doc_comment_text(), Some(macro_label(&src.ast))));
111 } 108 }
112 Some(FieldAccess(it)) => { 109 Some(Field(it)) => {
113 let src = it.source(db); 110 let src = it.source(db);
114 if let hir::FieldSource::Named(it) = src.ast { 111 if let hir::FieldSource::Named(it) = src.ast {
115 res.extend(hover_text(it.doc_comment_text(), it.short_label())); 112 res.extend(hover_text(it.doc_comment_text(), it.short_label()));
diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs
index 24f1b91f6..19669a7f0 100644
--- a/crates/ra_ide_api/src/lib.rs
+++ b/crates/ra_ide_api/src/lib.rs
@@ -19,7 +19,6 @@ mod feature_flags;
19mod status; 19mod status;
20mod completion; 20mod completion;
21mod runnables; 21mod runnables;
22mod name_ref_kind;
23mod goto_definition; 22mod goto_definition;
24mod goto_type_definition; 23mod goto_type_definition;
25mod extend_selection; 24mod extend_selection;
@@ -52,7 +51,7 @@ use std::sync::Arc;
52use ra_cfg::CfgOptions; 51use ra_cfg::CfgOptions;
53use ra_db::{ 52use ra_db::{
54 salsa::{self, ParallelDatabase}, 53 salsa::{self, ParallelDatabase},
55 CheckCanceled, SourceDatabase, 54 CheckCanceled, FileLoader, SourceDatabase,
56}; 55};
57use ra_syn