Skip to content

Commit a5bd4fe

Browse files
authored
Unrolled build for rust-lang#136235
Rollup merge of rust-lang#136235 - oli-obk:transmuty-pat-tys, r=RalfJung Pretty print pattern type values with transmute if they don't satisfy their pattern Instead of printing `0_u32 is 1..`, we now print the default fallback rendering that we also use for invalid bools, chars, ...: `{transmute(0x00000000): (u32) is 1..=}`. These cases can occur in mir dumps when const prop propagates a constant across a safety check that would prevent the actually UB value from existing. That's fine though, as it's dead code and we always need to allow UB in dead code. follow-up to rust-lang#136176 cc ``@compiler-errors`` ``@scottmcm`` r? ``@RalfJung`` because of the interpreter changes
2 parents 79f82ad + ab31159 commit a5bd4fe

File tree

7 files changed

+51
-16
lines changed

7 files changed

+51
-16
lines changed

compiler/rustc_const_eval/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ pub fn provide(providers: &mut Providers) {
5151
providers.check_validity_requirement = |tcx, (init_kind, param_env_and_ty)| {
5252
util::check_validity_requirement(tcx, init_kind, param_env_and_ty)
5353
};
54+
providers.hooks.validate_scalar_in_layout =
55+
|tcx, scalar, layout| util::validate_scalar_in_layout(tcx, scalar, layout);
5456
}
5557

5658
/// `rustc_driver::main` installs a handler that will set this to `true` if

compiler/rustc_const_eval/src/util/check_validity_requirement.rs

+41-13
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use rustc_abi::{BackendRepr, FieldsShape, Scalar, Variants};
2-
use rustc_middle::bug;
32
use rustc_middle::ty::layout::{
43
HasTyCtxt, LayoutCx, LayoutError, LayoutOf, TyAndLayout, ValidityRequirement,
54
};
6-
use rustc_middle::ty::{PseudoCanonicalInput, Ty, TyCtxt};
5+
use rustc_middle::ty::{PseudoCanonicalInput, ScalarInt, Ty, TyCtxt};
6+
use rustc_middle::{bug, ty};
7+
use rustc_span::DUMMY_SP;
78

89
use crate::const_eval::{CanAccessMutGlobal, CheckAlignment, CompileTimeMachine};
910
use crate::interpret::{InterpCx, MemoryKind};
@@ -34,7 +35,7 @@ pub fn check_validity_requirement<'tcx>(
3435

3536
let layout_cx = LayoutCx::new(tcx, input.typing_env);
3637
if kind == ValidityRequirement::Uninit || tcx.sess.opts.unstable_opts.strict_init_checks {
37-
check_validity_requirement_strict(layout, &layout_cx, kind)
38+
Ok(check_validity_requirement_strict(layout, &layout_cx, kind))
3839
} else {
3940
check_validity_requirement_lax(layout, &layout_cx, kind)
4041
}
@@ -46,10 +47,10 @@ fn check_validity_requirement_strict<'tcx>(
4647
ty: TyAndLayout<'tcx>,
4748
cx: &LayoutCx<'tcx>,
4849
kind: ValidityRequirement,
49-
) -> Result<bool, &'tcx LayoutError<'tcx>> {
50+
) -> bool {
5051
let machine = CompileTimeMachine::new(CanAccessMutGlobal::No, CheckAlignment::Error);
5152

52-
let mut cx = InterpCx::new(cx.tcx(), rustc_span::DUMMY_SP, cx.typing_env, machine);
53+
let mut cx = InterpCx::new(cx.tcx(), DUMMY_SP, cx.typing_env, machine);
5354

5455
let allocated = cx
5556
.allocate(ty, MemoryKind::Machine(crate::const_eval::MemoryKind::Heap))
@@ -69,14 +70,13 @@ fn check_validity_requirement_strict<'tcx>(
6970
// due to this.
7071
// The value we are validating is temporary and discarded at the end of this function, so
7172
// there is no point in reseting provenance and padding.
72-
Ok(cx
73-
.validate_operand(
74-
&allocated.into(),
75-
/*recursive*/ false,
76-
/*reset_provenance_and_padding*/ false,
77-
)
78-
.discard_err()
79-
.is_some())
73+
cx.validate_operand(
74+
&allocated.into(),
75+
/*recursive*/ false,
76+
/*reset_provenance_and_padding*/ false,
77+
)
78+
.discard_err()
79+
.is_some()
8080
}
8181

