Auto merge of #104844 - cjgillot:mention-eval-place, r=jackh726,RalfJung

Evaluate place expression in `PlaceMention`

https://github.com/rust-lang/rust/pull/102256 introduces a `PlaceMention(place)` MIR statement which keep trace of `let _ = place` statements from surface rust, but without semantics.

This PR proposes to change the behaviour of `let _ =` patterns with respect to the borrow-checker to verify that the bound place is live.

Specifically, consider this code:
```rust
let _ = {
    let a = 5;
    &a
};
```

This passes borrowck without error on stable. Meanwhile, replacing `_` by `_: _` or `_p` errors with "error[E0597]: `a` does not live long enough", [see playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=c448d25a7c205dc95a0967fe96bccce8).

This PR *does not* change how `_` patterns behave with respect to initializedness: it remains ok to bind a moved-from place to `_`.

The relevant test is `tests/ui/borrowck/let_underscore_temporary.rs`. Crater check found no regression.

For consistency, this PR changes miri to evaluate the place found in `PlaceMention`, and report eventual dangling pointers found within it.

r? `@RalfJung`
This commit is contained in:
bors 2023-04-22 09:54:21 +00:00
commit 21fab435da
22 changed files with 201 additions and 39 deletions

View file

@ -52,12 +52,16 @@ pub fn categorize(context: PlaceContext) -> Option<DefUse> {
PlaceContext::NonMutatingUse(NonMutatingUseContext::ShallowBorrow) | PlaceContext::NonMutatingUse(NonMutatingUseContext::ShallowBorrow) |
PlaceContext::NonMutatingUse(NonMutatingUseContext::UniqueBorrow) | PlaceContext::NonMutatingUse(NonMutatingUseContext::UniqueBorrow) |
// `PlaceMention` and `AscribeUserType` both evaluate the place, which must not
// contain dangling references.
PlaceContext::NonUse(NonUseContext::PlaceMention) |
PlaceContext::NonUse(NonUseContext::AscribeUserTy) |
PlaceContext::MutatingUse(MutatingUseContext::AddressOf) | PlaceContext::MutatingUse(MutatingUseContext::AddressOf) |
PlaceContext::NonMutatingUse(NonMutatingUseContext::AddressOf) | PlaceContext::NonMutatingUse(NonMutatingUseContext::AddressOf) |
PlaceContext::NonMutatingUse(NonMutatingUseContext::Inspect) | PlaceContext::NonMutatingUse(NonMutatingUseContext::Inspect) |
PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy) | PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy) |
PlaceContext::NonMutatingUse(NonMutatingUseContext::Move) | PlaceContext::NonMutatingUse(NonMutatingUseContext::Move) |
PlaceContext::NonUse(NonUseContext::AscribeUserTy) |
PlaceContext::MutatingUse(MutatingUseContext::Retag) => PlaceContext::MutatingUse(MutatingUseContext::Retag) =>
Some(DefUse::Use), Some(DefUse::Use),
@ -72,8 +76,6 @@ pub fn categorize(context: PlaceContext) -> Option<DefUse> {
PlaceContext::MutatingUse(MutatingUseContext::Drop) => PlaceContext::MutatingUse(MutatingUseContext::Drop) =>
Some(DefUse::Drop), Some(DefUse::Drop),
// This statement exists to help unsafeck. It does not require the place to be live.
PlaceContext::NonUse(NonUseContext::PlaceMention) => None,
// Debug info is neither def nor use. // Debug info is neither def nor use.
PlaceContext::NonUse(NonUseContext::VarDebugInfo) => None, PlaceContext::NonUse(NonUseContext::VarDebugInfo) => None,

View file

@ -79,7 +79,7 @@ impl<'cx, 'tcx> Visitor<'tcx> for InvalidationGenerator<'cx, 'tcx> {
} }
// Only relevant for mir typeck // Only relevant for mir typeck
StatementKind::AscribeUserType(..) StatementKind::AscribeUserType(..)
// Only relevant for unsafeck // Only relevant for liveness and unsafeck
| StatementKind::PlaceMention(..) | StatementKind::PlaceMention(..)
// Doesn't have any language semantics // Doesn't have any language semantics
| StatementKind::Coverage(..) | StatementKind::Coverage(..)

View file

