From 415cdc52108807126f0339fbf812856582f01c18 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 12 Jan 2019 18:47:43 +0100 Subject: Add some tests --- crates/ra_hir/src/ty/tests.rs | 63 ++++++++++++++++++++++ .../ra_hir/src/ty/tests/data/function_generics.txt | 14 +++++ crates/ra_hir/src/ty/tests/data/generic_chain.txt | 29 ++++++++++ .../ra_hir/src/ty/tests/data/struct_generics.txt | 15 ++++++ 4 files changed, 121 insertions(+) create mode 100644 crates/ra_hir/src/ty/tests/data/function_generics.txt create mode 100644 crates/ra_hir/src/ty/tests/data/generic_chain.txt create mode 100644 crates/ra_hir/src/ty/tests/data/struct_generics.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 fc4054159..c590a09db 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -418,6 +418,69 @@ fn test() { ); } +#[test] +fn infer_struct_generics() { + check_inference( + r#" +struct A { + x: T, +} + +fn test(a1: A, i: i32) { + a1.x; + let a2 = A { x: i }; + a2.x; + let a3 = A:: { x: 1 }; + a3.x; +} +"#, + "struct_generics.txt", + ); +} + +#[test] +fn infer_function_generics() { + check_inference( + r#" +fn id(t: T) -> T { t } + +fn test() { + id(1u32); + id::(1); + let x: u64 = id(1); +} +"#, + "function_generics.txt", + ); +} + +#[test] +fn infer_generic_chain() { + check_inference( + r#" +struct A { + x: T, +} +impl A { + fn x(self) -> T2 { + self.x + } +} +fn id(t: T) -> T { t } + +fn test() -> i128 { + let x = 1; + let y = id(x); + let a = A { x: id(y) }; + let z = id(a.x); + let b = A { x: z }; + b.x() +} +"#, + "generic_chain.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/function_generics.txt b/crates/ra_hir/src/ty/tests/data/function_generics.txt new file mode 100644 index 000000000..179e136a0 --- /dev/null +++ b/crates/ra_hir/src/ty/tests/data/function_generics.txt @@ -0,0 +1,14 @@ +[10; 11) 't': [unknown] +[21; 26) '{ t }': [unknown] +[23; 24) 't': [unknown] +[38; 98) '{ ...(1); }': () +[44; 46) 'id': fn([unknown]) -> [unknown] +[44; 52) 'id(1u32)': [unknown] +[47; 51) '1u32': [unknown] +[58; 68) 'id::': fn([unknown]) -> [unknown] +[58; 71) 'id::(1)': [unknown] +[69; 70) '1': [unknown] +[81; 82) 'x': u64 +[90; 92) 'id': fn([unknown]) -> u64 +[90; 95) 'id(1)': u64 +[93; 94) '1': [unknown] diff --git a/crates/ra_hir/src/ty/tests/data/generic_chain.txt b/crates/ra_hir/src/ty/tests/data/generic_chain.txt new file mode 100644 index 000000000..720609153 --- /dev/null +++ b/crates/ra_hir/src/ty/tests/data/generic_chain.txt @@ -0,0 +1,29 @@ +[53; 57) 'self': A +[65; 87) '{ ... }': [unknown] +[75; 79) 'self': A +[75; 81) 'self.x': [unknown] +[99; 100) 't': [unknown] +[110; 115) '{ t }': [unknown] +[112; 113) 't': [unknown] +[135; 261) '{ ....x() }': i128 +[146; 147) 'x': [unknown] +[150; 151) '1': [unknown] +[162; 163) 'y': [unknown] +[166; 168) 'id': fn([unknown]) -> [unknown] +[166; 171) 'id(x)': [unknown] +[169; 170) 'x': [unknown] +[182; 183) 'a': A +[186; 200) 'A { x: id(y) }': A +[193; 195) 'id': fn([unknown]) -> [unknown] +[193; 198) 'id(y)': [unknown] +[196; 197) 'y': [unknown] +[211; 212) 'z': [unknown] +[215; 217) 'id': fn([unknown]) -> [unknown] +[215; 222) 'id(a.x)': [unknown] +[218; 219) 'a': A +[218; 221) 'a.x': [unknown] +[233; 234) 'b': A +[237; 247) 'A { x: z }': A +[244; 245) 'z': [unknown] +[254; 255) 'b': A +[254; 259) 'b.x()': i128 diff --git a/crates/ra_hir/src/ty/tests/data/struct_generics.txt b/crates/ra_hir/src/ty/tests/data/struct_generics.txt new file mode 100644 index 000000000..d1026b459 --- /dev/null +++ b/crates/ra_hir/src/ty/tests/data/struct_generics.txt @@ -0,0 +1,15 @@ +[36; 38) 'a1': A +[48; 49) 'i': i32 +[56; 147) '{ ...3.x; }': () +[62; 64) 'a1': A +[62; 66) 'a1.x': [unknown] +[76; 78) 'a2': A +[81; 91) 'A { x: i }': A +[88; 89) 'i': i32 +[97; 99) 'a2': A +[97; 101) 'a2.x': [unknown] +[111; 113) 'a3': A +[116; 134) 'A:: Date: Sat, 12 Jan 2019 21:27:35 +0100 Subject: Implement beginnings of generics - add HIR for generic params - resolve generic params in type paths - add substitions for ADTs - insert type variables for substitutions --- crates/ra_hir/src/ty/method_resolution.rs | 17 +++++++-- .../ra_hir/src/ty/tests/data/function_generics.txt | 20 +++++----- crates/ra_hir/src/ty/tests/data/generic_chain.txt | 44 +++++++++++----------- .../ra_hir/src/ty/tests/data/struct_generics.txt | 22 +++++------ 4 files changed, 57 insertions(+), 46 deletions(-) (limited to 'crates/ra_hir/src/ty') diff --git a/crates/ra_hir/src/ty/method_resolution.rs b/crates/ra_hir/src/ty/method_resolution.rs index b221bd142..53b6d15a8 100644 --- a/crates/ra_hir/src/ty/method_resolution.rs +++ b/crates/ra_hir/src/ty/method_resolution.rs @@ -8,7 +8,11 @@ use rustc_hash::FxHashMap; use ra_db::SourceRootId; -use crate::{HirDatabase, DefId, module_tree::ModuleId, Module, Crate, Name, Function, impl_block::{ImplId, ImplBlock, ImplItem}}; +use crate::{ + HirDatabase, DefId, module_tree::ModuleId, Module, Crate, Name, Function, + impl_block::{ImplId, ImplBlock, ImplItem}, + generics::Generics +}; use super::Ty; /// This is used as a key for indexing impls. @@ -64,8 +68,15 @@ impl CrateImplBlocks { if let Some(_target_trait) = impl_data.target_trait() { // ignore for now } else { - let target_ty = - Ty::from_hir(db, &module, Some(&impl_block), impl_data.target_type()); + // TODO provide generics of impl + let generics = Generics::default(); + let target_ty = Ty::from_hir( + db, + &module, + Some(&impl_block), + &generics, + impl_data.target_type(), + ); if let Some(target_ty_fp) = TyFingerprint::for_impl(&target_ty) { self.impls .entry(target_ty_fp) diff --git a/crates/ra_hir/src/ty/tests/data/function_generics.txt b/crates/ra_hir/src/ty/tests/data/function_generics.txt index 179e136a0..e44d26cfd 100644 --- a/crates/ra_hir/src/ty/tests/data/function_generics.txt +++ b/crates/ra_hir/src/ty/tests/data/function_generics.txt @@ -2,13 +2,13 @@ [21; 26) '{ t }': [unknown] [23; 24) 't': [unknown] [38; 98) '{ ...(1); }': () -[44; 46) 'id': fn([unknown]) -> [unknown] -[44; 52) 'id(1u32)': [unknown] -[47; 51) '1u32': [unknown] -[58; 68) 'id::': fn([unknown]) -> [unknown] -[58; 71) 'id::(1)': [unknown] -[69; 70) '1': [unknown] -[81; 82) 'x': u64 -[90; 92) 'id': fn([unknown]) -> u64 -[90; 95) 'id(1)': u64 -[93; 94) '1': [unknown] +[44; 46) 'id': fn(T) -> T +[44; 52) 'id(1u32)': T +[47; 51) '1u32': u32 +[58; 68) 'id::': fn(T) -> T +[58; 71) 'id::(1)': T +[69; 70) '1': T +[81; 82) 'x': T +[90; 92) 'id': fn(T) -> T +[90; 95) 'id(1)': T +[93; 94) '1': T diff --git a/crates/ra_hir/src/ty/tests/data/generic_chain.txt b/crates/ra_hir/src/ty/tests/data/generic_chain.txt index 720609153..568e00846 100644 --- a/crates/ra_hir/src/ty/tests/data/generic_chain.txt +++ b/crates/ra_hir/src/ty/tests/data/generic_chain.txt @@ -1,29 +1,29 @@ -[53; 57) 'self': A +[53; 57) 'self': A<[unknown]> [65; 87) '{ ... }': [unknown] -[75; 79) 'self': A +[75; 79) 'self': A<[unknown]> [75; 81) 'self.x': [unknown] [99; 100) 't': [unknown] [110; 115) '{ t }': [unknown] [112; 113) 't': [unknown] [135; 261) '{ ....x() }': i128 -[146; 147) 'x': [unknown] -[150; 151) '1': [unknown] -[162; 163) 'y': [unknown] -[166; 168) 'id': fn([unknown]) -> [unknown] -[166; 171) 'id(x)': [unknown] -[169; 170) 'x': [unknown] -[182; 183) 'a': A -[186; 200) 'A { x: id(y) }': A -[193; 195) 'id': fn([unknown]) -> [unknown] -[193; 198) 'id(y)': [unknown] -[196; 197) 'y': [unknown] -[211; 212) 'z': [unknown] -[215; 217) 'id': fn([unknown]) -> [unknown] -[215; 222) 'id(a.x)': [unknown] -[218; 219) 'a': A -[218; 221) 'a.x': [unknown] -[233; 234) 'b': A -[237; 247) 'A { x: z }': A -[244; 245) 'z': [unknown] -[254; 255) 'b': A +[146; 147) 'x': T +[150; 151) '1': T +[162; 163) 'y': T +[166; 168) 'id': fn(T) -> T +[166; 171) 'id(x)': T +[169; 170) 'x': T +[182; 183) 'a': A +[186; 200) 'A { x: id(y) }': A +[193; 195) 'id': fn(T) -> T +[193; 198) 'id(y)': T +[196; 197) 'y': T +[211; 212) 'z': T +[215; 217) 'id': fn(T) -> T +[215; 222) 'id(a.x)': T +[218; 219) 'a': A +[218; 221) 'a.x': T +[233; 234) 'b': A +[237; 247) 'A { x: z }': A +[244; 245) 'z': T +[254; 255) 'b': A [254; 259) 'b.x()': i128 diff --git a/crates/ra_hir/src/ty/tests/data/struct_generics.txt b/crates/ra_hir/src/ty/tests/data/struct_generics.txt index d1026b459..88cd2b409 100644 --- a/crates/ra_hir/src/ty/tests/data/struct_generics.txt +++ b/crates/ra_hir/src/ty/tests/data/struct_generics.txt @@ -1,15 +1,15 @@ -[36; 38) 'a1': A +[36; 38) 'a1': A<[unknown]> [48; 49) 'i': i32 [56; 147) '{ ...3.x; }': () -[62; 64) 'a1': A +[62; 64) 'a1': A<[unknown]> [62; 66) 'a1.x': [unknown] -[76; 78) 'a2': A -[81; 91) 'A { x: i }': A +[76; 78) 'a2': A +[81; 91) 'A { x: i }': A [88; 89) 'i': i32 -[97; 99) 'a2': A -[97; 101) 'a2.x': [unknown] -[111; 113) 'a3': A -[116; 134) 'A:: +[97; 101) 'a2.x': i32 +[111; 113) 'a3': A +[116; 134) 'A:: +[131; 132) '1': i32 +[140; 142) 'a3': A +[140; 144) 'a3.x': i32 -- cgit v1.2.3 From cc4562ab6ecfced33f5d7a2e428c8caca8f9c3f1 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 13 Jan 2019 21:00:31 +0100 Subject: Collect generic args in type paths E.g. `let x: A` is handled correctly. --- crates/ra_hir/src/ty/tests/data/struct_generics.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'crates/ra_hir/src/ty') diff --git a/crates/ra_hir/src/ty/tests/data/struct_generics.txt b/crates/ra_hir/src/ty/tests/data/struct_generics.txt index 88cd2b409..c6be2a5f5 100644 --- a/crates/ra_hir/src/ty/tests/data/struct_generics.txt +++ b/crates/ra_hir/src/ty/tests/data/struct_generics.txt @@ -1,8 +1,8 @@ -[36; 38) 'a1': A<[unknown]> +[36; 38) 'a1': A [48; 49) 'i': i32 [56; 147) '{ ...3.x; }': () -[62; 64) 'a1': A<[unknown]> -[62; 66) 'a1.x': [unknown] +[62; 64) 'a1': A +[62; 66) 'a1.x': u32 [76; 78) 'a2': A [81; 91) 'A { x: i }': A [88; 89) 'i': i32 -- cgit v1.2.3 From d37bb128effd19e3aec347e3d4f2e27b5cdb9404 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 13 Jan 2019 21:12:36 +0100 Subject: Collect generic args in struct variant paths as well --- crates/ra_hir/src/ty/tests/data/struct_generics.txt | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'crates/ra_hir/src/ty') diff --git a/crates/ra_hir/src/ty/tests/data/struct_generics.txt b/crates/ra_hir/src/ty/tests/data/struct_generics.txt index c6be2a5f5..1024a39a9 100644 --- a/crates/ra_hir/src/ty/tests/data/struct_generics.txt +++ b/crates/ra_hir/src/ty/tests/data/struct_generics.txt @@ -8,8 +8,8 @@ [88; 89) 'i': i32 [97; 99) 'a2': A [97; 101) 'a2.x': i32 -[111; 113) 'a3': A -[116; 134) 'A:: -[131; 132) '1': i32 -[140; 142) 'a3': A -[140; 144) 'a3.x': i32 +[111; 113) 'a3': A +[116; 134) 'A:: +[131; 132) '1': i128 +[140; 142) 'a3': A +[140; 144) 'a3.x': i128 -- cgit v1.2.3 From 9e4b5ecec4fa4f6a20bb4d47f09de602e9c29608 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 19 Jan 2019 15:48:55 +0100 Subject: Make generics work in struct patterns --- crates/ra_hir/src/ty/tests.rs | 26 ++++++++++++++++++++++ .../src/ty/tests/data/generics_in_patterns.txt | 17 ++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 crates/ra_hir/src/ty/tests/data/generics_in_patterns.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 c590a09db..06e32df59 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -438,6 +438,32 @@ fn test(a1: A, i: i32) { ); } +#[test] +fn infer_generics_in_patterns() { + check_inference( + r#" +struct A { + x: T, +} + +enum Option { + Some(T), + None, +} + +fn test(a1: A, o: Option) { + let A { x: x2 } = a1; + let A:: { x: x3 } = A { x: 1 }; + match o { + Option::Some(t) => t, + _ => 1, + }; +} +"#, + "generics_in_patterns.txt", + ); +} + #[test] fn infer_function_generics() { check_inference( diff --git a/crates/ra_hir/src/ty/tests/data/generics_in_patterns.txt b/crates/ra_hir/src/ty/tests/data/generics_in_patterns.txt new file mode 100644 index 000000000..1b01ef19e --- /dev/null +++ b/crates/ra_hir/src/ty/tests/data/generics_in_patterns.txt @@ -0,0 +1,17 @@ +[79; 81) 'a1': A +[91; 92) 'o': Option +[107; 244) '{ ... }; }': () +[117; 128) 'A { x: x2 }': A +[124; 126) 'x2': u32 +[131; 133) 'a1': A +[143; 161) 'A:: +[157; 159) 'x3': i64 +[164; 174) 'A { x: 1 }': A +[171; 172) '1': i64 +[180; 241) 'match ... }': u64 +[186; 187) 'o': Option +[198; 213) 'Option::Some(t)': Option +[211; 212) 't': u64 +[217; 218) 't': u64 +[228; 229) '_': Option +[233; 234) '1': u64 -- cgit v1.2.3 From 969f588025ab2c3224757e282ef78a546c4f56c4 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 19 Jan 2019 18:58:04 +0100 Subject: Generics -> GenericParams --- crates/ra_hir/src/ty/method_resolution.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'crates/ra_hir/src/ty') diff --git a/crates/ra_hir/src/ty/method_resolution.rs b/crates/ra_hir/src/ty/method_resolution.rs index 53b6d15a8..9f65c5fe1 100644 --- a/crates/ra_hir/src/ty/method_resolution.rs +++ b/crates/ra_hir/src/ty/method_resolution.rs @@ -11,7 +11,7 @@ use ra_db::SourceRootId; use crate::{ HirDatabase, DefId, module_tree::ModuleId, Module, Crate, Name, Function, impl_block::{ImplId, ImplBlock, ImplItem}, - generics::Generics + generics::GenericParams }; use super::Ty; @@ -69,7 +69,7 @@ impl CrateImplBlocks { // ignore for now } else { // TODO provide generics of impl - let generics = Generics::default(); + let generics = GenericParams::default(); let target_ty = Ty::from_hir( db, &module, -- cgit v1.2.3