-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validate unsize coercion in MIR validation
- Loading branch information
1 parent
702987f
commit bb7137a
Showing
4 changed files
with
100 additions
and
29 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 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`. |