about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDylan DPC <99973273+Dylan-DPC@users.noreply.github.com>2023-07-19 22:37:06 +0530
committerGitHub <noreply@github.com>2023-07-19 22:37:06 +0530
commitc2257b94129ff39129f8f684244eaa961b720799 (patch)
tree3f78dd54838e4358b5da2201eb6e829a87954f1c
parent77e24f90f599070af2d8051ef9adad7fe528dd78 (diff)
parent662e9d00b0ea2137366374e9ac1fdd6ea568a903 (diff)
downloadrust-c2257b94129ff39129f8f684244eaa961b720799.tar.gz
rust-c2257b94129ff39129f8f684244eaa961b720799.zip
Rollup merge of #113444 - lcnr:alias-bound-test, r=compiler-errors
add tests for alias bound preference

cc https://github.com/rust-lang/trait-system-refactor-initiative/issues/45

r? ``@compiler-errors``
-rw-r--r--tests/ui/traits/new-solver/alias-bound-preference.rs39
1 files changed, 39 insertions, 0 deletions
diff --git a/tests/ui/traits/new-solver/alias-bound-preference.rs b/tests/ui/traits/new-solver/alias-bound-preference.rs
new file mode 100644
index 00000000000..e4e0f634ef7
--- /dev/null
+++ b/tests/ui/traits/new-solver/alias-bound-preference.rs
@@ -0,0 +1,39 @@
+// revisions: old next
+//[next] compile-flags: -Ztrait-solver=next
+// run-pass
+
+// A test for https://github.com/rust-lang/trait-system-refactor-initiative/issues/45.
+
+trait Trait {
+    type Assoc: Into<u32>;
+}
+impl<T: Into<u32>> Trait for T {
+    type Assoc = T;
+}
+fn prefer_alias_bound_projection<T: Trait>(x: T::Assoc) {
+    // There are two possible types for `x`:
+    // - `u32` by using the "alias bound" of `<T as Trait>::Assoc`
+    // - `<T as Trait>::Assoc`, i.e. `u16`, by using `impl<T> From<T> for T`
+    //
+    // We infer the type of `x` to be `u32` here as it is highly likely
+    // that this is expected by the user.
+    let x = x.into();
+    assert_eq!(std::mem::size_of_val(&x), 4);
+}
+
+fn impl_trait() -> impl Into<u32> {
+    0u16
+}
+
+fn main() {
+    // There are two possible types for `x`:
+    // - `u32` by using the "alias bound" of `impl Into<u32>`
+    // - `impl Into<u32>`, i.e. `u16`, by using `impl<T> From<T> for T`
+    //
+    // We infer the type of `x` to be `u32` here as it is highly likely
+    // that this is expected by the user.
+    let x = impl_trait().into();
+    assert_eq!(std::mem::size_of_val(&x), 4);
+
+    prefer_alias_bound_projection::<u16>(1);
+}