Skip to content

Commit c2257b9

Browse files
authored
Rollup merge of #113444 - lcnr:alias-bound-test, r=compiler-errors
add tests for alias bound preference cc rust-lang/trait-system-refactor-initiative#45 r? ``@compiler-errors``
2 parents 77e24f9 + 662e9d0 commit c2257b9

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// revisions: old next
2+
//[next] compile-flags: -Ztrait-solver=next
3+
// run-pass
4+
5+
// A test for https://github.com/rust-lang/trait-system-refactor-initiative/issues/45.
6+
7+
trait Trait {
8+
type Assoc: Into<u32>;
9+
}
10+
impl<T: Into<u32>> Trait for T {
11+
type Assoc = T;
12+
}
13+
fn prefer_alias_bound_projection<T: Trait>(x: T::Assoc) {
14+
// There are two possible types for `x`:
15+
// - `u32` by using the "alias bound" of `<T as Trait>::Assoc`
16+
// - `<T as Trait>::Assoc`, i.e. `u16`, by using `impl<T> From<T> for T`
17+
//
18+
// We infer the type of `x` to be `u32` here as it is highly likely
19+
// that this is expected by the user.
20+
let x = x.into();
21+
assert_eq!(std::mem::size_of_val(&x), 4);
22+
}
23+
24+
fn impl_trait() -> impl Into<u32> {
25+
0u16
26+
}
27+
28+
fn main() {
29+
// There are two possible types for `x`:
30+
// - `u32` by using the "alias bound" of `impl Into<u32>`
31+
// - `impl Into<u32>`, i.e. `u16`, by using `impl<T> From<T> for T`
32+
//
33+
// We infer the type of `x` to be `u32` here as it is highly likely
34+
// that this is expected by the user.
35+
let x = impl_trait().into();
36+
assert_eq!(std::mem::size_of_val(&x), 4);
37+
38+
prefer_alias_bound_projection::<u16>(1);
39+
}

0 commit comments

Comments
 (0)