Skip to content

Commit c7aadcf

Browse files
committed
Change const_field and const_caller_location to return ConstValue instead of Const as the type
in the returned const isn't needed.
1 parent c423a86 commit c7aadcf

File tree

7 files changed

+33
-42
lines changed

7 files changed

+33
-42
lines changed

src/librustc/query/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ rustc_queries! {
517517
/// Extracts a field of a (variant of a) const.
518518
query const_field(
519519
key: ty::ParamEnvAnd<'tcx, (&'tcx ty::Const<'tcx>, mir::Field)>
520-
) -> &'tcx ty::Const<'tcx> {
520+
) -> ConstValue<'tcx> {
521521
no_force
522522
desc { "extract field of const" }
523523
}
@@ -531,7 +531,7 @@ rustc_queries! {
531531
desc { "destructure constant" }
532532
}
533533

534-
query const_caller_location(key: (rustc_span::Symbol, u32, u32)) -> &'tcx ty::Const<'tcx> {
534+
query const_caller_location(key: (rustc_span::Symbol, u32, u32)) -> ConstValue<'tcx> {
535535
no_force
536536
desc { "get a &core::panic::Location referring to a span" }
537537
}

src/librustc/ty/query/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::middle::resolve_lifetime::{ObjectLifetimeDefault, Region, ResolveLife
1414
use crate::middle::stability::{self, DeprecationEntry};
1515
use crate::mir;
1616
use crate::mir::interpret::GlobalId;
17-
use crate::mir::interpret::{ConstEvalRawResult, ConstEvalResult};
17+
use crate::mir::interpret::{ConstEvalRawResult, ConstEvalResult, ConstValue};
1818
use crate::mir::interpret::{LitToConstError, LitToConstInput};
1919
use crate::mir::mono::CodegenUnit;
2020
use crate::session::config::{EntryFnType, OptLevel, OutputFilenames, SymbolManglingVersion};

src/librustc_codegen_llvm/intrinsic.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,7 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
193193
.tcx
194194
.const_eval_instance(ty::ParamEnv::reveal_all(), instance, None)
195195
.unwrap();
196-
let const_ = ty::Const { val: ty::ConstKind::Value(ty_name), ty: ret_ty };
197-
OperandRef::from_const(self, &const_).immediate_or_packed_pair(self)
196+
OperandRef::from_const(self, ty_name, ret_ty).immediate_or_packed_pair(self)
198197
}
199198
"init" => {
200199
let ty = substs.type_at(0);

src/librustc_codegen_ssa/mir/block.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -991,7 +991,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
991991
caller.line as u32,
992992
caller.col_display as u32 + 1,
993993
));
994-
OperandRef::from_const(bx, const_loc)
994+
OperandRef::from_const(bx, const_loc, bx.tcx().caller_location_ty())
995995
})
996996
}
997997

src/librustc_codegen_ssa/mir/constant.rs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::mir::operand::OperandRef;
22
use crate::traits::*;
33
use rustc::mir;
4-
use rustc::mir::interpret::ErrorHandled;
4+
use rustc::mir::interpret::{ConstValue, ErrorHandled};
55
use rustc::ty::layout::{self, HasTyCtxt};
66
use rustc::ty::{self, Ty};
77
use rustc_index::vec::Idx;
@@ -30,27 +30,21 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
3030
}
3131
_ => {
3232
let val = self.eval_mir_constant(constant)?;
33-
Ok(OperandRef::from_const(bx, &val))
33+
Ok(OperandRef::from_const(bx, val.clone(), constant.literal.ty))
3434
}
3535
}
3636
}
3737

3838
pub fn eval_mir_constant(
3939
&mut self,
4040
constant: &mir::Constant<'tcx>,
41-
) -> Result<&'tcx ty::Const<'tcx>, ErrorHandled> {
41+
) -> Result<ConstValue<'tcx>, ErrorHandled> {
4242
match constant.literal.val {
4343
ty::ConstKind::Unevaluated(def_id, substs, promoted) => {
4444
let substs = self.monomorphize(&substs);
4545
self.cx
4646
.tcx()
4747
.const_eval_resolve(ty::ParamEnv::reveal_all(), def_id, substs, promoted, None)
48-
.map(|val| {
49-
self.cx.tcx().mk_const(ty::Const {
50-
val: ty::ConstKind::Value(val),
51-
ty: constant.literal.ty,
52-
})
53-
})
5448
.map_err(|err| {
5549
if promoted.is_none() {
5650
self.cx
@@ -61,7 +55,15 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
6155
err
6256
})
6357
}
64-
_ => Ok(self.monomorphize(&constant.literal)),
58+
ty::ConstKind::Value(value) => Ok(value),
59+
_ => {
60+
let const_ = self.monomorphize(&constant.literal);
61+
if let ty::ConstKind::Value(value) = const_.val {
62+
Ok(value)
63+
} else {
64+
bug!("encountered bad ConstKind in codegen");
65+
}
66+
}
6567
}
6668
}
6769

