Add some tests

This commit is contained in:
Florian Diebold 2019-01-12 18:47:43 +01:00
parent bade91db08
commit 415cdc5210
4 changed files with 121 additions and 0 deletions

View File

@ -418,6 +418,69 @@ fn test() {
); );
} }
#[test]
fn infer_struct_generics() {
check_inference(
r#"
struct A<T> {
x: T,
}
fn test(a1: A<u32>, i: i32) {
a1.x;
let a2 = A { x: i };
a2.x;
let a3 = A::<i128> { x: 1 };
a3.x;
}
"#,
"struct_generics.txt",
);
}
#[test]
fn infer_function_generics() {
check_inference(
r#"
fn id<T>(t: T) -> T { t }
fn test() {
id(1u32);
id::<i128>(1);
let x: u64 = id(1);
}
"#,
"function_generics.txt",
);
}
#[test]
fn infer_generic_chain() {
check_inference(
r#"
struct A<T> {
x: T,
}
impl<T2> A<T2> {
fn x(self) -> T2 {
self.x
}
}
fn id<T>(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 { fn infer(content: &str) -> String {
let (db, _, file_id) = MockDatabase::with_single_file(content); let (db, _, file_id) = MockDatabase::with_single_file(content);
let source_file = db.source_file(file_id); let source_file = db.source_file(file_id);

View File

@ -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::<i128>': fn([unknown]) -> [unknown]
[58; 71) 'id::<i128>(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]

View File

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

View File

@ -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::<i1...x: 1 }': A
[131; 132) '1': [unknown]
[140; 142) 'a3': A
[140; 144) 'a3.x': [unknown]