Skip to content

Commit f27cd60

Browse files
committed
no more action on ref or cast, but add new MIR statement for escaping a ptr to raw
1 parent 8315b11 commit f27cd60

File tree

23 files changed

+159
-110
lines changed

23 files changed

+159
-110
lines changed

src/librustc/ich/impls_mir.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,9 @@ for mir::StatementKind<'gcx> {
257257
mir::StatementKind::EndRegion(ref region_scope) => {
258258
region_scope.hash_stable(hcx, hasher);
259259
}
260+
mir::StatementKind::EscapeToRaw(ref place) => {
261+
place.hash_stable(hcx, hasher);
262+
}
260263
mir::StatementKind::Retag { fn_entry, ref place } => {
261264
fn_entry.hash_stable(hcx, hasher);
262265
place.hash_stable(hcx, hasher);

src/librustc/mir/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1766,6 +1766,13 @@ pub enum StatementKind<'tcx> {
17661766
place: Place<'tcx>,
17671767
},
17681768

1769+
/// Escape the given reference to a raw pointer, so that it can be accessed
1770+
/// without precise provenance tracking. These statements are currently only interpreted
1771+
/// by miri and only generated when "-Z mir-emit-retag" is passed.
1772+
/// See <https://internals.rust-lang.org/t/stacked-borrows-an-aliasing-model-for-rust/8153/>
1773+
/// for more details.
1774+
EscapeToRaw(Operand<'tcx>),
1775+
17691776
/// Mark one terminating point of a region scope (i.e. static region).
17701777
/// (The starting point(s) arise implicitly from borrows.)
17711778
EndRegion(region::Scope),
@@ -1827,6 +1834,7 @@ impl<'tcx> Debug for Statement<'tcx> {
18271834
EndRegion(ref ce) => write!(fmt, "EndRegion({})", ty::ReScope(*ce)),
18281835
Retag { fn_entry, ref place } =>
18291836
write!(fmt, "Retag({}{:?})", if fn_entry { "[fn entry] " } else { "" }, place),
1837+
EscapeToRaw(ref place) => write!(fmt, "EscapeToRaw({:?})", place),
18301838
StorageLive(ref place) => write!(fmt, "StorageLive({:?})", place),
18311839
StorageDead(ref place) => write!(fmt, "StorageDead({:?})", place),
18321840
SetDiscriminant {
@@ -2968,6 +2976,7 @@ EnumTypeFoldableImpl! {
29682976
(StatementKind::StorageDead)(a),
29692977
(StatementKind::InlineAsm) { asm, outputs, inputs },
29702978
(StatementKind::Retag) { fn_entry, place },
2979+
(StatementKind::EscapeToRaw)(place),
29712980
(StatementKind::EndRegion)(a),
29722981
(StatementKind::AscribeUserType)(a, v, b),
29732982
(StatementKind::Nop),

src/librustc/mir/visit.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,9 @@ macro_rules! make_mir_visitor {
385385
location
386386
);
387387
}
388+
StatementKind::EscapeToRaw(ref $($mutability)* op) => {
389+
self.visit_operand(op, location);
390+
}
388391
StatementKind::StorageLive(ref $($mutability)* local) => {
389392
self.visit_local(
390393
local,
@@ -1022,7 +1025,7 @@ pub enum MutatingUseContext<'tcx> {
10221025
/// f(&mut x.y);
10231026
///
10241027
Projection,
1025-
/// Retagging (updating the "Stacked Borrows" tag)
1028+
/// Retagging, a "Stacked Borrows" shadow state operation
10261029
Retag,
10271030
}
10281031

src/librustc_codegen_llvm/mir/statement.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,9 @@ impl FunctionCx<'a, 'll, 'tcx> {
105105
bx
106106
}
107107
mir::StatementKind::FakeRead(..) |
108-
mir::StatementKind::EndRegion(_) |
108+
mir::StatementKind::EndRegion(..) |
109109
mir::StatementKind::Retag { .. } |
110+
mir::StatementKind::EscapeToRaw { .. } |
110111
mir::StatementKind::AscribeUserType(..) |
111112
mir::StatementKind::Nop => bx,
112113
}

src/librustc_mir/borrow_check/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,7 @@ impl<'cx, 'gcx, 'tcx> DataflowResultsConsumer<'cx, 'tcx> for MirBorrowckCtxt<'cx
601601
StatementKind::Nop
602602
| StatementKind::AscribeUserType(..)
603603
| StatementKind::Retag { .. }
604+
| StatementKind::EscapeToRaw { .. }
604605
| StatementKind::StorageLive(..) => {
605606
// `Nop`, `AscribeUserType`, `Retag`, and `StorageLive` are irrelevant
606607
// to borrow check.

src/librustc_mir/borrow_check/nll/invalidation.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ impl<'cx, 'tcx, 'gcx> Visitor<'tcx> for InvalidationGenerator<'cx, 'tcx, 'gcx> {
137137
StatementKind::Nop |
138138
StatementKind::AscribeUserType(..) |
139139
StatementKind::Retag { .. } |
140+
StatementKind::EscapeToRaw { .. } |
140141
StatementKind::StorageLive(..) => {
141142
// `Nop`, `AscribeUserType`, `Retag`, and `StorageLive` are irrelevant
142143
// to borrow check.

src/librustc_mir/borrow_check/nll/type_check/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,11 +1290,12 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
12901290
}
12911291
}
12921292
StatementKind::FakeRead(..)
1293-
| StatementKind::StorageLive(_)
1294-
| StatementKind::StorageDead(_)
1293+
| StatementKind::StorageLive(..)
1294+
| StatementKind::StorageDead(..)
12951295
| StatementKind::InlineAsm { .. }
12961296
| StatementKind::EndRegion(_)
12971297
| StatementKind::Retag { .. }
1298+
| StatementKind::EscapeToRaw { .. }
12981299
| StatementKind::Nop => {}
12991300
}
13001301
}

