Skip to content

Commit

Permalink
Allow constraining opaque types during auto trait casting
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed May 27, 2024
1 parent e6acb0d commit 0a6a35e
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1203,7 +1203,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
let InferOk { mut obligations, .. } = self
.infcx
.at(&obligation.cause, obligation.param_env)
.sup(DefineOpaqueTypes::No, target, source_trait)
.sup(DefineOpaqueTypes::Yes, target, source_trait)
.map_err(|_| Unimplemented)?;

// Register one obligation for 'a: 'b.
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/impl-trait/trait_upcasting.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Test that we allow unsizing `Trait<Concrete>` to `Trait<Opaque>` and vice versa
//@ check-pass

trait Trait<T> {}

impl<T, U> Trait<T> for U {}
Expand All @@ -8,7 +10,6 @@ fn hello() -> &'static (dyn Trait<impl Sized> + Send) {
if false {
let x = hello();
let _: &'static dyn Trait<()> = x;
//~^ ERROR: mismatched types
}
todo!()
}
Expand All @@ -18,7 +19,6 @@ fn bye() -> &'static dyn Trait<impl Sized> {
let mut x = bye();
let y: &'static (dyn Trait<()> + Send) = &();
x = y;
//~^ ERROR: mismatched types
}
todo!()
}
Expand Down
29 changes: 0 additions & 29 deletions tests/ui/impl-trait/trait_upcasting.stderr

This file was deleted.

18 changes: 18 additions & 0 deletions tests/ui/impl-trait/trait_upcasting_reference_mismatch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//! Show an uninformative diagnostic that we could possibly improve in the future
trait Trait<T> {}

impl<T, U> Trait<T> for U {}

fn hello() -> &'static (dyn Trait<impl Sized> + Send) {
//~^ ERROR: type annotations needed
if false {
let x = hello();
let _: &'static dyn Trait<()> = &x;
//^ Note the extra `&`, paired with the blanket impl causing
// `impl Sized` to never get a hidden type registered.
}
todo!()
}

fn main() {}
9 changes: 9 additions & 0 deletions tests/ui/impl-trait/trait_upcasting_reference_mismatch.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0282]: type annotations needed
--> $DIR/trait_upcasting_reference_mismatch.rs:7:35
|
LL | fn hello() -> &'static (dyn Trait<impl Sized> + Send) {
| ^^^^^^^^^^ cannot infer type

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0282`.

0 comments on commit 0a6a35e

Please sign in to comment.