Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ multipleCallTargets
| dereference.rs:184:17:184:30 | ... .foo() |
| dereference.rs:186:17:186:25 | S.bar(...) |
| dereference.rs:187:17:187:29 | S.bar(...) |
| main.rs:2437:13:2437:31 | ...::from(...) |
| main.rs:2438:13:2438:31 | ...::from(...) |
| main.rs:2439:13:2439:31 | ...::from(...) |
| main.rs:2445:13:2445:31 | ...::from(...) |
| main.rs:2446:13:2446:31 | ...::from(...) |
| main.rs:2447:13:2447:31 | ...::from(...) |
| main.rs:2482:13:2482:31 | ...::from(...) |
| main.rs:2483:13:2483:31 | ...::from(...) |
| main.rs:2484:13:2484:31 | ...::from(...) |
| main.rs:2490:13:2490:31 | ...::from(...) |
| main.rs:2491:13:2491:31 | ...::from(...) |
| main.rs:2492:13:2492:31 | ...::from(...) |
46 changes: 46 additions & 0 deletions rust/ql/test/library-tests/type-inference/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,51 @@ mod type_parameter_bounds {
}
}

mod trait_default_self_type_parameter {
// A trait with a type parameter that defaults to `Self`.
// trait TraitWithSelfTp<A = Self> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove

trait TraitWithSelfTp<A = Option<Self>> {
// TraitWithSelfTp::get_a
fn get_a(&self) -> A;
}

fn get_a<A, T: TraitWithSelfTp<A>>(thing: &T) -> A {
thing.get_a() // $ target=TraitWithSelfTp::get_a
}

// The trait bound on `T` uses the default for `A` which contains `Self`
fn tp_uses_default<S: TraitWithSelfTp>(thing: S) -> i64 {
let _ms = thing.get_a(); // $ target=TraitWithSelfTp::get_a MISSING: type=_ms:T.S
0
}

// The supertrait uses the default for `A` which contains `Self`
trait SubTraitOfTraitWithSelfTp: TraitWithSelfTp + Sized {}

fn get_a_through_tp<S: SubTraitOfTraitWithSelfTp>(thing: &S) {
// `thing` is a `TraitWithSelfTp` through the trait hierarchy
let _ms = get_a(thing); // $ target=get_a MISSING: type=_ms:T.S
}

struct MyStruct {
value: i32,
}

// The implementing trait uses the default for `A` which contains `Self`
impl TraitWithSelfTp for MyStruct {
fn get_a(&self) -> Option<Self> {
Some(MyStruct { value: self.value }) // $ fieldof=MyStruct
}
}

impl SubTraitOfTraitWithSelfTp for MyStruct {}

pub fn test() {
let s = MyStruct { value: 0 };
let _ms = get_a(&s); // $ target=get_a MISSING: type=_ms:T.MyStruct
}
}

mod function_trait_bounds {
#[derive(Debug, Clone, Copy)]
struct MyThing<T> {
Expand Down Expand Up @@ -2753,6 +2798,7 @@ fn main() {
method_impl::g(method_impl::Foo {}, method_impl::Foo {}); // $ target=g
method_non_parametric_impl::f(); // $ target=f
method_non_parametric_trait_impl::f(); // $ target=f
trait_default_self_type_parameter::test(); // $ target=test
function_trait_bounds::f(); // $ target=f
associated_type_in_trait::f(); // $ target=f
generic_enum::f(); // $ target=f
Expand Down
Loading