src/librustc_mir/dataflow/impls/borrows.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ impl<'a, 'gcx, 'tcx> BitDenotation for Borrows<'a, 'gcx, 'tcx> {
339339
mir::StatementKind::SetDiscriminant { .. } |
340340
mir::StatementKind::StorageLive(..) |
341341
mir::StatementKind::Retag { .. } |
342+
mir::StatementKind::EscapeToRaw { .. } |
342343
mir::StatementKind::AscribeUserType(..) |
343344
mir::StatementKind::Nop => {}
344345

src/librustc_mir/dataflow/move_paths/builder.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,9 @@ impl<'b, 'a, 'gcx, 'tcx> Gatherer<'b, 'a, 'gcx, 'tcx> {
301301
span_bug!(stmt.source_info.span,
302302
"SetDiscriminant should not exist during borrowck");
303303
}
304-
StatementKind::EndRegion(_) |
304+
StatementKind::EndRegion(..) |
305305
StatementKind::Retag { .. } |
306+
StatementKind::EscapeToRaw { .. } |
306307
StatementKind::AscribeUserType(..) |
307308
StatementKind::Nop => {}
308309
}

src/librustc_mir/interpret/cast.rs

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,40 +44,28 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
4444
}
4545

4646
Misc => {
47-
let src_layout = src.layout;
4847
let src = self.read_immediate(src)?;
4948

50-
let src = if M::ENABLE_PTR_TRACKING_HOOKS && src_layout.ty.is_region_ptr() {
51-
// The only `Misc` casts on references are those creating raw pointers.
52-
assert!(dest.layout.ty.is_unsafe_ptr());
53-
// For the purpose of the "ptr tag hooks", treat this as creating
54-
// a new, raw reference.
55-
let place = self.ref_to_mplace(src)?;
56-
self.create_ref(place, None)?
57-
} else {
58-
*src
59-
};
60-
61-
if self.type_is_fat_ptr(src_layout.ty) {
62-
match (src, self.type_is_fat_ptr(dest.layout.ty)) {
49+
if self.type_is_fat_ptr(src.layout.ty) {
50+
match (*src, self.type_is_fat_ptr(dest.layout.ty)) {
6351
// pointers to extern types
6452
(Immediate::Scalar(_),_) |
6553
// slices and trait objects to other slices/trait objects
6654
(Immediate::ScalarPair(..), true) => {
6755
// No change to immediate
68-
self.write_immediate(src, dest)?;
56+
self.write_immediate(*src, dest)?;
6957
}
7058
// slices and trait objects to thin pointers (dropping the metadata)
7159
(Immediate::ScalarPair(data, _), false) => {
7260
self.write_scalar(data, dest)?;
7361
}
7462
}
7563
} else {
76-
match src_layout.variants {
64+
match src.layout.variants {
7765
layout::Variants::Single { index } => {
78-
if let Some(def) = src_layout.ty.ty_adt_def() {
66+
if let Some(def) = src.layout.ty.ty_adt_def() {
7967
// Cast from a univariant enum
80-
assert!(src_layout.is_zst());
68+
assert!(src.layout.is_zst());
8169
let discr_val = def
8270
.discriminant_for_variant(*self.tcx, index)
8371
.val;
@@ -90,8 +78,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
9078
layout::Variants::NicheFilling { .. } => {},
9179
}
9280

93-
let src = src.to_scalar()?;
94-
let dest_val = self.cast_scalar(src, src_layout, dest.layout)?;
81+
let dest_val = self.cast_scalar(src.to_scalar()?, src.layout, dest.layout)?;
9582
self.write_scalar(dest_val, dest)?;
9683
}
9784
}

0 commit comments

Comments
 (0)