aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.github/workflows/ci.yaml17
-rw-r--r--Cargo.lock49
-rw-r--r--crates/ra_assists/src/doc_tests/generated.rs17
-rw-r--r--crates/ra_assists/src/handlers/add_missing_impl_members.rs34
-rw-r--r--crates/ra_assists/src/handlers/reorder_fields.rs224
-rw-r--r--crates/ra_assists/src/lib.rs2
-rw-r--r--crates/ra_hir/src/source_analyzer.rs36
-rw-r--r--crates/ra_hir_def/src/adt.rs20
-rw-r--r--crates/ra_hir_def/src/attr.rs1
-rw-r--r--crates/ra_hir_def/src/body.rs66
-rw-r--r--crates/ra_hir_def/src/body/lower.rs53
-rw-r--r--crates/ra_hir_def/src/data.rs37
-rw-r--r--crates/ra_hir_def/src/nameres/collector.rs14
-rw-r--r--crates/ra_hir_def/src/nameres/tests/incremental.rs6
-rw-r--r--crates/ra_hir_def/src/path.rs5
-rw-r--r--crates/ra_hir_expand/src/name.rs9
-rw-r--r--crates/ra_hir_ty/src/db.rs9
-rw-r--r--crates/ra_hir_ty/src/expr.rs55
-rw-r--r--crates/ra_hir_ty/src/method_resolution.rs45
-rw-r--r--crates/ra_hir_ty/src/tests.rs4
-rw-r--r--crates/ra_hir_ty/src/tests/patterns.rs3
-rw-r--r--crates/ra_hir_ty/src/traits.rs14
-rw-r--r--crates/ra_hir_ty/src/traits/chalk.rs11
-rw-r--r--crates/ra_ide/src/call_info.rs21
-rw-r--r--crates/ra_ide/src/completion/complete_pattern.rs4
-rw-r--r--crates/ra_ide/src/completion/complete_record.rs2
-rw-r--r--crates/ra_ide/src/completion/complete_unqualified_path.rs9
-rw-r--r--crates/ra_ide/src/completion/completion_context.rs14
-rw-r--r--crates/ra_ide/src/inlay_hints.rs17
-rw-r--r--crates/ra_ide_db/src/defs.rs2
-rw-r--r--crates/ra_parser/src/grammar.rs11
-rw-r--r--crates/ra_parser/src/grammar/expressions.rs37
-rw-r--r--crates/ra_parser/src/grammar/patterns.rs50
-rw-r--r--crates/ra_proc_macro_srv/Cargo.toml5
-rw-r--r--crates/ra_proc_macro_srv/src/dylib.rs211
-rw-r--r--crates/ra_proc_macro_srv/src/lib.rs36
-rw-r--r--crates/ra_proc_macro_srv/src/rustc_server.rs4
-rw-r--r--crates/ra_proc_macro_srv/src/tests/fixtures/test_serialize_proc_macro.txt188
-rw-r--r--crates/ra_proc_macro_srv/src/tests/mod.rs47
-rw-r--r--crates/ra_proc_macro_srv/src/tests/utils.rs65
-rw-r--r--crates/ra_syntax/src/ast.rs4
-rw-r--r--crates/ra_syntax/src/ast/extensions.rs60
-rw-r--r--crates/ra_syntax/src/ast/generated/nodes.rs2
-rw-r--r--crates/ra_syntax/src/ast/make.rs3
-rw-r--r--crates/ra_syntax/src/lib.rs5
-rw-r--r--crates/ra_syntax/test_data/parser/inline/err/0014_record_literal_before_ellipsis_recovery.rast49
-rw-r--r--crates/ra_syntax/test_data/parser/inline/err/0014_record_literal_before_ellipsis_recovery.rs3
-rw-r--r--crates/ra_syntax/test_data/parser/inline/ok/0061_record_lit.rast14
-rw-r--r--crates/ra_syntax/test_data/parser/inline/ok/0102_record_field_pat_list.rast26
-rw-r--r--crates/ra_syntax/test_data/parser/inline/ok/0143_box_pat.rast15
-rw-r--r--crates/ra_syntax/test_data/parser/inline/ok/0145_record_field_pat.rast5
-rw-r--r--crates/ra_syntax/test_data/parser/ok/0063_trait_fn_patterns.rast14
-rw-r--r--crates/ra_syntax/test_data/parser/ok/0064_impl_fn_params.rast14
-rw-r--r--crates/rust-analyzer/src/cli/analysis_stats.rs12
-rw-r--r--crates/rust-analyzer/src/cli/load_cargo.rs4
-rw-r--r--crates/rust-analyzer/src/world.rs4
-rw-r--r--crates/test_utils/src/lib.rs2
-rw-r--r--docs/user/assists.md17
-rw-r--r--editors/code/src/config.ts39
-rw-r--r--editors/code/src/inlay_hints.ts10
-rw-r--r--xtask/src/ast_src.rs2
-rw-r--r--xtask/tests/tidy-tests/main.rs11
62 files changed, 1439 insertions, 330 deletions
diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml
index 02a3b6228..00f299ff1 100644
--- a/.github/workflows/ci.yaml
+++ b/.github/workflows/ci.yaml
@@ -20,25 +20,14 @@ jobs:
20 name: Audit Rust vulnerabilities 20 name: Audit Rust vulnerabilities
21 runs-on: ubuntu-latest 21 runs-on: ubuntu-latest
22 steps: 22 steps:
23 - name: Install Rust toolchain
24 uses: actions-rs/toolchain@v1
25 with:
26 toolchain: stable
27 profile: minimal
28 override: true
29
30 - name: Checkout repository 23 - name: Checkout repository
31 uses: actions/checkout@v2 24 uses: actions/checkout@v2
32 25
33 - run: sudo chown -R $(whoami):$(id -ng) ~/.cargo/ 26 - uses: actions-rs/[email protected]
34
35 - name: Cache cargo
36 uses: actions/cache@v1
37 with: 27 with:
38 path: ~/.cargo/ 28 crate: cargo-audit
39 key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} 29 use-tool-cache: true
40 30
41 - run: cargo install cargo-audit
42 - run: cargo audit 31 - run: cargo audit
43 32
44 rust: 33 rust:
diff --git a/Cargo.lock b/Cargo.lock
index 34c535162..975c1aef8 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -425,6 +425,17 @@ dependencies = [
425] 425]
426 426
427[[package]] 427[[package]]
428name = "goblin"
429version = "0.2.1"
430source = "registry+https://github.com/rust-lang/crates.io-index"
431checksum = "ddd5e3132801a1ac34ac53b97acde50c4685414dd2f291b9ea52afa6f07468c8"
432dependencies = [
433 "log",
434 "plain",
435 "scroll",
436]
437
438[[package]]
428name = "heck" 439name = "heck"
429version = "0.3.1" 440version = "0.3.1"
430source = "registry+https://github.com/rust-lang/crates.io-index" 441source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -587,6 +598,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
587checksum = "dea0c0405123bba743ee3f91f49b1c7cfb684eef0da0a50110f758ccf24cdff0" 598checksum = "dea0c0405123bba743ee3f91f49b1c7cfb684eef0da0a50110f758ccf24cdff0"
588 599
589[[package]] 600[[package]]
601name = "libloading"
602version = "0.6.0"
603source = "registry+https://github.com/rust-lang/crates.io-index"
604checksum = "2c979a19ffb457f0273965c333053f3d586bf759bf7b683fbebc37f9a9ebedc4"
605dependencies = [
606 "winapi 0.3.8",
607]
608
609[[package]]
590name = "linked-hash-map" 610name = "linked-hash-map"
591version = "0.5.2" 611version = "0.5.2"
592source = "registry+https://github.com/rust-lang/crates.io-index" 612source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -826,6 +846,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
826checksum = "3ad1f1b834a05d42dae330066e9699a173b28185b3bdc3dbf14ca239585de8cc" 846checksum = "3ad1f1b834a05d42dae330066e9699a173b28185b3bdc3dbf14ca239585de8cc"
827 847
828[[package]] 848[[package]]
849name = "plain"
850version = "0.2.3"
851source = "registry+https://github.com/rust-lang/crates.io-index"
852checksum = "b4596b6d070b27117e987119b4dac604f3c58cfb0b191112e24771b2faeac1a6"
853
854[[package]]
829name = "ppv-lite86" 855name = "ppv-lite86"
830version = "0.2.6" 856version = "0.2.6"
831source = "registry+https://github.com/rust-lang/crates.io-index" 857source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1082,10 +1108,13 @@ version = "0.1.0"
1082dependencies = [ 1108dependencies = [
1083 "cargo_metadata", 1109 "cargo_metadata",
1084 "difference", 1110 "difference",
1111 "goblin",
1112 "libloading",
1085 "ra_mbe", 1113 "ra_mbe",
1086 "ra_proc_macro", 1114 "ra_proc_macro",
1087 "ra_tt", 1115 "ra_tt",
1088 "serde_derive", 1116 "serde_derive",
1117 "test_utils",
1089] 1118]
1090 1119
1091[[package]] 1120[[package]]
@@ -1404,6 +1433,26 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
1404checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" 1433checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
1405 1434
1406[[package]] 1435[[package]]
1436name = "scroll"
1437version = "0.10.1"
1438source = "registry+https://github.com/rust-lang/crates.io-index"
1439checksum = "abb2332cb595d33f7edd5700f4cbf94892e680c7f0ae56adab58a35190b66cb1"
1440dependencies = [
1441 "scroll_derive",
1442]
1443
1444[[package]]
1445name = "scroll_derive"
1446version = "0.10.1"
1447source = "registry+https://github.com/rust-lang/crates.io-index"
1448checksum = "f8584eea9b9ff42825b46faf46a8c24d2cff13ec152fa2a50df788b87c07ee28"
1449dependencies = [
1450 "proc-macro2",
1451 "quote",
1452 "syn",
1453]
1454
1455[[package]]
1407name = "semver" 1456name = "semver"
1408version = "0.9.0" 1457version = "0.9.0"
1409source = "registry+https://github.com/rust-lang/crates.io-index" 1458source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/crates/ra_assists/src/doc_tests/generated.rs b/crates/ra_assists/src/doc_tests/generated.rs
index 64444ee3a..b63b4d81a 100644
--- a/crates/ra_assists/src/doc_tests/generated.rs
+++ b/crates/ra_assists/src/doc_tests/generated.rs
@@ -180,7 +180,7 @@ trait Trait<T> {
180} 180}
181 181
182impl Trait<u32> for () { 182impl Trait<u32> for () {
183 fn foo(&self) -> u32 { unimplemented!() } 183 fn foo(&self) -> u32 { todo!() }
184 184
185} 185}
186"#####, 186"#####,
@@ -607,6 +607,21 @@ impl Walrus {
607} 607}
608 608
609#[test] 609#[test]
610fn doctest_reorder_fields() {
611 check(
612 "reorder_fields",
613 r#####"
614struct Foo {foo: i32, bar: i32};
615const test: Foo = <|>Foo {bar: 0, foo: 1}
616"#####,
617 r#####"
618struct Foo {foo: i32, bar: i32};
619const test: Foo = Foo {foo: 1, bar: 0}
620"#####,
621 )
622}
623
624#[test]
610fn doctest_replace_if_let_with_match() { 625fn doctest_replace_if_let_with_match() {
611 check( 626 check(
612 "replace_if_let_with_match", 627 "replace_if_let_with_match",
diff --git a/crates/ra_assists/src/handlers/add_missing_impl_members.rs b/crates/ra_assists/src/handlers/add_missing_impl_members.rs
index 722f207e2..2d6d44980 100644
--- a/crates/ra_assists/src/handlers/add_missing_impl_members.rs
+++ b/crates/ra_assists/src/handlers/add_missing_impl_members.rs
@@ -40,7 +40,7 @@ enum AddMissingImplMembersMode {
40// } 40// }
41// 41//
42// impl Trait<u32> for () { 42// impl Trait<u32> for () {
43// fn foo(&self) -> u32 { unimplemented!() } 43// fn foo(&self) -> u32 { todo!() }
44// 44//
45// } 45// }
46// ``` 46// ```
@@ -165,7 +165,7 @@ fn add_missing_impl_members_inner(
165 165
166fn add_body(fn_def: ast::FnDef) -> ast::FnDef { 166fn add_body(fn_def: ast::FnDef) -> ast::FnDef {
167 if fn_def.body().is_none() { 167 if fn_def.body().is_none() {
168 fn_def.with_body(make::block_from_expr(make::expr_unimplemented())) 168 fn_def.with_body(make::block_from_expr(make::expr_todo()))
169 } else { 169 } else {
170 fn_def 170 fn_def
171 } 171 }
@@ -215,8 +215,8 @@ impl Foo for S {
215 fn bar(&self) {} 215 fn bar(&self) {}
216 <|>type Output; 216 <|>type Output;
217 const CONST: usize = 42; 217 const CONST: usize = 42;
218 fn foo(&self) { unimplemented!() } 218 fn foo(&self) { todo!() }
219 fn baz(&self) { unimplemented!() } 219 fn baz(&self) { todo!() }
220 220
221}", 221}",
222 ); 222 );
@@ -250,7 +250,7 @@ struct S;
250 250
251impl Foo for S { 251impl Foo for S {
252 fn bar(&self) {} 252 fn bar(&self) {}
253 <|>fn foo(&self) { unimplemented!() } 253 <|>fn foo(&self) { todo!() }
254 254
255}", 255}",
256 ); 256 );
@@ -268,7 +268,7 @@ impl Foo for S { <|> }",
268trait Foo { fn foo(&self); } 268trait Foo { fn foo(&self); }
269struct S; 269struct S;
270impl Foo for S { 270impl Foo for S {
271 <|>fn foo(&self) { unimplemented!() } 271 <|>fn foo(&self) { todo!() }
272}", 272}",
273 ); 273 );
274 } 274 }
@@ -285,7 +285,7 @@ impl Foo<u32> for S { <|> }",
285trait Foo<T> { fn foo(&self, t: T) -> &T; } 285trait Foo<T> { fn foo(&self, t: T) -> &T; }
286struct S; 286struct S;
287impl Foo<u32> for S { 287impl Foo<u32> for S {
288 <|>fn foo(&self, t: u32) -> &u32 { unimplemented!() } 288 <|>fn foo(&self, t: u32) -> &u32 { todo!() }
289}", 289}",
290 ); 290 );
291 } 291 }
@@ -302,7 +302,7 @@ impl<U> Foo<U> for S { <|> }",
302trait Foo<T> { fn foo(&self, t: T) -> &T; } 302trait Foo<T> { fn foo(&self, t: T) -> &T; }
303struct S; 303struct S;
304impl<U> Foo<U> for S { 304impl<U> Foo<U> for S {
305 <|>fn foo(&self, t: U) -> &U { unimplemented!() } 305 <|>fn foo(&self, t: U) -> &U { todo!() }
306}", 306}",
307 ); 307 );
308 } 308 }
@@ -319,7 +319,7 @@ impl Foo for S {}<|>",
319trait Foo { fn foo(&self); } 319trait Foo { fn foo(&self); }
320struct S; 320struct S;
321impl Foo for S { 321impl Foo for S {
322 <|>fn foo(&self) { unimplemented!() } 322 <|>fn foo(&self) { todo!() }
323}", 323}",
324 ) 324 )
325 } 325 }
@@ -342,7 +342,7 @@ mod foo {
342} 342}
343struct S; 343struct S;
344impl foo::Foo for S { 344impl foo::Foo for S {
345 <|>fn foo(&self, bar: foo::Bar) { unimplemented!() } 345 <|>fn foo(&self, bar: foo::Bar) { todo!() }
346}", 346}",
347 ); 347 );
348 } 348 }
@@ -365,7 +365,7 @@ mod foo {
365} 365}
366struct S; 366struct S;
367impl foo::Foo for S { 367impl foo::Foo for S {
368 <|>fn foo(&self, bar: foo::Bar<u32>) { unimplemented!() } 368 <|>fn foo(&self, bar: foo::Bar<u32>) { todo!() }
369}", 369}",
370 ); 370 );
371 } 371 }
@@ -388,7 +388,7 @@ mod foo {
388} 388}
389struct S; 389struct S;
390impl foo::Foo<u32> for S { 390impl foo::Foo<u32> for S {
391 <|>fn foo(&self, bar: foo::Bar<u32>) { unimplemented!() } 391 <|>fn foo(&self, bar: foo::Bar<u32>) { todo!() }
392}", 392}",
393 ); 393 );
394 } 394 }
@@ -414,7 +414,7 @@ mod foo {
414struct Param; 414struct Param;
415struct S; 415struct S;
416impl foo::Foo<Param> for S { 416impl foo::Foo<Param> for S {
417 <|>fn foo(&self, bar: Param) { unimplemented!() } 417 <|>fn foo(&self, bar: Param) { todo!() }
418}", 418}",
419 ); 419 );
420 } 420 }
@@ -439,7 +439,7 @@ mod foo {
439} 439}
440struct S; 440struct S;
441impl foo::Foo for S { 441impl foo::Foo for S {
442 <|>fn foo(&self, bar: foo::Bar<u32>::Assoc) { unimplemented!() } 442 <|>fn foo(&self, bar: foo::Bar<u32>::Assoc) { todo!() }
443}", 443}",
444 ); 444 );
445 } 445 }
@@ -464,7 +464,7 @@ mod foo {
464} 464}
465struct S; 465struct S;
466impl foo::Foo for S { 466impl foo::Foo for S {
467 <|>fn foo(&self, bar: foo::Bar<foo::Baz>) { unimplemented!() } 467 <|>fn foo(&self, bar: foo::Bar<foo::Baz>) { todo!() }
468}", 468}",
469 ); 469 );
470 } 470 }
@@ -487,7 +487,7 @@ mod foo {
487} 487}
488struct S; 488struct S;
489impl foo::Foo for S { 489impl foo::Foo for S {
490 <|>fn foo(&self, bar: dyn Fn(u32) -> i32) { unimplemented!() } 490 <|>fn foo(&self, bar: dyn Fn(u32) -> i32) { todo!() }
491}", 491}",
492 ); 492 );
493 } 493 }
@@ -544,7 +544,7 @@ trait Foo {
544struct S; 544struct S;
545impl Foo for S { 545impl Foo for S {
546 <|>type Output; 546 <|>type Output;
547 fn foo(&self) { unimplemented!() } 547 fn foo(&self) { todo!() }
548}"#, 548}"#,
549 ) 549 )
550 } 550 }
diff --git a/crates/ra_assists/src/handlers/reorder_fields.rs b/crates/ra_assists/src/handlers/reorder_fields.rs
new file mode 100644
index 000000000..a43e53a11
--- /dev/null
+++ b/crates/ra_assists/src/handlers/reorder_fields.rs
@@ -0,0 +1,224 @@
1use std::collections::HashMap;
2
3use hir::{Adt, ModuleDef, PathResolution, Semantics, Struct};
4use itertools::Itertools;
5use ra_ide_db::RootDatabase;
6use ra_syntax::{
7 algo,
8 ast::{self, Path, RecordLit, RecordPat},
9 match_ast, AstNode, SyntaxKind,
10 SyntaxKind::*,
11 SyntaxNode,
12};
13
14use crate::{
15 assist_ctx::{Assist, AssistCtx},
16 AssistId,
17};
18
19// Assist: reorder_fields
20//
21// Reorder the fields of record literals and record patterns in the same order as in
22// the definition.
23//
24// ```
25// struct Foo {foo: i32, bar: i32};
26// const test: Foo = <|>Foo {bar: 0, foo: 1}
27// ```
28// ->
29// ```
30// struct Foo {foo: i32, bar: i32};
31// const test: Foo = Foo {foo: 1, bar: 0}
32// ```
33//
34pub(crate) fn reorder_fields(ctx: AssistCtx) -> Option<Assist> {
35 reorder::<RecordLit>(ctx.clone()).or_else(|| reorder::<RecordPat>(ctx))
36}
37
38fn reorder<R: AstNode>(ctx: AssistCtx) -> Option<Assist> {
39 let record = ctx.find_node_at_offset::<R>()?;
40 let path = record.syntax().children().find_map(Path::cast)?;
41
42 let ranks = compute_fields_ranks(&path, &ctx)?;
43
44 let fields = get_fields(&record.syntax());
45 let sorted_fields = sorted_by_rank(&fields, |node| {
46 *ranks.get(&get_field_name(node)).unwrap_or(&usize::max_value())
47 });
48
49 if sorted_fields == fields {
50 return None;
51 }
52
53 ctx.add_assist(AssistId("reorder_fields"), "Reorder record fields", |edit| {
54 for (old, new) in fields.iter().zip(&sorted_fields) {
55 algo::diff(old, new).into_text_edit(edit.text_edit_builder());
56 }
57 edit.target(record.syntax().text_range())
58 })
59}
60
61fn get_fields_kind(node: &SyntaxNode) -> Vec<SyntaxKind> {
62 match node.kind() {
63 RECORD_LIT => vec![RECORD_FIELD],
64 RECORD_PAT => vec![RECORD_FIELD_PAT, BIND_PAT],
65 _ => vec![],
66 }
67}
68
69fn get_field_name(node: &SyntaxNode) -> String {
70 let res = match_ast! {
71 match node {
72 ast::RecordField(field) => { field.field_name().map(|it| it.to_string()) },
73 ast::RecordFieldPat(field) => { field.field_name().map(|it| it.to_string()) },
74 _ => None,
75 }
76 };
77 res.unwrap_or_default()
78}
79
80fn get_fields(record: &SyntaxNode) -> Vec<SyntaxNode> {
81 let kinds = get_fields_kind(record);
82 record.children().flat_map(|n| n.children()).filter(|n| kinds.contains(&n.kind())).collect()
83}
84
85fn sorted_by_rank(
86 fields: &[SyntaxNode],
87 get_rank: impl Fn(&SyntaxNode) -> usize,
88) -> Vec<SyntaxNode> {
89 fields.iter().cloned().sorted_by_key(get_rank).collect()
90}
91
92fn struct_definition(path: &ast::Path, sema: &Semantics<RootDatabase>) -> Option<Struct> {
93 match sema.resolve_path(path) {
94 Some(PathResolution::Def(ModuleDef::Adt(Adt::Struct(s)))) => Some(s),
95 _ => None,
96 }
97}
98
99fn compute_fields_ranks(path: &Path, ctx: &AssistCtx) -> Option<HashMap<String, usize>> {
100 Some(
101 struct_definition(path, ctx.sema)?
102 .fields(ctx.db)
103 .iter()
104 .enumerate()
105 .map(|(idx, field)| (field.name(ctx.db).to_string(), idx))
106 .collect(),
107 )
108}
109
110#[cfg(test)]
111mod tests {
112 use crate::helpers::{check_assist, check_assist_not_applicable};
113
114 use super::*;
115
116 #[test]
117 fn not_applicable_if_sorted() {
118 check_assist_not_applicable(
119 reorder_fields,
120 r#"
121 struct Foo {
122 foo: i32,
123 bar: i32,
124 }
125
126 const test: Foo = <|>Foo { foo: 0, bar: 0 };
127 "#,
128 )
129 }
130
131 #[test]
132 fn trivial_empty_fields() {
133 check_assist_not_applicable(
134 reorder_fields,
135 r#"
136 struct Foo {};
137 const test: Foo = <|>Foo {}
138 "#,
139 )
140 }
141
142 #[test]
143 fn reorder_struct_fields() {
144 check_assist(
145 reorder_fields,
146 r#"
147 struct Foo {foo: i32, bar: i32};
148 const test: Foo = <|>Foo {bar: 0, foo: 1}
149 "#,
150 r#"
151 struct Foo {foo: i32, bar: i32};
152 const test: Foo = <|>Foo {foo: 1, bar: 0}
153 "#,
154 )
155 }
156
157 #[test]
158 fn reorder_struct_pattern() {
159 check_assist(
160 reorder_fields,
161 r#"
162 struct Foo { foo: i64, bar: i64, baz: i64 }
163
164 fn f(f: Foo) -> {
165 match f {
166 <|>Foo { baz: 0, ref mut bar, .. } => (),
167 _ => ()
168 }
169 }
170 "#,
171 r#"
172 struct Foo { foo: i64, bar: i64, baz: i64 }
173
174 fn f(f: Foo) -> {
175 match f {
176 <|>Foo { ref mut bar, baz: 0, .. } => (),
177 _ => ()
178 }
179 }
180 "#,
181 )
182 }
183
184 #[test]
185 fn reorder_with_extra_field() {
186 check_assist(
187 reorder_fields,
188 r#"
189 struct Foo {
190 foo: String,
191 bar: String,
192 }
193
194 impl Foo {
195 fn new() -> Foo {
196 let foo = String::new();
197 <|>Foo {
198 bar: foo.clone(),
199 extra: "Extra field",
200 foo,
201 }
202 }
203 }
204 "#,
205 r#"
206 struct Foo {
207 foo: String,
208 bar: String,
209 }
210
211 impl Foo {
212 fn new() -> Foo {
213 let foo = String::new();
214 <|>Foo {
215 foo,
216 bar: foo.clone(),
217 extra: "Extra field",
218 }
219 }
220 }
221 "#,
222 )
223 }
224}
diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs
index 5ba5254fd..a00136da1 100644
--- a/crates/ra_assists/src/lib.rs
+++ b/crates/ra_assists/src/lib.rs
@@ -129,6 +129,7 @@ mod handlers {
129 mod replace_unwrap_with_match; 129 mod replace_unwrap_with_match;
130 mod split_import; 130 mod split_import;
131 mod add_from_impl_for_enum; 131 mod add_from_impl_for_enum;
132 mod reorder_fields;
132 133
133 pub(crate) fn all() -> &'static [AssistHandler] { 134 pub(crate) fn all() -> &'static [AssistHandler] {
134 &[ 135 &[
@@ -170,6 +171,7 @@ mod handlers {
170 // These are manually sorted for better priorities 171 // These are manually sorted for better priorities
171 add_missing_impl_members::add_missing_impl_members, 172 add_missing_impl_members::add_missing_impl_members,
172 add_missing_impl_members::add_missing_default_members, 173 add_missing_impl_members::add_missing_default_members,
174 reorder_fields::reorder_fields,
173 ] 175 ]
174 } 176 }
175} 177}
diff --git a/crates/ra_hir/src/source_analyzer.rs b/crates/ra_hir/src/source_analyzer.rs
index 226fb4534..58ae6ce41 100644
--- a/crates/ra_hir/src/source_analyzer.rs
+++ b/crates/ra_hir/src/source_analyzer.rs
@@ -139,7 +139,7 @@ impl SourceAnalyzer {
139 &self, 139 &self,
140 db: &dyn HirDatabase, 140 db: &dyn HirDatabase,
141 field: &ast::FieldExpr, 141 field: &ast::FieldExpr,
142 ) -> Option<crate::StructField> { 142 ) -> Option<StructField> {
143 let expr_id = self.expr_id(db, &field.clone().into())?; 143 let expr_id = self.expr_id(db, &field.clone().into())?;
144 self.infer.as_ref()?.field_resolution(expr_id).map(|it| it.into()) 144 self.infer.as_ref()?.field_resolution(expr_id).map(|it| it.into())
145 } 145 }
@@ -148,21 +148,19 @@ impl SourceAnalyzer {
148 &self, 148 &self,
149 db: &dyn HirDatabase, 149 db: &dyn HirDatabase,
150 field: &ast::RecordField, 150 field: &ast::RecordField,
151 ) -> Option<(crate::StructField, Option<Local>)> { 151 ) -> Option<(StructField, Option<Local>)> {
152 let (expr_id, local) = match field.expr() { 152 let expr = field.expr()?;
153 Some(it) => (self.expr_id(db, &it)?, None), 153 let expr_id = self.expr_id(db, &expr)?;
154 None => { 154 let local = if field.name_ref().is_some() {
155 let src = InFile { file_id: self.file_id, value: field }; 155 None
156 let expr_id = self.body_source_map.as_ref()?.field_init_shorthand_expr(src)?; 156 } else {
157 let local_name = field.name_ref()?.as_name(); 157 let local_name = field.field_name()?.as_name();
158 let path = ModPath::from_segments(PathKind::Plain, once(local_name)); 158 let path = ModPath::from_segments(PathKind::Plain, once(local_name));
159 let local = match self.resolver.resolve_path_in_value_ns_fully(db.upcast(), &path) { 159 match self.resolver.resolve_path_in_value_ns_fully(db.upcast(), &path) {
160 Some(ValueNs::LocalBinding(pat_id)) => { 160 Some(ValueNs::LocalBinding(pat_id)) => {
161 Some(Local { pat_id, parent: self.resolver.body_owner()? }) 161 Some(Local { pat_id, parent: self.resolver.body_owner()? })
162 } 162 }
163 _ => None, 163 _ => None,
164 };
165 (expr_id, local)
166 } 164 }
167 }; 165 };
168 let struct_field = self.infer.as_ref()?.record_field_resolution(expr_id)?; 166 let struct_field = self.infer.as_ref()?.record_field_resolution(expr_id)?;
@@ -319,8 +317,7 @@ fn scope_for_offset(
319 if source.file_id != offset.file_id { 317 if source.file_id != offset.file_id {
320 return None; 318 return None;
321 } 319 }
322 let syntax_node_ptr = 320 let syntax_node_ptr = source.value.syntax_node_ptr();
323 source.value.either(|it| it.syntax_node_ptr(), |it| it.syntax_node_ptr());
324 Some((syntax_node_ptr, scope)) 321 Some((syntax_node_ptr, scope))
325 }) 322 })
326 // find containing scope 323 // find containing scope
@@ -399,8 +396,7 @@ fn adjust(
399 if source.file_id != file_id { 396 if source.file_id != file_id {
400 return None; 397 return None;
401 } 398 }
402 let syntax_node_ptr = 399 let syntax_node_ptr = source.value.syntax_node_ptr();
403 source.value.either(|it| it.syntax_node_ptr(), |it| it.syntax_node_ptr());
404 Some((syntax_node_ptr, scope)) 400 Some((syntax_node_ptr, scope))
405 }) 401 })
406 .map(|(ptr, scope)| (ptr.range(), scope)) 402 .map(|(ptr, scope)| (ptr.range(), scope))
diff --git a/crates/ra_hir_def/src/adt.rs b/crates/ra_hir_def/src/adt.rs
index be4b0accb..7c0d93691 100644
--- a/crates/ra_hir_def/src/adt.rs
+++ b/crates/ra_hir_def/src/adt.rs
@@ -4,7 +4,6 @@ use std::sync::Arc;
4 4
5use either::Either; 5use either::Either;
6use hir_expand::{ 6use hir_expand::{
7 hygiene::Hygiene,
8 name::{AsName, Name}, 7 name::{AsName, Name},
9 InFile, 8 InFile,
10}; 9};
@@ -13,7 +12,7 @@ use ra_prof::profile;
13use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner, VisibilityOwner}; 12use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner, VisibilityOwner};
14 13
15use crate::{ 14use crate::{
16 attr::Attrs, db::DefDatabase, src::HasChildSource, src::HasSource, trace::Trace, 15 body::CfgExpander, db::DefDatabase, src::HasChildSource, src::HasSource, trace::Trace,
17 type_ref::TypeRef, visibility::RawVisibility, EnumId, HasModule, LocalEnumVariantId, 16 type_ref::TypeRef, visibility::RawVisibility, EnumId, HasModule, LocalEnumVariantId,
18 LocalStructFieldId, Lookup, ModuleId, StructId, UnionId, VariantId, 17 LocalStructFieldId, Lookup, ModuleId, StructId, UnionId, VariantId,
19}; 18};
@@ -125,8 +124,9 @@ fn lower_enum(
125 124
126impl VariantData { 125impl VariantData {
127 fn new(db: &dyn DefDatabase, flavor: InFile<ast::StructKind>, module_id: ModuleId) -> Self { 126 fn new(db: &dyn DefDatabase, flavor: InFile<ast::StructKind>, module_id: ModuleId) -> Self {
127 let mut expander = CfgExpander::new(db, flavor.file_id, module_id.krate);
128 let mut trace = Trace::new_for_arena(); 128 let mut trace = Trace::new_for_arena();
129 match lower_struct(db, &mut trace, &flavor, module_id) { 129 match lower_struct(db, &mut expander, &mut trace, &flavor) {
130 StructKind::Tuple => VariantData::Tuple(trace.into_arena()), 130 StructKind::Tuple => VariantData::Tuple(trace.into_arena()),
131 StructKind::Record => VariantData::Record(trace.into_arena()), 131 StructKind::Record => VariantData::Record(trace.into_arena()),
132 StructKind::Unit => VariantData::Unit, 132 StructKind::Unit => VariantData::Unit,
@@ -178,8 +178,9 @@ impl HasChildSource for VariantId {
178 it.lookup(db).container.module(db), 178 it.lookup(db).container.module(db),
179 ), 179 ),
180 }; 180 };
181 let mut expander = CfgExpander::new(db, src.file_id, module_id.krate);
181 let mut trace = Trace::new_for_map(); 182 let mut trace = Trace::new_for_map();
182 lower_struct(db, &mut trace, &src, module_id); 183 lower_struct(db, &mut expander, &mut trace, &src);
183 src.with_value(trace.into_map()) 184 src.with_value(trace.into_map())
184 } 185 }
185} 186}
@@ -193,16 +194,15 @@ pub enum StructKind {
193 194
194fn lower_struct( 195fn lower_struct(
195 db: &dyn DefDatabase, 196 db: &dyn DefDatabase,
197 expander: &mut CfgExpander,
196 trace: &mut Trace<StructFieldData, Either<ast::TupleFieldDef, ast::RecordFieldDef>>, 198 trace: &mut Trace<StructFieldData, Either<ast::TupleFieldDef, ast::RecordFieldDef>>,
197 ast: &InFile<ast::StructKind>, 199 ast: &InFile<ast::StructKind>,
198 module_id: ModuleId,
199) -> StructKind { 200) -> StructKind {
200 let crate_graph = db.crate_graph();
201 match &ast.value { 201 match &ast.value {
202 ast::StructKind::Tuple(fl) => { 202 ast::StructKind::Tuple(fl) => {
203 for (i, fd) in fl.fields().enumerate() { 203 for (i, fd) in fl.fields().enumerate() {
204 let attrs = Attrs::new(&fd, &Hygiene::new(db.upcast(), ast.file_id)); 204 let attrs = expander.parse_attrs(&fd);
205 if !attrs.is_cfg_enabled(&crate_graph[module_id.krate].cfg_options) { 205 if !expander.is_cfg_enabled(&attrs) {
206 continue; 206 continue;
207 } 207 }
208 208
@@ -219,8 +219,8 @@ fn lower_struct(
219 } 219 }
220 ast::StructKind::Record(fl) => { 220 ast::StructKind::Record(fl) => {
221 for fd in fl.fields() { 221 for fd in fl.fields() {
222 let attrs = Attrs::new(&fd, &Hygiene::new(db.upcast(), ast.file_id)); 222 let attrs = expander.parse_attrs(&fd);
223 if !attrs.is_cfg_enabled(&crate_graph[module_id.krate].cfg_options) { 223 if !expander.is_cfg_enabled(&attrs) {
224 continue; 224 continue;
225 } 225 }
226 226
diff --git a/crates/ra_hir_def/src/attr.rs b/crates/ra_hir_def/src/attr.rs
index 7b0c506b1..2f2e3e5ba 100644
--- a/crates/ra_hir_def/src/attr.rs
+++ b/crates/ra_hir_def/src/attr.rs
@@ -93,6 +93,7 @@ impl Attrs {
93 } 93 }
94 94
95 pub(crate) fn is_cfg_enabled(&self, cfg_options: &CfgOptions) -> bool { 95 pub(crate) fn is_cfg_enabled(&self, cfg_options: &CfgOptions) -> bool {
96 // FIXME: handle cfg_attr :-)
96 self.by_key("cfg").tt_values().all(|tt| cfg_options.is_cfg_enabled(tt) != Some(false)) 97 self.by_key("cfg").tt_values().all(|tt| cfg_options.is_cfg_enabled(tt) != Some(false))
97 } 98 }
98} 99}
diff --git a/crates/ra_hir_def/src/body.rs b/crates/ra_hir_def/src/body.rs
index e09996c6f..eafaf48c1 100644
--- a/crates/ra_hir_def/src/body.rs
+++ b/crates/ra_hir_def/src/body.rs
@@ -9,11 +9,14 @@ use drop_bomb::DropBomb;
9use either::Either; 9use either::Either;
10use hir_expand::{ast_id_map::AstIdMap, hygiene::Hygiene, AstId, HirFileId, InFile, MacroDefId}; 10use hir_expand::{ast_id_map::AstIdMap, hygiene::Hygiene, AstId, HirFileId, InFile, MacroDefId};
11use ra_arena::{map::ArenaMap, Arena}; 11use ra_arena::{map::ArenaMap, Arena};
12use ra_cfg::CfgOptions;
13use ra_db::CrateId;
12use ra_prof::profile; 14use ra_prof::profile;
13use ra_syntax::{ast, AstNode, AstPtr}; 15use ra_syntax::{ast, AstNode, AstPtr};
14use rustc_hash::FxHashMap; 16use rustc_hash::FxHashMap;
15 17
16use crate::{ 18use crate::{
19 attr::Attrs,
17 db::DefDatabase, 20 db::DefDatabase,
18 expr::{Expr, ExprId, Pat, PatId}, 21 expr::{Expr, ExprId, Pat, PatId},
19 item_scope::BuiltinShadowMode, 22 item_scope::BuiltinShadowMode,
@@ -24,25 +27,59 @@ use crate::{
24 AsMacroCall, DefWithBodyId, HasModule, Lookup, ModuleId, 27 AsMacroCall, DefWithBodyId, HasModule, Lookup, ModuleId,
25}; 28};
26 29
30/// A subser of Exander that only deals with cfg attributes. We only need it to
31/// avoid cyclic queries in crate def map during enum processing.
32pub(crate) struct CfgExpander {
33 cfg_options: CfgOptions,
34 hygiene: Hygiene,
35}
36
27pub(crate) struct Expander { 37pub(crate) struct Expander {
38 cfg_expander: CfgExpander,
28 crate_def_map: Arc<CrateDefMap>, 39 crate_def_map: Arc<CrateDefMap>,
29 current_file_id: HirFileId, 40 current_file_id: HirFileId,
30 hygiene: Hygiene,
31 ast_id_map: Arc<AstIdMap>, 41 ast_id_map: Arc<AstIdMap>,
32 module: ModuleId, 42 module: ModuleId,
33 recursive_limit: usize, 43 recursive_limit: usize,
34} 44}
35 45
46impl CfgExpander {
47 pub(crate) fn new(
48 db: &dyn DefDatabase,
49 current_file_id: HirFileId,
50 krate: CrateId,
51 ) -> CfgExpander {
52 let hygiene = Hygiene::new(db.upcast(), current_file_id);
53 let cfg_options = db.crate_graph()[krate].cfg_options.clone();
54 CfgExpander { cfg_options, hygiene }
55 }
56
57 pub(crate) fn parse_attrs(&self, owner: &dyn ast::AttrsOwner) -> Attrs {
58 Attrs::new(owner, &self.hygiene)
59 }
60
61 pub(crate) fn is_cfg_enabled(&self, attrs: &Attrs) -> bool {
62 attrs.is_cfg_enabled(&self.cfg_options)
63 }
64}
65
36impl Expander { 66impl Expander {
37 pub(crate) fn new( 67 pub(crate) fn new(
38 db: &dyn DefDatabase, 68 db: &dyn DefDatabase,
39 current_file_id: HirFileId, 69 current_file_id: HirFileId,
40 module: ModuleId, 70 module: ModuleId,
41 ) -> Expander { 71 ) -> Expander {
72 let cfg_expander = CfgExpander::new(db, current_file_id, module.krate);
42 let crate_def_map = db.crate_def_map(module.krate); 73 let crate_def_map = db.crate_def_map(module.krate);
43 let hygiene = Hygiene::new(db.upcast(), current_file_id);
44 let ast_id_map = db.ast_id_map(current_file_id); 74 let ast_id_map = db.ast_id_map(current_file_id);
45 Expander { crate_def_map, current_file_id, hygiene, ast_id_map, module, recursive_limit: 0 } 75 Expander {
76 cfg_expander,
77 crate_def_map,
78 current_file_id,
79 ast_id_map,
80 module,
81 recursive_limit: 0,
82 }
46 } 83 }
47 84
48 pub(crate) fn enter_expand<T: ast::AstNode>( 85 pub(crate) fn enter_expand<T: ast::AstNode>(
@@ -75,7 +112,7 @@ impl Expander {
75 ast_id_map: mem::take(&mut self.ast_id_map), 112 ast_id_map: mem::take(&mut self.ast_id_map),
76 bomb: DropBomb::new("expansion mark dropped"), 113 bomb: DropBomb::new("expansion mark dropped"),
77 }; 114 };
78 self.hygiene = Hygiene::new(db.upcast(), file_id); 115 self.cfg_expander.hygiene = Hygiene::new(db.upcast(), file_id);
79 self.current_file_id = file_id; 116 self.current_file_id = file_id;
80 self.ast_id_map = db.ast_id_map(file_id); 117 self.ast_id_map = db.ast_id_map(file_id);
81 self.recursive_limit += 1; 118 self.recursive_limit += 1;
@@ -91,7 +128,7 @@ impl Expander {
91 } 128 }
92 129
93 pub(crate) fn exit(&mut self, db: &dyn DefDatabase, mut mark: Mark) { 130 pub(crate) fn exit(&mut self, db: &dyn DefDatabase, mut mark: Mark) {
94 self.hygiene = Hygiene::new(db.upcast(), mark.file_id); 131 self.cfg_expander.hygiene = Hygiene::new(db.upcast(), mark.file_id);
95 self.current_file_id = mark.file_id; 132 self.current_file_id = mark.file_id;
96 self.ast_id_map = mem::take(&mut mark.ast_id_map); 133 self.ast_id_map = mem::take(&mut mark.ast_id_map);
97 self.recursive_limit -= 1; 134 self.recursive_limit -= 1;
@@ -102,8 +139,16 @@ impl Expander {
102 InFile { file_id: self.current_file_id, value } 139 InFile { file_id: self.current_file_id, value }
103 } 140 }
104 141
142 pub(crate) fn parse_attrs(&self, owner: &dyn ast::AttrsOwner) -> Attrs {
143 self.cfg_expander.parse_attrs(owner)
144 }
145
146 pub(crate) fn is_cfg_enabled(&self, attrs: &Attrs) -> bool {
147 self.cfg_expander.is_cfg_enabled(attrs)
148 }
149
105 fn parse_path(&mut self, path: ast::Path) -> Option<Path> { 150 fn parse_path(&mut self, path: ast::Path) -> Option<Path> {
106 Path::from_src(path, &self.hygiene) 151 Path::from_src(path, &self.cfg_expander.hygiene)
107 } 152 }
108 153
109 fn resolve_path_as_macro(&self, db: &dyn DefDatabase, path: &ModPath) -> Option<MacroDefId> { 154 fn resolve_path_as_macro(&self, db: &dyn DefDatabase, path: &ModPath) -> Option<MacroDefId> {
@@ -142,7 +187,7 @@ pub struct Body {
142 pub item_scope: ItemScope, 187 pub item_scope: ItemScope,
143} 188}
144 189
145pub type ExprPtr = Either<AstPtr<ast::Expr>, AstPtr<ast::RecordField>>; 190pub type ExprPtr = AstPtr<ast::Expr>;
146pub type ExprSource = InFile<ExprPtr>; 191pub type ExprSource = InFile<ExprPtr>;
147 192
148pub type PatPtr = Either<AstPtr<ast::Pat>, AstPtr<ast::SelfParam>>; 193pub type PatPtr = Either<AstPtr<ast::Pat>, AstPtr<ast::SelfParam>>;
@@ -240,7 +285,7 @@ impl BodySourceMap {
240 } 285 }
241 286
242 pub fn node_expr(&self, node: InFile<&ast::Expr>) -> Option<ExprId> { 287 pub fn node_expr(&self, node: InFile<&ast::Expr>) -> Option<ExprId> {
243 let src = node.map(|it| Either::Left(AstPtr::new(it))); 288 let src = node.map(|it| AstPtr::new(it));
244 self.expr_map.get(&src).cloned() 289 self.expr_map.get(&src).cloned()
245 } 290 }
246 291
@@ -249,11 +294,6 @@ impl BodySourceMap {
249 self.expansions.get(&src).cloned() 294 self.expansions.get(&src).cloned()
250 } 295 }
251 296
252 pub fn field_init_shorthand_expr(&self, node: InFile<&ast::RecordField>) -> Option<ExprId> {
253 let src = node.map(|it| Either::Right(AstPtr::new(it)));
254 self.expr_map.get(&src).cloned()
255 }
256
257 pub fn pat_syntax(&self, pat: PatId) -> Result<PatSource, SyntheticSyntax> { 297 pub fn pat_syntax(&self, pat: PatId) -> Result<PatSource, SyntheticSyntax> {
258 self.pat_map_back[pat].clone() 298 self.pat_map_back[pat].clone()
259 } 299 }
diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs
index 9d6ee095e..6caa87db4 100644
--- a/crates/ra_hir_def/src/body/lower.rs
+++ b/crates/ra_hir_def/src/body/lower.rs
@@ -2,9 +2,7 @@
2//! representation. 2//! representation.
3 3
4use either::Either; 4use either::Either;
5
6use hir_expand::{ 5use hir_expand::{
7 hygiene::Hygiene,
8 name::{name, AsName, Name}, 6 name::{name, AsName, Name},
9 MacroDefId, MacroDefKind, 7 MacroDefId, MacroDefKind,
10}; 8};
@@ -18,10 +16,8 @@ use ra_syntax::{
18}; 16};
19use test_utils::tested_by; 17use test_utils::tested_by;
20 18
21use super::{ExprSource, PatSource};
22use crate::{ 19use crate::{
23 adt::StructKind, 20 adt::StructKind,
24 attr::Attrs,
25 body::{Body, BodySourceMap, Expander, PatPtr, SyntheticSyntax}, 21 body::{Body, BodySourceMap, Expander, PatPtr, SyntheticSyntax},
26 builtin_type::{BuiltinFloat, BuiltinInt}, 22 builtin_type::{BuiltinFloat, BuiltinInt},
27 db::DefDatabase, 23 db::DefDatabase,
@@ -31,12 +27,13 @@ use crate::{
31 }, 27 },
32 item_scope::BuiltinShadowMode, 28 item_scope::BuiltinShadowMode,
33 path::GenericArgs, 29 path::GenericArgs,
34 path::Path,
35 type_ref::{Mutability, TypeRef}, 30 type_ref::{Mutability, TypeRef},
36 AdtId, ConstLoc, ContainerId, DefWithBodyId, EnumLoc, FunctionLoc, HasModule, Intern, 31 AdtId, ConstLoc, ContainerId, DefWithBodyId, EnumLoc, FunctionLoc, Intern, ModuleDefId,
37 ModuleDefId, StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc, 32 StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc,
38}; 33};
39 34
35use super::{ExprSource, PatSource};
36
40pub(super) fn lower( 37pub(super) fn lower(
41 db: &dyn DefDatabase, 38 db: &dyn DefDatabase,
42 def: DefWithBodyId, 39 def: DefWithBodyId,
@@ -104,7 +101,6 @@ impl ExprCollector<'_> {
104 } 101 }
105 102
106 fn alloc_expr(&mut self, expr: Expr, ptr: AstPtr<ast::Expr>) -> ExprId { 103 fn alloc_expr(&mut self, expr: Expr, ptr: AstPtr<ast::Expr>) -> ExprId {
107 let ptr = Either::Left(ptr);
108 let src = self.expander.to_source(ptr); 104 let src = self.expander.to_source(ptr);
109 let id = self.make_expr(expr, Ok(src.clone())); 105 let id = self.make_expr(expr, Ok(src.clone()));
110 self.source_map.expr_map.insert(src, id); 106 self.source_map.expr_map.insert(src, id);
@@ -115,13 +111,6 @@ impl ExprCollector<'_> {
115 fn alloc_expr_desugared(&mut self, expr: Expr) -> ExprId { 111 fn alloc_expr_desugared(&mut self, expr: Expr) -> ExprId {
116 self.make_expr(expr, Err(SyntheticSyntax)) 112 self.make_expr(expr, Err(SyntheticSyntax))
117 } 113 }
118 fn alloc_expr_field_shorthand(&mut self, expr: Expr, ptr: AstPtr<ast::RecordField>) -> ExprId {
119 let ptr = Either::Right(ptr);
120 let src = self.expander.to_source(ptr);
121 let id = self.make_expr(expr, Ok(src.clone()));
122 self.source_map.expr_map.insert(src, id);
123 id
124 }
125 fn empty_block(&mut self) -> ExprId { 114 fn empty_block(&mut self) -> ExprId {
126 self.alloc_expr_desugared(Expr::Block { statements: Vec::new(), tail: None }) 115 self.alloc_expr_desugared(Expr::Block { statements: Vec::new(), tail: None })
127 } 116 }
@@ -291,7 +280,7 @@ impl ExprCollector<'_> {
291 ast::Expr::ParenExpr(e) => { 280 ast::Expr::ParenExpr(e) => {
292 let inner = self.collect_expr_opt(e.expr()); 281 let inner = self.collect_expr_opt(e.expr());
293 // make the paren expr point to the inner expression as well 282 // make the paren expr point to the inner expression as well
294 let src = self.expander.to_source(Either::Left(syntax_ptr)); 283 let src = self.expander.to_source(syntax_ptr);
295 self.source_map.expr_map.insert(src, inner); 284 self.source_map.expr_map.insert(src, inner);
296 inner 285 inner
297 } 286 }
@@ -300,7 +289,6 @@ impl ExprCollector<'_> {
300 self.alloc_expr(Expr::Return { expr }, syntax_ptr) 289 self.alloc_expr(Expr::Return { expr }, syntax_ptr)
301 } 290 }
302 ast::Expr::RecordLit(e) => { 291 ast::Expr::RecordLit(e) => {
303 let crate_graph = self.db.crate_graph();
304 let path = e.path().and_then(|path| self.expander.parse_path(path)); 292 let path = e.path().and_then(|path| self.expander.parse_path(path));
305 let mut field_ptrs = Vec::new(); 293 let mut field_ptrs = Vec::new();
306 let record_lit = if let Some(nfl) = e.record_field_list() { 294 let record_lit = if let Some(nfl) = e.record_field_list() {
@@ -308,31 +296,17 @@ impl ExprCollector<'_> {
308 .fields() 296 .fields()
309 .inspect(|field| field_ptrs.push(AstPtr::new(field))) 297 .inspect(|field| field_ptrs.push(AstPtr::new(field)))
310 .filter_map(|field| { 298 .filter_map(|field| {
311 let module_id = ContainerId::DefWithBodyId(self.def).module(self.db); 299 let attrs = self.expander.parse_attrs(&field);
312 let attrs = Attrs::new( 300 if !self.expander.is_cfg_enabled(&attrs) {
313 &field,
314 &Hygiene::new(self.db.upcast(), self.expander.current_file_id),
315 );
316
317 if !attrs.is_cfg_enabled(&crate_graph[module_id.krate].cfg_options) {
318 return None; 301 return None;
319 } 302 }
303 let name = field.field_name()?.as_name();
320 304
321 Some(RecordLitField { 305 Some(RecordLitField {
322 name: field 306 name,
323 .name_ref() 307 expr: match field.expr() {
324 .map(|nr| nr.as_name()) 308 Some(e) => self.collect_expr(e),
325 .unwrap_or_else(Name::missing), 309 None => self.missing_expr(),
326 expr: if let Some(e) = field.expr() {
327 self.collect_expr(e)
328 } else if let Some(nr) = field.name_ref() {
329 // field shorthand
330 self.alloc_expr_field_shorthand(
331 Expr::Path(Path::from_name_ref(&nr)),
332 AstPtr::new(&field),
333 )
334 } else {
335 self.missing_expr()
336 }, 310 },
337 }) 311 })
338 }) 312 })
@@ -663,7 +637,7 @@ impl ExprCollector<'_> {
663 let iter = record_field_pat_list.record_field_pats().filter_map(|f| { 637 let iter = record_field_pat_list.record_field_pats().filter_map(|f| {
664 let ast_pat = f.pat()?; 638 let ast_pat = f.pat()?;
665 let pat = self.collect_pat(ast_pat); 639 let pat = self.collect_pat(ast_pat);
666 let name = f.name()?.as_name(); 640 let name = f.field_name()?.as_name();
667 Some(RecordFieldPat { name, pat }) 641 Some(RecordFieldPat { name, pat })
668 }); 642 });
669 fields.extend(iter); 643 fields.extend(iter);
@@ -691,7 +665,6 @@ impl ExprCollector<'_> {
691 Pat::Missing 665 Pat::Missing
692 } 666 }
693 } 667 }
694
695 // FIXME: implement 668 // FIXME: implement
696 ast::Pat::BoxPat(_) | ast::Pat::RangePat(_) | ast::Pat::MacroPat(_) => Pat::Missing, 669 ast::Pat::BoxPat(_) | ast::Pat::RangePat(_) | ast::Pat::MacroPat(_) => Pat::Missing,
697 }; 670 };
diff --git a/crates/ra_hir_def/src/data.rs b/crates/ra_hir_def/src/data.rs
index b8fbf0ed4..56a20c5bd 100644
--- a/crates/ra_hir_def/src/data.rs
+++ b/crates/ra_hir_def/src/data.rs
@@ -20,7 +20,7 @@ use crate::{
20 type_ref::{Mutability, TypeBound, TypeRef}, 20 type_ref::{Mutability, TypeBound, TypeRef},
21 visibility::RawVisibility, 21 visibility::RawVisibility,
22 AssocContainerId, AssocItemId, ConstId, ConstLoc, Expander, FunctionId, FunctionLoc, HasModule, 22 AssocContainerId, AssocItemId, ConstId, ConstLoc, Expander, FunctionId, FunctionLoc, HasModule,
23 ImplId, Intern, Lookup, ModuleId, StaticId, TraitId, TypeAliasId, TypeAliasLoc, 23 ImplId, Intern, Lookup, StaticId, TraitId, TypeAliasId, TypeAliasLoc,
24}; 24};
25 25
26#[derive(Debug, Clone, PartialEq, Eq)] 26#[derive(Debug, Clone, PartialEq, Eq)]
@@ -218,10 +218,17 @@ impl ImplData {
218 let mut items = Vec::new(); 218 let mut items = Vec::new();
219 219
220 if let Some(item_list) = src.value.item_list() { 220 if let Some(item_list) = src.value.item_list() {
221 items.extend(collect_impl_items(db, item_list.impl_items(), src.file_id, id)); 221 let mut expander = Expander::new(db, impl_loc.ast_id.file_id, module_id);
222 items.extend(collect_impl_items(
223 db,
224 &mut expander,
225 item_list.impl_items(),
226 src.file_id,
227 id,
228 ));
222 items.extend(collect_impl_items_in_macros( 229 items.extend(collect_impl_items_in_macros(
223 db, 230 db,
224 module_id, 231 &mut expander,
225 &src.with_value(item_list), 232 &src.with_value(item_list),
226 id, 233 id,
227 )); 234 ));
@@ -268,18 +275,17 @@ impl ConstData {
268 275
269fn collect_impl_items_in_macros( 276fn collect_impl_items_in_macros(
270 db: &dyn DefDatabase, 277 db: &dyn DefDatabase,
271 module_id: ModuleId, 278 expander: &mut Expander,
272 impl_def: &InFile<ast::ItemList>, 279 impl_def: &InFile<ast::ItemList>,
273 id: ImplId, 280 id: ImplId,
274) -> Vec<AssocItemId> { 281) -> Vec<AssocItemId> {
275 let mut expander = Expander::new(db, impl_def.file_id, module_id);
276 let mut res = Vec::new(); 282 let mut res = Vec::new();
277 283
278 // We set a limit to protect against infinite recursion 284 // We set a limit to protect against infinite recursion
279 let limit = 100; 285 let limit = 100;
280 286
281 for m in impl_def.value.syntax().children().filter_map(ast::MacroCall::cast) { 287 for m in impl_def.value.syntax().children().filter_map(ast::MacroCall::cast) {
282 res.extend(collect_impl_items_in_macro(db, &mut expander, m, id, limit)) 288 res.extend(collect_impl_items_in_macro(db, expander, m, id, limit))
283 } 289 }
284 290
285 res 291 res
@@ -300,6 +306,7 @@ fn collect_impl_items_in_macro(
300 let items: InFile<ast::MacroItems> = expander.to_source(items); 306 let items: InFile<ast::MacroItems> = expander.to_source(items);
301 let mut res = collect_impl_items( 307 let mut res = collect_impl_items(
302 db, 308 db,
309 expander,
303 items.value.items().filter_map(|it| ImplItem::cast(it.syntax().clone())), 310 items.value.items().filter_map(|it| ImplItem::cast(it.syntax().clone())),
304 items.file_id, 311 items.file_id,
305 id, 312 id,
@@ -319,32 +326,26 @@ fn collect_impl_items_in_macro(
319 326
320fn collect_impl_items( 327fn collect_impl_items(
321 db: &dyn DefDatabase, 328 db: &dyn DefDatabase,
329 expander: &mut Expander,
322 impl_items: impl Iterator<Item = ImplItem>, 330 impl_items: impl Iterator<Item = ImplItem>,
323 file_id: crate::HirFileId, 331 file_id: crate::HirFileId,
324 id: ImplId, 332 id: ImplId,
325) -> Vec<AssocItemId> { 333) -> Vec<AssocItemId> {
326 let items = db.ast_id_map(file_id); 334 let items = db.ast_id_map(file_id);
327 let crate_graph = db.crate_graph();
328 let module_id = id.lookup(db).container.module(db);
329 335
330 impl_items 336 impl_items
331 .filter_map(|item_node| match item_node { 337 .filter_map(|item_node| match item_node {
332 ast::ImplItem::FnDef(it) => { 338 ast::ImplItem::FnDef(it) => {
339 let attrs = expander.parse_attrs(&it);
340 if !expander.is_cfg_enabled(&attrs) {
341 return None;
342 }
333 let def = FunctionLoc { 343 let def = FunctionLoc {
334 container: AssocContainerId::ImplId(id), 344 container: AssocContainerId::ImplId(id),
335 ast_id: AstId::new(file_id, items.ast_id(&it)), 345 ast_id: AstId::new(file_id, items.ast_id(&it)),
336 } 346 }
337 .intern(db); 347 .intern(db);
338 348 Some(def.into())
339 if !db
340 .function_data(def)
341 .attrs
342 .is_cfg_enabled(&crate_graph[module_id.krate].cfg_options)
343 {
344 None
345 } else {
346 Some(def.into())
347 }
348 } 349 }
349 ast::ImplItem::ConstDef(it) => { 350 ast::ImplItem::ConstDef(it) => {
350 let def = ConstLoc { 351 let def = ConstLoc {
diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs
index 8fe3f8617..98c74fe25 100644
--- a/crates/ra_hir_def/src/nameres/collector.rs
+++ b/crates/ra_hir_def/src/nameres/collector.rs
@@ -462,6 +462,14 @@ impl DefCollector<'_> {
462 Some(ModuleDefId::AdtId(AdtId::EnumId(e))) => { 462 Some(ModuleDefId::AdtId(AdtId::EnumId(e))) => {
463 tested_by!(glob_enum); 463 tested_by!(glob_enum);
464 // glob import from enum => just import all the variants 464 // glob import from enum => just import all the variants
465
466 // XXX: urgh, so this works by accident! Here, we look at
467 // the enum data, and, in theory, this might require us to
468 // look back at the crate_def_map, creating a cycle. For
469 // example, `enum E { crate::some_macro!(); }`. Luckely, the
470 // only kind of macro that is allowed inside enum is a
471 // `cfg_macro`, and we don't need to run name resolution for
472 // it, but this is sheer luck!
465 let enum_data = self.db.enum_data(e); 473 let enum_data = self.db.enum_data(e);
466 let resolutions = enum_data 474 let resolutions = enum_data
467 .variants 475 .variants
@@ -977,11 +985,7 @@ impl ModCollector<'_, '_> {
977 } 985 }
978 986
979 fn is_cfg_enabled(&self, attrs: &Attrs) -> bool { 987 fn is_cfg_enabled(&self, attrs: &Attrs) -> bool {
980 // FIXME: handle cfg_attr :-) 988 attrs.is_cfg_enabled(self.def_collector.cfg_options)
981 attrs
982 .by_key("cfg")
983 .tt_values()
984 .all(|tt| self.def_collector.cfg_options.is_cfg_enabled(tt) != Some(false))
985 } 989 }
986} 990}
987 991
diff --git a/crates/ra_hir_def/src/nameres/tests/incremental.rs b/crates/ra_hir_def/src/nameres/tests/incremental.rs
index 496fc6b08..87165ac33 100644
--- a/crates/ra_hir_def/src/nameres/tests/incremental.rs
+++ b/crates/ra_hir_def/src/nameres/tests/incremental.rs
@@ -32,6 +32,9 @@ fn typing_inside_a_function_should_not_invalidate_def_map() {
32 32
33 use crate::foo::bar::Baz; 33 use crate::foo::bar::Baz;
34 34
35 enum E { A, B }
36 use E::*;
37
35 fn foo() -> i32 { 38 fn foo() -> i32 {
36 1 + 1 39 1 + 1
37 } 40 }
@@ -46,6 +49,9 @@ fn typing_inside_a_function_should_not_invalidate_def_map() {
46 49
47 use crate::foo::bar::Baz; 50 use crate::foo::bar::Baz;
48 51
52 enum E { A, B }
53 use E::*;
54
49 fn foo() -> i32 { 92 } 55 fn foo() -> i32 { 92 }
50 ", 56 ",
51 ); 57 );
diff --git a/crates/ra_hir_def/src/path.rs b/crates/ra_hir_def/src/path.rs
index 904080341..91c7b3e09 100644
--- a/crates/ra_hir_def/src/path.rs
+++ b/crates/ra_hir_def/src/path.rs
@@ -134,11 +134,6 @@ impl Path {
134 lower::lower_path(path, hygiene) 134 lower::lower_path(path, hygiene)
135 } 135 }
136 136
137 /// Converts an `ast::NameRef` into a single-identifier `Path`.
138 pub(crate) fn from_name_ref(name_ref: &ast::NameRef) -> Path {
139 Path { type_anchor: None, mod_path: name_ref.as_name().into(), generic_args: vec![None] }
140 }
141
142 /// Converts a known mod path to `Path`. 137 /// Converts a known mod path to `Path`.
143 pub(crate) fn from_known_path( 138 pub(crate) fn from_known_path(
144 path: ModPath, 139 path: ModPath,
diff --git a/crates/ra_hir_expand/src/name.rs b/crates/ra_hir_expand/src/name.rs
index 25cc1e9fc..fecce224e 100644
--- a/crates/ra_hir_expand/src/name.rs
+++ b/crates/ra_hir_expand/src/name.rs
@@ -83,6 +83,15 @@ impl AsName for ast::Name {
83 } 83 }
84} 84}
85 85
86impl AsName for ast::NameOrNameRef {
87 fn as_name(&self) -> Name {
88 match self {
89 ast::NameOrNameRef::Name(it) => it.as_name(),
90 ast::NameOrNameRef::NameRef(it) => it.as_name(),
91 }
92 }
93}
94
86impl AsName for tt::Ident { 95impl AsName for tt::Ident {
87 fn as_name(&self) -> Name { 96 fn as_name(&self) -> Name {
88 Name::resolve(&self.text) 97 Name::resolve(&self.text)
diff --git a/crates/ra_hir_ty/src/db.rs b/crates/ra_hir_ty/src/db.rs
index 1462b053f..33da16b48 100644
--- a/crates/ra_hir_ty/src/db.rs
+++ b/crates/ra_hir_ty/src/db.rs
@@ -11,7 +11,7 @@ use ra_db::{impl_intern_key, salsa, CrateId, Upcast};
11use ra_prof::profile; 11use ra_prof::profile;
12 12
13use crate::{ 13use crate::{
14 method_resolution::CrateImplDefs, 14 method_resolution::{CrateImplDefs, TyFingerprint},
15 traits::{chalk, AssocTyValue, Impl}, 15 traits::{chalk, AssocTyValue, Impl},
16 Binders, CallableDef, GenericPredicate, InferenceResult, PolyFnSig, Substs, TraitRef, Ty, 16 Binders, CallableDef, GenericPredicate, InferenceResult, PolyFnSig, Substs, TraitRef, Ty,
17 TyDefId, TypeCtor, ValueTyDefId, 17 TyDefId, TypeCtor, ValueTyDefId,
@@ -65,7 +65,12 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
65 fn impls_in_crate(&self, krate: CrateId) -> Arc<CrateImplDefs>; 65 fn impls_in_crate(&self, krate: CrateId) -> Arc<CrateImplDefs>;
66 66
67 #[salsa::invoke(crate::traits::impls_for_trait_query)] 67 #[salsa::invoke(crate::traits::impls_for_trait_query)]
68 fn impls_for_trait(&self, krate: CrateId, trait_: TraitId) -> Arc<[ImplId]>; 68 fn impls_for_trait(
69 &self,
70 krate: CrateId,
71 trait_: TraitId,
72 self_ty_fp: Option<TyFingerprint>,
73 ) -> Arc<[ImplId]>;
69 74
70 // Interned IDs for Chalk integration 75 // Interned IDs for Chalk integration
71 #[salsa::interned] 76 #[salsa::interned]
diff --git a/crates/ra_hir_ty/src/expr.rs b/crates/ra_hir_ty/src/expr.rs
index 03ef488b9..21abbcf1e 100644
--- a/crates/ra_hir_ty/src/expr.rs
+++ b/crates/ra_hir_ty/src/expr.rs
@@ -89,21 +89,19 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
89 let (_, source_map) = db.body_with_source_map(self.func.into()); 89 let (_, source_map) = db.body_with_source_map(self.func.into());
90 90
91 if let Ok(source_ptr) = source_map.expr_syntax(id) { 91 if let Ok(source_ptr) = source_map.expr_syntax(id) {
92 if let Some(expr) = source_ptr.value.as_ref().left() { 92 let root = source_ptr.file_syntax(db.upcast());
93 let root = source_ptr.file_syntax(db.upcast()); 93 if let ast::Expr::RecordLit(record_lit) = &source_ptr.value.to_node(&root) {
94 if let ast::Expr::RecordLit(record_lit) = expr.to_node(&root) { 94 if let Some(field_list) = record_lit.record_field_list() {
95 if let Some(field_list) = record_lit.record_field_list() { 95 let variant_data = variant_data(db.upcast(), variant_def);
96 let variant_data = variant_data(db.upcast(), variant_def); 96 let missed_fields = missed_fields
97 let missed_fields = missed_fields 97 .into_iter()
98 .into_iter() 98 .map(|idx| variant_data.fields()[idx].name.clone())
99 .map(|idx| variant_data.fields()[idx].name.clone()) 99 .collect();
100 .collect(); 100 self.sink.push(MissingFields {
101 self.sink.push(MissingFields { 101 file: source_ptr.file_id,
102 file: source_ptr.file_id, 102 field_list: AstPtr::new(&field_list),
103 field_list: AstPtr::new(&field_list), 103 missed_fields,
104 missed_fields, 104 })
105 })
106 }
107 } 105 }
108 } 106 }
109 } 107 }
@@ -206,18 +204,16 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
206 } 204 }
207 205
208 if let Ok(source_ptr) = source_map.expr_syntax(id) { 206 if let Ok(source_ptr) = source_map.expr_syntax(id) {
209 if let Some(expr) = source_ptr.value.as_ref().left() { 207 let root = source_ptr.file_syntax(db.upcast());
210 let root = source_ptr.file_syntax(db.upcast()); 208 if let ast::Expr::MatchExpr(match_expr) = &source_ptr.value.to_node(&root) {
211 if let ast::Expr::MatchExpr(match_expr) = expr.to_node(&root) { 209 if let (Some(match_expr), Some(arms)) =
212 if let (Some(match_expr), Some(arms)) = 210 (match_expr.expr(), match_expr.match_arm_list())
213 (match_expr.expr(), match_expr.match_arm_list()) 211 {
214 { 212 self.sink.push(MissingMatchArms {
215 self.sink.push(MissingMatchArms { 213 file: source_ptr.file_id,
216 file: source_ptr.file_id, 214 match_expr: AstPtr::new(&match_expr),
217 match_expr: AstPtr::new(&match_expr), 215 arms: AstPtr::new(&arms),
218 arms: AstPtr::new(&arms), 216 })
219 })
220 }
221 } 217 }
222 } 218 }
223 } 219 }
@@ -248,9 +244,8 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
248 let (_, source_map) = db.body_with_source_map(self.func.into()); 244 let (_, source_map) = db.body_with_source_map(self.func.into());
249 245
250 if let Ok(source_ptr) = source_map.expr_syntax(id) { 246 if let Ok(source_ptr) = source_map.expr_syntax(id) {
251 if let Some(expr) = source_ptr.value.left() { 247 self.sink
252 self.sink.push(MissingOkInTailExpr { file: source_ptr.file_id, expr }); 248 .push(MissingOkInTailExpr { file: source_ptr.file_id, expr: source_ptr.value });
253 }
254 } 249 }
255 } 250 }
256 } 251 }
diff --git a/crates/ra_hir_ty/src/method_resolution.rs b/crates/ra_hir_ty/src/method_resolution.rs
index 74a0bc7db..657284fd0 100644
--- a/crates/ra_hir_ty/src/method_resolution.rs
+++ b/crates/ra_hir_ty/src/method_resolution.rs
@@ -34,7 +34,7 @@ impl TyFingerprint {
34 /// Creates a TyFingerprint for looking up an impl. Only certain types can 34 /// Creates a TyFingerprint for looking up an impl. Only certain types can
35 /// have impls: if we have some `struct S`, we can have an `impl S`, but not 35 /// have impls: if we have some `struct S`, we can have an `impl S`, but not
36 /// `impl &S`. Hence, this will return `None` for reference types and such. 36 /// `impl &S`. Hence, this will return `None` for reference types and such.
37 fn for_impl(ty: &Ty) -> Option<TyFingerprint> { 37 pub(crate) fn for_impl(ty: &Ty) -> Option<TyFingerprint> {
38 match ty { 38 match ty {
39 Ty::Apply(a_ty) => Some(TyFingerprint::Apply(a_ty.ctor)), 39 Ty::Apply(a_ty) => Some(TyFingerprint::Apply(a_ty.ctor)),
40 _ => None, 40 _ => None,
@@ -45,7 +45,7 @@ impl TyFingerprint {
45#[derive(Debug, PartialEq, Eq)] 45#[derive(Debug, PartialEq, Eq)]
46pub struct CrateImplDefs { 46pub struct CrateImplDefs {
47 impls: FxHashMap<TyFingerprint, Vec<ImplId>>, 47 impls: FxHashMap<TyFingerprint, Vec<ImplId>>,
48 impls_by_trait: FxHashMap<TraitId, Vec<ImplId>>, 48 impls_by_trait: FxHashMap<TraitId, FxHashMap<Option<TyFingerprint>, Vec<ImplId>>>,
49} 49}
50 50
51impl CrateImplDefs { 51impl CrateImplDefs {
@@ -59,7 +59,14 @@ impl CrateImplDefs {
59 for impl_id in module_data.scope.impls() { 59 for impl_id in module_data.scope.impls() {
60 match db.impl_trait(impl_id) { 60 match db.impl_trait(impl_id) {
61 Some(tr) => { 61 Some(tr) => {
62 res.impls_by_trait.entry(tr.value.trait_).or_default().push(impl_id); 62 let self_ty = db.impl_self_ty(impl_id);
63 let self_ty_fp = TyFingerprint::for_impl(&self_ty.value);
64 res.impls_by_trait
65 .entry(tr.value.trait_)
66 .or_default()
67 .entry(self_ty_fp)
68 .or_default()
69 .push(impl_id);
63 } 70 }
64 None => { 71 None => {
65 let self_ty = db.impl_self_ty(impl_id); 72 let self_ty = db.impl_self_ty(impl_id);
@@ -79,11 +86,39 @@ impl CrateImplDefs {
79 } 86 }
80 87
81 pub fn lookup_impl_defs_for_trait(&self, tr: TraitId) -> impl Iterator<Item = ImplId> + '_ { 88 pub fn lookup_impl_defs_for_trait(&self, tr: TraitId) -> impl Iterator<Item = ImplId> + '_ {
82 self.impls_by_trait.get(&tr).into_iter().flatten().copied() 89 self.impls_by_trait
90 .get(&tr)
91 .into_iter()
92 .flat_map(|m| m.values().flat_map(|v| v.iter().copied()))
93 }
94
95 pub fn lookup_impl_defs_for_trait_and_ty(
96 &self,
97 tr: TraitId,
98 fp: TyFingerprint,
99 ) -> impl Iterator<Item = ImplId> + '_ {
100 self.impls_by_trait
101 .get(&tr)
102 .and_then(|m| m.get(&Some(fp)))
103 .into_iter()
104 .flatten()
105 .copied()
106 .chain(
107 self.impls_by_trait
108 .get(&tr)
109 .and_then(|m| m.get(&None))
110 .into_iter()
111 .flatten()
112 .copied(),
113 )
83 } 114 }
84 115
85 pub fn all_impls<'a>(&'a self) -> impl Iterator<Item = ImplId> + 'a { 116 pub fn all_impls<'a>(&'a self) -> impl Iterator<Item = ImplId> + 'a {
86 self.impls.values().chain(self.impls_by_trait.values()).flatten().copied() 117 self.impls
118 .values()
119 .chain(self.impls_by_trait.values().flat_map(|m| m.values()))
120 .flatten()
121 .copied()
87 } 122 }
88} 123}
89 124
diff --git a/crates/ra_hir_ty/src/tests.rs b/crates/ra_hir_ty/src/tests.rs
index 54e31602f..81fc0f63a 100644
--- a/crates/ra_hir_ty/src/tests.rs
+++ b/crates/ra_hir_ty/src/tests.rs
@@ -82,9 +82,7 @@ fn infer_with_mismatches(content: &str, include_mismatches: bool) -> String {
82 82
83 for (expr, ty) in inference_result.type_of_expr.iter() { 83 for (expr, ty) in inference_result.type_of_expr.iter() {
84 let syntax_ptr = match body_source_map.expr_syntax(expr) { 84 let syntax_ptr = match body_source_map.expr_syntax(expr) {
85 Ok(sp) => { 85 Ok(sp) => sp.map(|ast| ast.syntax_node_ptr()),
86 sp.map(|ast| ast.either(|it| it.syntax_node_ptr(), |it| it.syntax_node_ptr()))
87 }
88 Err(SyntheticSyntax) => continue, 86 Err(SyntheticSyntax) => continue,
89 }; 87 };
90 types.push((syntax_ptr.clone(), ty)); 88 types.push((syntax_ptr.clone(), ty));
diff --git a/crates/ra_hir_ty/src/tests/patterns.rs b/crates/ra_hir_ty/src/tests/patterns.rs
index 6e5d2247c..07cbc521a 100644
--- a/crates/ra_hir_ty/src/tests/patterns.rs
+++ b/crates/ra_hir_ty/src/tests/patterns.rs
@@ -1,7 +1,8 @@
1use super::{infer, infer_with_mismatches};
2use insta::assert_snapshot; 1use insta::assert_snapshot;
3use test_utils::covers; 2use test_utils::covers;
4 3
4use super::{infer, infer_with_mismatches};
5
5#[test] 6#[test]
6fn infer_pattern() { 7fn infer_pattern() {
7 assert_snapshot!( 8 assert_snapshot!(
diff --git a/crates/ra_hir_ty/src/traits.rs b/crates/ra_hir_ty/src/traits.rs
index 21e233379..43d8d1e80 100644
--- a/crates/ra_hir_ty/src/traits.rs
+++ b/crates/ra_hir_ty/src/traits.rs
@@ -7,7 +7,7 @@ use ra_db::{impl_intern_key, salsa, CrateId};
7use ra_prof::profile; 7use ra_prof::profile;
8use rustc_hash::FxHashSet; 8use rustc_hash::FxHashSet;
9 9
10use crate::{db::HirDatabase, DebruijnIndex}; 10use crate::{db::HirDatabase, method_resolution::TyFingerprint, DebruijnIndex};
11 11
12use super::{Canonical, GenericPredicate, HirDisplay, ProjectionTy, TraitRef, Ty, TypeWalk}; 12use super::{Canonical, GenericPredicate, HirDisplay, ProjectionTy, TraitRef, Ty, TypeWalk};
13 13
@@ -40,7 +40,12 @@ pub(crate) fn impls_for_trait_query(
40 db: &dyn HirDatabase, 40 db: &dyn HirDatabase,
41 krate: CrateId, 41 krate: CrateId,
42 trait_: TraitId, 42 trait_: TraitId,
43 self_ty_fp: Option<TyFingerprint>,
43) -> Arc<[ImplId]> { 44) -> Arc<[ImplId]> {
45 // FIXME: We could be a lot smarter here - because of the orphan rules and
46 // the fact that the trait and the self type need to be in the dependency
47 // tree of a crate somewhere for an impl to exist, we could skip looking in
48 // a lot of crates completely
44 let mut impls = FxHashSet::default(); 49 let mut impls = FxHashSet::default();
45 // We call the query recursively here. On the one hand, this means we can 50 // We call the query recursively here. On the one hand, this means we can
46 // reuse results from queries for different crates; on the other hand, this 51 // reuse results from queries for different crates; on the other hand, this
@@ -48,10 +53,13 @@ pub(crate) fn impls_for_trait_query(
48 // ones the user is editing), so this may actually be a waste of memory. I'm 53 // ones the user is editing), so this may actually be a waste of memory. I'm
49 // doing it like this mainly for simplicity for now. 54 // doing it like this mainly for simplicity for now.
50 for dep in &db.crate_graph()[krate].dependencies { 55 for dep in &db.crate_graph()[krate].dependencies {
51 impls.extend(db.impls_for_trait(dep.crate_id, trait_).iter()); 56 impls.extend(db.impls_for_trait(dep.crate_id, trait_, self_ty_fp).iter());
52 } 57 }
53 let crate_impl_defs = db.impls_in_crate(krate); 58 let crate_impl_defs = db.impls_in_crate(krate);
54 impls.extend(crate_impl_defs.lookup_impl_defs_for_trait(trait_)); 59 match self_ty_fp {
60 Some(fp) => impls.extend(crate_impl_defs.lookup_impl_defs_for_trait_and_ty(trait_, fp)),
61 None => impls.extend(crate_impl_defs.lookup_impl_defs_for_trait(trait_)),
62 }
55 impls.into_iter().collect() 63 impls.into_iter().collect()
56} 64}
57 65
diff --git a/crates/ra_hir_ty/src/traits/chalk.rs b/crates/ra_hir_ty/src/traits/chalk.rs
index c5f1b5232..e05fea843 100644
--- a/crates/ra_hir_ty/src/traits/chalk.rs
+++ b/crates/ra_hir_ty/src/traits/chalk.rs
@@ -16,8 +16,8 @@ use ra_db::{
16 16
17use super::{builtin, AssocTyValue, Canonical, ChalkContext, Impl, Obligation}; 17use super::{builtin, AssocTyValue, Canonical, ChalkContext, Impl, Obligation};
18use crate::{ 18use crate::{
19 db::HirDatabase, display::HirDisplay, utils::generics, ApplicationTy, GenericPredicate, 19 db::HirDatabase, display::HirDisplay, method_resolution::TyFingerprint, utils::generics,
20 ProjectionTy, Substs, TraitRef, Ty, TypeCtor, 20 ApplicationTy, GenericPredicate, ProjectionTy, Substs, TraitRef, Ty, TypeCtor,
21}; 21};
22 22
23pub(super) mod tls; 23pub(super) mod tls;
@@ -647,19 +647,22 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> {
647 debug!("impls_for_trait {:?}", trait_id); 647 debug!("impls_for_trait {:?}", trait_id);
648 let trait_: hir_def::TraitId = from_chalk(self.db, trait_id); 648 let trait_: hir_def::TraitId = from_chalk(self.db, trait_id);
649 649
650 let ty: Ty = from_chalk(self.db, parameters[0].assert_ty_ref(&Interner).clone());
651
652 let self_ty_fp = TyFingerprint::for_impl(&ty);
653
650 // Note: Since we're using impls_for_trait, only impls where the trait 654 // Note: Since we're using impls_for_trait, only impls where the trait
651 // can be resolved should ever reach Chalk. `impl_datum` relies on that 655 // can be resolved should ever reach Chalk. `impl_datum` relies on that
652 // and will panic if the trait can't be resolved. 656 // and will panic if the trait can't be resolved.
653 let mut result: Vec<_> = self 657 let mut result: Vec<_> = self
654 .db 658 .db
655 .impls_for_trait(self.krate, trait_) 659 .impls_for_trait(self.krate, trait_, self_ty_fp)
656 .iter() 660 .iter()
657 .copied() 661 .copied()
658 .map(Impl::ImplDef) 662 .map(Impl::ImplDef)
659 .map(|impl_| impl_.to_chalk(self.db)) 663 .map(|impl_| impl_.to_chalk(self.db))
660 .collect(); 664 .collect();
661 665
662 let ty: Ty = from_chalk(self.db, parameters[0].assert_ty_ref(&Interner).clone());
663 let arg: Option<Ty> = 666 let arg: Option<Ty> =
664 parameters.get(1).map(|p| from_chalk(self.db, p.assert_ty_ref(&Interner).clone())); 667 parameters.get(1).map(|p| from_chalk(self.db, p.assert_ty_ref(&Interner).clone()));
665 668
diff --git a/crates/ra_ide/src/call_info.rs b/crates/ra_ide/src/call_info.rs
index ca57eceff..f95b6baf3 100644
--- a/crates/ra_ide/src/call_info.rs
+++ b/crates/ra_ide/src/call_info.rs
@@ -208,9 +208,20 @@ mod tests {
208 } 208 }
209 } 209 }
210 210
211 fn call_info(text: &str) -> CallInfo { 211 fn call_info_helper(text: &str) -> Option<CallInfo> {
212 let (analysis, position) = single_file_with_position(text); 212 let (analysis, position) = single_file_with_position(text);
213 analysis.call_info(position).unwrap().unwrap() 213 analysis.call_info(position).unwrap()
214 }
215
216 fn call_info(text: &str) -> CallInfo {
217 let info = call_info_helper(text);
218 assert!(info.is_some());
219 info.unwrap()
220 }
221
222 fn no_call_info(text: &str) {
223 let info = call_info_helper(text);
224 assert!(info.is_none());
214 } 225 }
215 226
216 #[test] 227 #[test]
@@ -558,9 +569,8 @@ fn main() {
558 } 569 }
559 570
560 #[test] 571 #[test]
561 #[should_panic]
562 fn cant_call_named_structs() { 572 fn cant_call_named_structs() {
563 let _ = call_info( 573 no_call_info(
564 r#" 574 r#"
565struct TS { x: u32, y: i32 } 575struct TS { x: u32, y: i32 }
566fn main() { 576fn main() {
@@ -594,9 +604,8 @@ fn main() {
594 } 604 }
595 605
596 #[test] 606 #[test]
597 #[should_panic]
598 fn cant_call_enum_records() { 607 fn cant_call_enum_records() {
599 let _ = call_info( 608 no_call_info(
600 r#" 609 r#"
601enum E { 610enum E {
602 /// A Variant 611 /// A Variant
diff --git a/crates/ra_ide/src/completion/complete_pattern.rs b/crates/ra_ide/src/completion/complete_pattern.rs
index 1b7d3122f..a8b4ce114 100644
--- a/crates/ra_ide/src/completion/complete_pattern.rs
+++ b/crates/ra_ide/src/completion/complete_pattern.rs
@@ -7,6 +7,10 @@ pub(super) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) {
7 if !ctx.is_pat_binding_or_const { 7 if !ctx.is_pat_binding_or_const {
8 return; 8 return;
9 } 9 }
10 if ctx.record_pat_syntax.is_some() {
11 return;
12 }
13
10 // FIXME: ideally, we should look at the type we are matching against and 14 // FIXME: ideally, we should look at the type we are matching against and
11 // suggest variants + auto-imports 15 // suggest variants + auto-imports
12 ctx.scope().process_all_names(&mut |name, res| { 16 ctx.scope().process_all_names(&mut |name, res| {
diff --git a/crates/ra_ide/src/completion/complete_record.rs b/crates/ra_ide/src/completion/complete_record.rs
index f46bcee5c..83a553155 100644
--- a/crates/ra_ide/src/completion/complete_record.rs
+++ b/crates/ra_ide/src/completion/complete_record.rs
@@ -2,7 +2,7 @@
2use crate::completion::{CompletionContext, Completions}; 2use crate::completion::{CompletionContext, Completions};
3 3
4pub(super) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> { 4pub(super) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
5 let missing_fields = match (ctx.record_lit_pat.as_ref(), ctx.record_lit_syntax.as_ref()) { 5 let missing_fields = match (ctx.record_pat_syntax.as_ref(), ctx.record_lit_syntax.as_ref()) {
6 (None, None) => return None, 6 (None, None) => return None,
7 (Some(_), Some(_)) => unreachable!("A record cannot be both a literal and a pattern"), 7 (Some(_), Some(_)) => unreachable!("A record cannot be both a literal and a pattern"),
8 (Some(record_pat), _) => ctx.sema.record_pattern_missing_fields(record_pat), 8 (Some(record_pat), _) => ctx.sema.record_pattern_missing_fields(record_pat),
diff --git a/crates/ra_ide/src/completion/complete_unqualified_path.rs b/crates/ra_ide/src/completion/complete_unqualified_path.rs
index efde9bf73..2d8e0776c 100644
--- a/crates/ra_ide/src/completion/complete_unqualified_path.rs
+++ b/crates/ra_ide/src/completion/complete_unqualified_path.rs
@@ -3,7 +3,14 @@
3use crate::completion::{CompletionContext, Completions}; 3use crate::completion::{CompletionContext, Completions};
4 4
5pub(super) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionContext) { 5pub(super) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionContext) {
6 if !(ctx.is_trivial_path && !ctx.is_pat_binding_or_const) { 6 if !ctx.is_trivial_path {
7 return;
8 }
9
10 if ctx.is_pat_binding_or_const
11 || ctx.record_lit_syntax.is_some()
12 || ctx.record_pat_syntax.is_some()
13 {
7 return; 14 return;
8 } 15 }
9 16
diff --git a/crates/ra_ide/src/completion/completion_context.rs b/crates/ra_ide/src/completion/completion_context.rs
index 6637afaf7..8b3401595 100644
--- a/crates/ra_ide/src/completion/completion_context.rs
+++ b/crates/ra_ide/src/completion/completion_context.rs
@@ -30,7 +30,7 @@ pub(crate) struct CompletionContext<'a> {
30 pub(super) function_syntax: Option<ast::FnDef>, 30 pub(super) function_syntax: Option<ast::FnDef>,
31 pub(super) use_item_syntax: Option<ast::UseItem>, 31 pub(super) use_item_syntax: Option<ast::UseItem>,
32 pub(super) record_lit_syntax: Option<ast::RecordLit>, 32 pub(super) record_lit_syntax: Option<ast::RecordLit>,
33 pub(super) record_lit_pat: Option<ast::RecordPat>, 33 pub(super) record_pat_syntax: Option<ast::RecordPat>,
34 pub(super) impl_def: Option<ast::ImplDef>, 34 pub(super) impl_def: Option<ast::ImplDef>,
35 pub(super) is_param: bool, 35 pub(super) is_param: bool,
36 /// If a name-binding or reference to a const in a pattern. 36 /// If a name-binding or reference to a const in a pattern.
@@ -93,7 +93,7 @@ impl<'a> CompletionContext<'a> {
93 function_syntax: None, 93 function_syntax: None,
94 use_item_syntax: None, 94 use_item_syntax: None,
95 record_lit_syntax: None, 95 record_lit_syntax: None,
96 record_lit_pat: None, 96 record_pat_syntax: None,
97 impl_def: None, 97 impl_def: None,
98 is_param: false, 98 is_param: false,
99 is_pat_binding_or_const: false, 99 is_pat_binding_or_const: false,
@@ -182,6 +182,11 @@ impl<'a> CompletionContext<'a> {
182 self.is_param = true; 182 self.is_param = true;
183 return; 183 return;
184 } 184 }
185 // FIXME: remove this (V) duplication and make the check more precise
186 if name_ref.syntax().ancestors().find_map(ast::RecordFieldPatList::cast).is_some() {
187 self.record_pat_syntax =
188 self.sema.find_node_at_offset_with_macros(&original_file, offset);
189 }
185 self.classify_name_ref(original_file, name_ref, offset); 190 self.classify_name_ref(original_file, name_ref, offset);
186 } 191 }
187 192
@@ -211,8 +216,9 @@ impl<'a> CompletionContext<'a> {
211 self.is_param = true; 216 self.is_param = true;
212 return; 217 return;
213 } 218 }
219 // FIXME: remove this (^) duplication and make the check more precise
214 if name.syntax().ancestors().find_map(ast::RecordFieldPatList::cast).is_some() { 220 if name.syntax().ancestors().find_map(ast::RecordFieldPatList::cast).is_some() {
215 self.record_lit_pat = 221 self.record_pat_syntax =
216 self.sema.find_node_at_offset_with_macros(&original_file, offset); 222 self.sema.find_node_at_offset_with_macros(&original_file, offset);
217 } 223 }
218 } 224 }
@@ -227,7 +233,7 @@ impl<'a> CompletionContext<'a> {
227 self.name_ref_syntax = 233 self.name_ref_syntax =
228 find_node_at_offset(&original_file, name_ref.syntax().text_range().start()); 234 find_node_at_offset(&original_file, name_ref.syntax().text_range().start());
229 let name_range = name_ref.syntax().text_range(); 235 let name_range = name_ref.syntax().text_range();
230 if name_ref.syntax().parent().and_then(ast::RecordField::cast).is_some() { 236 if ast::RecordField::for_field_name(&name_ref).is_some() {
231 self.record_lit_syntax = 237 self.record_lit_syntax =
232 self.sema.find_node_at_offset_with_macros(&original_file, offset); 238 self.sema.find_node_at_offset_with_macros(&original_file, offset);
233 } 239 }
diff --git a/crates/ra_ide/src/inlay_hints.rs b/crates/ra_ide/src/inlay_hints.rs
index da9f55a69..45b9f7802 100644
--- a/crates/ra_ide/src/inlay_hints.rs
+++ b/crates/ra_ide/src/inlay_hints.rs
@@ -235,7 +235,10 @@ fn should_show_param_hint(
235 param_name: &str, 235 param_name: &str,
236 argument: &ast::Expr, 236 argument: &ast::Expr,
237) -> bool { 237) -> bool {
238 if param_name.is_empty() || is_argument_similar_to_param(argument, param_name) { 238 if param_name.is_empty()
239 || is_argument_similar_to_param(argument, param_name)
240 || Some(param_name) == fn_signature.name.as_ref().map(String::as_str)
241 {
239 return false; 242 return false;
240 } 243 }
241 244
@@ -247,10 +250,7 @@ fn should_show_param_hint(
247 250
248 // avoid displaying hints for common functions like map, filter, etc. 251 // avoid displaying hints for common functions like map, filter, etc.
249 // or other obvious words used in std 252 // or other obvious words used in std
250 if parameters_len == 1 && is_obvious_param(param_name) { 253 parameters_len != 1 || !is_obvious_param(param_name)
251 return false;
252 }
253 true
254} 254}
255 255
256fn is_argument_similar_to_param(argument: &ast::Expr, param_name: &str) -> bool { 256fn is_argument_similar_to_param(argument: &ast::Expr, param_name: &str) -> bool {
@@ -1086,6 +1086,8 @@ impl Test {
1086 } 1086 }
1087 1087
1088 fn no_hints_expected(&self, _: i32, test_var: i32) {} 1088 fn no_hints_expected(&self, _: i32, test_var: i32) {}
1089
1090 fn frob(&self, frob: bool) {}
1089} 1091}
1090 1092
1091struct Param {} 1093struct Param {}
@@ -1093,6 +1095,8 @@ struct Param {}
1093fn different_order(param: &Param) {} 1095fn different_order(param: &Param) {}
1094fn different_order_mut(param: &mut Param) {} 1096fn different_order_mut(param: &mut Param) {}
1095 1097
1098fn twiddle(twiddle: bool) {}
1099
1096fn main() { 1100fn main() {
1097 let container: TestVarContainer = TestVarContainer { test_var: 42 }; 1101 let container: TestVarContainer = TestVarContainer { test_var: 42 };
1098 let test: Test = Test {}; 1102 let test: Test = Test {};
@@ -1105,6 +1109,9 @@ fn main() {
1105 let test_var: i32 = 55; 1109 let test_var: i32 = 55;
1106 test_processed.no_hints_expected(22, test_var); 1110 test_processed.no_hints_expected(22, test_var);
1107 test_processed.no_hints_expected(33, container.test_var); 1111 test_processed.no_hints_expected(33, container.test_var);
1112 test_processed.frob(false);
1113
1114 twiddle(true);
1108 1115
1109 let param_begin: Param = Param {}; 1116 let param_begin: Param = Param {};
1110 different_order(&param_begin); 1117 different_order(&param_begin);
diff --git a/crates/ra_ide_db/src/defs.rs b/crates/ra_ide_db/src/defs.rs
index e9934844e..49a8c74fb 100644
--- a/crates/ra_ide_db/src/defs.rs
+++ b/crates/ra_ide_db/src/defs.rs
@@ -216,7 +216,7 @@ pub fn classify_name_ref(
216 } 216 }
217 } 217 }
218 218
219 if let Some(record_field) = ast::RecordField::cast(parent.clone()) { 219 if let Some(record_field) = ast::RecordField::for_field_name(name_ref) {
220 tested_by!(goto_def_for_record_fields; force); 220 tested_by!(goto_def_for_record_fields; force);
221 tested_by!(goto_def_for_field_init_shorthand; force); 221 tested_by!(goto_def_for_field_init_shorthand; force);
222 if let Some((field, local)) = sema.resolve_record_field(&record_field) { 222 if let Some((field, local)) = sema.resolve_record_field(&record_field) {
diff --git a/crates/ra_parser/src/grammar.rs b/crates/ra_parser/src/grammar.rs
index d0530955e..c2a6e82e9 100644
--- a/crates/ra_parser/src/grammar.rs
+++ b/crates/ra_parser/src/grammar.rs
@@ -282,13 +282,10 @@ fn name_ref(p: &mut Parser) {
282} 282}
283 283
284fn name_ref_or_index(p: &mut Parser) { 284fn name_ref_or_index(p: &mut Parser) {
285 if p.at(IDENT) || p.at(INT_NUMBER) { 285 assert!(p.at(IDENT) || p.at(INT_NUMBER));
286 let m = p.start(); 286 let m = p.start();
287 p.bump_any(); 287 p.bump_any();
288 m.complete(p, NAME_REF); 288 m.complete(p, NAME_REF);
289 } else {
290 p.err_and_bump("expected identifier");
291 }
292} 289}
293 290
294fn error_block(p: &mut Parser, message: &str) { 291fn error_block(p: &mut Parser, message: &str) {
diff --git a/crates/ra_parser/src/grammar/expressions.rs b/crates/ra_parser/src/grammar/expressions.rs
index a1bd53063..cb30b25a8 100644
--- a/crates/ra_parser/src/grammar/expressions.rs
+++ b/crates/ra_parser/src/grammar/expressions.rs
@@ -619,26 +619,39 @@ pub(crate) fn record_field_list(p: &mut Parser) {
619 let m = p.start(); 619 let m = p.start();
620 p.bump(T!['{']); 620 p.bump(T!['{']);
621 while !p.at(EOF) && !p.at(T!['}']) { 621 while !p.at(EOF) && !p.at(T!['}']) {
622 let m = p.start();
623 // test record_literal_field_with_attr
624 // fn main() {
625 // S { #[cfg(test)] field: 1 }
626 // }
627 attributes::outer_attributes(p);
628
622 match p.current() { 629 match p.current() {
623 // test record_literal_field_with_attr 630 IDENT | INT_NUMBER => {
624 // fn main() { 631 // test_err record_literal_before_ellipsis_recovery
625 // S { #[cfg(test)] field: 1 } 632 // fn main() {
626 // } 633 // S { field ..S::default() }
627 IDENT | INT_NUMBER | T![#] => { 634 // }
628 let m = p.start(); 635 if p.nth_at(1, T![:]) || p.nth_at(1, T![..]) {
629 attributes::outer_attributes(p); 636 name_ref_or_index(p);
630 name_ref_or_index(p); 637 p.expect(T![:]);
631 if p.eat(T![:]) {
632 expr(p);
633 } 638 }
639 expr(p);
634 m.complete(p, RECORD_FIELD); 640 m.complete(p, RECORD_FIELD);
635 } 641 }
636 T![.] if p.at(T![..]) => { 642 T![.] if p.at(T![..]) => {
643 m.abandon(p);
637 p.bump(T![..]); 644 p.bump(T![..]);
638 expr(p); 645 expr(p);
639 } 646 }
640 T!['{'] => error_block(p, "expected a field"), 647 T!['{'] => {
641 _ => p.err_and_bump("expected identifier"), 648 error_block(p, "expected a field");
649 m.abandon(p);
650 }
651 _ => {
652 p.err_and_bump("expected identifier");
653 m.abandon(p);
654 }
642 } 655 }
643 if !p.at(T!['}']) { 656 if !p.at(T!['}']) {
644 p.expect(T![,]); 657 p.expect(T![,]);
diff --git a/crates/ra_parser/src/grammar/patterns.rs b/crates/ra_parser/src/grammar/patterns.rs
index 936d27575..68fb2fc73 100644
--- a/crates/ra_parser/src/grammar/patterns.rs
+++ b/crates/ra_parser/src/grammar/patterns.rs
@@ -192,14 +192,30 @@ fn record_field_pat_list(p: &mut Parser) {
192 match p.current() { 192 match p.current() {
193 // A trailing `..` is *not* treated as a DOT_DOT_PAT. 193 // A trailing `..` is *not* treated as a DOT_DOT_PAT.
194 T![.] if p.at(T![..]) => p.bump(T![..]), 194 T![.] if p.at(T![..]) => p.bump(T![..]),
195
196 IDENT | INT_NUMBER if p.nth(1) == T![:] => record_field_pat(p),
197 T!['{'] => error_block(p, "expected ident"), 195 T!['{'] => error_block(p, "expected ident"),
198 T![box] => { 196
199 box_pat(p); 197 c => {
200 } 198 let m = p.start();
201 _ => { 199 match c {
202 bind_pat(p, false); 200 // test record_field_pat
201 // fn foo() {
202 // let S { 0: 1 } = ();
203 // let S { x: 1 } = ();
204 // }
205 IDENT | INT_NUMBER if p.nth(1) == T![:] => {
206 name_ref_or_index(p);
207 p.bump(T![:]);
208 pattern(p);
209 }
210 T![box] => {
211 // FIXME: not all box patterns should be allowed
212 box_pat(p);
213 }
214 _ => {
215 bind_pat(p, false);
216 }
217 }
218 m.complete(p, RECORD_FIELD_PAT);
203 } 219 }
204 } 220 }
205 if !p.at(T!['}']) { 221 if !p.at(T!['}']) {
@@ -210,26 +226,6 @@ fn record_field_pat_list(p: &mut Parser) {
210 m.complete(p, RECORD_FIELD_PAT_LIST); 226 m.complete(p, RECORD_FIELD_PAT_LIST);
211} 227}
212 228
213// test record_field_pat
214// fn foo() {
215// let S { 0: 1 } = ();
216// let S { x: 1 } = ();
217// }
218fn record_field_pat(p: &mut Parser) {
219 assert!(p.at(IDENT) || p.at(INT_NUMBER));
220 assert!(p.nth(1) == T![:]);
221
222 let m = p.start();
223
224 if !p.eat(INT_NUMBER) {
225 name(p)
226 }
227
228 p.bump_any();
229 pattern(p);
230 m.complete(p, RECORD_FIELD_PAT);
231}
232
233// test placeholder_pat 229// test placeholder_pat
234// fn main() { let _ = (); } 230// fn main() { let _ = (); }
235fn placeholder_pat(p: &mut Parser) -> CompletedMarker { 231fn placeholder_pat(p: &mut Parser) -> CompletedMarker {
diff --git a/crates/ra_proc_macro_srv/Cargo.toml b/crates/ra_proc_macro_srv/Cargo.toml
index f08de5fc7..1e0f50339 100644
--- a/crates/ra_proc_macro_srv/Cargo.toml
+++ b/crates/ra_proc_macro_srv/Cargo.toml
@@ -12,9 +12,12 @@ doctest = false
12ra_tt = { path = "../ra_tt" } 12ra_tt = { path = "../ra_tt" }
13ra_mbe = { path = "../ra_mbe" } 13ra_mbe = { path = "../ra_mbe" }
14ra_proc_macro = { path = "../ra_proc_macro" } 14ra_proc_macro = { path = "../ra_proc_macro" }
15goblin = "0.2.1"
16libloading = "0.6.0"
17test_utils = { path = "../test_utils" }
15 18
16[dev-dependencies] 19[dev-dependencies]
17cargo_metadata = "0.9.1" 20cargo_metadata = "0.9.1"
18difference = "2.0.0" 21difference = "2.0.0"
19# used as proc macro test target 22# used as proc macro test target
20serde_derive = "=1.0.104" \ No newline at end of file 23serde_derive = "=1.0.104"
diff --git a/crates/ra_proc_macro_srv/src/dylib.rs b/crates/ra_proc_macro_srv/src/dylib.rs
new file mode 100644
index 000000000..ec63d587b
--- /dev/null
+++ b/crates/ra_proc_macro_srv/src/dylib.rs
@@ -0,0 +1,211 @@
1//! Handles dynamic library loading for proc macro
2
3use crate::{proc_macro::bridge, rustc_server::TokenStream};
4use std::path::Path;
5
6use goblin::{mach::Mach, Object};
7use libloading::Library;
8use ra_proc_macro::ProcMacroKind;
9
10use std::io::Error as IoError;
11use std::io::ErrorKind as IoErrorKind;
12
13const NEW_REGISTRAR_SYMBOL: &str = "_rustc_proc_macro_decls_";
14
15fn invalid_data_err(e: impl Into<Box<dyn std::error::Error + Send + Sync>>) -> IoError {
16 IoError::new(IoErrorKind::InvalidData, e)
17}
18
19fn get_symbols_from_lib(file: &Path) -> Result<Vec<String>, IoError> {
20 let buffer = std::fs::read(file)?;
21 let object = Object::parse(&buffer).map_err(invalid_data_err)?;
22
23 match object {
24 Object::Elf(elf) => {
25 let symbols = elf.dynstrtab.to_vec().map_err(invalid_data_err)?;
26 let names = symbols.iter().map(|s| s.to_string()).collect();
27 Ok(names)
28 }
29 Object::PE(pe) => {
30 let symbol_names =
31 pe.exports.iter().flat_map(|s| s.name).map(|n| n.to_string()).collect();
32 Ok(symbol_names)
33 }
34 Object::Mach(mach) => match mach {
35 Mach::Binary(binary) => {
36 let exports = binary.exports().map_err(invalid_data_err)?;
37 let names = exports
38 .into_iter()
39 .map(|s| {
40 // In macos doc:
41 // https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/dlsym.3.html
42 // Unlike other dyld API's, the symbol name passed to dlsym() must NOT be
43 // prepended with an underscore.
44 if s.name.starts_with("_") {
45 s.name[1..].to_string()
46 } else {
47 s.name
48 }
49 })
50 .collect();
51 Ok(names)
52 }
53 Mach::Fat(_) => Ok(vec![]),
54 },
55 Object::Archive(_) | Object::Unknown(_) => Ok(vec![]),
56 }
57}
58
59fn is_derive_registrar_symbol(symbol: &str) -> bool {
60 symbol.contains(NEW_REGISTRAR_SYMBOL)
61}
62
63fn find_registrar_symbol(file: &Path) -> Result<Option<String>, IoError> {
64 let symbols = get_symbols_from_lib(file)?;
65 Ok(symbols.into_iter().find(|s| is_derive_registrar_symbol(s)))
66}
67
68/// Loads dynamic library in platform dependent manner.
69///
70/// For unix, you have to use RTLD_DEEPBIND flag to escape problems described
71/// [here](https://github.com/fedochet/rust-proc-macro-panic-inside-panic-expample)
72/// and [here](https://github.com/rust-lang/rust/issues/60593).
73///
74/// Usage of RTLD_DEEPBIND
75/// [here](https://github.com/fedochet/rust-proc-macro-panic-inside-panic-expample/issues/1)
76///
77/// It seems that on Windows that behaviour is default, so we do nothing in that case.
78#[cfg(windows)]
79fn load_library(file: &Path) -> Result<Library, libloading::Error> {
80 Library::new(file)
81}
82
83#[cfg(unix)]
84fn load_library(file: &Path) -> Result<Library, libloading::Error> {
85 use libloading::os::unix::Library as UnixLibrary;
86 use std::os::raw::c_int;
87
88 const RTLD_NOW: c_int = 0x00002;
89 const RTLD_DEEPBIND: c_int = 0x00008;
90
91 UnixLibrary::open(Some(file), RTLD_NOW | RTLD_DEEPBIND).map(|lib| lib.into())
92}
93
94struct ProcMacroLibraryLibloading {
95 // Hold the dylib to prevent it for unloadeding
96 _lib: Library,
97 exported_macros: Vec<bridge::client::ProcMacro>,
98}
99
100impl ProcMacroLibraryLibloading {
101 fn open(file: &Path) -> Result<Self, IoError> {
102 let symbol_name = find_registrar_symbol(file)?
103 .ok_or(invalid_data_err(format!("Cannot find registrar symbol in file {:?}", file)))?;
104
105 let lib = load_library(file).map_err(invalid_data_err)?;
106 let exported_macros = {
107 let macros: libloading::Symbol<&&[bridge::client::ProcMacro]> =
108 unsafe { lib.get(symbol_name.as_bytes()) }.map_err(invalid_data_err)?;
109 macros.to_vec()
110 };
111
112 Ok(ProcMacroLibraryLibloading { _lib: lib, exported_macros })
113 }
114}
115
116type ProcMacroLibraryImpl = ProcMacroLibraryLibloading;
117
118pub struct Expander {
119 libs: Vec<ProcMacroLibraryImpl>,
120}
121
122impl Expander {
123 pub fn new<P: AsRef<Path>>(lib: &P) -> Result<Expander, String> {
124 let mut libs = vec![];
125 /* Some libraries for dynamic loading require canonicalized path (even when it is
126 already absolute
127 */
128 let lib =
129 lib.as_ref().canonicalize().expect(&format!("Cannot canonicalize {:?}", lib.as_ref()));
130
131 let library = ProcMacroLibraryImpl::open(&lib).map_err(|e| e.to_string())?;
132 libs.push(library);
133
134 Ok(Expander { libs })
135 }
136
137 pub fn expand(
138 &self,
139 macro_name: &str,
140 macro_body: &ra_tt::Subtree,
141 attributes: Option<&ra_tt::Subtree>,
142 ) -> Result<ra_tt::Subtree, bridge::PanicMessage> {
143 let parsed_body = TokenStream::with_subtree(macro_body.clone());
144
145 let parsed_attributes = attributes
146 .map_or(crate::rustc_server::TokenStream::new(), |attr| {
147 TokenStream::with_subtree(attr.clone())
148 });
149
150 for lib in &self.libs {
151 for proc_macro in &lib.exported_macros {
152 match proc_macro {
153 bridge::client::ProcMacro::CustomDerive { trait_name, client, .. }
154 if *trait_name == macro_name =>
155 {
156 let res = client.run(
157 &crate::proc_macro::bridge::server::SameThread,
158 crate::rustc_server::Rustc::default(),
159 parsed_body,
160 );
161 return res.map(|it| it.subtree);
162 }
163 bridge::client::ProcMacro::Bang { name, client } if *name == macro_name => {
164 let res = client.run(
165 &crate::proc_macro::bridge::server::SameThread,
166 crate::rustc_server::Rustc::default(),
167 parsed_body,
168 );
169 return res.map(|it| it.subtree);
170 }
171 bridge::client::ProcMacro::Attr { name, client } if *name == macro_name => {
172 let res = client.run(
173 &crate::proc_macro::bridge::server::SameThread,
174 crate::rustc_server::Rustc::default(),
175 parsed_attributes,
176 parsed_body,
177 );
178
179 return res.map(|it| it.subtree);
180 }
181 _ => continue,
182 }
183 }
184 }
185
186 Err(bridge::PanicMessage::String("Nothing to expand".to_string()))
187 }
188
189 pub fn list_macros(&self) -> Result<Vec<(String, ProcMacroKind)>, bridge::PanicMessage> {
190 let mut result = vec![];
191
192 for lib in &self.libs {
193 for proc_macro in &lib.exported_macros {
194 let res = match proc_macro {
195 bridge::client::ProcMacro::CustomDerive { trait_name, .. } => {
196 (trait_name.to_string(), ProcMacroKind::CustomDerive)
197 }
198 bridge::client::ProcMacro::Bang { name, .. } => {
199 (name.to_string(), ProcMacroKind::FuncLike)
200 }
201 bridge::client::ProcMacro::Attr { name, .. } => {
202 (name.to_string(), ProcMacroKind::Attr)
203 }
204 };
205 result.push(res);
206 }
207 }
208
209 Ok(result)
210 }
211}
diff --git a/crates/ra_proc_macro_srv/src/lib.rs b/crates/ra_proc_macro_srv/src/lib.rs
index f376df236..59716cbb3 100644
--- a/crates/ra_proc_macro_srv/src/lib.rs
+++ b/crates/ra_proc_macro_srv/src/lib.rs
@@ -17,13 +17,41 @@ mod proc_macro;
17#[doc(hidden)] 17#[doc(hidden)]
18mod rustc_server; 18mod rustc_server;
19 19
20mod dylib;
21
20use proc_macro::bridge::client::TokenStream; 22use proc_macro::bridge::client::TokenStream;
21use ra_proc_macro::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask}; 23use ra_proc_macro::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask};
22 24
23pub fn expand_task(_task: &ExpansionTask) -> Result<ExpansionResult, String> { 25pub fn expand_task(task: &ExpansionTask) -> Result<ExpansionResult, String> {
24 unimplemented!() 26 let expander = dylib::Expander::new(&task.lib)
27 .expect(&format!("Cannot expand with provided libraries: ${:?}", &task.lib));
28
29 match expander.expand(&task.macro_name, &task.macro_body, task.attributes.as_ref()) {
30 Ok(expansion) => Ok(ExpansionResult { expansion }),
31 Err(msg) => {
32 let reason = format!(
33 "Cannot perform expansion for {}: error {:?}!",
34 &task.macro_name,
35 msg.as_str()
36 );
37 Err(reason)
38 }
39 }
25} 40}
26 41
27pub fn list_macros(_task: &ListMacrosTask) -> Result<ListMacrosResult, String> { 42pub fn list_macros(task: &ListMacrosTask) -> Result<ListMacrosResult, String> {
28 unimplemented!() 43 let expander = dylib::Expander::new(&task.lib)
44 .expect(&format!("Cannot expand with provided libraries: ${:?}", &task.lib));
45
46 match expander.list_macros() {
47 Ok(macros) => Ok(ListMacrosResult { macros }),
48 Err(msg) => {
49 let reason =
50 format!("Cannot perform expansion for {:?}: error {:?}!", &task.lib, msg.as_str());
51 Err(reason)
52 }
53 }
29} 54}
55
56#[cfg(test)]
57mod tests;
diff --git a/crates/ra_proc_macro_srv/src/rustc_server.rs b/crates/ra_proc_macro_srv/src/rustc_server.rs
index 92d1fd989..ec0d35692 100644
--- a/crates/ra_proc_macro_srv/src/rustc_server.rs
+++ b/crates/ra_proc_macro_srv/src/rustc_server.rs
@@ -34,6 +34,10 @@ impl TokenStream {
34 TokenStream { subtree: Default::default() } 34 TokenStream { subtree: Default::default() }
35 } 35 }
36 36
37 pub fn with_subtree(subtree: tt::Subtree) -> Self {
38 TokenStream { subtree }
39 }
40
37 pub fn is_empty(&self) -> bool { 41 pub fn is_empty(&self) -> bool {
38 self.subtree.token_trees.is_empty() 42 self.subtree.token_trees.is_empty()
39 } 43 }
diff --git a/crates/ra_proc_macro_srv/src/tests/fixtures/test_serialize_proc_macro.txt b/crates/ra_proc_macro_srv/src/tests/fixtures/test_serialize_proc_macro.txt
new file mode 100644
index 000000000..24507d98d
--- /dev/null
+++ b/crates/ra_proc_macro_srv/src/tests/fixtures/test_serialize_proc_macro.txt
@@ -0,0 +1,188 @@
1SUBTREE $
2 PUNCH # [alone] 4294967295
3 SUBTREE [] 4294967295
4 IDENT allow 4294967295
5 SUBTREE () 4294967295
6 IDENT non_upper_case_globals 4294967295
7 PUNCH , [alone] 4294967295
8 IDENT unused_attributes 4294967295
9 PUNCH , [alone] 4294967295
10 IDENT unused_qualifications 4294967295
11 IDENT const 4294967295
12 IDENT _IMPL_SERIALIZE_FOR_Foo 4294967295
13 PUNCH : [alone] 4294967295
14 SUBTREE () 4294967295
15 PUNCH = [alone] 4294967295
16 SUBTREE {} 4294967295
17 PUNCH # [alone] 4294967295
18 SUBTREE [] 4294967295
19 IDENT allow 4294967295
20 SUBTREE () 4294967295
21 IDENT unknown_lints 4294967295
22 PUNCH # [alone] 4294967295
23 SUBTREE [] 4294967295
24 IDENT cfg_attr 4294967295
25 SUBTREE () 4294967295
26 IDENT feature 4294967295
27 PUNCH = [alone] 4294967295
28 SUBTREE $
29 LITERAL "cargo-clippy" 0
30 PUNCH , [alone] 4294967295
31 IDENT allow 4294967295
32 SUBTREE () 4294967295
33 IDENT useless_attribute 4294967295
34 PUNCH # [alone] 4294967295
35 SUBTREE [] 4294967295
36 IDENT allow 4294967295
37 SUBTREE () 4294967295
38 IDENT rust_2018_idioms 4294967295
39 IDENT extern 4294967295
40 IDENT crate 4294967295
41 IDENT serde 4294967295
42 IDENT as 4294967295
43 IDENT _serde 4294967295
44 PUNCH ; [alone] 4294967295
45 PUNCH # [alone] 4294967295
46 SUBTREE [] 4294967295
47 IDENT allow 4294967295
48 SUBTREE () 4294967295
49 IDENT unused_macros 4294967295
50 IDENT macro_rules 4294967295
51 PUNCH ! [alone] 4294967295
52 IDENT try 4294967295
53 SUBTREE {} 4294967295
54 SUBTREE () 4294967295
55 PUNCH $ [alone] 4294967295
56 IDENT __expr 4294967295
57 PUNCH : [alone] 4294967295
58 IDENT expr 4294967295
59 PUNCH = [joint] 4294967295
60 PUNCH > [alone] 4294967295
61 SUBTREE {} 4294967295
62 IDENT match 4294967295
63 PUNCH $ [alone] 4294967295
64 IDENT __expr 4294967295
65 SUBTREE {} 4294967295
66 IDENT _serde 4294967295
67 PUNCH : [joint] 4294967295
68 PUNCH : [alone] 4294967295
69 IDENT export 4294967295
70 PUNCH : [joint] 4294967295
71 PUNCH : [alone] 4294967295
72 IDENT Ok 4294967295
73 SUBTREE () 4294967295
74 IDENT __val 4294967295
75 PUNCH = [joint] 4294967295
76 PUNCH > [alone] 4294967295
77 IDENT __val 4294967295
78 PUNCH , [alone] 4294967295
79 IDENT _serde 4294967295
80 PUNCH : [joint] 4294967295
81 PUNCH : [alone] 4294967295
82 IDENT export 4294967295
83 PUNCH : [joint] 4294967295
84 PUNCH : [alone] 4294967295
85 IDENT Err 4294967295
86 SUBTREE () 4294967295
87 IDENT __err 4294967295
88 PUNCH = [joint] 4294967295
89 PUNCH > [alone] 4294967295
90 SUBTREE {} 4294967295
91 IDENT return 4294967295
92 IDENT _serde 4294967295
93 PUNCH : [joint] 4294967295
94 PUNCH : [alone] 4294967295
95 IDENT export 4294967295
96 PUNCH : [joint] 4294967295
97 PUNCH : [alone] 4294967295
98 IDENT Err 4294967295
99 SUBTREE () 4294967295
100 IDENT __err 4294967295
101 PUNCH ; [alone] 4294967295
102 PUNCH # [alone] 4294967295
103 SUBTREE [] 4294967295
104 IDENT automatically_derived 4294967295
105 IDENT impl 4294967295
106 IDENT _serde 4294967295
107 PUNCH : [joint] 4294967295
108 PUNCH : [alone] 4294967295
109 IDENT Serialize 4294967295
110 IDENT for 4294967295
111 IDENT Foo 1
112 SUBTREE {} 4294967295
113 IDENT fn 4294967295
114 IDENT serialize 4294967295
115 PUNCH < [alone] 4294967295
116 IDENT __S 4294967295
117 PUNCH > [alone] 4294967295
118 SUBTREE () 4294967295
119 PUNCH & [alone] 4294967295
120 IDENT self 4294967295
121 PUNCH , [alone] 4294967295
122 IDENT __serializer 4294967295
123 PUNCH : [alone] 4294967295
124 IDENT __S 4294967295
125 PUNCH - [joint] 4294967295
126 PUNCH > [alone] 4294967295
127 IDENT _serde 4294967295
128 PUNCH : [joint] 4294967295
129 PUNCH : [alone] 4294967295
130 IDENT export 4294967295
131 PUNCH : [joint] 4294967295
132 PUNCH : [alone] 4294967295
133 IDENT Result 4294967295
134 PUNCH < [alone] 4294967295
135 IDENT __S 4294967295
136 PUNCH : [joint] 4294967295
137 PUNCH : [alone] 4294967295
138 IDENT Ok 4294967295
139 PUNCH , [alone] 4294967295
140 IDENT __S 4294967295
141 PUNCH : [joint] 4294967295
142 PUNCH : [alone] 4294967295
143 IDENT Error 4294967295
144 PUNCH > [alone] 4294967295
145 IDENT where 4294967295
146 IDENT __S 4294967295
147 PUNCH : [alone] 4294967295
148 IDENT _serde 4294967295
149 PUNCH : [joint] 4294967295
150 PUNCH : [alone] 4294967295
151 IDENT Serializer 4294967295
152 PUNCH , [alone] 4294967295
153 SUBTREE {} 4294967295
154 IDENT let 4294967295
155 IDENT __serde_state 4294967295
156 PUNCH = [alone] 4294967295
157 IDENT try 4294967295
158 PUNCH ! [alone] 4294967295
159 SUBTREE () 4294967295
160 IDENT _serde 4294967295
161 PUNCH : [joint] 4294967295
162 PUNCH : [alone] 4294967295
163 IDENT Serializer 4294967295
164 PUNCH : [joint] 4294967295
165 PUNCH : [alone] 4294967295
166 IDENT serialize_struct 4294967295
167 SUBTREE () 4294967295
168 IDENT __serializer 4294967295
169 PUNCH , [alone] 4294967295
170 LITERAL "Foo" 4294967295
171 PUNCH , [alone] 4294967295
172 IDENT false 4294967295
173 IDENT as 4294967295
174 IDENT usize 4294967295
175 PUNCH ; [alone] 4294967295
176 IDENT _serde 4294967295
177 PUNCH : [joint] 4294967295
178 PUNCH : [alone] 4294967295
179 IDENT ser 4294967295
180 PUNCH : [joint] 4294967295
181 PUNCH : [alone] 4294967295
182 IDENT SerializeStruct 4294967295
183 PUNCH : [joint] 4294967295
184 PUNCH : [alone] 4294967295
185 IDENT end 4294967295
186 SUBTREE () 4294967295
187 IDENT __serde_state 4294967295
188 PUNCH ; [alone] 4294967295 \ No newline at end of file
diff --git a/crates/ra_proc_macro_srv/src/tests/mod.rs b/crates/ra_proc_macro_srv/src/tests/mod.rs
new file mode 100644
index 000000000..03f79bc5d
--- /dev/null
+++ b/crates/ra_proc_macro_srv/src/tests/mod.rs
@@ -0,0 +1,47 @@
1//! proc-macro tests
2
3#[macro_use]
4mod utils;
5use test_utils::assert_eq_text;
6use utils::*;
7
8#[test]
9fn test_derive_serialize_proc_macro() {
10 assert_expand(
11 "serde_derive",
12 "Serialize",
13 "1.0.104",
14 r##"struct Foo {}"##,
15 include_str!("fixtures/test_serialize_proc_macro.txt"),
16 );
17}
18
19#[test]
20fn test_derive_serialize_proc_macro_failed() {
21 assert_expand(
22 "serde_derive",
23 "Serialize",
24 "1.0.104",
25 r##"
26 struct {}
27"##,
28 r##"
29SUBTREE $
30 IDENT compile_error 4294967295
31 PUNCH ! [alone] 4294967295
32 SUBTREE {} 4294967295
33 LITERAL "expected identifier" 4294967295
34"##,
35 );
36}
37
38#[test]
39fn test_derive_proc_macro_list() {
40 let res = list("serde_derive", "1.0.104").join("\n");
41
42 assert_eq_text!(
43 &res,
44 r#"Serialize [CustomDerive]
45Deserialize [CustomDerive]"#
46 );
47}
diff --git a/crates/ra_proc_macro_srv/src/tests/utils.rs b/crates/ra_proc_macro_srv/src/tests/utils.rs
new file mode 100644
index 000000000..1ee409449
--- /dev/null
+++ b/crates/ra_proc_macro_srv/src/tests/utils.rs
@@ -0,0 +1,65 @@
1//! utils used in proc-macro tests
2
3use crate::dylib;
4use crate::list_macros;
5pub use difference::Changeset as __Changeset;
6use ra_proc_macro::ListMacrosTask;
7use std::str::FromStr;
8use test_utils::assert_eq_text;
9
10mod fixtures {
11 use cargo_metadata::{parse_messages, Message};
12 use std::process::Command;
13
14 // Use current project metadata to get the proc-macro dylib path
15 pub fn dylib_path(crate_name: &str, version: &str) -> std::path::PathBuf {
16 let command = Command::new("cargo")
17 .args(&["check", "--message-format", "json"])
18 .output()
19 .unwrap()
20 .stdout;
21
22 for message in parse_messages(command.as_slice()) {
23 match message.unwrap() {
24 Message::CompilerArtifact(artifact) => {
25 if artifact.target.kind.contains(&"proc-macro".to_string()) {
26 let repr = format!("{} {}", crate_name, version);
27 if artifact.package_id.repr.starts_with(&repr) {
28 return artifact.filenames[0].clone();
29 }
30 }
31 }
32 _ => (), // Unknown message
33 }
34 }
35
36 panic!("No proc-macro dylib for {} found!", crate_name);
37 }
38}
39
40fn parse_string(code: &str) -> Option<crate::rustc_server::TokenStream> {
41 Some(crate::rustc_server::TokenStream::from_str(code).unwrap())
42}
43
44pub fn assert_expand(
45 crate_name: &str,
46 macro_name: &str,
47 version: &str,
48 fixture: &str,
49 expect: &str,
50) {
51 let path = fixtures::dylib_path(crate_name, version);
52 let expander = dylib::Expander::new(&path).unwrap();
53 let fixture = parse_string(fixture).unwrap();
54
55 let res = expander.expand(macro_name, &fixture.subtree, None).unwrap();
56 assert_eq_text!(&format!("{:?}", res), &expect.trim());
57}
58
59pub fn list(crate_name: &str, version: &str) -> Vec<String> {
60 let path = fixtures::dylib_path(crate_name, version);
61 let task = ListMacrosTask { lib: path };
62
63 let res = list_macros(&task).unwrap();
64 res.macros.into_iter().map(|(name, kind)| format!("{} [{:?}]", name, kind)).collect()
65}
diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs
index 99c6b7219..7fca5661e 100644
--- a/crates/ra_syntax/src/ast.rs
+++ b/crates/ra_syntax/src/ast.rs
@@ -18,8 +18,8 @@ use crate::{
18pub use self::{ 18pub use self::{
19 expr_extensions::{ArrayExprKind, BinOp, ElseBranch, LiteralKind, PrefixOp, RangeOp}, 19 expr_extensions::{ArrayExprKind, BinOp, ElseBranch, LiteralKind, PrefixOp, RangeOp},
20 extensions::{ 20 extensions::{
21 AttrKind, FieldKind, PathSegmentKind, SelfParamKind, SlicePatComponents, StructKind, 21 AttrKind, FieldKind, NameOrNameRef, PathSegmentKind, SelfParamKind, SlicePatComponents,
22 TypeBoundKind, VisibilityKind, 22 StructKind, TypeBoundKind, VisibilityKind,
23 }, 23 },
24 generated::{nodes::*, tokens::*}, 24 generated::{nodes::*, tokens::*},
25 tokens::*, 25 tokens::*,
diff --git a/crates/ra_syntax/src/ast/extensions.rs b/crates/ra_syntax/src/ast/extensions.rs
index 76b7655e6..f2ea5088e 100644
--- a/crates/ra_syntax/src/ast/extensions.rs
+++ b/crates/ra_syntax/src/ast/extensions.rs
@@ -1,6 +1,8 @@
1//! Various extension methods to ast Nodes, which are hard to code-generate. 1//! Various extension methods to ast Nodes, which are hard to code-generate.
2//! Extensions for various expressions live in a sibling `expr_extensions` module. 2//! Extensions for various expressions live in a sibling `expr_extensions` module.
3 3
4use std::fmt;
5
4use itertools::Itertools; 6use itertools::Itertools;
5use ra_parser::SyntaxKind; 7use ra_parser::SyntaxKind;
6 8
@@ -187,6 +189,64 @@ impl ast::StructDef {
187 } 189 }
188} 190}
189 191
192impl ast::RecordField {
193 pub fn for_field_name(field_name: &ast::NameRef) -> Option<ast::RecordField> {
194 let candidate =
195 field_name.syntax().parent().and_then(ast::RecordField::cast).or_else(|| {
196 field_name.syntax().ancestors().nth(4).and_then(ast::RecordField::cast)
197 })?;
198 if candidate.field_name().as_ref() == Some(field_name) {
199 Some(candidate)
200 } else {
201 None
202 }
203 }
204
205 /// Deals with field init shorthand
206 pub fn field_name(&self) -> Option<ast::NameRef> {
207 if let Some(name_ref) = self.name_ref() {
208 return Some(name_ref);
209 }
210 if let Some(ast::Expr::PathExpr(expr)) = self.expr() {
211 let path = expr.path()?;
212 let segment = path.segment()?;
213 let name_ref = segment.name_ref()?;
214 if path.qualifier().is_none() {
215 return Some(name_ref);
216 }
217 }
218 None
219 }
220}
221
222pub enum NameOrNameRef {
223 Name(ast::Name),
224 NameRef(ast::NameRef),
225}
226
227impl fmt::Display for NameOrNameRef {
228 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
229 match self {
230 NameOrNameRef::Name(it) => fmt::Display::fmt(it, f),
231 NameOrNameRef::NameRef(it) => fmt::Display::fmt(it, f),
232 }
233 }
234}
235
236impl ast::RecordFieldPat {
237 /// Deals with field init shorthand
238 pub fn field_name(&self) -> Option<NameOrNameRef> {
239 if let Some(name_ref) = self.name_ref() {
240 return Some(NameOrNameRef::NameRef(name_ref));
241 }
242 if let Some(ast::Pat::BindPat(pat)) = self.pat() {
243 let name = pat.name()?;
244 return Some(NameOrNameRef::Name(name));
245 }
246 None
247 }
248}
249
190impl ast::EnumVariant { 250impl ast::EnumVariant {
191 pub fn parent_enum(&self) -> ast::EnumDef { 251 pub fn parent_enum(&self) -> ast::EnumDef {
192 self.syntax() 252 self.syntax()
diff --git a/crates/ra_syntax/src/ast/generated/nodes.rs b/crates/ra_syntax/src/ast/generated/nodes.rs
index f1098755b..188f0df96 100644
--- a/crates/ra_syntax/src/ast/generated/nodes.rs
+++ b/crates/ra_syntax/src/ast/generated/nodes.rs
@@ -1806,8 +1806,8 @@ impl AstNode for RecordFieldPat {
1806 fn syntax(&self) -> &SyntaxNode { &self.syntax } 1806 fn syntax(&self) -> &SyntaxNode { &self.syntax }
1807} 1807}
1808impl ast::AttrsOwner for RecordFieldPat {} 1808impl ast::AttrsOwner for RecordFieldPat {}
1809impl ast::NameOwner for RecordFieldPat {}
1810impl RecordFieldPat { 1809impl RecordFieldPat {
1810 pub fn name_ref(&self) -> Option<NameRef> { support::child(&self.syntax) }
1811 pub fn colon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![:]) } 1811 pub fn colon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![:]) }
1812 pub fn pat(&self) -> Option<Pat> { support::child(&self.syntax) } 1812 pub fn pat(&self) -> Option<Pat> { support::child(&self.syntax) }
1813} 1813}
diff --git a/crates/ra_syntax/src/ast/make.rs b/crates/ra_syntax/src/ast/make.rs
index f39559e9e..0f4a50be4 100644
--- a/crates/ra_syntax/src/ast/make.rs
+++ b/crates/ra_syntax/src/ast/make.rs
@@ -100,6 +100,9 @@ pub fn expr_empty_block() -> ast::Expr {
100pub fn expr_unimplemented() -> ast::Expr { 100pub fn expr_unimplemented() -> ast::Expr {
101 expr_from_text("unimplemented!()") 101 expr_from_text("unimplemented!()")
102} 102}
103pub fn expr_todo() -> ast::Expr {
104 expr_from_text("todo!()")
105}
103pub fn expr_path(path: ast::Path) -> ast::Expr { 106pub fn expr_path(path: ast::Path) -> ast::Expr {
104 expr_from_text(&path.to_string()) 107 expr_from_text(&path.to_string())
105} 108}
diff --git a/crates/ra_syntax/src/lib.rs b/crates/ra_syntax/src/lib.rs
index f0e16dc2b..a796e78b1 100644
--- a/crates/ra_syntax/src/lib.rs
+++ b/crates/ra_syntax/src/lib.rs
@@ -19,6 +19,11 @@
19//! [RFC]: <https://github.com/rust-lang/rfcs/pull/2256> 19//! [RFC]: <https://github.com/rust-lang/rfcs/pull/2256>
20//! [Swift]: <https://github.com/apple/swift/blob/13d593df6f359d0cb2fc81cfaac273297c539455/lib/Syntax/README.md> 20//! [Swift]: <https://github.com/apple/swift/blob/13d593df6f359d0cb2fc81cfaac273297c539455/lib/Syntax/README.md>
21 21
22#[allow(unused)]
23macro_rules! eprintln {
24 ($($tt:tt)*) => { stdx::eprintln!($($tt)*) };
25}
26
22mod syntax_node; 27mod syntax_node;
23mod syntax_error; 28mod syntax_error;
24mod parsing; 29mod parsing;
diff --git a/crates/ra_syntax/test_data/parser/inline/err/0014_record_literal_before_ellipsis_recovery.rast b/crates/ra_syntax/test_data/parser/inline/err/0014_record_literal_before_ellipsis_recovery.rast
new file mode 100644
index 000000000..75043c9c0
--- /dev/null
+++ b/crates/ra_syntax/test_data/parser/inline/err/0014_record_literal_before_ellipsis_recovery.rast
@@ -0,0 +1,49 @@
1SOURCE_FILE@[0; 45)
2 FN_DEF@[0; 44)
3 FN_KW@[0; 2) "fn"
4 WHITESPACE@[2; 3) " "
5 NAME@[3; 7)
6 IDENT@[3; 7) "main"
7 PARAM_LIST@[7; 9)
8 L_PAREN@[7; 8) "("
9 R_PAREN@[8; 9) ")"
10 WHITESPACE@[9; 10) " "
11 BLOCK_EXPR@[10; 44)
12 BLOCK@[10; 44)
13 L_CURLY@[10; 11) "{"
14 WHITESPACE@[11; 16) "\n "
15 RECORD_LIT@[16; 42)
16 PATH@[16; 17)
17 PATH_SEGMENT@[16; 17)
18 NAME_REF@[16; 17)
19 IDENT@[16; 17) "S"
20 WHITESPACE@[17; 18) " "
21 RECORD_FIELD_LIST@[18; 42)
22 L_CURLY@[18; 19) "{"
23 WHITESPACE@[19; 20) " "
24 RECORD_FIELD@[20; 40)
25 NAME_REF@[20; 25)
26 IDENT@[20; 25) "field"
27 WHITESPACE@[25; 26) " "
28 RANGE_EXPR@[26; 40)
29 DOT2@[26; 28) ".."
30 CALL_EXPR@[28; 40)
31 PATH_EXPR@[28; 38)
32 PATH@[28; 38)
33 PATH@[28; 29)
34 PATH_SEGMENT@[28; 29)
35 NAME_REF@[28; 29)
36 IDENT@[28; 29) "S"
37 COLON2@[29; 31) "::"
38 PATH_SEGMENT@[31; 38)
39 NAME_REF@[31; 38)
40 IDENT@[31; 38) "default"
41 ARG_LIST@[38; 40)
42 L_PAREN@[38; 39) "("
43 R_PAREN@[39; 40) ")"
44 WHITESPACE@[40; 41) " "
45 R_CURLY@[41; 42) "}"
46 WHITESPACE@[42; 43) "\n"
47 R_CURLY@[43; 44) "}"
48 WHITESPACE@[44; 45) "\n"
49error [25; 25): expected COLON
diff --git a/crates/ra_syntax/test_data/parser/inline/err/0014_record_literal_before_ellipsis_recovery.rs b/crates/ra_syntax/test_data/parser/inline/err/0014_record_literal_before_ellipsis_recovery.rs
new file mode 100644
index 000000000..a4e5b2f69
--- /dev/null
+++ b/crates/ra_syntax/test_data/parser/inline/err/0014_record_literal_before_ellipsis_recovery.rs
@@ -0,0 +1,3 @@
1fn main() {
2 S { field ..S::default() }
3}
diff --git a/crates/ra_syntax/test_data/parser/inline/ok/0061_record_lit.rast b/crates/ra_syntax/test_data/parser/inline/ok/0061_record_lit.rast
index f4206858b..89a611799 100644
--- a/crates/ra_syntax/test_data/parser/inline/ok/0061_record_lit.rast
+++ b/crates/ra_syntax/test_data/parser/inline/ok/0061_record_lit.rast
@@ -35,8 +35,11 @@ SOURCE_FILE@[0; 112)
35 L_CURLY@[27; 28) "{" 35 L_CURLY@[27; 28) "{"
36 WHITESPACE@[28; 29) " " 36 WHITESPACE@[28; 29) " "
37 RECORD_FIELD@[29; 30) 37 RECORD_FIELD@[29; 30)
38 NAME_REF@[29; 30) 38 PATH_EXPR@[29; 30)
39 IDENT@[29; 30) "x" 39 PATH@[29; 30)
40 PATH_SEGMENT@[29; 30)
41 NAME_REF@[29; 30)
42 IDENT@[29; 30) "x"
40 COMMA@[30; 31) "," 43 COMMA@[30; 31) ","
41 WHITESPACE@[31; 32) " " 44 WHITESPACE@[31; 32) " "
42 RECORD_FIELD@[32; 37) 45 RECORD_FIELD@[32; 37)
@@ -62,8 +65,11 @@ SOURCE_FILE@[0; 112)
62 L_CURLY@[48; 49) "{" 65 L_CURLY@[48; 49) "{"
63 WHITESPACE@[49; 50) " " 66 WHITESPACE@[49; 50) " "
64 RECORD_FIELD@[50; 51) 67 RECORD_FIELD@[50; 51)
65 NAME_REF@[50; 51) 68 PATH_EXPR@[50; 51)
66 IDENT@[50; 51) "x" 69 PATH@[50; 51)
70 PATH_SEGMENT@[50; 51)
71 NAME_REF@[50; 51)
72 IDENT@[50; 51) "x"
67 COMMA@[51; 52) "," 73 COMMA@[51; 52) ","
68 WHITESPACE@[52; 53) " " 74 WHITESPACE@[52; 53) " "
69 RECORD_FIELD@[53; 58) 75 RECORD_FIELD@[53; 58)
diff --git a/crates/ra_syntax/test_data/parser/inline/ok/0102_record_field_pat_list.rast b/crates/ra_syntax/test_data/parser/inline/ok/0102_record_field_pat_list.rast
index c2614543c..fcd099de9 100644
--- a/crates/ra_syntax/test_data/parser/inline/ok/0102_record_field_pat_list.rast
+++ b/crates/ra_syntax/test_data/parser/inline/ok/0102_record_field_pat_list.rast
@@ -44,18 +44,20 @@ SOURCE_FILE@[0; 119)
44 RECORD_FIELD_PAT_LIST@[40; 56) 44 RECORD_FIELD_PAT_LIST@[40; 56)
45 L_CURLY@[40; 41) "{" 45 L_CURLY@[40; 41) "{"
46 WHITESPACE@[41; 42) " " 46 WHITESPACE@[41; 42) " "
47 BIND_PAT@[42; 43) 47 RECORD_FIELD_PAT@[42; 43)
48 NAME@[42; 43) 48 BIND_PAT@[42; 43)
49 IDENT@[42; 43) "f" 49 NAME@[42; 43)
50 IDENT@[42; 43) "f"
50 COMMA@[43; 44) "," 51 COMMA@[43; 44) ","
51 WHITESPACE@[44; 45) " " 52 WHITESPACE@[44; 45) " "
52 BIND_PAT@[45; 54) 53 RECORD_FIELD_PAT@[45; 54)
53 REF_KW@[45; 48) "ref" 54 BIND_PAT@[45; 54)
54 WHITESPACE@[48; 49) " " 55 REF_KW@[45; 48) "ref"
55 MUT_KW@[49; 52) "mut" 56 WHITESPACE@[48; 49) " "
56 WHITESPACE@[52; 53) " " 57 MUT_KW@[49; 52) "mut"
57 NAME@[53; 54) 58 WHITESPACE@[52; 53) " "
58 IDENT@[53; 54) "g" 59 NAME@[53; 54)
60 IDENT@[53; 54) "g"
59 WHITESPACE@[54; 55) " " 61 WHITESPACE@[54; 55) " "
60 R_CURLY@[55; 56) "}" 62 R_CURLY@[55; 56) "}"
61 WHITESPACE@[56; 57) " " 63 WHITESPACE@[56; 57) " "
@@ -79,7 +81,7 @@ SOURCE_FILE@[0; 119)
79 L_CURLY@[73; 74) "{" 81 L_CURLY@[73; 74) "{"
80 WHITESPACE@[74; 75) " " 82 WHITESPACE@[74; 75) " "
81 RECORD_FIELD_PAT@[75; 79) 83 RECORD_FIELD_PAT@[75; 79)
82 NAME@[75; 76) 84 NAME_REF@[75; 76)
83 IDENT@[75; 76) "h" 85 IDENT@[75; 76) "h"
84 COLON@[76; 77) ":" 86 COLON@[76; 77) ":"
85 WHITESPACE@[77; 78) " " 87 WHITESPACE@[77; 78) " "
@@ -110,7 +112,7 @@ SOURCE_FILE@[0; 119)
110 L_CURLY@[101; 102) "{" 112 L_CURLY@[101; 102) "{"
111 WHITESPACE@[102; 103) " " 113 WHITESPACE@[102; 103) " "
112 RECORD_FIELD_PAT@[103; 107) 114 RECORD_FIELD_PAT@[103; 107)
113 NAME@[103; 104) 115 NAME_REF@[103; 104)
114 IDENT@[103; 104) "h" 116 IDENT@[103; 104) "h"
115 COLON@[104; 105) ":" 117 COLON@[104; 105) ":"
116 WHITESPACE@[105; 106) " " 118 WHITESPACE@[105; 106) " "
diff --git a/crates/ra_syntax/test_data/parser/inline/ok/0143_box_pat.rast b/crates/ra_syntax/test_data/parser/inline/ok/0143_box_pat.rast
index f75673070..1d245f8f3 100644
--- a/crates/ra_syntax/test_data/parser/inline/ok/0143_box_pat.rast
+++ b/crates/ra_syntax/test_data/parser/inline/ok/0143_box_pat.rast
@@ -44,16 +44,17 @@ SOURCE_FILE@[0; 118)
44 RECORD_FIELD_PAT_LIST@[50; 81) 44 RECORD_FIELD_PAT_LIST@[50; 81)
45 L_CURLY@[50; 51) "{" 45 L_CURLY@[50; 51) "{"
46 WHITESPACE@[51; 52) " " 46 WHITESPACE@[51; 52) " "
47 BOX_PAT@[52; 57) 47 RECORD_FIELD_PAT@[52; 57)
48 BOX_KW@[52; 55) "box" 48 BOX_PAT@[52; 57)
49 WHITESPACE@[55; 56) " " 49 BOX_KW@[52; 55) "box"
50 BIND_PAT@[56; 57) 50 WHITESPACE@[55; 56) " "
51 NAME@[56; 57) 51 BIND_PAT@[56; 57)
52 IDENT@[56; 57) "i" 52 NAME@[56; 57)
53 IDENT@[56; 57) "i"
53 COMMA@[57; 58) "," 54 COMMA@[57; 58) ","
54 WHITESPACE@[58; 59) " " 55 WHITESPACE@[58; 59) " "
55 RECORD_FIELD_PAT@[59; 79) 56 RECORD_FIELD_PAT@[59; 79)
56 NAME@[59; 60) 57 NAME_REF@[59; 60)
57 IDENT@[59; 60) "j" 58 IDENT@[59; 60) "j"
58 COLON@[60; 61) ":" 59 COLON@[60; 61) ":"
59 WHITESPACE@[61; 62) " " 60 WHITESPACE@[61; 62) " "
diff --git a/crates/ra_syntax/test_data/parser/inline/ok/0145_record_field_pat.rast b/crates/ra_syntax/test_data/parser/inline/ok/0145_record_field_pat.rast
index 0d786f597..cac2ffdcf 100644
--- a/crates/ra_syntax/test_data/parser/inline/ok/0145_record_field_pat.rast
+++ b/crates/ra_syntax/test_data/parser/inline/ok/0145_record_field_pat.rast
@@ -25,7 +25,8 @@ SOURCE_FILE@[0; 63)
25 L_CURLY@[21; 22) "{" 25 L_CURLY@[21; 22) "{"
26 WHITESPACE@[22; 23) " " 26 WHITESPACE@[22; 23) " "
27 RECORD_FIELD_PAT@[23; 27) 27 RECORD_FIELD_PAT@[23; 27)
28 INT_NUMBER@[23; 24) "0" 28 NAME_REF@[23; 24)
29 INT_NUMBER@[23; 24) "0"
29 COLON@[24; 25) ":" 30 COLON@[24; 25) ":"
30 WHITESPACE@[25; 26) " " 31 WHITESPACE@[25; 26) " "
31 LITERAL_PAT@[26; 27) 32 LITERAL_PAT@[26; 27)
@@ -54,7 +55,7 @@ SOURCE_FILE@[0; 63)
54 L_CURLY@[46; 47) "{" 55 L_CURLY@[46; 47) "{"
55 WHITESPACE@[47; 48) " " 56 WHITESPACE@[47; 48) " "
56 RECORD_FIELD_PAT@[48; 52) 57 RECORD_FIELD_PAT@[48; 52)
57 NAME@[48; 49) 58 NAME_REF@[48; 49)
58 IDENT@[48; 49) "x" 59 IDENT@[48; 49) "x"
59 COLON@[49; 50) ":" 60 COLON@[49; 50) ":"
60 WHITESPACE@[50; 51) " " 61 WHITESPACE@[50; 51) " "
diff --git a/crates/ra_syntax/test_data/parser/ok/0063_trait_fn_patterns.rast b/crates/ra_syntax/test_data/parser/ok/0063_trait_fn_patterns.rast
index 9b5954ebd..d0623ba90 100644
--- a/crates/ra_syntax/test_data/parser/ok/0063_trait_fn_patterns.rast
+++ b/crates/ra_syntax/test_data/parser/ok/0063_trait_fn_patterns.rast
@@ -68,14 +68,16 @@ SOURCE_FILE@[0; 170)
68 RECORD_FIELD_PAT_LIST@[59; 67) 68 RECORD_FIELD_PAT_LIST@[59; 67)
69 L_CURLY@[59; 60) "{" 69 L_CURLY@[59; 60) "{"
70 WHITESPACE@[60; 61) " " 70 WHITESPACE@[60; 61) " "
71 BIND_PAT@[61; 62) 71 RECORD_FIELD_PAT@[61; 62)
72 NAME@[61; 62) 72 BIND_PAT@[61; 62)
73 IDENT@[61; 62) "a" 73 NAME@[61; 62)
74 IDENT@[61; 62) "a"
74 COMMA@[62; 63) "," 75 COMMA@[62; 63) ","
75 WHITESPACE@[63; 64) " " 76 WHITESPACE@[63; 64) " "
76 BIND_PAT@[64; 65) 77 RECORD_FIELD_PAT@[64; 65)
77 NAME@[64; 65) 78 BIND_PAT@[64; 65)
78 IDENT@[64; 65) "b" 79 NAME@[64; 65)
80 IDENT@[64; 65) "b"
79 WHITESPACE@[65; 66) " " 81 WHITESPACE@[65; 66) " "
80 R_CURLY@[66; 67) "}" 82 R_CURLY@[66; 67) "}"
81 COLON@[67; 68) ":" 83 COLON@[67; 68) ":"
diff --git a/crates/ra_syntax/test_data/parser/ok/0064_impl_fn_params.rast b/crates/ra_syntax/test_data/parser/ok/0064_impl_fn_params.rast
index b30030de3..5e96b695b 100644
--- a/crates/ra_syntax/test_data/parser/ok/0064_impl_fn_params.rast
+++ b/crates/ra_syntax/test_data/parser/ok/0064_impl_fn_params.rast
@@ -71,14 +71,16 @@ SOURCE_FILE@[0; 137)
71 RECORD_FIELD_PAT_LIST@[58; 66) 71 RECORD_FIELD_PAT_LIST@[58; 66)
72 L_CURLY@[58; 59) "{" 72 L_CURLY@[58; 59) "{"
73 WHITESPACE@[59; 60) " " 73 WHITESPACE@[59; 60) " "
74 BIND_PAT@[60; 61) 74 RECORD_FIELD_PAT@[60; 61)
75 NAME@[60; 61) 75 BIND_PAT@[60; 61)
76 IDENT@[60; 61) "a" 76 NAME@[60; 61)
77 IDENT@[60; 61) "a"
77 COMMA@[61; 62) "," 78 COMMA@[61; 62) ","
78 WHITESPACE@[62; 63) " " 79 WHITESPACE@[62; 63) " "
79 BIND_PAT@[63; 64) 80 RECORD_FIELD_PAT@[63; 64)
80 NAME@[63; 64) 81 BIND_PAT@[63; 64)
81 IDENT@[63; 64) "b" 82 NAME@[63; 64)
83 IDENT@[63; 64) "b"
82 WHITESPACE@[64; 65) " " 84 WHITESPACE@[64; 65) " "
83 R_CURLY@[65; 66) "}" 85 R_CURLY@[65; 66) "}"
84 COLON@[66; 67) ":" 86 COLON@[66; 67) ":"
diff --git a/crates/rust-analyzer/src/cli/analysis_stats.rs b/crates/rust-analyzer/src/cli/analysis_stats.rs
index 75cf2dae5..e9ee0b888 100644
--- a/crates/rust-analyzer/src/cli/analysis_stats.rs
+++ b/crates/rust-analyzer/src/cli/analysis_stats.rs
@@ -163,10 +163,7 @@ pub fn analysis_stats(
163 if let Ok(src) = src { 163 if let Ok(src) = src {
164 let original_file = src.file_id.original_file(db); 164 let original_file = src.file_id.original_file(db);
165 let line_index = host.analysis().file_line_index(original_file).unwrap(); 165 let line_index = host.analysis().file_line_index(original_file).unwrap();
166 let text_range = src.value.either( 166 let text_range = src.value.syntax_node_ptr().range();
167 |it| it.syntax_node_ptr().range(),
168 |it| it.syntax_node_ptr().range(),
169 );
170 let (start, end) = ( 167 let (start, end) = (
171 line_index.line_col(text_range.start()), 168 line_index.line_col(text_range.start()),
172 line_index.line_col(text_range.end()), 169 line_index.line_col(text_range.end()),
@@ -192,12 +189,7 @@ pub fn analysis_stats(
192 // FIXME: it might be nice to have a function (on Analysis?) that goes from Source<T> -> (LineCol, LineCol) directly 189 // FIXME: it might be nice to have a function (on Analysis?) that goes from Source<T> -> (LineCol, LineCol) directly
193 // But also, we should just turn the type mismatches into diagnostics and provide these 190 // But also, we should just turn the type mismatches into diagnostics and provide these
194 let root = db.parse_or_expand(src.file_id).unwrap(); 191 let root = db.parse_or_expand(src.file_id).unwrap();
195 let node = src.map(|e| { 192 let node = src.map(|e| e.to_node(&root).syntax().clone());
196 e.either(
197 |p| p.to_node(&root).syntax().clone(),
198 |p| p.to_node(&root).syntax().clone(),
199 )
200 });
201 let original_range = original_range(db, node.as_ref()); 193 let original_range = original_range(db, node.as_ref());
202 let path = db.file_relative_path(original_range.file_id); 194 let path = db.file_relative_path(original_range.file_id);
203 let line_index = 195 let line_index =
diff --git a/crates/rust-analyzer/src/cli/load_cargo.rs b/crates/rust-analyzer/src/cli/load_cargo.rs
index 69133e4e4..43062ea10 100644
--- a/crates/rust-analyzer/src/cli/load_cargo.rs
+++ b/crates/rust-analyzer/src/cli/load_cargo.rs
@@ -152,7 +152,9 @@ pub(crate) fn load(
152 &extern_source_roots, 152 &extern_source_roots,
153 proc_macro_client, 153 proc_macro_client,
154 &mut |path: &Path| { 154 &mut |path: &Path| {
155 let vfs_file = vfs.load(path); 155 // Some path from metadata will be non canonicalized, e.g. /foo/../bar/lib.rs
156 let path = path.canonicalize().ok()?;
157 let vfs_file = vfs.load(&path);
156 log::debug!("vfs file {:?} -> {:?}", path, vfs_file); 158 log::debug!("vfs file {:?} -> {:?}", path, vfs_file);
157 vfs_file.map(vfs_file_to_id) 159 vfs_file.map(vfs_file_to_id)
158 }, 160 },
diff --git a/crates/rust-analyzer/src/world.rs b/crates/rust-analyzer/src/world.rs
index 365f57d8c..6c42e1d76 100644
--- a/crates/rust-analyzer/src/world.rs
+++ b/crates/rust-analyzer/src/world.rs
@@ -139,7 +139,9 @@ impl WorldState {
139 // Create crate graph from all the workspaces 139 // Create crate graph from all the workspaces
140 let mut crate_graph = CrateGraph::default(); 140 let mut crate_graph = CrateGraph::default();
141 let mut load = |path: &std::path::Path| { 141 let mut load = |path: &std::path::Path| {
142 let vfs_file = vfs.load(path); 142 // Some path from metadata will be non canonicalized, e.g. /foo/../bar/lib.rs
143 let path = path.canonicalize().ok()?;
144 let vfs_file = vfs.load(&path);
143 vfs_file.map(|f| FileId(f.0)) 145 vfs_file.map(|f| FileId(f.0))
144 }; 146 };
145 147
diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs
index 3b60c55f3..4164bfd5e 100644
--- a/crates/test_utils/src/lib.rs
+++ b/crates/test_utils/src/lib.rs
@@ -395,7 +395,7 @@ pub fn skip_slow_tests() -> bool {
395 should_skip 395 should_skip
396} 396}
397 397
398const REWRITE: bool = true; 398const REWRITE: bool = false;
399 399
400/// Asserts that `expected` and `actual` strings are equal. If they differ only 400/// Asserts that `expected` and `actual` strings are equal. If they differ only
401/// in trailing or leading whitespace the test won't fail and 401/// in trailing or leading whitespace the test won't fail and
diff --git a/docs/user/assists.md b/docs/user/assists.md
index 754131f6f..1d9510423 100644
--- a/docs/user/assists.md
+++ b/docs/user/assists.md
@@ -175,7 +175,7 @@ trait Trait<T> {
175} 175}
176 176
177impl Trait<u32> for () { 177impl Trait<u32> for () {
178 fn foo(&self) -> u32 { unimplemented!() } 178 fn foo(&self) -> u32 { todo!() }
179 179
180} 180}
181``` 181```
@@ -582,6 +582,21 @@ impl Walrus {
582} 582}
583``` 583```
584 584
585## `reorder_fields`
586
587Reorder the fields of record literals and record patterns in the same order as in
588the definition.
589
590```rust
591// BEFORE
592struct Foo {foo: i32, bar: i32};
593const test: Foo = ┃Foo {bar: 0, foo: 1}
594
595// AFTER
596struct Foo {foo: i32, bar: i32};
597const test: Foo = Foo {foo: 1, bar: 0}
598```
599
585## `replace_if_let_with_match` 600## `replace_if_let_with_match`
586 601
587Replaces `if let` with an else branch with a `match` expression. 602Replaces `if let` with an else branch with a `match` expression.
diff --git a/editors/code/src/config.ts b/editors/code/src/config.ts
index 21c1c9f23..35a05131c 100644
--- a/editors/code/src/config.ts
+++ b/editors/code/src/config.ts
@@ -66,23 +66,44 @@ export class Config {
66 return vscode.workspace.getConfiguration(this.rootSection); 66 return vscode.workspace.getConfiguration(this.rootSection);
67 } 67 }
68 68
69 get serverPath() { return this.cfg.get<null | string>("serverPath")!; } 69 /**
70 get channel() { return this.cfg.get<UpdatesChannel>("updates.channel")!; } 70 * Beware that postfix `!` operator erases both `null` and `undefined`.
71 get askBeforeDownload() { return this.cfg.get<boolean>("updates.askBeforeDownload")!; } 71 * This is why the following doesn't work as expected:
72 get traceExtension() { return this.cfg.get<boolean>("trace.extension")!; } 72 *
73 * ```ts
74 * const nullableNum = vscode
75 * .workspace
76 * .getConfiguration
77 * .getConfiguration("rust-analyer")
78 * .get<number | null>(path)!;
79 *
80 * // What happens is that type of `nullableNum` is `number` but not `null | number`:
81 * const fullFledgedNum: number = nullableNum;
82 * ```
83 * So this getter handles this quirk by not requiring the caller to use postfix `!`
84 */
85 private get<T>(path: string): T {
86 return this.cfg.get<T>(path)!;
87 }
88
89 get serverPath() { return this.get<null | string>("serverPath"); }
90 get channel() { return this.get<UpdatesChannel>("updates.channel"); }
91 get askBeforeDownload() { return this.get<boolean>("updates.askBeforeDownload"); }
92 get traceExtension() { return this.get<boolean>("trace.extension"); }
93
73 94
74 get inlayHints() { 95 get inlayHints() {
75 return { 96 return {
76 typeHints: this.cfg.get<boolean>("inlayHints.typeHints")!, 97 typeHints: this.get<boolean>("inlayHints.typeHints"),
77 parameterHints: this.cfg.get<boolean>("inlayHints.parameterHints")!, 98 parameterHints: this.get<boolean>("inlayHints.parameterHints"),
78 chainingHints: this.cfg.get<boolean>("inlayHints.chainingHints")!, 99 chainingHints: this.get<boolean>("inlayHints.chainingHints"),
79 maxLength: this.cfg.get<null | number>("inlayHints.maxLength")!, 100 maxLength: this.get<null | number>("inlayHints.maxLength"),
80 }; 101 };
81 } 102 }
82 103
83 get checkOnSave() { 104 get checkOnSave() {
84 return { 105 return {
85 command: this.cfg.get<string>("checkOnSave.command")!, 106 command: this.get<string>("checkOnSave.command"),
86 }; 107 };
87 } 108 }
88} 109}
diff --git a/editors/code/src/inlay_hints.ts b/editors/code/src/inlay_hints.ts
index 6a8bd942e..da74f03d2 100644
--- a/editors/code/src/inlay_hints.ts
+++ b/editors/code/src/inlay_hints.ts
@@ -229,13 +229,13 @@ interface InlaysDecorations {
229} 229}
230 230
231interface RustSourceFile { 231interface RustSourceFile {
232 /* 232 /**
233 * Source of the token to cancel in-flight inlay hints request if any. 233 * Source of the token to cancel in-flight inlay hints request if any.
234 */ 234 */
235 inlaysRequest: null | vscode.CancellationTokenSource; 235 inlaysRequest: null | vscode.CancellationTokenSource;
236 /** 236 /**
237 * Last applied decorations. 237 * Last applied decorations.
238 */ 238 */
239 cachedDecorations: null | InlaysDecorations; 239 cachedDecorations: null | InlaysDecorations;
240 240
241 document: RustDocument; 241 document: RustDocument;
diff --git a/xtask/src/ast_src.rs b/xtask/src/ast_src.rs
index 69cba9168..9c02f7c6f 100644
--- a/xtask/src/ast_src.rs
+++ b/xtask/src/ast_src.rs
@@ -511,7 +511,7 @@ pub(crate) const AST_SRC: AstSrc = AstSrc {
511 T![..], 511 T![..],
512 T!['}'] 512 T!['}']
513 } 513 }
514 struct RecordFieldPat: AttrsOwner, NameOwner { T![:], Pat } 514 struct RecordFieldPat: AttrsOwner { NameRef, T![:], Pat }
515 515
516 struct TupleStructPat { Path, T!['('], args: [Pat], T![')'] } 516 struct TupleStructPat { Path, T!['('], args: [Pat], T![')'] }
517 struct TuplePat { T!['('], args: [Pat], T![')'] } 517 struct TuplePat { T!['('], args: [Pat], T![')'] }
diff --git a/xtask/tests/tidy-tests/main.rs b/xtask/tests/tidy-tests/main.rs
index 80911a68e..4f525fcd0 100644
--- a/xtask/tests/tidy-tests/main.rs
+++ b/xtask/tests/tidy-tests/main.rs
@@ -20,7 +20,16 @@ fn rust_files_are_tidy() {
20} 20}
21 21
22fn check_todo(path: &Path, text: &str) { 22fn check_todo(path: &Path, text: &str) {
23 if path.ends_with("tests/cli.rs") { 23 let whitelist = &[
24 // This file itself is whitelisted since this test itself contains matches.
25 "tests/cli.rs",
26 // Some of our assists generate `todo!()` so those files are whitelisted.
27 "doc_tests/generated.rs",
28 "handlers/add_missing_impl_members.rs",
29 // To support generating `todo!()` in assists, we have `expr_todo()` in ast::make.
30 "ast/make.rs",
31 ];
32 if whitelist.iter().any(|p| path.ends_with(p)) {
24 return; 33 return;
25 } 34 }
26 if text.contains("TODO") || text.contains("TOOD") || text.contains("todo!") { 35 if text.contains("TODO") || text.contains("TOOD") || text.contains("todo!") {