@@ -71,21 +73,22 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
7173
bx: &Bx,
7274
span: Span,
7375
ty: Ty<'tcx>,
74-
constant: Result<&'tcx ty::Const<'tcx>, ErrorHandled>,
76+
constant: Result<ConstValue<'tcx>, ErrorHandled>,
7577
) -> (Bx::Value, Ty<'tcx>) {
7678
constant
77-
.map(|c| {
78-
let field_ty = c.ty.builtin_index().unwrap();
79-
let fields = match c.ty.kind {
79+
.map(|val| {
80+
let field_ty = ty.builtin_index().unwrap();
81+
let fields = match ty.kind {
8082
ty::Array(_, n) => n.eval_usize(bx.tcx(), ty::ParamEnv::reveal_all()),
81-
_ => bug!("invalid simd shuffle type: {}", c.ty),
83+
_ => bug!("invalid simd shuffle type: {}", ty),
8284
};
85+
let c = bx.tcx().mk_const(ty::Const { val: ty::ConstKind::Value(val), ty });
8386
let values: Vec<_> = (0..fields)
8487
.map(|field| {
8588
let field = bx.tcx().const_field(
8689
ty::ParamEnv::reveal_all().and((&c, mir::Field::new(field as usize))),
8790
);
88-
if let Some(prim) = field.val.try_to_scalar() {
91+
if let Some(prim) = field.try_to_scalar() {
8992
let layout = bx.layout_of(field_ty);
9093
let scalar = match layout.abi {
9194
layout::Abi::Scalar(ref x) => x,

src/librustc_codegen_ssa/mir/operand.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use crate::MemFlags;
88

99
use rustc::mir;
1010
use rustc::mir::interpret::{ConstValue, ErrorHandled, Pointer, Scalar};
11-
use rustc::ty;
1211
use rustc::ty::layout::{self, Align, LayoutOf, Size, TyLayout};
12+
use rustc::ty::Ty;
1313

1414
use std::fmt;
1515

@@ -66,20 +66,16 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
6666

6767
pub fn from_const<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
6868
bx: &mut Bx,
69-
val: &ty::Const<'tcx>,
69+
val: ConstValue<'tcx>,
70+
ty: Ty<'tcx>,
7071
) -> Self {
71-
let layout = bx.layout_of(val.ty);
72+
let layout = bx.layout_of(ty);
7273

7374
if layout.is_zst() {
7475
return OperandRef::new_zst(bx, layout);
7576
}
7677

77-
let val_val = match val.val {
78-
ty::ConstKind::Value(val_val) => val_val,
79-
_ => bug!("encountered bad ConstKind in codegen"),
80-
};
81-
82-
let val = match val_val {
78+
let val = match val {
8379
ConstValue::Scalar(x) => {
8480
let scalar = match layout.abi {
8581
layout::Abi::Scalar(ref x) => x,

src/librustc_mir/const_eval.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub(crate) fn const_field<'tcx>(
2626
variant: Option<VariantIdx>,
2727
field: mir::Field,
2828
value: &'tcx ty::Const<'tcx>,
29-
) -> &'tcx ty::Const<'tcx> {
29+
) -> ConstValue<'tcx> {
3030
trace!("const_field: {:?}, {:?}", field, value);
3131
let ecx = mk_eval_cx(tcx, DUMMY_SP, param_env, false);
3232
// get the operand again
@@ -40,26 +40,19 @@ pub(crate) fn const_field<'tcx>(
4040
let field = ecx.operand_field(down, field.index() as u64).unwrap();
4141
// and finally move back to the const world, always normalizing because
4242
// this is not called for statics.
43-
let val = op_to_const(&ecx, field);
44-
tcx.mk_const(ty::Const { val: ty::ConstKind::Value(val), ty: op.layout.ty })
43+
op_to_const(&ecx, field)
4544
}
4645

4746
pub(crate) fn const_caller_location<'tcx>(
4847
tcx: TyCtxt<'tcx>,
4948
(file, line, col): (Symbol, u32, u32),
50-
) -> &'tcx ty::Const<'tcx> {
49+
) -> ConstValue<'tcx> {
5150
trace!("const_caller_location: {}:{}:{}", file, line, col);
5251
let mut ecx = mk_eval_cx(tcx, DUMMY_SP, ty::ParamEnv::reveal_all(), false);
5352

54-
let loc_ty = tcx.caller_location_ty();
5553
let loc_place = ecx.alloc_caller_location(file, line, col);
5654
intern_const_alloc_recursive(&mut ecx, InternKind::Constant, loc_place, false).unwrap();
57-
let loc_const = ty::Const {
58-
ty: loc_ty,
59-
val: ty::ConstKind::Value(ConstValue::Scalar(loc_place.ptr.into())),
60-
};
61-
62-
tcx.mk_const(loc_const)
55+
ConstValue::Scalar(loc_place.ptr.into())
6356
}
6457

6558
// this function uses `unwrap` copiously, because an already validated constant

0 commit comments

Comments
 (0)