Skip to content

Commit 7ec029c

Browse files
committed
Pretty print pattern type values with transmute if they don't satisfy their pattern
1 parent 5e55679 commit 7ec029c

File tree

7 files changed

+48
-15
lines changed

7 files changed

+48
-15
lines changed

compiler/rustc_const_eval/src/lib.rs

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

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

compiler/rustc_const_eval/src/util/check_validity_requirement.rs

+38-12
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;
2+
use rustc_middle::query::TyCtxtAt;
33
use rustc_middle::ty::layout::{
44
HasTyCtxt, LayoutCx, LayoutError, LayoutOf, TyAndLayout, ValidityRequirement,
55
};
6-
use rustc_middle::ty::{PseudoCanonicalInput, Ty, TyCtxt};
6+
use rustc_middle::ty::{PseudoCanonicalInput, ScalarInt, Ty, TyCtxt};
7+
use rustc_middle::{bug, ty};
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,7 +47,7 @@ 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

5253
let mut cx = InterpCx::new(cx.tcx(), rustc_span::DUMMY_SP, cx.typing_env, machine);
@@ -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,29 @@ fn check_validity_requirement_lax<'tcx>(
168168

169169
Ok(true)
170170
}
171+
172+
pub(crate) fn validate_scalar_in_layout<'tcx>(
173+
tcx: TyCtxtAt<'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.tcx, tcx.span, typing_env, machine);
181+
182+
let Ok(layout) = cx.layout_of(ty) else { return false };
183+
let allocated = cx
184+
.allocate(layout, MemoryKind::Machine(crate::const_eval::MemoryKind::Heap))
185+
.expect("OOM: failed to allocate for uninit check");
186+
187+
cx.write_scalar(scalar, &allocated).unwrap();
188+
189+
cx.validate_operand(
190+
&allocated.into(),
191+
/*recursive*/ false,
192+
/*reset_provenance_and_padding*/ false,
193+
)
194+
.discard_err()
195+
.is_some()
196+
}

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
@@ -121,6 +121,10 @@ declare_hooks! {
121121
hook save_dep_graph() -> ();
122122

123123
hook query_key_hash_verify_all() -> ();
124+
125+
/// Ensure the given scalar is valid for the given type.
126+
/// This checks non-recursive runtime validity.
127+
hook validate_scalar_in_layout(scalar: crate::ty::ScalarInt, ty: Ty<'tcx>) -> bool;
124128
}
125129

126130
#[cold]

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

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

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)