Skip to content

Commit 95ec7ab

Browse files
committed
Introduce Operand::unevaluated_constant.
1 parent a066433 commit 95ec7ab

File tree

5 files changed

+24
-32
lines changed

5 files changed

+24
-32
lines changed

compiler/rustc_middle/src/mir/statement.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,18 @@ impl<'tcx> Operand<'tcx> {
597597
}))
598598
}
599599

600+
/// Convenience helper to make a constant that refers to the given `DefId` and args. Since this
601+
/// is used to synthesize MIR, assumes `user_ty` is None.
602+
pub fn unevaluated_constant(
603+
tcx: TyCtxt<'tcx>,
604+
def_id: DefId,
605+
args: &[GenericArg<'tcx>],
606+
span: Span,
607+
) -> Self {
608+
let const_ = Const::from_unevaluated(tcx, def_id).instantiate(tcx, args);
609+
Operand::Constant(Box::new(ConstOperand { span, user_ty: None, const_ }))
610+
}
611+
600612
pub fn is_move(&self) -> bool {
601613
matches!(self, Operand::Move(..))
602614
}

compiler/rustc_mir_build/src/builder/expr/as_rvalue.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -127,21 +127,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
127127
let source_info = this.source_info(expr_span);
128128

129129
let size = tcx.require_lang_item(LangItem::SizeOf, expr_span);
130-
let size = Const::from_unevaluated(tcx, size).instantiate(tcx, &[value_ty.into()]);
131-
let size = Operand::Constant(Box::new(ConstOperand {
132-
span: expr_span,
133-
user_ty: None,
134-
const_: size,
135-
}));
130+
let size = Operand::unevaluated_constant(tcx, size, &[value_ty.into()], expr_span);
136131

137132
let align = tcx.require_lang_item(LangItem::AlignOf, expr_span);
138133
let align =
139-
Const::from_unevaluated(tcx, align).instantiate(tcx, &[value_ty.into()]);
140-
let align = Operand::Constant(Box::new(ConstOperand {
141-
span: expr_span,
142-
user_ty: None,
143-
const_: align,
144-
}));
134+
Operand::unevaluated_constant(tcx, align, &[value_ty.into()], expr_span);
145135

146136
// malloc some memory of suitable size and align:
147137
let exchange_malloc = Operand::function_handle(

compiler/rustc_mir_transform/src/check_alignment.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,8 @@ fn insert_alignment_check<'tcx>(
6161

6262
// Get the alignment of the pointee
6363
let align_def_id = tcx.require_lang_item(LangItem::AlignOf, source_info.span);
64-
let align_const =
65-
Const::from_unevaluated(tcx, align_def_id).instantiate(tcx, &[pointee_ty.into()]);
66-
let alignment = Operand::Constant(Box::new(ConstOperand {
67-
span: source_info.span,
68-
user_ty: None,
69-
const_: align_const,
70-
}));
64+
let alignment =
65+
Operand::unevaluated_constant(tcx, align_def_id, &[pointee_ty.into()], source_info.span);
7166

7267
// Subtract 1 from the alignment to get the alignment mask
7368
let alignment_mask =

compiler/rustc_mir_transform/src/check_null.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,8 @@ fn insert_null_check<'tcx>(
6969
// Other usages of null pointers only are UB if the pointee is not a ZST.
7070
_ => {
7171
let size_of = tcx.require_lang_item(LangItem::SizeOf, source_info.span);
72-
let size_of = Operand::Constant(Box::new(ConstOperand {
73-
span: source_info.span,
74-
user_ty: None,
75-
const_: Const::from_unevaluated(tcx, size_of)
76-
.instantiate(tcx, &[pointee_ty.into()]),
77-
}));
72+
let size_of =
73+
Operand::unevaluated_constant(tcx, size_of, &[pointee_ty.into()], source_info.span);
7874

7975
let pointee_should_be_checked =
8076
local_decls.push(LocalDecl::with_source_info(tcx.types.bool, source_info)).into();

compiler/rustc_mir_transform/src/instsimplify.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -274,13 +274,12 @@ impl<'tcx> InstSimplifyContext<'_, 'tcx> {
274274
&& let ty::Slice(elem_ty) = *generics.type_at(0).kind()
275275
{
276276
let align_def_id = self.tcx.require_lang_item(LangItem::AlignOf, source_info.span);
277-
let align_const = Const::from_unevaluated(self.tcx, align_def_id)
278-
.instantiate(self.tcx, &[elem_ty.into()]);
279-
let align_const = Operand::Constant(Box::new(ConstOperand {
280-
span: source_info.span,
281-
user_ty: None,
282-
const_: align_const,
283-
}));
277+
let align_const = Operand::unevaluated_constant(
278+
self.tcx,
279+
align_def_id,
280+
&[elem_ty.into()],
281+
source_info.span,
282+
);
284283
statements.push(Statement::new(
285284
source_info,
286285
StatementKind::Assign(Box::new((*destination, Rvalue::Use(align_const)))),

0 commit comments

Comments
 (0)