Skip to content

Commit ff6cd18

Browse files
authored
Rollup merge of #130644 - compiler-errors:consts-in-codegen, r=BoxyUwU
Only expect valtree consts in codegen Turn a bunch of `Const::eval_*` calls into `Const::try_to_*` calls, which implicitly assert that we only have valtrees by the time we get to codegen. r? `@BoxyUwU`
2 parents 4336628 + c0d1a13 commit ff6cd18

File tree

11 files changed

+40
-22
lines changed

11 files changed

+40
-22
lines changed

Diff for: compiler/rustc_codegen_cranelift/src/base.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -785,8 +785,10 @@ fn codegen_stmt<'tcx>(
785785
}
786786
Rvalue::Repeat(ref operand, times) => {
787787
let operand = codegen_operand(fx, operand);
788-
let times =
789-
fx.monomorphize(times).eval_target_usize(fx.tcx, ParamEnv::reveal_all());
788+
let times = fx
789+
.monomorphize(times)
790+
.try_to_target_usize(fx.tcx)
791+
.expect("expected monomorphic const in codegen");
790792
if operand.layout().size.bytes() == 0 {
791793
// Do nothing for ZST's
792794
} else if fx.clif_type(operand.layout().ty) == Some(types::I8) {
@@ -944,7 +946,10 @@ fn codegen_stmt<'tcx>(
944946
fn codegen_array_len<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, place: CPlace<'tcx>) -> Value {
945947
match *place.layout().ty.kind() {
946948
ty::Array(_elem_ty, len) => {
947-
let len = fx.monomorphize(len).eval_target_usize(fx.tcx, ParamEnv::reveal_all()) as i64;
949+
let len = fx
950+
.monomorphize(len)
951+
.try_to_target_usize(fx.tcx)
952+
.expect("expected monomorphic const in codegen") as i64;
948953
fx.bcx.ins().iconst(fx.pointer_type, len)
949954
}
950955
ty::Slice(_elem_ty) => place.to_ptr_unsized().1,

Diff for: compiler/rustc_codegen_cranelift/src/debuginfo/types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl DebugContext {
4444
type_dbg,
4545
ty,
4646
*elem_ty,
47-
len.eval_target_usize(tcx, ty::ParamEnv::reveal_all()),
47+
len.try_to_target_usize(tcx).expect("expected monomorphic const in codegen"),
4848
),
4949
// ty::Slice(_) | ty::Str
5050
// ty::Dynamic

Diff for: compiler/rustc_codegen_cranelift/src/intrinsics/simd.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,8 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>(
131131

132132
let idx = generic_args[2]
133133
.expect_const()
134-
.eval(fx.tcx, ty::ParamEnv::reveal_all(), span)
135-
.unwrap()
136-
.1
134+
.try_to_valtree()
135+
.expect("expected monomorphic const in codegen")
137136
.unwrap_branch();
138137

139138
assert_eq!(x.layout(), y.layout());

Diff for: compiler/rustc_codegen_cranelift/src/unsize.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ pub(crate) fn unsized_info<'tcx>(
2424
let (source, target) =
2525
fx.tcx.struct_lockstep_tails_for_codegen(source, target, ParamEnv::reveal_all());
2626
match (&source.kind(), &target.kind()) {
27-
(&ty::Array(_, len), &ty::Slice(_)) => fx
28-
.bcx
29-
.ins()
30-
.iconst(fx.pointer_type, len.eval_target_usize(fx.tcx, ParamEnv::reveal_all()) as i64),
27+
(&ty::Array(_, len), &ty::Slice(_)) => fx.bcx.ins().iconst(
28+
fx.pointer_type,
29+
len.try_to_target_usize(fx.tcx).expect("expected monomorphic const in codegen") as i64,
30+
),
3131
(&ty::Dynamic(data_a, _, src_dyn_kind), &ty::Dynamic(data_b, _, target_dyn_kind))
3232
if src_dyn_kind == target_dyn_kind =>
3333
{

Diff for: compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,9 @@ fn build_fixed_size_array_di_node<'ll, 'tcx>(
125125

126126
let (size, align) = cx.size_and_align_of(array_type);
127127

128-
let upper_bound = len.eval_target_usize(cx.tcx, ty::ParamEnv::reveal_all()) as c_longlong;
128+
let upper_bound = len
129+
.try_to_target_usize(cx.tcx)
130+
.expect("expected monomorphic const in codegen") as c_longlong;
129131

130132
let subrange =
131133
unsafe { Some(llvm::LLVMRustDIBuilderGetOrCreateSubrange(DIB(cx), 0, upper_bound)) };

Diff for: compiler/rustc_codegen_ssa/src/base.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ fn unsized_info<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
115115
let (source, target) =
116116
cx.tcx().struct_lockstep_tails_for_codegen(source, target, bx.param_env());
117117
match (source.kind(), target.kind()) {
118-
(&ty::Array(_, len), &ty::Slice(_)) => {
119-
cx.const_usize(len.eval_target_usize(cx.tcx(), ty::ParamEnv::reveal_all()))
120-
}
118+
(&ty::Array(_, len), &ty::Slice(_)) => cx.const_usize(
119+
len.try_to_target_usize(cx.tcx()).expect("expected monomorphic const in codegen"),
120+
),
121121
(&ty::Dynamic(data_a, _, src_dyn_kind), &ty::Dynamic(data_b, _, target_dyn_kind))
122122
if src_dyn_kind == target_dyn_kind =>
123123
{

Diff for: compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,8 @@ fn push_debuginfo_type_name<'tcx>(
188188
_ => write!(
189189
output,
190190
",{}>",
191-
len.eval_target_usize(tcx, ty::ParamEnv::reveal_all())
191+
len.try_to_target_usize(tcx)
192+
.expect("expected monomorphic const in codegen")
192193
)
193194
.unwrap(),
194195
}
@@ -200,7 +201,8 @@ fn push_debuginfo_type_name<'tcx>(
200201
_ => write!(
201202
output,
202203
"; {}]",
203-
len.eval_target_usize(tcx, ty::ParamEnv::reveal_all())
204+
len.try_to_target_usize(tcx)
205+
.expect("expected monomorphic const in codegen")
204206
)
205207
.unwrap(),
206208
}

Diff for: compiler/rustc_codegen_ssa/src/mir/rvalue.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
114114

115115
let count = self
116116
.monomorphize(count)
117-
.eval_target_usize(bx.cx().tcx(), ty::ParamEnv::reveal_all());
117+
.try_to_target_usize(bx.tcx())
118+
.expect("expected monomorphic const in codegen");
118119

119120
bx.write_operand_repeatedly(cg_elem, count, dest);
120121
}
@@ -803,7 +804,9 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
803804
if let Some(index) = place.as_local() {
804805
if let LocalRef::Operand(op) = self.locals[index] {
805806
if let ty::Array(_, n) = op.layout.ty.kind() {
806-
let n = n.eval_target_usize(bx.cx().tcx(), ty::ParamEnv::reveal_all());
807+
let n = n
808+
.try_to_target_usize(bx.tcx())
809+
.expect("expected monomorphic const in codegen");
807810
return bx.cx().const_usize(n);
808811
}
809812
}

Diff for: compiler/rustc_middle/src/ty/consts.rs

+4
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,10 @@ impl<'tcx> Const<'tcx> {
521521
self.try_to_valtree()?.try_to_scalar()
522522
}
523523

524+
pub fn try_to_bool(self) -> Option<bool> {
525+
self.try_to_scalar()?.to_bool().ok()
526+
}
527+
524528
#[inline]
525529
pub fn try_to_target_usize(self, tcx: TyCtxt<'tcx>) -> Option<u64> {
526530
self.try_to_valtree()?.try_to_target_usize(tcx)

Diff for: compiler/rustc_sanitizers/src/cfi/typeid/itanium_cxx_abi/encode.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ fn encode_const<'tcx>(
145145
let _ = write!(s, "{val}");
146146
}
147147
ty::Bool => {
148-
let val = c.try_eval_bool(tcx, ty::ParamEnv::reveal_all()).unwrap();
148+
let val = c.try_to_bool().expect("expected monomorphic const in cfi");
149149
let _ = write!(s, "{val}");
150150
}
151151
_ => {
@@ -411,7 +411,7 @@ pub fn encode_ty<'tcx>(
411411

412412
ty::Array(ty0, len) => {
413413
// A<array-length><element-type>
414-
let len = len.eval_target_usize(tcx, ty::ParamEnv::reveal_all());
414+
let len = len.try_to_target_usize(tcx).expect("expected monomorphic const in cfi");
415415
let mut s = String::from("A");
416416
let _ = write!(s, "{len}");
417417
s.push_str(&encode_ty(tcx, *ty0, dict, options));

Diff for: compiler/rustc_sanitizers/src/cfi/typeid/itanium_cxx_abi/transform.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,10 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for TransformTy<'tcx> {
146146
!is_zst
147147
});
148148
if let Some(field) = field {
149-
let ty0 = self.tcx.erase_regions(field.ty(self.tcx, args));
149+
let ty0 = self.tcx.normalize_erasing_regions(
150+
ty::ParamEnv::reveal_all(),
151+
field.ty(self.tcx, args),
152+
);
150153
// Generalize any repr(transparent) user-defined type that is either a
151154
// pointer or reference, and either references itself or any other type that
152155
// contains or references itself, to avoid a reference cycle.

0 commit comments

Comments
 (0)