8282
/// Implements the 'lax' (default) version of the [`check_validity_requirement`] checks; see that
@@ -168,3 +168,31 @@ fn check_validity_requirement_lax<'tcx>(
168168

169169
Ok(true)
170170
}
171+
172+
pub(crate) fn validate_scalar_in_layout<'tcx>(
173+
tcx: TyCtxt<'tcx>,
174+
scalar: ScalarInt,
175+
ty: Ty<'tcx>,
176+
) -> bool {
177+
let machine = CompileTimeMachine::new(CanAccessMutGlobal::No, CheckAlignment::Error);
178+
179+
let typing_env = ty::TypingEnv::fully_monomorphized();
180+
let mut cx = InterpCx::new(tcx, DUMMY_SP, typing_env, machine);
181+
182+
let Ok(layout) = cx.layout_of(ty) else {
183+
bug!("could not compute layout of {scalar:?}:{ty:?}")
184+
};
185+
let allocated = cx
186+
.allocate(layout, MemoryKind::Machine(crate::const_eval::MemoryKind::Heap))
187+
.expect("OOM: failed to allocate for uninit check");
188+
189+
cx.write_scalar(scalar, &allocated).unwrap();
190+
191+
cx.validate_operand(
192+
&allocated.into(),
193+
/*recursive*/ false,
194+
/*reset_provenance_and_padding*/ false,
195+
)
196+
.discard_err()
197+
.is_some()
198+
}

compiler/rustc_const_eval/src/util/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ mod type_name;
88

99
pub use self::alignment::{is_disaligned, is_within_packed};
1010
pub use self::check_validity_requirement::check_validity_requirement;
11+
pub(crate) use self::check_validity_requirement::validate_scalar_in_layout;
1112
pub use self::compare_types::{relate_types, sub_types};
1213
pub use self::type_name::type_name;
1314

compiler/rustc_middle/src/hooks/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ declare_hooks! {
9898
hook save_dep_graph() -> ();
9999

100100
hook query_key_hash_verify_all() -> ();
101+
102+
/// Ensure the given scalar is valid for the given type.
103+
/// This checks non-recursive runtime validity.
104+
hook validate_scalar_in_layout(scalar: crate::ty::ScalarInt, ty: Ty<'tcx>) -> bool;
101105
}
102106

103107
#[cold]

compiler/rustc_middle/src/ty/print/pretty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1741,7 +1741,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
17411741
" as ",
17421742
)?;
17431743
}
1744-
ty::Pat(base_ty, pat) => {
1744+
ty::Pat(base_ty, pat) if self.tcx().validate_scalar_in_layout(int, ty) => {
17451745
self.pretty_print_const_scalar_int(int, *base_ty, print_ty)?;
17461746
p!(write(" is {pat:?}"));
17471747
}

tests/mir-opt/pattern_types.main.PreCodegen.after.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ fn main() -> () {
55
scope 1 {
66
debug x => const 2_u32 is 1..=;
77
scope 2 {
8-
debug y => const 0_u32 is 1..=;
8+
debug y => const {transmute(0x00000000): (u32) is 1..=};
99
}
1010
}
1111

tests/mir-opt/pattern_types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ use std::pat::pattern_type;
77
fn main() {
88
// CHECK: debug x => const 2_u32 is 1..=
99
let x: pattern_type!(u32 is 1..) = unsafe { std::mem::transmute(2) };
10-
// CHECK: debug y => const 0_u32 is 1..=
10+
// CHECK: debug y => const {transmute(0x00000000): (u32) is 1..=}
1111
let y: pattern_type!(u32 is 1..) = unsafe { std::mem::transmute(0) };
1212
}

0 commit comments

Comments
 (0)