Skip to content
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

Clarify that raw retags are not permitted in Mir #106012

Merged
merged 2 commits into from
Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions compiler/rustc_const_eval/src/transform/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use rustc_middle::mir::visit::{PlaceContext, Visitor};
use rustc_middle::mir::{
traversal, AggregateKind, BasicBlock, BinOp, Body, BorrowKind, CastKind, CopyNonOverlapping,
Local, Location, MirPass, MirPhase, NonDivergingIntrinsic, Operand, Place, PlaceElem, PlaceRef,
ProjectionElem, RuntimePhase, Rvalue, SourceScope, Statement, StatementKind, Terminator,
TerminatorKind, UnOp, START_BLOCK,
ProjectionElem, RetagKind, RuntimePhase, Rvalue, SourceScope, Statement, StatementKind,
Terminator, TerminatorKind, UnOp, START_BLOCK,
};
use rustc_middle::ty::{self, InstanceDef, ParamEnv, Ty, TyCtxt, TypeVisitable};
use rustc_mir_dataflow::impls::MaybeStorageLive;
Expand Down Expand Up @@ -667,10 +667,13 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
self.fail(location, "`Deinit`is not allowed until deaggregation");
}
}
StatementKind::Retag(_, _) => {
StatementKind::Retag(kind, _) => {
// FIXME(JakobDegen) The validator should check that `self.mir_phase <
// DropsLowered`. However, this causes ICEs with generation of drop shims, which
// seem to fail to set their `MirPhase` correctly.
if *kind == RetagKind::Raw {
self.fail(location, "explicit `RetagKind::Raw` is forbidden");
}
JakobDegen marked this conversation as resolved.
Show resolved Hide resolved
}
StatementKind::StorageLive(..)
| StatementKind::StorageDead(..)
Expand Down
7 changes: 5 additions & 2 deletions compiler/rustc_middle/src/mir/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,11 @@ pub enum StatementKind<'tcx> {
/// <https://internals.rust-lang.org/t/stacked-borrows-an-aliasing-model-for-rust/8153/> for
/// more details.
///
/// For code that is not specific to stacked borrows, you should consider retags to read
/// and modify the place in an opaque way.
/// For code that is not specific to stacked borrows, you should consider retags to read and
/// modify the place in an opaque way.
///
/// Explicit `RetagKind::Raw` is not permitted - it is implicit as a part of
/// `Rvalue::AddressOf`.
Retag(RetagKind, Box<Place<'tcx>>),

/// Encodes a user's type ascription. These need to be preserved
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ impl<'tcx, 'body> ParseCtxt<'tcx, 'body> {
@call("mir_retag", args) => {
Ok(StatementKind::Retag(RetagKind::Default, Box::new(self.parse_place(args[0])?)))
},
@call("mir_retag_raw", args) => {
Ok(StatementKind::Retag(RetagKind::Raw, Box::new(self.parse_place(args[0])?)))
},
@call("mir_set_discriminant", args) => {
let place = self.parse_place(args[0])?;
let var = self.parse_integer_literal(args[1])? as u32;
Expand Down
1 change: 0 additions & 1 deletion library/core/src/intrinsics/mir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,6 @@ define!("mir_drop", fn Drop<T>(place: T, goto: BasicBlock));
define!("mir_drop_and_replace", fn DropAndReplace<T>(place: T, value: T, goto: BasicBlock));
define!("mir_call", fn Call<T>(place: T, goto: BasicBlock, call: T));
define!("mir_retag", fn Retag<T>(place: T));
define!("mir_retag_raw", fn RetagRaw<T>(place: T));
define!("mir_move", fn Move<T>(place: T) -> T);
define!("mir_static", fn Static<T>(s: T) -> &'static T);
define!("mir_static_mut", fn StaticMut<T>(s: T) -> *mut T);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ fn immut_ref(_1: &i32) -> &i32 {

bb0: {
_2 = &raw const (*_1); // scope 0 at $DIR/references.rs:+5:13: +5:29
Retag([raw] _2); // scope 0 at $DIR/references.rs:+6:13: +6:24
_0 = &(*_2); // scope 0 at $DIR/references.rs:+7:13: +7:23
Retag(_0); // scope 0 at $DIR/references.rs:+8:13: +8:23
return; // scope 0 at $DIR/references.rs:+9:13: +9:21
_0 = &(*_2); // scope 0 at $DIR/references.rs:+6:13: +6:23
Retag(_0); // scope 0 at $DIR/references.rs:+7:13: +7:23
return; // scope 0 at $DIR/references.rs:+8:13: +8:21
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ fn mut_ref(_1: &mut i32) -> &mut i32 {

bb0: {
_2 = &raw mut (*_1); // scope 0 at $DIR/references.rs:+5:13: +5:33
Retag([raw] _2); // scope 0 at $DIR/references.rs:+6:13: +6:24
_0 = &mut (*_2); // scope 0 at $DIR/references.rs:+7:13: +7:26
Retag(_0); // scope 0 at $DIR/references.rs:+8:13: +8:23
return; // scope 0 at $DIR/references.rs:+9:13: +9:21
_0 = &mut (*_2); // scope 0 at $DIR/references.rs:+6:13: +6:26
Retag(_0); // scope 0 at $DIR/references.rs:+7:13: +7:23
return; // scope 0 at $DIR/references.rs:+8:13: +8:21
}
}
2 changes: 0 additions & 2 deletions src/test/mir-opt/building/custom/references.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ pub fn mut_ref(x: &mut i32) -> &mut i32 {

{
t = addr_of_mut!(*x);
RetagRaw(t);
RET = &mut *t;
Retag(RET);
Return()
Expand All @@ -28,7 +27,6 @@ pub fn immut_ref(x: &i32) -> &i32 {

{
t = addr_of!(*x);
RetagRaw(t);
RET = & *t;
Retag(RET);
Return()
Expand Down