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::