mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-22 06:44:35 +00:00
Allow constraining opaque types during auto trait casting
This commit is contained in:
parent
cbadf786bc
commit
4a86ef6f4c
@ -1161,7 +1161,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.
|
||||
|
@ -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 {}
|
||||
@ -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!()
|
||||
}
|
||||
@ -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!()
|
||||
}
|
||||
|
@ -1,29 +0,0 @@
|
||||
error[E0308]: mismatched types
|
||||
--> $DIR/trait_upcasting.rs:10:41
|
||||
|
|
||||
LL | fn hello() -> &'static (dyn Trait<impl Sized> + Send) {
|
||||
| ---------- the found opaque type
|
||||
...
|
||||
LL | let _: &'static dyn Trait<()> = x;
|
||||
| ---------------------- ^ expected trait `Trait<()>`, found trait `Trait<impl Sized> + Send`
|
||||
| |
|
||||
| expected due to this
|
||||
|
|
||||
= note: expected reference `&'static (dyn Trait<()> + 'static)`
|
||||
found reference `&dyn Trait<impl Sized> + Send`
|
||||
|
||||
error[E0308]: mismatched types
|
||||
--> $DIR/trait_upcasting.rs:20:13
|
||||
|
|
||||
LL | fn bye() -> &'static dyn Trait<impl Sized> {
|
||||
| ---------- the expected opaque type
|
||||
...
|
||||
LL | x = y;
|
||||
| ^ expected trait `Trait<impl Sized>`, found trait `Trait<()> + Send`
|
||||
|
|
||||
= note: expected reference `&dyn Trait<impl Sized>`
|
||||
found reference `&'static (dyn Trait<()> + Send + 'static)`
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
||||
For more information about this error, try `rustc --explain E0308`.
|
18
tests/ui/impl-trait/trait_upcasting_reference_mismatch.rs
Normal file
18
tests/ui/impl-trait/trait_upcasting_reference_mismatch.rs
Normal file
@ -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() {}
|
@ -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`.
|
Loading…
Reference in New Issue
Block a user