From b5b68f2094d49cacde6d7f0c49f521a0b25f34bd Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Mon, 24 Dec 2018 19:07:48 +0100 Subject: Add basic HIR and types for structs/enums --- crates/ra_hir/src/ty/tests.rs | 23 +++++++++++++++++++++++ crates/ra_hir/src/ty/tests/data/0004_struct.txt | 10 ++++++++++ 2 files changed, 33 insertions(+) create mode 100644 crates/ra_hir/src/ty/tests/data/0004_struct.txt (limited to 'crates/ra_hir/src/ty') diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index b6c02cd80..170eef147 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -68,6 +68,29 @@ fn test() { ); } +#[test] +fn infer_struct() { + check_inference( + r#" +struct A { + b: B, + c: C, +} +struct B; +struct C(usize); + +fn test() { + let c = C(1); + B; + let a: A = A { b: B, c: C() }; + a.b; + a.c; +} +"#, + "0004_struct.txt", + ); +} + fn infer(content: &str) -> String { let (db, _, file_id) = MockDatabase::with_single_file(content); let source_file = db.source_file(file_id); diff --git a/crates/ra_hir/src/ty/tests/data/0004_struct.txt b/crates/ra_hir/src/ty/tests/data/0004_struct.txt new file mode 100644 index 000000000..70ad055ff --- /dev/null +++ b/crates/ra_hir/src/ty/tests/data/0004_struct.txt @@ -0,0 +1,10 @@ +[86; 90) 'C(1)': [unknown] +[72; 153) '{ ...a.c; }': () +[86; 87) 'C': C +[107; 108) 'a': A +[114; 132) 'A { b:... C() }': [unknown] +[138; 141) 'a.b': [unknown] +[147; 150) 'a.c': [unknown] +[96; 97) 'B': B +[88; 89) '1': [unknown] +[82; 83) 'c': [unknown] -- cgit v1.2.3 From 4ff161852016c6c15954d6f30bd637834a2b2b68 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Mon, 24 Dec 2018 20:32:39 +0100 Subject: Do name resolution by namespace (types/values) --- crates/ra_hir/src/ty/tests/data/0004_struct.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'crates/ra_hir/src/ty') diff --git a/crates/ra_hir/src/ty/tests/data/0004_struct.txt b/crates/ra_hir/src/ty/tests/data/0004_struct.txt index 70ad055ff..a4371c5a5 100644 --- a/crates/ra_hir/src/ty/tests/data/0004_struct.txt +++ b/crates/ra_hir/src/ty/tests/data/0004_struct.txt @@ -1,10 +1,10 @@ [86; 90) 'C(1)': [unknown] [72; 153) '{ ...a.c; }': () -[86; 87) 'C': C +[86; 87) 'C': [unknown] [107; 108) 'a': A [114; 132) 'A { b:... C() }': [unknown] [138; 141) 'a.b': [unknown] [147; 150) 'a.c': [unknown] -[96; 97) 'B': B +[96; 97) 'B': [unknown] [88; 89) '1': [unknown] [82; 83) 'c': [unknown] -- cgit v1.2.3 From 6fcd38cc81bdcc9921da767872dfce65ee7d2d27 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Mon, 24 Dec 2018 21:00:14 +0100 Subject: Infer result of struct literals, and recurse into their child expressions --- crates/ra_hir/src/ty/tests.rs | 2 +- crates/ra_hir/src/ty/tests/data/0004_struct.txt | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) (limited to 'crates/ra_hir/src/ty') diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index 170eef147..9bb58ec85 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -82,7 +82,7 @@ struct C(usize); fn test() { let c = C(1); B; - let a: A = A { b: B, c: C() }; + let a: A = A { b: B, c: C(1) }; a.b; a.c; } diff --git a/crates/ra_hir/src/ty/tests/data/0004_struct.txt b/crates/ra_hir/src/ty/tests/data/0004_struct.txt index a4371c5a5..41357749f 100644 --- a/crates/ra_hir/src/ty/tests/data/0004_struct.txt +++ b/crates/ra_hir/src/ty/tests/data/0004_struct.txt @@ -1,10 +1,14 @@ [86; 90) 'C(1)': [unknown] -[72; 153) '{ ...a.c; }': () +[121; 122) 'B': [unknown] [86; 87) 'C': [unknown] +[129; 130) '1': [unknown] [107; 108) 'a': A -[114; 132) 'A { b:... C() }': [unknown] -[138; 141) 'a.b': [unknown] -[147; 150) 'a.c': [unknown] +[127; 128) 'C': [unknown] +[139; 142) 'a.b': [unknown] +[114; 133) 'A { b:...C(1) }': A +[148; 151) 'a.c': [unknown] +[72; 154) '{ ...a.c; }': () [96; 97) 'B': [unknown] [88; 89) '1': [unknown] [82; 83) 'c': [unknown] +[127; 131) 'C(1)': [unknown] -- cgit v1.2.3 From 55c941cd9fb90c9340f01981e113aabd058b185b Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Tue, 25 Dec 2018 13:54:38 +0100 Subject: Type field accesses --- crates/ra_hir/src/ty/tests/data/0004_struct.txt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'crates/ra_hir/src/ty') diff --git a/crates/ra_hir/src/ty/tests/data/0004_struct.txt b/crates/ra_hir/src/ty/tests/data/0004_struct.txt index 41357749f..cc8f3665b 100644 --- a/crates/ra_hir/src/ty/tests/data/0004_struct.txt +++ b/crates/ra_hir/src/ty/tests/data/0004_struct.txt @@ -4,9 +4,11 @@ [129; 130) '1': [unknown] [107; 108) 'a': A [127; 128) 'C': [unknown] -[139; 142) 'a.b': [unknown] +[139; 142) 'a.b': B [114; 133) 'A { b:...C(1) }': A -[148; 151) 'a.c': [unknown] +[148; 151) 'a.c': C +[148; 149) 'a': A +[139; 140) 'a': A [72; 154) '{ ...a.c; }': () [96; 97) 'B': [unknown] [88; 89) '1': [unknown] -- cgit v1.2.3 From 2870effd5c69941bbf32a44c0ee6d9d42e0b038d Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Tue, 25 Dec 2018 17:17:39 +0100 Subject: Implement reference / pointer types - parse them - infer types of & and * expressions --- crates/ra_hir/src/ty/tests.rs | 22 ++++++++++++++++++++++ crates/ra_hir/src/ty/tests/data/0001_basics.txt | 4 ++-- crates/ra_hir/src/ty/tests/data/0005_refs.txt | 23 +++++++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 crates/ra_hir/src/ty/tests/data/0005_refs.txt (limited to 'crates/ra_hir/src/ty') diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index 9bb58ec85..a76925b58 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -91,6 +91,28 @@ fn test() { ); } +#[test] +fn infer_refs_and_ptrs() { + check_inference( + r#" +fn test(a: &u32, b: &mut u32, c: *const u32, d: *mut u32) { + a; + *a; + &a; + &mut a; + b; + *b; + &b; + c; + *c; + d; + *d; +} +"#, + "0005_refs.txt", + ); +} + fn infer(content: &str) -> String { let (db, _, file_id) = MockDatabase::with_single_file(content); let source_file = db.source_file(file_id); diff --git a/crates/ra_hir/src/ty/tests/data/0001_basics.txt b/crates/ra_hir/src/ty/tests/data/0001_basics.txt index 0c46f243a..212e92e00 100644 --- a/crates/ra_hir/src/ty/tests/data/0001_basics.txt +++ b/crates/ra_hir/src/ty/tests/data/0001_basics.txt @@ -1,4 +1,4 @@ -[33; 34) 'd': [unknown] +[33; 34) 'd': &[unknown] [88; 94) '1isize': [unknown] [48; 49) 'a': u32 [55; 56) 'b': isize @@ -10,4 +10,4 @@ [17; 18) 'b': isize [100; 106) '"test"': [unknown] [42; 121) '{ ...f32; }': () -[69; 70) 'd': [unknown] +[69; 70) 'd': &[unknown] diff --git a/crates/ra_hir/src/ty/tests/data/0005_refs.txt b/crates/ra_hir/src/ty/tests/data/0005_refs.txt new file mode 100644 index 000000000..296e955c1 --- /dev/null +++ b/crates/ra_hir/src/ty/tests/data/0005_refs.txt @@ -0,0 +1,23 @@ +[115; 117) '&b': &&mut u32 +[88; 94) '&mut a': &mut &u32 +[146; 147) 'd': *mut u32 +[145; 147) '*d': u32 +[65; 66) 'a': &u32 +[46; 47) 'd': *mut u32 +[59; 150) '{ ... *d; }': () +[116; 117) 'b': &mut u32 +[131; 132) 'c': *const u32 +[130; 132) '*c': u32 +[72; 74) '*a': u32 +[107; 109) '*b': u32 +[108; 109) 'b': &mut u32 +[9; 10) 'a': &u32 +[18; 19) 'b': &mut u32 +[93; 94) 'a': &u32 +[100; 101) 'b': &mut u32 +[81; 82) 'a': &u32 +[80; 82) '&a': &&u32 +[73; 74) 'a': &u32 +[123; 124) 'c': *const u32 +[31; 32) 'c': *const u32 +[138; 139) 'd': *mut u32 -- cgit v1.2.3