Skip to content

Commit 4a86ef6

Browse files
committed
Allow constraining opaque types during auto trait casting
1 parent cbadf78 commit 4a86ef6

File tree

5 files changed

+30
-32
lines changed

5 files changed

+30
-32
lines changed

compiler/rustc_trait_selection/src/traits/select/confirmation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1161,7 +1161,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
11611161
let InferOk { mut obligations, .. } = self
11621162
.infcx
11631163
.at(&obligation.cause, obligation.param_env)
1164-
.sup(DefineOpaqueTypes::No, target, source_trait)
1164+
.sup(DefineOpaqueTypes::Yes, target, source_trait)
11651165
.map_err(|_| Unimplemented)?;
11661166

11671167
// Register one obligation for 'a: 'b.

tests/ui/impl-trait/trait_upcasting.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Test that we allow unsizing `Trait<Concrete>` to `Trait<Opaque>` and vice versa
22
3+
//@ check-pass
4+
35
trait Trait<T> {}
46

57
impl<T, U> Trait<T> for U {}
@@ -8,7 +10,6 @@ fn hello() -> &'static (dyn Trait<impl Sized> + Send) {
810
if false {
911
let x = hello();
1012
let _: &'static dyn Trait<()> = x;
11-
//~^ ERROR: mismatched types
1213
}
1314
todo!()
1415
}
@@ -18,7 +19,6 @@ fn bye() -> &'static dyn Trait<impl Sized> {
1819
let mut x = bye();
1920
let y: &'static (dyn Trait<()> + Send) = &();
2021
x = y;
21-
//~^ ERROR: mismatched types
2222
}
2323
todo!()
2424
}

tests/ui/impl-trait/trait_upcasting.stderr

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//! Show an uninformative diagnostic that we could possibly improve in the future
2+
3+
trait Trait<T> {}
4+
5+
impl<T, U> Trait<T> for U {}
6+
7+
fn hello() -> &'static (dyn Trait<impl Sized> + Send) {
8+
//~^ ERROR: type annotations needed
9+
if false {
10+
let x = hello();
11+
let _: &'static dyn Trait<()> = &x;
12+
//^ Note the extra `&`, paired with the blanket impl causing
13+
// `impl Sized` to never get a hidden type registered.
14+
}
15+
todo!()
16+
}
17+
18+
fn main() {}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0282]: type annotations needed
2+
--> $DIR/trait_upcasting_reference_mismatch.rs:7:35
3+
|
4+
LL | fn hello() -> &'static (dyn Trait<impl Sized> + Send) {
5+
| ^^^^^^^^^^ cannot infer type
6+
7+
error: aborting due to 1 previous error
8+
9+
For more information about this error, try `rustc --explain E0282`.

0 commit comments

Comments
 (0)