Skip to content

Commit 6e9a32b

Browse files
authored
Unrolled build for rust-lang#115972
Rollup merge of rust-lang#115972 - RalfJung:const-consistency, r=oli-obk rename mir::Constant -> mir::ConstOperand, mir::ConstKind -> mir::Const Also, be more consistent with the `to/eval_bits` methods... we had some that take a type and some that take a size, and then sometimes the one that takes a type is called `bits_for_ty`. Turns out that `ty::Const`/`mir::ConstKind` carry their type with them, so we don't need to even pass the type to those `eval_bits` functions at all. However this is not properly consistent yet: in `ty` we have most of the methods on `ty::Const`, but in `mir` we have them on `mir::ConstKind`. And indeed those two types are the ones that correspond to each other. So `mir::ConstantKind` should actually be renamed to `mir::Const`. But what to do with `mir::Constant`? It carries around a span, that's really more like a constant operand that appears as a MIR operand... it's more suited for `syntax.rs` than `consts.rs`, but the bigger question is, which name should it get if we want to align the `mir` and `ty` types? `ConstOperand`? `ConstOp`? `Literal`? It's not a literal but it has a field called `literal` so it would at least be consistently wrong-ish... ``@oli-obk`` any ideas?
2 parents e4a361a + c94410c commit 6e9a32b

File tree

76 files changed

+462
-494
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+462
-494
lines changed

