-
Notifications
You must be signed in to change notification settings - Fork 13k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow constraining opaque types during various unsizing casts #125610
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//! 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 {} | ||
|
||
fn hello() -> &'static (dyn Trait<impl Sized> + Send) { | ||
if false { | ||
let x = hello(); | ||
let _: &'static dyn Trait<()> = x; | ||
} | ||
todo!() | ||
} | ||
|
||
fn bye() -> &'static dyn Trait<impl Sized> { | ||
if false { | ||
let mut x = bye(); | ||
let y: &'static (dyn Trait<()> + Send) = &(); | ||
x = y; | ||
} | ||
todo!() | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
9
tests/ui/impl-trait/trait_upcasting_reference_mismatch.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
error[E0271]: type mismatch resolving `impl Trait <: dyn Trait` | ||
--> $DIR/unsized_coercion.rs:14:17 | ||
| | ||
LL | let x = hello(); | ||
| ^^^^^^^ types differ | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/unsized_coercion.rs:18:14 | ||
| | ||
LL | fn hello() -> Box<impl Trait> { | ||
| ---------- the expected opaque type | ||
... | ||
LL | Box::new(1u32) | ||
| -------- ^^^^ types differ | ||
| | | ||
| arguments to this function are incorrect | ||
| | ||
= note: expected opaque type `impl Trait` | ||
found type `u32` | ||
note: associated function defined here | ||
--> $SRC_DIR/alloc/src/boxed.rs:LL:COL | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
Some errors have detailed explanations: E0271, E0308. | ||
For more information about an error, try `rustc --explain E0271`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
//! This test checks that opaque types get unsized instead of | ||
//! constraining their hidden type to a trait object. | ||
|
||
//@ revisions: next old | ||
//@[next] compile-flags: -Znext-solver | ||
//@[old] check-pass | ||
|
||
trait Trait {} | ||
|
||
impl Trait for u32 {} | ||
|
||
fn hello() -> Box<impl Trait> { | ||
if true { | ||
let x = hello(); | ||
//[next]~^ ERROR: type mismatch resolving `impl Trait <: dyn Trait` | ||
let y: Box<dyn Trait> = x; | ||
} | ||
Box::new(1u32) //[next]~ ERROR: mismatched types | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
error[E0277]: the size for values of type `impl Trait + ?Sized` cannot be known at compilation time | ||
--> $DIR/unsized_coercion2.rs:15:33 | ||
| | ||
LL | let y: Box<dyn Trait> = x; | ||
| ^ doesn't have a size known at compile-time | ||
| | ||
= help: the trait `Sized` is not implemented for `impl Trait + ?Sized` | ||
= note: required for the cast from `Box<impl Trait + ?Sized>` to `Box<dyn Trait>` | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0277`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
//! This test checks that opaque types get unsized instead of | ||
//! constraining their hidden type to a trait object. | ||
|
||
//@ revisions: next old | ||
//@[next] compile-flags: -Znext-solver | ||
//@[next] check-pass | ||
|
||
trait Trait {} | ||
|
||
impl Trait for u32 {} | ||
|
||
fn hello() -> Box<impl Trait + ?Sized> { | ||
if true { | ||
let x = hello(); | ||
let y: Box<dyn Trait> = x; | ||
//[old]~^ ERROR: the size for values of type `impl Trait + ?Sized` cannot be know | ||
} | ||
Box::new(1u32) | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
error[E0271]: type mismatch resolving `impl Trait + ?Sized <: dyn Send` | ||
--> $DIR/unsized_coercion3.rs:13:17 | ||
| | ||
LL | let x = hello(); | ||
| ^^^^^^^ types differ | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/unsized_coercion3.rs:19:14 | ||
| | ||
LL | fn hello() -> Box<impl Trait + ?Sized> { | ||
| ------------------- the expected opaque type | ||
... | ||
LL | Box::new(1u32) | ||
| -------- ^^^^ types differ | ||
| | | ||
| arguments to this function are incorrect | ||
| | ||
= note: expected opaque type `impl Trait + ?Sized` | ||
found type `u32` | ||
note: associated function defined here | ||
--> $SRC_DIR/alloc/src/boxed.rs:LL:COL | ||
|
||
error[E0277]: the size for values of type `impl Trait + ?Sized` cannot be known at compilation time | ||
--> $DIR/unsized_coercion3.rs:19:14 | ||
| | ||
LL | Box::new(1u32) | ||
| -------- ^^^^ doesn't have a size known at compile-time | ||
| | | ||
| required by a bound introduced by this call | ||
| | ||
= help: the trait `Sized` is not implemented for `impl Trait + ?Sized` | ||
note: required by a bound in `Box::<T>::new` | ||
--> $SRC_DIR/alloc/src/boxed.rs:LL:COL | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
Some errors have detailed explanations: E0271, E0277, E0308. | ||
For more information about an error, try `rustc --explain E0271`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
error: cannot check whether the hidden type of opaque type satisfies auto traits | ||
--> $DIR/unsized_coercion3.rs:15:32 | ||
| | ||
LL | let y: Box<dyn Send> = x; | ||
| ^ | ||
| | ||
= note: fetching the hidden types of an opaque inside of the defining scope is not supported. You can try moving the opaque type and the item that actually registers a hidden type into a new submodule | ||
note: opaque type is declared here | ||
--> $DIR/unsized_coercion3.rs:11:19 | ||
| | ||
LL | fn hello() -> Box<impl Trait + ?Sized> { | ||
| ^^^^^^^^^^^^^^^^^^^ | ||
= note: required for the cast from `Box<impl Trait + ?Sized>` to `Box<dyn Send>` | ||
|
||
error[E0277]: the size for values of type `impl Trait + ?Sized` cannot be known at compilation time | ||
--> $DIR/unsized_coercion3.rs:15:32 | ||
| | ||
LL | let y: Box<dyn Send> = x; | ||
| ^ doesn't have a size known at compile-time | ||
| | ||
= help: the trait `Sized` is not implemented for `impl Trait + ?Sized` | ||
= note: required for the cast from `Box<impl Trait + ?Sized>` to `Box<dyn Send>` | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0277`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
//! This test checks that opaque types get unsized instead of | ||
//! constraining their hidden type to a trait object. | ||
|
||
//@ revisions: next old | ||
//@[next] compile-flags: -Znext-solver | ||
|
||
trait Trait {} | ||
|
||
impl Trait for u32 {} | ||
|
||
fn hello() -> Box<impl Trait + ?Sized> { | ||
if true { | ||
let x = hello(); | ||
//[next]~^ ERROR: type mismatch resolving `impl Trait + ?Sized <: dyn Send` | ||
let y: Box<dyn Send> = x; | ||
//[old]~^ ERROR: the size for values of type `impl Trait + ?Sized` cannot be know | ||
//[old]~| ERROR: cannot check whether the hidden type of opaque type satisfies auto traits | ||
} | ||
Box::new(1u32) | ||
//[next]~^ ERROR: mismatched types | ||
//[next]~| ERROR: the size for values of type `impl Trait + ?Sized` cannot be know | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
//! This test checks that opaque types get unsized instead of | ||
//! constraining their hidden type to a trait object. | ||
|
||
//@ revisions: next old | ||
//@[next] compile-flags: -Znext-solver | ||
//@check-pass | ||
|
||
trait Trait {} | ||
|
||
impl Trait for u32 {} | ||
|
||
fn hello() -> Box<impl Trait + ?Sized> { | ||
if true { | ||
let x = hello() as Box<u32>; | ||
let y: Box<dyn Send> = x; | ||
} | ||
Box::new(1u32) | ||
} | ||
|
||
fn main() {} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I explained why this fails with
-Znext-solver
in #125610 (comment)I feel like this should compile. We can fix this by partially reverting #119989:
sub_relations
in theFnCtxt
instead of theInferCtxt
(so they aren't used by anything inside of the trait solverThis allows us to consider subtype relations in
obligations_for_self_ty
, e.g. when coercing types, which seems a lot more clearly desirable to me, without having to pass them into canonical queries. The reason I removed them isTracking them in canonical inputs has a significant performance and complexity cost and is unnecessary for this case.
This is very similar to the following which has previously never worked:
I personally am in favor of implementing that change in the new solver but don't think it's a priority until we're close to stabilizing it