Skip to content

Commit e7027f0

Browse files
committed
Rust: Add type inference tests for type aliases
1 parent 04d37c3 commit e7027f0

File tree

2 files changed

+423
-374
lines changed

2 files changed

+423
-374
lines changed

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

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,17 @@ mod type_aliases {
540540
PairBoth(Fst, Snd),
541541
}
542542

543+
impl<Fst, Snd> PairOption<Fst, Snd> {
544+
fn unwrapSnd(self) -> Snd {
545+
match self {
546+
PairOption::PairNone() => panic!("PairNone has no second element"),
547+
PairOption::PairFst(_) => panic!("PairFst has no second element"),
548+
PairOption::PairSnd(snd) => snd,
549+
PairOption::PairBoth(_, snd) => snd,
550+
}
551+
}
552+
}
553+
543554
#[derive(Debug)]
544555
struct S1;
545556

@@ -553,24 +564,37 @@ mod type_aliases {
553564
type MyPair = PairOption<S1, S2>;
554565

555566
// Generic type alias that partially applies the generic type
556-
type AnotherPair<Thr> = PairOption<S2, Thr>;
567+
type AnotherPair<A3> = PairOption<S2, A3>;
568+
569+
// Alias to another alias
570+
type AliasToAlias<A4> = AnotherPair<A4>;
571+
572+
// Alias that appears nested withing another alias
573+
type NestedAlias<A5> = AnotherPair<AliasToAlias<A5>>;
574+
575+
fn g(t: NestedAlias<S3>) {
576+
let x = t.unwrapSnd().unwrapSnd(); // $ method=unwrapSnd MISSING: type=x:S3
577+
println!("{:?}", x);
578+
}
557579

558580
pub fn f() {
559581
// Type can be inferred from the constructor
560582
let p1: MyPair = PairOption::PairBoth(S1, S2);
561583
println!("{:?}", p1);
562584

563585
// Type can be only inferred from the type alias
564-
let p2: MyPair = PairOption::PairNone(); // types for `Fst` and `Snd` missing
586+
let p2: MyPair = PairOption::PairNone(); // $ MISSING: type=p2:Fst.S1 MISSING: type=p2:Snd.S2
565587
println!("{:?}", p2);
566588

567589
// First type from alias, second from constructor
568-
let p3: AnotherPair<_> = PairOption::PairSnd(S3); // type for `Fst` missing
590+
let p3: AnotherPair<_> = PairOption::PairSnd(S3); // $ MISSING: type=p3:Fst.S2
569591
println!("{:?}", p3);
570592

571593
// First type from alias definition, second from argument to alias
572-
let p3: AnotherPair<S3> = PairOption::PairNone(); // type for `Snd` missing, spurious `S3` for `Fst`
594+
let p3: AnotherPair<S3> = PairOption::PairNone(); // $ SPURIOUS: type=p3:Fst.S3 MISSING: type=p3:Snd.S3
573595
println!("{:?}", p3);
596+
597+
g(PairOption::PairSnd(PairOption::PairSnd(S3)));
574598
}
575599
}
576600

0 commit comments

Comments
 (0)