Skip to content

Commit f2f26bc

Browse files
authored
Rollup merge of #164088 - RalfJung:const-check-ub, r=oli-obk
const-check: clarify when and how we can rely on non-UB assumptions Turns out const-checking relies on `&x` being immutable. This is the "most obvious" property we get from the aliasing model, and we emit LLVM attributes and have MIR-opts that rely on it. I just didn't realize that taking away this UB would actually be a breaking change; there is code that runs on stable that we'd have to stop accepting if we took away this UB: ```rust struct NotConstDestruct; impl Drop for NotConstDestruct { fn drop(&mut self) { eprintln!("this is not const"); } } const _: () = { let x = None::<NotConstDestruct>; let ptr = std::ptr::from_ref(&x); unsafe { ptr.cast_mut().write(Some(NotConstDestruct)) }; }; ``` This means that I accidentally fixed a hole in const-checking when I added `&` immutability checking to const-eval. r? @oli-obk Cc @rust-lang/opsem @tmiasko
2 parents ebe0c47 + 0e50382 commit f2f26bc

3 files changed

Lines changed: 16 additions & 8 deletions

File tree

‎compiler/rustc_const_eval/src/check_consts/qualifs.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ pub fn in_any_value_of_ty<'tcx>(
4141
/// To accomplish this, const-checking and promotion use a value-based analysis (as opposed to a
4242
/// type-based one). Qualifications propagate structurally across variables: If a local (or a
4343
/// projection of a local) is assigned a qualified value, that local itself becomes qualified.
44+
///
45+
/// Note that whenever this analysis relies on something being UB and hence impossible in
46+
/// well-defined code, it is crucial that const-eval actually detects that UB! Const-eval is not
47+
/// guaranteed to detect all UB, and if it would skip this particular UB we may end up executing
48+
/// code that has not been const-checked.
4449
pub trait Qualif {
4550
/// The name of the file used to debug the dataflow analysis that computes this qualif.
4651
const ANALYSIS_NAME: &'static str;

‎compiler/rustc_const_eval/src/check_consts/resolver.rs‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,13 @@ where
112112

113113
/// `&` only allow mutation if the borrowed place is `!Freeze`.
114114
///
115-
/// This assumes that it is UB to take the address of a struct field whose type is
116-
/// `Freeze`, then use pointer arithmetic to derive a pointer to a *different* field of
117-
/// that same struct whose type is `!Freeze`. If we decide that this is not UB, we will
118-
/// have to check the type of the borrowed **local** instead of the borrowed **place**
119-
/// below. See [rust-lang/unsafe-code-guidelines#134].
120-
///
121-
/// [rust-lang/unsafe-code-guidelines#134]: https://github.com/rust-lang/unsafe-code-guidelines/issues/134
115+
/// This assumes that const-eval will abort when one creates a shared reference to a struct
116+
/// field whose type is `Freeze`, then uses pointer arithmetic to derive a pointer to a
117+
/// *different* field of that same struct whose type is `!Freeze` and mutates that. It is not
118+
/// enough for that to be UB (which we are pretty sure it is, and it would be a breaking change
119+
/// to make it non-UB as we'd then have to make const checking more conservative here),
120+
/// const-eval also has to reliably detect that UB to ensure we never invoke a destructor after
121+
/// such a mutation occurs!
122122
fn shared_borrow_allows_mutation(&self, place: mir::Place<'tcx>) -> bool {
123123
!place.ty(self.ccx.body, self.ccx.tcx).ty.is_freeze(self.ccx.tcx, self.ccx.typing_env)
124124
}

‎compiler/rustc_const_eval/src/const_eval/machine.rs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1132,7 +1132,10 @@ impl<'tcx> interpret::Machine<'tcx> for CompileTimeMachine<'tcx> {
11321132
return interp_ok(None);
11331133
}
11341134
// If it's a frozen shared reference that's not already immutable, potentially make it immutable.
1135-
// (Do nothing on `None` provenance, that cannot store immutability anyway.)
1135+
// (Do nothing on `None` provenance, that cannot be used to perform mutation anyway.)
1136+
// Note that const-checking relies on the UB detection we are implementing here! Specifically,
1137+
// the analysis for which destructors may be invoked relies on `&impl Freeze` references not
1138+
// being able to change what gets dropped.
11361139
if let ty::Ref(_, ty, mutbl) = val.layout.ty.kind()
11371140
&& *mutbl == Mutability::Not
11381141
&& val.to_scalar_and_meta().0.to_pointer(ecx).provenance.is_some_and(|p| !p.immutable())

0 commit comments

Comments
 (0)