forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#130735 - compiler-errors:validate-unsize, r=spastorino,RalfJung Simple validation for unsize coercion in MIR validation This adds the most basic validity check to unsize coercions in MIR. The src and target of an unsize cast must *at least* implement `Src: CoerceUnsized<Target>` for this to be valid. This doesn't the second, more subtle validity check that is taken of advantage in codegen [here](https://github.com/rust-lang/rust/blob/914193c8f40528fe82696e1054828de8c399882e/compiler/rustc_codegen_ssa/src/base.rs#L126), but I did leave a beefy FIXME for that explaining what it is. As a consequence, this also fixes an ICE with GVN and invalid unsize coercions. This is somewhat coincidental, since MIR inlining will check that a body is valid before inlining it; so now that we determine it to be invalid, we don't inline it, and we don't encounter the GVN ICE. I'm not certain if the same GVN ICE is triggerable without the inliner, and perhaps instead with trivial where clauses or something. cc `@RalfJung`
- Loading branch information
Showing
6 changed files
with
123 additions
and
32 deletions.
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
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,33 @@ | ||
//@ compile-flags: -Zmir-opt-level=0 -Zmir-enable-passes=+Inline,+GVN -Zvalidate-mir | ||
|
||
#![feature(unsize)] | ||
|
||
use std::marker::Unsize; | ||
|
||
pub trait CastTo<U: ?Sized>: Unsize<U> {} | ||
|
||
// Not well-formed! | ||
impl<T: ?Sized, U: ?Sized> CastTo<U> for T {} | ||
//~^ ERROR the trait bound `T: Unsize<U>` is not satisfied | ||
|
||
pub trait Cast { | ||
fn cast<U: ?Sized>(&self) | ||
where | ||
Self: CastTo<U>; | ||
} | ||
impl<T: ?Sized> Cast for T { | ||
#[inline(always)] | ||
fn cast<U: ?Sized>(&self) | ||
where | ||
Self: CastTo<U>, | ||
{ | ||
let x: &U = self; | ||
} | ||
} | ||
|
||
fn main() { | ||
// When we inline this call, then we run GVN, then | ||
// GVN tries to evaluate the `() -> [i32]` unsize. | ||
// That's invalid! | ||
().cast::<[i32]>(); | ||
} |
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 @@ | ||
error[E0277]: the trait bound `T: Unsize<U>` is not satisfied | ||
--> $DIR/validate-unsize-cast.rs:10:42 | ||
| | ||
LL | impl<T: ?Sized, U: ?Sized> CastTo<U> for T {} | ||
| ^ the trait `Unsize<U>` is not implemented for `T` | ||
| | ||
= note: all implementations of `Unsize` are provided automatically by the compiler, see <https://doc.rust-lang.org/stable/std/marker/trait.Unsize.html> for more information | ||
note: required by a bound in `CastTo` | ||
--> $DIR/validate-unsize-cast.rs:7:30 | ||
| | ||
LL | pub trait CastTo<U: ?Sized>: Unsize<U> {} | ||
| ^^^^^^^^^ required by this bound in `CastTo` | ||
help: consider further restricting this bound | ||
| | ||
LL | impl<T: ?Sized + std::marker::Unsize<U>, U: ?Sized> CastTo<U> for T {} | ||
| ++++++++++++++++++++++++ | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0277`. |