-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Simple validation for unsize coercion in MIR validation #130735
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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
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 |
---|---|---|
|
@@ -4,7 +4,8 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet}; | |
use rustc_hir::LangItem; | ||
use rustc_index::IndexVec; | ||
use rustc_index::bit_set::BitSet; | ||
use rustc_infer::traits::Reveal; | ||
use rustc_infer::infer::TyCtxtInferExt; | ||
use rustc_infer::traits::{Obligation, ObligationCause, Reveal}; | ||
use rustc_middle::mir::coverage::CoverageKind; | ||
use rustc_middle::mir::visit::{NonUseContext, PlaceContext, Visitor}; | ||
use rustc_middle::mir::*; | ||
|
@@ -16,6 +17,8 @@ use rustc_middle::ty::{ | |
use rustc_middle::{bug, span_bug}; | ||
use rustc_target::abi::{FIRST_VARIANT, Size}; | ||
use rustc_target::spec::abi::Abi; | ||
use rustc_trait_selection::traits::ObligationCtxt; | ||
use rustc_type_ir::Upcast; | ||
|
||
use crate::util::{is_within_packed, relate_types}; | ||
|
||
|
@@ -586,6 +589,22 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { | |
|
||
crate::util::relate_types(self.tcx, self.param_env, variance, src, dest) | ||
} | ||
|
||
/// Check that the given predicate definitely holds in the param-env of this MIR body. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we already have a "test this predicate holds" function somewhere in the validator? I couldn't find one when doing a simple search. |
||
fn predicate_must_hold_modulo_regions( | ||
&self, | ||
pred: impl Upcast<TyCtxt<'tcx>, ty::Predicate<'tcx>>, | ||
) -> bool { | ||
let infcx = self.tcx.infer_ctxt().build(); | ||
let ocx = ObligationCtxt::new(&infcx); | ||
ocx.register_obligation(Obligation::new( | ||
self.tcx, | ||
ObligationCause::dummy(), | ||
self.param_env, | ||
pred, | ||
)); | ||
ocx.select_all_or_error().is_empty() | ||
} | ||
} | ||
|
||
impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> { | ||
|
@@ -1202,8 +1221,18 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> { | |
} | ||
} | ||
CastKind::PointerCoercion(PointerCoercion::Unsize, _) => { | ||
// This is used for all `CoerceUnsized` types, | ||
// not just pointers/references, so is hard to check. | ||
// Pointers being unsize coerced should at least implement | ||
// `CoerceUnsized`. | ||
if !self.predicate_must_hold_modulo_regions(ty::TraitRef::new( | ||
self.tcx, | ||
self.tcx.require_lang_item( | ||
LangItem::CoerceUnsized, | ||
Some(self.body.source_info(location).span), | ||
), | ||
[op_ty, *target_type], | ||
)) { | ||
self.fail(location, format!("Unsize coercion, but `{op_ty}` isn't coercible to `{target_type}`")); | ||
} | ||
} | ||
CastKind::PointerCoercion(PointerCoercion::DynStar, _) => { | ||
// FIXME(dyn-star): make sure nothing needs to be done here. | ||
|
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`. |
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.
That doesn't check that the projections are equal though, does it?
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.
No but there is not an upcast for which this is valid. That would literally be unsound in the type system.
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.
Like, checking projection validity seems more appropriate as an assertion on the Unsize trait goal in the trait system or something, but even that seems excessive.
Like, the only reason miri needs to check projections are compatible is because of transmutes.
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.
And also because I don't trust the
library/
and would prefer this to be double-checked in the compiler. ;)But maybe that's overly paranoid, and anyway it's a different PR. I just wanted to understand what is and is not being checked here, and the comments should reflect that.
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.
It's not really
library/
, it's just the soundness of theUnsize
goal, and the fact that you can't implimpl Foo for Bar { type Assoc = A; }
andimpl Foo for Bar { type Assoc = B; }
. That is, normalization is a function.