Skip to content

Commit 2907ae6

Browse files
committed
Rust: Add type inference tests involving Self
1 parent f2380d3 commit 2907ae6

File tree

3 files changed

+3881
-3790
lines changed

3 files changed

+3881
-3790
lines changed

rust/ql/test/library-tests/type-inference/CONSISTENCY/PathResolutionConsistency.expected

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ multipleCallTargets
55
| dereference.rs:184:17:184:30 | ... .foo() |
66
| dereference.rs:186:17:186:25 | S.bar(...) |
77
| dereference.rs:187:17:187:29 | S.bar(...) |
8-
| main.rs:2437:13:2437:31 | ...::from(...) |
9-
| main.rs:2438:13:2438:31 | ...::from(...) |
10-
| main.rs:2439:13:2439:31 | ...::from(...) |
11-
| main.rs:2445:13:2445:31 | ...::from(...) |
12-
| main.rs:2446:13:2446:31 | ...::from(...) |
13-
| main.rs:2447:13:2447:31 | ...::from(...) |
8+
| main.rs:2478:13:2478:31 | ...::from(...) |
9+
| main.rs:2479:13:2479:31 | ...::from(...) |
10+
| main.rs:2480:13:2480:31 | ...::from(...) |
11+
| main.rs:2486:13:2486:31 | ...::from(...) |
12+
| main.rs:2487:13:2487:31 | ...::from(...) |
13+
| main.rs:2488:13:2488:31 | ...::from(...) |

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,51 @@ mod type_parameter_bounds {
652652
}
653653
}
654654

655+
mod trait_default_self_type_parameter {
656+
// A trait with a type parameter that defaults to `Self`.
657+
// trait TraitWithSelfTp<A = Self> {
658+
trait TraitWithSelfTp<A = Option<Self>> {
659+
// TraitWithSelfTp::get_a
660+
fn get_a(&self) -> A;
661+
}
662+
663+
fn get_a<A, T: TraitWithSelfTp<A>>(thing: &T) -> A {
664+
thing.get_a() // $ target=TraitWithSelfTp::get_a
665+
}
666+
667+
// The trait bound on `T` uses the default for `A` which contains `Self`
668+
fn tp_uses_default<S: TraitWithSelfTp>(thing: S) -> i64 {
669+
let _ms = thing.get_a(); // $ target=TraitWithSelfTp::get_a MISSING: type=_ms:T.S
670+
0
671+
}
672+
673+
// The supertrait uses the default for `A` which contains `Self`
674+
trait SubTraitOfTraitWithSelfTp: TraitWithSelfTp + Sized {}
675+
676+
fn get_a_through_tp<S: SubTraitOfTraitWithSelfTp>(thing: &S) {
677+
// `thing` is a `TraitWithSelfTp` through the trait hierarchy
678+
let _ms = get_a(thing); // $ target=get_a MISSING: type=_ms:T.S
679+
}
680+
681+
struct MyStruct {
682+
value: i32,
683+
}
684+
685+
// The implementing trait uses the default for `A` which contains `Self`
686+
impl TraitWithSelfTp for MyStruct {
687+
fn get_a(&self) -> Option<Self> {
688+
Some(MyStruct { value: self.value }) // $ fieldof=MyStruct
689+
}
690+
}
691+
692+
impl SubTraitOfTraitWithSelfTp for MyStruct {}
693+
694+
pub fn test() {
695+
let s = MyStruct { value: 0 };
696+
let _ms = get_a(&s); // $ target=get_a MISSING: type=_ms:T.MyStruct
697+
}
698+
}
699+
655700
mod function_trait_bounds {
656701
#[derive(Debug, Clone, Copy)]
657702
struct MyThing<T> {
@@ -2753,6 +2798,7 @@ fn main() {
27532798
method_impl::g(method_impl::Foo {}, method_impl::Foo {}); // $ target=g
27542799
method_non_parametric_impl::f(); // $ target=f
27552800
method_non_parametric_trait_impl::f(); // $ target=f
2801+
trait_default_self_type_parameter::test(); // $ target=test
27562802
function_trait_bounds::f(); // $ target=f
27572803
associated_type_in_trait::f(); // $ target=f
27582804
generic_enum::f(); // $ target=f

0 commit comments

Comments
 (0)