compiler/rustc_borrowck/src/diagnostics/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_index::IndexSlice;
1313
use rustc_infer::infer::LateBoundRegionConversionTime;
1414
use rustc_middle::mir::tcx::PlaceTy;
1515
use rustc_middle::mir::{
16-
AggregateKind, CallSource, Constant, FakeReadCause, Local, LocalInfo, LocalKind, Location,
16+
AggregateKind, CallSource, ConstOperand, FakeReadCause, Local, LocalInfo, LocalKind, Location,
1717
Operand, Place, PlaceRef, ProjectionElem, Rvalue, Statement, StatementKind, Terminator,
1818
TerminatorKind,
1919
};
@@ -101,12 +101,12 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
101101
let terminator = self.body[location.block].terminator();
102102
debug!("add_moved_or_invoked_closure_note: terminator={:?}", terminator);
103103
if let TerminatorKind::Call {
104-
func: Operand::Constant(box Constant { literal, .. }),
104+
func: Operand::Constant(box ConstOperand { const_, .. }),
105105
args,
106106
..
107107
} = &terminator.kind
108108
{
109-
if let ty::FnDef(id, _) = *literal.ty().kind() {
109+
if let ty::FnDef(id, _) = *const_.ty().kind() {
110110
debug!("add_moved_or_invoked_closure_note: id={:?}", id);
111111
if Some(self.infcx.tcx.parent(id)) == self.infcx.tcx.lang_items().fn_once_trait() {
112112
let closure = match args.first() {

compiler/rustc_borrowck/src/renumber.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ use crate::BorrowckInferCtxt;
44
use rustc_index::IndexSlice;
55
use rustc_infer::infer::NllRegionVariableOrigin;
66
use rustc_middle::mir::visit::{MutVisitor, TyContext};
7-
use rustc_middle::mir::Constant;
8-
use rustc_middle::mir::{Body, Location, Promoted};
7+
use rustc_middle::mir::{Body, ConstOperand, Location, Promoted};
98
use rustc_middle::ty::GenericArgsRef;
109
use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable};
1110
use rustc_span::{Span, Symbol};
@@ -117,9 +116,9 @@ impl<'a, 'tcx> MutVisitor<'tcx> for RegionRenumberer<'a, 'tcx> {
117116
}
118117

119118
#[instrument(skip(self), level = "debug")]
120-
fn visit_constant(&mut self, constant: &mut Constant<'tcx>, location: Location) {
121-
let literal = constant.literal;
122-
constant.literal = self.renumber_regions(literal, || RegionCtxt::Location(location));
119+
fn visit_constant(&mut self, constant: &mut ConstOperand<'tcx>, location: Location) {
120+
let const_ = constant.const_;
121+
constant.const_ = self.renumber_regions(const_, || RegionCtxt::Location(location));
123122
debug!("constant: {:#?}", constant);
124123
}
125124
}

compiler/rustc_borrowck/src/type_check/mod.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -302,11 +302,11 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> {
302302
self.sanitize_place(place, location, context);
303303
}
304304

305-
fn visit_constant(&mut self, constant: &Constant<'tcx>, location: Location) {
305+
fn visit_constant(&mut self, constant: &ConstOperand<'tcx>, location: Location) {
306306
debug!(?constant, ?location, "visit_constant");
307307

308308
self.super_constant(constant, location);
309-
let ty = self.sanitize_type(constant, constant.literal.ty());
309+
let ty = self.sanitize_type(constant, constant.const_.ty());
310310

311311
self.cx.infcx.tcx.for_each_free_region(&ty, |live_region| {
312312
let live_region_vid =
@@ -328,7 +328,7 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> {
328328

329329
if let Some(annotation_index) = constant.user_ty {
330330
if let Err(terr) = self.cx.relate_type_and_user_type(
331-
constant.literal.ty(),
331+
constant.const_.ty(),
332332
ty::Variance::Invariant,
333333
&UserTypeProjection { base: annotation_index, projs: vec![] },
334334
locations,
@@ -340,20 +340,20 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> {
340340
constant,
341341
"bad constant user type {:?} vs {:?}: {:?}",
342342
annotation,
343-
constant.literal.ty(),
343+
constant.const_.ty(),
344344
terr,
345345
);
346346
}
347347
} else {
348348
let tcx = self.tcx();
349-
let maybe_uneval = match constant.literal {
350-
ConstantKind::Ty(ct) => match ct.kind() {
349+
let maybe_uneval = match constant.const_ {
350+
Const::Ty(ct) => match ct.kind() {
351351
ty::ConstKind::Unevaluated(_) => {
352-
bug!("should not encounter unevaluated ConstantKind::Ty here, got {:?}", ct)
352+
bug!("should not encounter unevaluated Const::Ty here, got {:?}", ct)
353353
}
354354
_ => None,
355355
},
356-
ConstantKind::Unevaluated(uv, _) => Some(uv),
356+
Const::Unevaluated(uv, _) => Some(uv),
357357
_ => None,
358358
};
359359

@@ -384,15 +384,15 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> {
384384
check_err(self, promoted_body, ty, promoted_ty);
385385
} else {
386386
self.cx.ascribe_user_type(
387-
constant.literal.ty(),
387+
constant.const_.ty(),
388388
UserType::TypeOf(uv.def, UserArgs { args: uv.args, user_self_ty: None }),
389389
locations.span(&self.cx.body),
390390
);
391391
}
392392
} else if let Some(static_def_id) = constant.check_static_ptr(tcx) {
393393
let unnormalized_ty = tcx.type_of(static_def_id).instantiate_identity();
394394
let normalized_ty = self.cx.normalize(unnormalized_ty, locations);
395-
let literal_ty = constant.literal.ty().builtin_deref(true).unwrap().ty;
395+
let literal_ty = constant.const_.ty().builtin_deref(true).unwrap().ty;
396396

397397
if let Err(terr) = self.cx.eq_types(
398398
literal_ty,
@@ -404,7 +404,7 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> {
404404
}
405405
}
406406

407-
if let ty::FnDef(def_id, args) = *constant.literal.ty().kind() {
407+
if let ty::FnDef(def_id, args) = *constant.const_.ty().kind() {
408408
let instantiated_predicates = tcx.predicates_of(def_id).instantiate(tcx, args);
409409
self.cx.normalize_and_prove_instantiated_predicates(
410410
def_id,
@@ -1801,9 +1801,9 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
18011801
debug!(?op, ?location, "check_operand");
18021802

18031803
if let Operand::Constant(constant) = op {
1804-
let maybe_uneval = match constant.literal {
1805-
ConstantKind::Val(..) | ConstantKind::Ty(_) => None,
1806-
ConstantKind::Unevaluated(uv, _) => Some(uv),
1804+
let maybe_uneval = match constant.const_ {
1805+
Const::Val(..) | Const::Ty(_) => None,
1806+
Const::Unevaluated(uv, _) => Some(uv),
18071807
};
18081808

18091809
if let Some(uv) = maybe_uneval {

compiler/rustc_codegen_cranelift/src/constant.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ pub(crate) fn codegen_tls_ref<'tcx>(
6464

6565
pub(crate) fn eval_mir_constant<'tcx>(
6666
fx: &FunctionCx<'_, '_, 'tcx>,
67-
constant: &Constant<'tcx>,
67+
constant: &ConstOperand<'tcx>,
6868
) -> (ConstValue<'tcx>, Ty<'tcx>) {
69-
let cv = fx.monomorphize(constant.literal);
69+
let cv = fx.monomorphize(constant.const_);
7070
// This cannot fail because we checked all required_consts in advance.
7171
let val = cv
7272
.eval(fx.tcx, ty::ParamEnv::reveal_all(), Some(constant.span))
@@ -76,7 +76,7 @@ pub(crate) fn eval_mir_constant<'tcx>(
7676

7777
pub(crate) fn codegen_constant_operand<'tcx>(
7878
fx: &mut FunctionCx<'_, '_, 'tcx>,
79-
constant: &Constant<'tcx>,
79+
constant: &ConstOperand<'tcx>,
8080
) -> CValue<'tcx> {
8181
let (const_val, ty) = eval_mir_constant(fx, constant);
8282
codegen_const_value(fx, const_val, ty)

compiler/rustc_codegen_cranelift/src/inline_asm.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,8 @@ pub(crate) fn codegen_inline_asm<'tcx>(
252252
CInlineAsmOperand::Const { value }
253253
}
254254
InlineAsmOperand::SymFn { ref value } => {
255-
let literal = fx.monomorphize(value.literal);
256-
if let ty::FnDef(def_id, args) = *literal.ty().kind() {
255+
let const_ = fx.monomorphize(value.const_);
256+
if let ty::FnDef(def_id, args) = *const_.ty().kind() {
257257
let instance = ty::Instance::resolve_for_fn_ptr(
258258
fx.tcx,
259259
ty::ParamEnv::reveal_all(),

compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -660,12 +660,12 @@ fn push_const_param<'tcx>(tcx: TyCtxt<'tcx>, ct: ty::Const<'tcx>, output: &mut S
660660
}
661661
_ => match ct.ty().kind() {
662662
ty::Int(ity) => {
663-
let bits = ct.eval_bits(tcx, ty::ParamEnv::reveal_all(), ct.ty());
663+
let bits = ct.eval_bits(tcx, ty::ParamEnv::reveal_all());
664664
let val = Integer::from_int_ty(&tcx, *ity).size().sign_extend(bits) as i128;
665665
write!(output, "{val}")
666666
}
667667
ty::Uint(_) => {
668-
let val = ct.eval_bits(tcx, ty::ParamEnv::reveal_all(), ct.ty());
668+
let val = ct.eval_bits(tcx, ty::ParamEnv::reveal_all());
669669
write!(output, "{val}")
670670
}
671671
ty::Bool => {

compiler/rustc_codegen_ssa/src/mir/block.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1098,8 +1098,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
10981098
InlineAsmOperandRef::Const { string }
10991099
}
11001100
mir::InlineAsmOperand::SymFn { ref value } => {
1101-
let literal = self.monomorphize(value.literal);
1102-
if let ty::FnDef(def_id, args) = *literal.ty().kind() {
1101+
let const_ = self.monomorphize(value.const_);
1102+
if let ty::FnDef(def_id, args) = *const_.ty().kind() {
11031103
let instance = ty::Instance::resolve_for_fn_ptr(
11041104
bx.tcx(),
11051105
ty::ParamEnv::reveal_all(),

compiler/rustc_codegen_ssa/src/mir/constant.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,37 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
1313
pub fn eval_mir_constant_to_operand(
1414
&self,
1515
bx: &mut Bx,
16-
constant: &mir::Constant<'tcx>,
16+
constant: &mir::ConstOperand<'tcx>,
1717
) -> OperandRef<'tcx, Bx::Value> {
1818
let val = self.eval_mir_constant(constant);
1919
let ty = self.monomorphize(constant.ty());
2020
OperandRef::from_const(bx, val, ty)
2121
}
2222

23-
pub fn eval_mir_constant(&self, constant: &mir::Constant<'tcx>) -> mir::ConstValue<'tcx> {
24-
self.monomorphize(constant.literal)
23+
pub fn eval_mir_constant(&self, constant: &mir::ConstOperand<'tcx>) -> mir::ConstValue<'tcx> {
24+
self.monomorphize(constant.const_)
2525
.eval(self.cx.tcx(), ty::ParamEnv::reveal_all(), Some(constant.span))
2626
.expect("erroneous constant not captured by required_consts")
2727
}
2828

2929
/// This is a convenience helper for `simd_shuffle_indices`. It has the precondition
30-
/// that the given `constant` is an `ConstantKind::Unevaluated` and must be convertible to
30+
/// that the given `constant` is an `Const::Unevaluated` and must be convertible to
3131
/// a `ValTree`. If you want a more general version of this, talk to `wg-const-eval` on zulip.
3232
///
3333
/// Note that this function is cursed, since usually MIR consts should not be evaluated to valtrees!
3434
pub fn eval_unevaluated_mir_constant_to_valtree(
3535
&self,
36-
constant: &mir::Constant<'tcx>,
36+
constant: &mir::ConstOperand<'tcx>,
3737
) -> Result<Option<ty::ValTree<'tcx>>, ErrorHandled> {
38-
let uv = match self.monomorphize(constant.literal) {
39-
mir::ConstantKind::Unevaluated(uv, _) => uv.shrink(),
40-
mir::ConstantKind::Ty(c) => match c.kind() {
38+
let uv = match self.monomorphize(constant.const_) {
39+
mir::Const::Unevaluated(uv, _) => uv.shrink(),
40+
mir::Const::Ty(c) => match c.kind() {
4141
// A constant that came from a const generic but was then used as an argument to old-style
4242
// simd_shuffle (passing as argument instead of as a generic param).
4343
rustc_type_ir::ConstKind::Value(valtree) => return Ok(Some(valtree)),
4444
other => span_bug!(constant.span, "{other:#?}"),
4545
},
46-
// We should never encounter `ConstantKind::Val` unless MIR opts (like const prop) evaluate
46+
// We should never encounter `Const::Val` unless MIR opts (like const prop) evaluate
4747
// a constant and write that value back into `Operand`s. This could happen, but is unlikely.
4848
// Also: all users of `simd_shuffle` are on unstable and already need to take a lot of care
4949
// around intrinsics. For an issue to happen here, it would require a macro expanding to a
@@ -65,7 +65,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
6565
pub fn simd_shuffle_indices(
6666
&mut self,
6767
bx: &Bx,
68-
constant: &mir::Constant<'tcx>,
68+
constant: &mir::ConstOperand<'tcx>,
6969
) -> (Bx::Value, Ty<'tcx>) {
7070
let ty = self.monomorphize(constant.ty());
7171
let val = self

compiler/rustc_const_eval/src/interpret/eval_context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1089,7 +1089,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
10891089

10901090
pub fn eval_mir_constant(
10911091
&self,
1092-
val: &mir::ConstantKind<'tcx>,
1092+
val: &mir::Const<'tcx>,
10931093
span: Option<Span>,
10941094
layout: Option<TyAndLayout<'tcx>>,
10951095
) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {

compiler/rustc_const_eval/src/interpret/operand.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
692692

693693
Constant(constant) => {
694694
let c =
695-
self.subst_from_current_frame_and_normalize_erasing_regions(constant.literal)?;
695+
self.subst_from_current_frame_and_normalize_erasing_regions(constant.const_)?;
696696

697697
// This can still fail:
698698
// * During ConstProp, with `TooGeneric` or since the `required_consts` were not all

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -346,20 +346,20 @@ where
346346
};
347347

348348
// Check the qualifs of the value of `const` items.
349-
let uneval = match constant.literal {
350-
ConstantKind::Ty(ct)
349+
let uneval = match constant.const_ {
350+
Const::Ty(ct)
351351
if matches!(
352352
ct.kind(),
353353
ty::ConstKind::Param(_) | ty::ConstKind::Error(_) | ty::ConstKind::Value(_)
354354
) =>
355355
{
356356
None
357357
}
358-
ConstantKind::Ty(c) => {
358+
Const::Ty(c) => {
359359
bug!("expected ConstKind::Param or ConstKind::Value here, found {:?}", c)
360360
}
361-
ConstantKind::Unevaluated(uv, _) => Some(uv),
362-
ConstantKind::Val(..) => None,
361+
Const::Unevaluated(uv, _) => Some(uv),
362+
Const::Val(..) => None,
363363
};
364364

365365
if let Some(mir::UnevaluatedConst { def, args: _, promoted }) = uneval {
@@ -383,5 +383,5 @@ where
383383
}
384384

385385
// Otherwise use the qualifs of the type.
386-
Q::in_any_value_of_ty(cx, constant.literal.ty())
386+
Q::in_any_value_of_ty(cx, constant.const_.ty())
387387
}

compiler/rustc_const_eval/src/transform/promote_consts.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ impl<'tcx> Validator<'_, 'tcx> {
372372
StatementKind::Assign(box (
373373
_,
374374
Rvalue::Use(Operand::Constant(c)),
375-
)) => c.literal.try_eval_target_usize(self.tcx, self.param_env),
375+
)) => c.const_.try_eval_target_usize(self.tcx, self.param_env),
376376
_ => None,
377377
}
378378
} else {
@@ -554,7 +554,7 @@ impl<'tcx> Validator<'_, 'tcx> {
554554
// Integer division: the RHS must be a non-zero const.
555555
let const_val = match rhs {
556556
Operand::Constant(c) => {
557-
c.literal.try_eval_bits(self.tcx, self.param_env, lhs_ty)
557+
c.const_.try_eval_bits(self.tcx, self.param_env)
558558
}
559559
_ => None,
560560
};
@@ -766,10 +766,10 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
766766
if self.keep_original {
767767
rhs.clone()
768768
} else {
769-
let unit = Rvalue::Use(Operand::Constant(Box::new(Constant {
769+
let unit = Rvalue::Use(Operand::Constant(Box::new(ConstOperand {
770770
span: statement.source_info.span,
771771
user_ty: None,
772-
literal: ConstantKind::zero_sized(self.tcx.types.unit),
772+
const_: Const::zero_sized(self.tcx.types.unit),
773773
})));
774774
mem::replace(rhs, unit)
775775
},
@@ -844,10 +844,10 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
844844
let args = tcx.erase_regions(GenericArgs::identity_for_item(tcx, def));
845845
let uneval = mir::UnevaluatedConst { def, args, promoted: Some(promoted_id) };
846846

847-
Operand::Constant(Box::new(Constant {
847+
Operand::Constant(Box::new(ConstOperand {
848848
span,
849849
user_ty: None,
850-
literal: ConstantKind::Unevaluated(uneval, ty),
850+
const_: Const::Unevaluated(uneval, ty),
851851
}))
852852
};
853853

@@ -1041,8 +1041,8 @@ pub fn is_const_fn_in_array_repeat_expression<'tcx>(
10411041
if let Some(Terminator { kind: TerminatorKind::Call { func, destination, .. }, .. }) =
10421042
&block.terminator
10431043
{
1044-
if let Operand::Constant(box Constant { literal, .. }) = func {
1045-
if let ty::FnDef(def_id, _) = *literal.ty().kind() {
1044+
if let Operand::Constant(box ConstOperand { const_, .. }) = func {
1045+
if let ty::FnDef(def_id, _) = *const_.ty().kind() {
10461046
if destination == place {
10471047
if ccx.tcx.is_const_fn(def_id) {
10481048
return true;

0 commit comments

Comments
 (0)