aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r--crates/ra_hir/src/ty/tests.rs63
-rw-r--r--crates/ra_hir/src/ty/tests/data/function_generics.txt14
-rw-r--r--crates/ra_hir/src/ty/tests/data/generic_chain.txt29
-rw-r--r--crates/ra_hir/src/ty/tests/data/struct_generics.txt15
4 files changed, 121 insertions, 0 deletions
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() {
418 ); 418 );
419} 419}
420 420
421#[test]
422fn infer_struct_generics() {
423 check_inference(
424 r#"
425struct A<T> {
426 x: T,
427}
428
429fn test(a1: A<u32>, i: i32) {
430 a1.x;
431 let a2 = A { x: i };
432 a2.x;
433 let a3 = A::<i128> { x: 1 };
434 a3.x;
435}
436"#,
437 "struct_generics.txt",
438 );
439}
440
441#[test]
442fn infer_function_generics() {
443 check_inference(
444 r#"
445fn id<T>(t: T) -> T { t }
446
447fn test() {
448 id(1u32);
449 id::<i128>(1);
450 let x: u64 = id(1);
451}
452"#,
453 "function_generics.txt",
454 );
455}
456
457#[test]
458fn infer_generic_chain() {
459 check_inference(
460 r#"
461struct A<T> {
462 x: T,
463}
464impl<T2> A<T2> {
465 fn x(self) -> T2 {
466 self.x
467 }
468}
469fn id<T>(t: T) -> T { t }
470
471fn test() -> i128 {
472 let x = 1;
473 let y = id(x);
474 let a = A { x: id(y) };
475 let z = id(a.x);
476 let b = A { x: z };
477 b.x()
478}
479"#,
480 "generic_chain.txt",
481 );
482}
483
421fn infer(content: &str) -> String { 484fn infer(content: &str) -> String {
422 let (db, _, file_id) = MockDatabase::with_single_file(content); 485 let (db, _, file_id) = MockDatabase::with_single_file(content);
423 let source_file = db.source_file(file_id); 486 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 @@
1[10; 11) 't': [unknown]
2[21; 26) '{ t }': [unknown]
3[23; 24) 't': [unknown]
4[38; 98) '{ ...(1); }': ()
5[44; 46) 'id': fn([unknown]) -> [unknown]
6[44; 52) 'id(1u32)': [unknown]
7[47; 51) '1u32': [unknown]
8[58; 68) 'id::<i128>': fn([unknown]) -> [unknown]
9[58; 71) 'id::<i128>(1)': [unknown]
10[69; 70) '1': [unknown]
11[81; 82) 'x': u64
12[90; 92) 'id': fn([unknown]) -> u64
13[90; 95) 'id(1)': u64
14[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 @@
1[53; 57) 'self': A
2[65; 87) '{ ... }': [unknown]
3[75; 79) 'self': A
4[75; 81) 'self.x': [unknown]
5[99; 100) 't': [unknown]
6[110; 115) '{ t }': [unknown]
7[112; 113) 't': [unknown]
8[135; 261) '{ ....x() }': i128
9[146; 147) 'x': [unknown]
10[150; 151) '1': [unknown]
11[162; 163) 'y': [unknown]
12[166; 168) 'id': fn([unknown]) -> [unknown]
13[166; 171) 'id(x)': [unknown]
14[169; 170) 'x': [unknown]
15[182; 183) 'a': A
16[186; 200) 'A { x: id(y) }': A
17[193; 195) 'id': fn([unknown]) -> [unknown]
18[193; 198) 'id(y)': [unknown]
19[196; 197) 'y': [unknown]
20[211; 212) 'z': [unknown]
21[215; 217) 'id': fn([unknown]) -> [unknown]
22[215; 222) 'id(a.x)': [unknown]
23[218; 219) 'a': A
24[218; 221) 'a.x': [unknown]
25[233; 234) 'b': A
26[237; 247) 'A { x: z }': A
27[244; 245) 'z': [unknown]
28[254; 255) 'b': A
29[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 @@
1[36; 38) 'a1': A
2[48; 49) 'i': i32
3[56; 147) '{ ...3.x; }': ()
4[62; 64) 'a1': A
5[62; 66) 'a1.x': [unknown]
6[76; 78) 'a2': A
7[81; 91) 'A { x: i }': A
8[88; 89) 'i': i32
9[97; 99) 'a2': A
10[97; 101) 'a2.x': [unknown]
11[111; 113) 'a3': A
12[116; 134) 'A::<i1...x: 1 }': A
13[131; 132) '1': [unknown]
14[140; 142) 'a3': A
15[140; 144) 'a3.x': [unknown]