@ -665,7 +665,7 @@ impl<'cx, 'tcx> rustc_mir_dataflow::ResultsVisitor<'cx, 'tcx> for MirBorrowckCtx
} }
// Only relevant for mir typeck // Only relevant for mir typeck
StatementKind::AscribeUserType(..) StatementKind::AscribeUserType(..)
// Only relevant for unsafeck // Only relevant for liveness and unsafeck
| StatementKind::PlaceMention(..) | StatementKind::PlaceMention(..)
// Doesn't have any language semantics // Doesn't have any language semantics
| StatementKind::Coverage(..) | StatementKind::Coverage(..)

View file

@ -113,8 +113,14 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
Intrinsic(box intrinsic) => self.emulate_nondiverging_intrinsic(intrinsic)?, Intrinsic(box intrinsic) => self.emulate_nondiverging_intrinsic(intrinsic)?,
// Statements we do not track. // Evaluate the place expression, without reading from it.
PlaceMention(..) | AscribeUserType(..) => {} PlaceMention(box place) => {
let _ = self.eval_place(*place)?;
}
// This exists purely to guide borrowck lifetime inference, and does not have
// an operational effect.
AscribeUserType(..) => {}
// Currently, Miri discards Coverage statements. Coverage statements are only injected // Currently, Miri discards Coverage statements. Coverage statements are only injected
// via an optional compile time MIR pass and have no side effects. Since Coverage // via an optional compile time MIR pass and have no side effects. Since Coverage

View file

