Skip to content

Commit e5f0ef6

Browse files
committed
Rust: Add more type inference tests
1 parent a791640 commit e5f0ef6

File tree

2 files changed

+2818
-2484
lines changed

2 files changed

+2818
-2484
lines changed

rust/ql/test/library-tests/type-inference/main.rs

Lines changed: 81 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -406,12 +406,12 @@ mod impl_overlap {
406406
impl OverlappingTrait for S1 {
407407
// <S1_as_OverlappingTrait>::common_method
408408
fn common_method(self) -> S1 {
409-
panic!("not called");
409+
S1
410410
}
411411

412412
// <S1_as_OverlappingTrait>::common_method_2
413413
fn common_method_2(self, s1: S1) -> S1 {
414-
panic!("not called");
414+
S1
415415
}
416416
}
417417

@@ -427,10 +427,78 @@ mod impl_overlap {
427427
}
428428
}
429429

430+
struct S2<T2>(T2);
431+
432+
impl S2<i32> {
433+
// S2<i32>::common_method
434+
fn common_method(self) -> S1 {
435+
S1
436+
}
437+
438+
// S2<i32>::common_method
439+
fn common_method_2(self) -> S1 {
440+
S1
441+
}
442+
}
443+
444+
impl OverlappingTrait for S2<i32> {
445+
// <S2<i32>_as_OverlappingTrait>::common_method
446+
fn common_method(self) -> S1 {
447+
S1
448+
}
449+
450+
// <S2<i32>_as_OverlappingTrait>::common_method_2
451+
fn common_method_2(self, s1: S1) -> S1 {
452+
S1
453+
}
454+
}
455+
456+
impl OverlappingTrait for S2<S1> {
457+
// <S2<S1>_as_OverlappingTrait>::common_method
458+
fn common_method(self) -> S1 {
459+
S1
460+
}
461+
462+
// <S2<S1>_as_OverlappingTrait>::common_method_2
463+
fn common_method_2(self, s1: S1) -> S1 {
464+
S1
465+
}
466+
}
467+
468+
#[derive(Debug)]
469+
struct S3<T3>(T3);
470+
471+
trait OverlappingTrait2<T> {
472+
fn m(&self, x: &T) -> &Self;
473+
}
474+
475+
impl<T> OverlappingTrait2<T> for S3<T> {
476+
// <S3<T>_as_OverlappingTrait2<T>>::m
477+
fn m(&self, x: &T) -> &Self {
478+
self
479+
}
480+
}
481+
482+
impl<T> S3<T> {
483+
// S3<T>::m
484+
fn m(&self, x: T) -> &Self {
485+
self
486+
}
487+
}
488+
430489
pub fn f() {
431490
let x = S1;
432491
println!("{:?}", x.common_method()); // $ method=S1::common_method
433492
println!("{:?}", x.common_method_2()); // $ method=S1::common_method_2
493+
494+
let y = S2(S1);
495+
println!("{:?}", y.common_method()); // $ method=<S2<S1>_as_OverlappingTrait>::common_method
496+
497+
let z = S2(0);
498+
println!("{:?}", z.common_method()); // $ method=S2<i32>::common_method
499+
500+
let w = S3(S1);
501+
println!("{:?}", w.m(x)); // $ method=S3<T>::m
434502
}
435503
}
436504

@@ -1959,22 +2027,25 @@ mod loops {
19592027
for s in &mut strings1 {} // $ MISSING: type=s:&T.str
19602028
for s in strings1 {} // $ type=s:str
19612029

1962-
let strings2 = [ // $ type=strings2:[T;...].String
2030+
let strings2 = // $ type=strings2:[T;...].String
2031+
[
19632032
String::from("foo"),
19642033
String::from("bar"),
19652034
String::from("baz"),
19662035
];
19672036
for s in strings2 {} // $ type=s:String
19682037

1969-
let strings3 = &[ // $ type=strings3:&T.[T;...].String
2038+
let strings3 = // $ type=strings3:&T.[T;...].String
2039+
&[
19702040
String::from("foo"),
19712041
String::from("bar"),
19722042
String::from("baz"),
19732043
];
19742044
for s in strings3 {} // $ MISSING: type=s:String
19752045

19762046
let callables = [MyCallable::new(), MyCallable::new(), MyCallable::new()]; // $ MISSING: type=callables:[T;...].MyCallable; 3
1977-
for c in callables // $ type=c:MyCallable
2047+
for c // $ type=c:MyCallable
2048+
in callables
19782049
{
19792050
let result = c.call(); // $ type=result:i64 method=call
19802051
}
@@ -1986,7 +2057,8 @@ mod loops {
19862057
let range = 0..10; // $ MISSING: type=range:Range type=range:Idx.i32
19872058
for i in range {} // $ MISSING: type=i:i32
19882059

1989-
let range1 = std::ops::Range { // $ type=range1:Range type=range1:Idx.u16
2060+
let range1 = // $ type=range1:Range type=range1:Idx.u16
2061+
std::ops::Range {
19902062
start: 0u16,
19912063
end: 10u16,
19922064
};
@@ -2031,10 +2103,11 @@ mod loops {
20312103
// while loops
20322104

20332105
let mut a: i64 = 0; // $ type=a:i64
2034-
while a < 10 // $ method=lt type=a:i64
2106+
#[rustfmt::skip]
2107+
let _ = while a < 10 // $ method=lt type=a:i64
20352108
{
20362109
a += 1; // $ type=a:i64 method=add_assign
2037-
}
2110+
};
20382111
}
20392112
}
20402113

0 commit comments

Comments
 (0)