From 4a86ef6f4c6069e94e8ea6ebb2b62206dce31cbf Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Mon, 27 May 2024 10:44:35 +0000 Subject: [PATCH] Allow constraining opaque types during auto trait casting --- .../src/traits/select/confirmation.rs | 2 +- tests/ui/impl-trait/trait_upcasting.rs | 4 +-- tests/ui/impl-trait/trait_upcasting.stderr | 29 ------------------- .../trait_upcasting_reference_mismatch.rs | 18 ++++++++++++ .../trait_upcasting_reference_mismatch.stderr | 9 ++++++ 5 files changed, 30 insertions(+), 32 deletions(-) delete mode 100644 tests/ui/impl-trait/trait_upcasting.stderr create mode 100644 tests/ui/impl-trait/trait_upcasting_reference_mismatch.rs create mode 100644 tests/ui/impl-trait/trait_upcasting_reference_mismatch.stderr diff --git a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs index 84c744be381..f883cd71893 100644 --- a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs +++ b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs @@ -1161,7 +1161,7 @@ fn confirm_builtin_unsize_candidate( 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. diff --git a/tests/ui/impl-trait/trait_upcasting.rs b/tests/ui/impl-trait/trait_upcasting.rs index cb3c17e87b4..ce811004fae 100644 --- a/tests/ui/impl-trait/trait_upcasting.rs +++ b/tests/ui/impl-trait/trait_upcasting.rs @@ -1,5 +1,7 @@ //! Test that we allow unsizing `Trait` to `Trait` and vice versa +//@ check-pass + trait Trait {} impl Trait for U {} @@ -8,7 +10,6 @@ fn hello() -> &'static (dyn Trait + Send) { if false { let x = hello(); let _: &'static dyn Trait<()> = x; - //~^ ERROR: mismatched types } todo!() } @@ -18,7 +19,6 @@ fn bye() -> &'static dyn Trait { let mut x = bye(); let y: &'static (dyn Trait<()> + Send) = &(); x = y; - //~^ ERROR: mismatched types } todo!() } diff --git a/tests/ui/impl-trait/trait_upcasting.stderr b/tests/ui/impl-trait/trait_upcasting.stderr deleted file mode 100644 index 2c1aa4bbf80..00000000000 --- a/tests/ui/impl-trait/trait_upcasting.stderr +++ /dev/null @@ -1,29 +0,0 @@ -error[E0308]: mismatched types - --> $DIR/trait_upcasting.rs:10:41 - | -LL | fn hello() -> &'static (dyn Trait + Send) { - | ---------- the found opaque type -... -LL | let _: &'static dyn Trait<()> = x; - | ---------------------- ^ expected trait `Trait<()>`, found trait `Trait + Send` - | | - | expected due to this - | - = note: expected reference `&'static (dyn Trait<()> + 'static)` - found reference `&dyn Trait + Send` - -error[E0308]: mismatched types - --> $DIR/trait_upcasting.rs:20:13 - | -LL | fn bye() -> &'static dyn Trait { - | ---------- the expected opaque type -... -LL | x = y; - | ^ expected trait `Trait`, found trait `Trait<()> + Send` - | - = note: expected reference `&dyn Trait` - found reference `&'static (dyn Trait<()> + Send + 'static)` - -error: aborting due to 2 previous errors - -For more information about this error, try `rustc --explain E0308`. diff --git a/tests/ui/impl-trait/trait_upcasting_reference_mismatch.rs b/tests/ui/impl-trait/trait_upcasting_reference_mismatch.rs new file mode 100644 index 00000000000..bed88db1acc --- /dev/null +++ b/tests/ui/impl-trait/trait_upcasting_reference_mismatch.rs @@ -0,0 +1,18 @@ +//! Show an uninformative diagnostic that we could possibly improve in the future + +trait Trait {} + +impl Trait for U {} + +fn hello() -> &'static (dyn Trait + 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() {} diff --git a/tests/ui/impl-trait/trait_upcasting_reference_mismatch.stderr b/tests/ui/impl-trait/trait_upcasting_reference_mismatch.stderr new file mode 100644 index 00000000000..92da47b08e9 --- /dev/null +++ b/tests/ui/impl-trait/trait_upcasting_reference_mismatch.stderr @@ -0,0 +1,9 @@ +error[E0282]: type annotations needed + --> $DIR/trait_upcasting_reference_mismatch.rs:7:35 + | +LL | fn hello() -> &'static (dyn Trait + Send) { + | ^^^^^^^^^^ cannot infer type + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0282`.