@ -802,14 +802,6 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
} }
} }
} }
StatementKind::PlaceMention(..) => {
if self.mir_phase >= MirPhase::Runtime(RuntimePhase::Initial) {
self.fail(
location,
"`PlaceMention` should have been removed after drop lowering phase",
);
}
}
StatementKind::AscribeUserType(..) => { StatementKind::AscribeUserType(..) => {
if self.mir_phase >= MirPhase::Runtime(RuntimePhase::Initial) { if self.mir_phase >= MirPhase::Runtime(RuntimePhase::Initial) {
self.fail( self.fail(
@ -919,6 +911,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
StatementKind::StorageDead(_) StatementKind::StorageDead(_)
| StatementKind::Coverage(_) | StatementKind::Coverage(_)
| StatementKind::ConstEvalCounter | StatementKind::ConstEvalCounter
| StatementKind::PlaceMention(..)
| StatementKind::Nop => {} | StatementKind::Nop => {}
} }

View file

@ -768,6 +768,7 @@ fn test_unstable_options_tracking_hash() {
tracked!(merge_functions, Some(MergeFunctions::Disabled)); tracked!(merge_functions, Some(MergeFunctions::Disabled));
tracked!(mir_emit_retag, true); tracked!(mir_emit_retag, true);
tracked!(mir_enable_passes, vec![("DestProp".to_string(), false)]); tracked!(mir_enable_passes, vec![("DestProp".to_string(), false)]);
tracked!(mir_keep_place_mention, true);
tracked!(mir_opt_level, Some(4)); tracked!(mir_opt_level, Some(4));
tracked!(move_size_limit, Some(4096)); tracked!(move_size_limit, Some(4096));
tracked!(mutable_noalias, false); tracked!(mutable_noalias, false);

View file

@ -331,9 +331,8 @@ pub enum StatementKind<'tcx> {
/// This is especially useful for `let _ = PLACE;` bindings that desugar to a single /// This is especially useful for `let _ = PLACE;` bindings that desugar to a single
/// `PlaceMention(PLACE)`. /// `PlaceMention(PLACE)`.
/// ///
/// When executed at runtime this is a nop. /// When executed at runtime, this computes the given place, but then discards
/// /// it without doing a load. It is UB if the place is not pointing to live memory.
/// Disallowed after drop elaboration.
PlaceMention(Box<Place<'tcx>>), PlaceMention(Box<Place<'tcx>>),
/// Encodes a user's type ascription. These need to be preserved /// Encodes a user's type ascription. These need to be preserved

View file

@ -24,7 +24,6 @@ impl<'tcx> MirPass<'tcx> for CleanupPostBorrowck {
for statement in basic_block.statements.iter_mut() { for statement in basic_block.statements.iter_mut() {
match statement.kind { match statement.kind {
StatementKind::AscribeUserType(..) StatementKind::AscribeUserType(..)
| StatementKind::PlaceMention(..)
| StatementKind::Assign(box (_, Rvalue::Ref(_, BorrowKind::Shallow, _))) | StatementKind::Assign(box (_, Rvalue::Ref(_, BorrowKind::Shallow, _)))
| StatementKind::FakeRead(..) => statement.make_nop(), | StatementKind::FakeRead(..) => statement.make_nop(),
_ => (), _ => (),

View file

@ -54,11 +54,10 @@ pub fn eliminate<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>, borrowed: &BitS
| StatementKind::Coverage(_) | StatementKind::Coverage(_)
| StatementKind::Intrinsic(_) | StatementKind::Intrinsic(_)
| StatementKind::ConstEvalCounter | StatementKind::ConstEvalCounter
| StatementKind::PlaceMention(_)
| StatementKind::Nop => (), | StatementKind::Nop => (),
StatementKind::FakeRead(_) StatementKind::FakeRead(_) | StatementKind::AscribeUserType(_, _) => {
| StatementKind::PlaceMention(_)
| StatementKind::AscribeUserType(_, _) => {
bug!("{:?} not found in this MIR phase!", &statement.kind) bug!("{:?} not found in this MIR phase!", &statement.kind)
} }
} }

View file

@ -582,10 +582,9 @@ impl WriteInfo {
| StatementKind::Nop | StatementKind::Nop
| StatementKind::Coverage(_) | StatementKind::Coverage(_)
| StatementKind::StorageLive(_) | StatementKind::StorageLive(_)
| StatementKind::StorageDead(_) => (), | StatementKind::StorageDead(_)
StatementKind::FakeRead(_) | StatementKind::PlaceMention(_) => (),
| StatementKind::AscribeUserType(_, _) StatementKind::FakeRead(_) | StatementKind::AscribeUserType(_, _) => {
| StatementKind::PlaceMention(_) => {
bug!("{:?} not found in this MIR phase", statement) bug!("{:?} not found in this MIR phase", statement)
} }
} }

View file

@ -48,6 +48,7 @@ mod add_retag;
mod check_const_item_mutation; mod check_const_item_mutation;
mod check_packed_ref; mod check_packed_ref;
pub mod check_unsafety; pub mod check_unsafety;
mod remove_place_mention;
// This pass is public to allow external drivers to perform MIR cleanup // This pass is public to allow external drivers to perform MIR cleanup
pub mod cleanup_post_borrowck; pub mod cleanup_post_borrowck;
mod const_debuginfo; mod const_debuginfo;
@ -460,8 +461,11 @@ fn run_runtime_lowering_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
/// Returns the sequence of passes that do the initial cleanup of runtime MIR. /// Returns the sequence of passes that do the initial cleanup of runtime MIR.
fn run_runtime_cleanup_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { fn run_runtime_cleanup_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
let passes: &[&dyn MirPass<'tcx>] = let passes: &[&dyn MirPass<'tcx>] = &[
&[&lower_intrinsics::LowerIntrinsics, &simplify::SimplifyCfg::ElaborateDrops]; &lower_intrinsics::LowerIntrinsics,
&remove_place_mention::RemovePlaceMention,
&simplify::SimplifyCfg::ElaborateDrops,
];
pm::run_passes(tcx, body, passes, Some(MirPhase::Runtime(RuntimePhase::PostCleanup))); pm::run_passes(tcx, body, passes, Some(MirPhase::Runtime(RuntimePhase::PostCleanup)));

View file

@ -0,0 +1,23 @@
//! This pass removes `PlaceMention` statement, which has no effect at codegen.
use crate::MirPass;
use rustc_middle::mir::*;
use rustc_middle::ty::TyCtxt;
pub struct RemovePlaceMention;
impl<'tcx> MirPass<'tcx> for RemovePlaceMention {
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
!sess.opts.unstable_opts.mir_keep_place_mention
}
fn run_pass(&self, _: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
trace!("Running RemovePlaceMention on {:?}", body.source);
for data in body.basic_blocks.as_mut_preserves_cfg() {
data.statements.retain(|statement| match statement.kind {
StatementKind::PlaceMention(..) | StatementKind::Nop => false,
_ => true,
})
}
}
}

View file

@ -1558,6 +1558,9 @@ options! {
"use like `-Zmir-enable-passes=+DestProp,-InstCombine`. Forces the specified passes to be \ "use like `-Zmir-enable-passes=+DestProp,-InstCombine`. Forces the specified passes to be \
enabled, overriding all other checks. Passes that are not specified are enabled or \ enabled, overriding all other checks. Passes that are not specified are enabled or \
disabled by other flags as usual."), disabled by other flags as usual."),
mir_keep_place_mention: bool = (false, parse_bool, [TRACKED],
"keep place mention MIR statements, interpreted e.g., by miri; implies -Zmir-opt-level=0 \
(default: no)"),
#[rustc_lint_opt_deny_field_access("use `Session::mir_opt_level` instead of this field")] #[rustc_lint_opt_deny_field_access("use `Session::mir_opt_level` instead of this field")]
mir_opt_level: Option<usize> = (None, parse_opt_number, [TRACKED], mir_opt_level: Option<usize> = (None, parse_opt_number, [TRACKED],
"MIR optimization level (0-4; default: 1 in non optimized builds and 2 in optimized builds)"), "MIR optimization level (0-4; default: 1 in non optimized builds and 2 in optimized builds)"),

View file

@ -25,7 +25,7 @@ fn else_if_option(string: Option<&str>) -> Option<(bool, &str)> {
fn unop_bad(string: &Option<&str>, mut num: Option<i32>) { fn unop_bad(string: &Option<&str>, mut num: Option<i32>) {
let _ = string.map_or(0, |s| s.len()); let _ = string.map_or(0, |s| s.len());
let _ = num.as_ref().map_or(&0, |s| s); let _ = num.as_ref().map_or(&0, |s| s);
let _ = num.as_mut().map_or(&mut 0, |s| { let _ = num.as_mut().map_or(&0, |s| {
*s += 1; *s += 1;
s s
}); });
@ -34,7 +34,7 @@ fn unop_bad(string: &Option<&str>, mut num: Option<i32>) {
s += 1; s += 1;
s s
}); });
let _ = num.as_mut().map_or(&mut 0, |s| { let _ = num.as_mut().map_or(&0, |s| {
*s += 1; *s += 1;
s s
}); });

View file

@ -33,7 +33,7 @@ fn unop_bad(string: &Option<&str>, mut num: Option<i32>) {
*s += 1; *s += 1;
s s
} else { } else {
&mut 0 &0
}; };
let _ = if let Some(ref s) = num { s } else { &0 }; let _ = if let Some(ref s) = num { s } else { &0 };
let _ = if let Some(mut s) = num { let _ = if let Some(mut s) = num {
@ -46,7 +46,7 @@ fn unop_bad(string: &Option<&str>, mut num: Option<i32>) {
*s += 1; *s += 1;
s s
} else { } else {
&mut 0 &0
}; };
} }

View file

@ -30,13 +30,13 @@ LL | let _ = if let Some(s) = &mut num {
LL | | *s += 1; LL | | *s += 1;
LL | | s LL | | s
LL | | } else { LL | | } else {
LL | | &mut 0 LL | | &0
LL | | }; LL | | };
| |_____^ | |_____^
| |
help: try help: try
| |
LL ~ let _ = num.as_mut().map_or(&mut 0, |s| { LL ~ let _ = num.as_mut().map_or(&0, |s| {
LL + *s += 1; LL + *s += 1;
LL + s LL + s
LL ~ }); LL ~ });
@ -76,13 +76,13 @@ LL | let _ = if let Some(ref mut s) = num {
LL | | *s += 1; LL | | *s += 1;
LL | | s LL | | s
LL | | } else { LL | | } else {
LL | | &mut 0 LL | | &0
LL | | }; LL | | };
| |_____^ | |_____^
| |
help: try help: try
| |
LL ~ let _ = num.as_mut().map_or(&mut 0, |s| { LL ~ let _ = num.as_mut().map_or(&0, |s| {
LL + *s += 1; LL + *s += 1;
LL + s LL + s
LL ~ }); LL ~ });

View file

@ -130,6 +130,7 @@ pub const MIRI_DEFAULT_ARGS: &[&str] = &[
"-Zalways-encode-mir", "-Zalways-encode-mir",
"-Zextra-const-ub-checks", "-Zextra-const-ub-checks",
"-Zmir-emit-retag", "-Zmir-emit-retag",
"-Zmir-keep-place-mention",
"-Zmir-opt-level=0", "-Zmir-opt-level=0",
"-Zmir-enable-passes=-CheckAlignment", "-Zmir-enable-passes=-CheckAlignment",
]; ];

View file

@ -0,0 +1,13 @@
// Make sure we find these even with many checks disabled.
//@compile-flags: -Zmiri-disable-alignment-check -Zmiri-disable-stacked-borrows -Zmiri-disable-validation
fn main() {
let p = {
let b = Box::new(42);
&*b as *const i32
};
unsafe {
let _ = *p; //~ ERROR: dereferenced after this allocation got freed
}
panic!("this should never print");
}

View file

@ -0,0 +1,15 @@
error: Undefined Behavior: pointer to ALLOC was dereferenced after this allocation got freed
--> $DIR/dangling_pointer_deref_underscore.rs:LL:CC
|
LL | let _ = *p;
| ^^ pointer to ALLOC was dereferenced after this allocation got freed
|
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
= note: BACKTRACE:
= note: inside `main` at $DIR/dangling_pointer_deref_underscore.rs:LL:CC
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
error: aborting due to previous error

View file

@ -1,4 +1,4 @@
// check-pass // check-fail
fn let_underscore(string: &Option<&str>, mut num: Option<i32>) { fn let_underscore(string: &Option<&str>, mut num: Option<i32>) {
let _ = if let Some(s) = *string { s.len() } else { 0 }; let _ = if let Some(s) = *string { s.len() } else { 0 };
@ -8,6 +8,7 @@ fn let_underscore(string: &Option<&str>, mut num: Option<i32>) {
s s
} else { } else {
&mut 0 &mut 0
//~^ ERROR temporary value dropped while borrowed
}; };
let _ = if let Some(ref s) = num { s } else { &0 }; let _ = if let Some(ref s) = num { s } else { &0 };
let _ = if let Some(mut s) = num { let _ = if let Some(mut s) = num {
@ -21,6 +22,33 @@ fn let_underscore(string: &Option<&str>, mut num: Option<i32>) {
s s
} else { } else {
&mut 0 &mut 0
//~^ ERROR temporary value dropped while borrowed
};
}
fn let_ascribe(string: &Option<&str>, mut num: Option<i32>) {
let _: _ = if let Some(s) = *string { s.len() } else { 0 };
let _: _ = if let Some(s) = &num { s } else { &0 };
let _: _ = if let Some(s) = &mut num {
*s += 1;
s
} else {
&mut 0
//~^ ERROR temporary value dropped while borrowed
};
let _: _ = if let Some(ref s) = num { s } else { &0 };
let _: _ = if let Some(mut s) = num {
s += 1;
s
} else {
0
};
let _: _ = if let Some(ref mut s) = num {
*s += 1;
s
} else {
&mut 0
//~^ ERROR temporary value dropped while borrowed
}; };
} }

View file

@ -0,0 +1,79 @@
error[E0716]: temporary value dropped while borrowed
--> $DIR/let_underscore_temporary.rs:10:14
|
LL | let _ = if let Some(s) = &mut num {
| _____________-
LL | | *s += 1;
LL | | s
LL | | } else {
LL | | &mut 0
| | ^ creates a temporary value which is freed while still in use
LL | |
LL | | };
| | -
| | |
| |_____temporary value is freed at the end of this statement
| borrow later used here
|
= note: consider using a `let` binding to create a longer lived value
error[E0716]: temporary value dropped while borrowed
--> $DIR/let_underscore_temporary.rs:24:14
|
LL | let _ = if let Some(ref mut s) = num {
| _____________-
LL | | *s += 1;
LL | | s
LL | | } else {
LL | | &mut 0
| | ^ creates a temporary value which is freed while still in use
LL | |
LL | | };
| | -
| | |
| |_____temporary value is freed at the end of this statement
| borrow later used here
|
= note: consider using a `let` binding to create a longer lived value
error[E0716]: temporary value dropped while borrowed
--> $DIR/let_underscore_temporary.rs:36:14
|
LL | let _: _ = if let Some(s) = &mut num {
| ________________-
LL | | *s += 1;
LL | | s
LL | | } else {
LL | | &mut 0
| | ^ creates a temporary value which is freed while still in use
LL | |
LL | | };
| | -
| | |
| |_____temporary value is freed at the end of this statement
| borrow later used here
|
= note: consider using a `let` binding to create a longer lived value
error[E0716]: temporary value dropped while borrowed
--> $DIR/let_underscore_temporary.rs:50:14
|
LL | let _: _ = if let Some(ref mut s) = num {
| ________________-
LL | | *s += 1;
LL | | s
LL | | } else {
LL | | &mut 0
| | ^ creates a temporary value which is freed while still in use
LL | |
LL | | };
| | -
| | |
| |_____temporary value is freed at the end of this statement
| borrow later used here
|
= note: consider using a `let` binding to create a longer lived value
error: aborting due to 4 previous errors
For more information about this error, try `rustc --explain E0716`.

View file

@ -25,8 +25,6 @@ LL | };
error[E0597]: `x` does not live long enough error[E0597]: `x` does not live long enough
--> $DIR/send-is-not-static-std-sync-2.rs:31:25 --> $DIR/send-is-not-static-std-sync-2.rs:31:25
| |
LL | let (_tx, rx) = {
| --- borrow later used here
LL | let x = 1; LL | let x = 1;
| - binding `x` declared here | - binding `x` declared here
LL | let (tx, rx) = mpsc::channel(); LL | let (tx, rx) = mpsc::channel();