Remove redundant ty fields from mir::Constant and hair::pattern::PatternRange.

This commit is contained in:
Eduard-Mihai Burtescu 2019-08-12 18:15:13 +03:00
parent 5a6d801bf9
commit b565ece5d8
26 changed files with 46 additions and 115 deletions

View File

@ -2197,7 +2197,6 @@ impl<'tcx> Operand<'tcx> {
let ty = tcx.type_of(def_id).subst(tcx, substs);
Operand::Constant(box Constant {
span,
ty,
user_ty: None,
literal: ty::Const::zero_sized(tcx, ty),
})
@ -2476,7 +2475,6 @@ impl<'tcx> Debug for Rvalue<'tcx> {
#[derive(Copy, Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable, HashStable)]
pub struct Constant<'tcx> {
pub span: Span,
pub ty: Ty<'tcx>,
/// Optional user-given type: for something like
/// `collect::<Vec<_>>`, this would be present and would
@ -3385,12 +3383,11 @@ impl<'tcx> TypeFoldable<'tcx> for Constant<'tcx> {
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
Constant {
span: self.span.clone(),
ty: self.ty.fold_with(folder),
user_ty: self.user_ty.fold_with(folder),
literal: self.literal.fold_with(folder),
}
}
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
self.ty.visit_with(visitor) || self.literal.visit_with(visitor)
self.literal.visit_with(visitor)
}
}

View File

@ -252,7 +252,7 @@ impl<'tcx> Operand<'tcx> {
match self {
&Operand::Copy(ref l) |
&Operand::Move(ref l) => l.ty(local_decls, tcx).ty,
&Operand::Constant(ref c) => c.ty,
&Operand::Constant(ref c) => c.literal.ty,
}
}
}

View File

@ -782,13 +782,11 @@ macro_rules! make_mir_visitor {
location: Location) {
let Constant {
span,
ty,
user_ty,
literal,
} = constant;
self.visit_span(span);
self.visit_ty(ty, TyContext::Location(location));
drop(user_ty); // no visit method for this
self.visit_const(literal, location);
}

View File

@ -221,7 +221,7 @@ impl<'mir, 'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> Visitor<'tcx>
mir::TerminatorKind::Call {
func: mir::Operand::Constant(ref c),
ref args, ..
} => match c.ty.sty {
} => match c.literal.ty.sty {
ty::FnDef(did, _) => Some((did, args)),
_ => None,
},

View File

@ -651,7 +651,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
let (llval, ty) = self.simd_shuffle_indices(
&bx,
constant.span,
constant.ty,
constant.literal.ty,
c,
);
return OperandRef {

View File

@ -466,7 +466,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
}
mir::Operand::Constant(ref constant) => {
let ty = self.monomorphize(&constant.ty);
self.eval_mir_constant(constant)
.map(|c| OperandRef::from_const(bx, c))
.unwrap_or_else(|err| {
@ -481,6 +480,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
// the above error (or silence it under some conditions) will not cause UB
bx.abort();
// We've errored, so we don't have to produce working code.
let ty = self.monomorphize(&constant.literal.ty);
let layout = bx.cx().layout_of(ty);
bx.load_operand(PlaceRef::new_sized(
bx.cx().const_undef(bx.cx().type_ptr_to(bx.cx().backend_type(layout))),

View File

@ -272,12 +272,11 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> {
fn visit_constant(&mut self, constant: &Constant<'tcx>, location: Location) {
self.super_constant(constant, location);
self.sanitize_constant(constant, location);
self.sanitize_type(constant, constant.ty);
self.sanitize_type(constant, constant.literal.ty);
if let Some(annotation_index) = constant.user_ty {
if let Err(terr) = self.cx.relate_type_and_user_type(
constant.ty,
constant.literal.ty,
ty::Variance::Invariant,
&UserTypeProjection { base: annotation_index, projs: vec![], },
location.to_locations(),
@ -289,7 +288,7 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> {
constant,
"bad constant user type {:?} vs {:?}: {:?}",
annotation,
constant.ty,
constant.literal.ty,
terr,
);
}
@ -299,7 +298,7 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> {
location.to_locations(),
ConstraintCategory::Boring,
self.cx.param_env.and(type_op::ascribe_user_type::AscribeUserType::new(
constant.ty, def_id, UserSubsts { substs, user_self_ty: None },
constant.literal.ty, def_id, UserSubsts { substs, user_self_ty: None },
)),
) {
span_mirbug!(
@ -403,41 +402,6 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
}
}
/// Checks that the constant's `ty` field matches up with what would be
/// expected from its literal. Unevaluated constants and well-formed
/// constraints are checked by `visit_constant`.
fn sanitize_constant(&mut self, constant: &Constant<'tcx>, location: Location) {
debug!(
"sanitize_constant(constant={:?}, location={:?})",
constant, location
);
let literal = constant.literal;
if let ConstValue::Unevaluated(..) = literal.val {
return;
}
debug!("sanitize_constant: expected_ty={:?}", literal.ty);
if let Err(terr) = self.cx.eq_types(
literal.ty,
constant.ty,
location.to_locations(),
ConstraintCategory::Boring,
) {
span_mirbug!(
self,
constant,
"constant {:?} should have type {:?} but has {:?} ({:?})",
constant,
literal.ty,
constant.ty,
terr,
);
}
}
/// Checks that the types internal to the `place` match up with
/// what would be expected.
fn sanitize_place(

View File

@ -40,7 +40,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
});
Constant {
span,
ty,
user_ty,
literal,
}

View File

@ -591,7 +591,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let n = (!0u128) >> (128 - bits);
let literal = ty::Const::from_bits(self.hir.tcx(), n, param_ty);
self.literal_operand(span, ty, literal)
self.literal_operand(span, literal)
}
// Helper to get the minimum value of the appropriate type
@ -602,6 +602,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let n = 1 << (bits - 1);
let literal = ty::Const::from_bits(self.hir.tcx(), n, param_ty);
self.literal_operand(span, ty, literal)
self.literal_operand(span, literal)
}
}

View File

@ -114,7 +114,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
destination,
Constant {
span: expr_span,
ty: this.hir.bool_ty(),
user_ty: None,
literal: this.hir.true_literal(),
},
@ -126,7 +125,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
destination,
Constant {
span: expr_span,
ty: this.hir.bool_ty(),
user_ty: None,
literal: this.hir.false_literal(),
},

View File

@ -108,8 +108,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
Err(match_pair)
}
PatternKind::Range(PatternRange { lo, hi, ty, end }) => {
let (range, bias) = match ty.sty {
PatternKind::Range(PatternRange { lo, hi, end }) => {
let (range, bias) = match lo.ty.sty {
ty::Char => {
(Some(('\u{0000}' as u128, '\u{10FFFF}' as u128, Size::from_bits(32))), 0)
}

View File

@ -63,7 +63,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
}
PatternKind::Range(range) => {
assert!(range.ty == match_pair.pattern.ty);
Test {
span: match_pair.pattern.span,
kind: TestKind::Range(range),
@ -270,8 +269,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
);
} else {
if let [success, fail] = *make_target_blocks(self) {
let expect = self.literal_operand(test.span, value);
let val = Operand::Copy(place.clone());
let expect = self.literal_operand(test.span, ty, value);
self.compare(block, success, fail, source_info, BinOp::Eq, expect, val);
} else {
bug!("`TestKind::Eq` should have two target blocks");
@ -279,13 +278,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
}
}
TestKind::Range(PatternRange { ref lo, ref hi, ty, ref end }) => {
TestKind::Range(PatternRange { ref lo, ref hi, ref end }) => {
let lower_bound_success = self.cfg.start_new_block();
let target_blocks = make_target_blocks(self);
// Test `val` by computing `lo <= val && val <= hi`, using primitive comparisons.
let lo = self.literal_operand(test.span, ty, lo);
let hi = self.literal_operand(test.span, ty, hi);
let lo = self.literal_operand(test.span, lo);
let hi = self.literal_operand(test.span, hi);
let val = Operand::Copy(place.clone());
if let [success, fail] = *target_blocks {
@ -387,7 +386,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
) {
use rustc::middle::lang_items::EqTraitLangItem;
let mut expect = self.literal_operand(source_info.span, value.ty, value);
let mut expect = self.literal_operand(source_info.span, value);
let mut val = Operand::Copy(place.clone());
// If we're using `b"..."` as a pattern, we need to insert an
@ -440,7 +439,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
};
let eq_def_id = self.hir.tcx().require_lang_item(EqTraitLangItem);
let (mty, method) = self.hir.trait_method(eq_def_id, sym::eq, deref_ty, &[deref_ty.into()]);
let method = self.hir.trait_method(eq_def_id, sym::eq, deref_ty, &[deref_ty.into()]);
let bool_ty = self.hir.bool_ty();
let eq_result = self.temp(bool_ty, source_info.span);
@ -449,7 +448,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
self.cfg.terminate(block, source_info, TerminatorKind::Call {
func: Operand::Constant(box Constant {
span: source_info.span,
ty: mty,
// FIXME(#54571): This constant comes from user input (a
// constant in a pattern). Are there forms where users can add
@ -656,8 +654,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let tcx = self.hir.tcx();
let lo = compare_const_vals(tcx, test.lo, pat.hi, self.hir.param_env, test.ty)?;
let hi = compare_const_vals(tcx, test.hi, pat.lo, self.hir.param_env, test.ty)?;
let test_ty = test.lo.ty;
let lo = compare_const_vals(tcx, test.lo, pat.hi, self.hir.param_env, test_ty)?;
let hi = compare_const_vals(tcx, test.hi, pat.lo, self.hir.param_env, test_ty)?;
match (test.end, pat.end, lo, hi) {
// pat < test
@ -774,8 +773,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let tcx = self.hir.tcx();
let a = compare_const_vals(tcx, range.lo, value, self.hir.param_env, range.ty)?;
let b = compare_const_vals(tcx, value, range.hi, self.hir.param_env, range.ty)?;
let a = compare_const_vals(tcx, range.lo, value, self.hir.param_env, range.lo.ty)?;
let b = compare_const_vals(tcx, value, range.hi, self.hir.param_env, range.lo.ty)?;
match (b, range.end) {
(Less, _) |

View File

@ -26,12 +26,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
/// without any user type annotation.
pub fn literal_operand(&mut self,
span: Span,
ty: Ty<'tcx>,
literal: &'tcx ty::Const<'tcx>)
-> Operand<'tcx> {
let constant = box Constant {
span,
ty,
user_ty: None,
literal,
};
@ -47,7 +45,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
pub fn zero_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {
let literal = ty::Const::from_bits(self.hir.tcx(), 0, ty::ParamEnv::empty().and(ty));
self.literal_operand(span, ty, literal)
self.literal_operand(span, literal)
}
pub fn push_usize(&mut self,
@ -61,7 +59,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
block, source_info, &temp,
Constant {
span: source_info.span,
ty: self.hir.usize_ty(),
user_ty: None,
literal: self.hir.usize_literal(value),
});

View File

@ -170,13 +170,13 @@ impl<'a, 'tcx> Cx<'a, 'tcx> {
method_name: Symbol,
self_ty: Ty<'tcx>,
params: &[Kind<'tcx>])
-> (Ty<'tcx>, &'tcx ty::Const<'tcx>) {
-> &'tcx ty::Const<'tcx> {
let substs = self.tcx.mk_substs_trait(self_ty, params);
for item in self.tcx.associated_items(trait_def_id) {
if item.kind == ty::AssocKind::Method && item.ident.name == method_name {
let method_ty = self.tcx.type_of(item.def_id);
let method_ty = method_ty.subst(self.tcx, substs);
return (method_ty, ty::Const::zero_sized(self.tcx, method_ty));
return ty::Const::zero_sized(self.tcx, method_ty);
}
}

View File

@ -609,7 +609,6 @@ impl<'tcx> Witness<'tcx> {
ConstantRange(lo, hi, ty, end) => PatternKind::Range(PatternRange {
lo: ty::Const::from_bits(cx.tcx, lo, ty::ParamEnv::empty().and(ty)),
hi: ty::Const::from_bits(cx.tcx, hi, ty::ParamEnv::empty().and(ty)),
ty,
end,
}),
_ => PatternKind::Wild,
@ -880,10 +879,10 @@ impl<'tcx> IntRange<'tcx> {
let range = loop {
match pat.kind {
box PatternKind::Constant { value } => break ConstantValue(value),
box PatternKind::Range(PatternRange { lo, hi, ty, end }) => break ConstantRange(
lo.eval_bits(tcx, param_env, ty),
hi.eval_bits(tcx, param_env, ty),
ty,
box PatternKind::Range(PatternRange { lo, hi, end }) => break ConstantRange(
lo.eval_bits(tcx, param_env, lo.ty),
hi.eval_bits(tcx, param_env, hi.ty),
lo.ty,
end,
),
box PatternKind::AscribeUserType { ref subpattern, .. } => {
@ -1339,11 +1338,11 @@ fn pat_constructors<'tcx>(cx: &mut MatchCheckCtxt<'_, 'tcx>,
Some(vec![Variant(adt_def.variants[variant_index].def_id)])
}
PatternKind::Constant { value } => Some(vec![ConstantValue(value)]),
PatternKind::Range(PatternRange { lo, hi, ty, end }) =>
PatternKind::Range(PatternRange { lo, hi, end }) =>
Some(vec![ConstantRange(
lo.eval_bits(cx.tcx, cx.param_env, ty),
hi.eval_bits(cx.tcx, cx.param_env, ty),
ty,
lo.eval_bits(cx.tcx, cx.param_env, lo.ty),
hi.eval_bits(cx.tcx, cx.param_env, hi.ty),
lo.ty,
end,
)]),
PatternKind::Array { .. } => match pcx.ty.sty {
@ -1656,7 +1655,7 @@ fn constructor_covered_by_range<'tcx>(
) -> Result<bool, ErrorReported> {
let (from, to, end, ty) = match pat.kind {
box PatternKind::Constant { value } => (value, value, RangeEnd::Included, value.ty),
box PatternKind::Range(PatternRange { lo, hi, end, ty }) => (lo, hi, end, ty),
box PatternKind::Range(PatternRange { lo, hi, end }) => (lo, hi, end, lo.ty),
_ => bug!("`constructor_covered_by_range` called with {:?}", pat),
};
trace!("constructor_covered_by_range {:#?}, {:#?}, {:#?}, {}", ctor, from, to, ty);

View File

@ -181,7 +181,6 @@ pub enum PatternKind<'tcx> {
pub struct PatternRange<'tcx> {
pub lo: &'tcx ty::Const<'tcx>,
pub hi: &'tcx ty::Const<'tcx>,
pub ty: Ty<'tcx>,
pub end: RangeEnd,
}
@ -296,7 +295,7 @@ impl<'tcx> fmt::Display for Pattern<'tcx> {
PatternKind::Constant { value } => {
write!(f, "{}", value)
}
PatternKind::Range(PatternRange { lo, hi, ty: _, end }) => {
PatternKind::Range(PatternRange { lo, hi, end }) => {
write!(f, "{}", lo)?;
match end {
RangeEnd::Included => write!(f, "..=")?,
@ -451,7 +450,7 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
);
match (end, cmp) {
(RangeEnd::Excluded, Some(Ordering::Less)) =>
PatternKind::Range(PatternRange { lo, hi, ty, end }),
PatternKind::Range(PatternRange { lo, hi, end }),
(RangeEnd::Excluded, _) => {
span_err!(
self.tcx.sess,
@ -465,7 +464,7 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
PatternKind::Constant { value: lo }
}
(RangeEnd::Included, Some(Ordering::Less)) => {
PatternKind::Range(PatternRange { lo, hi, ty, end })
PatternKind::Range(PatternRange { lo, hi, end })
}
(RangeEnd::Included, _) => {
let mut err = struct_span_err!(
@ -1416,17 +1415,7 @@ impl<'tcx> PatternFoldable<'tcx> for PatternKind<'tcx> {
} => PatternKind::Constant {
value,
},
PatternKind::Range(PatternRange {
lo,
hi,
ty,
end,
}) => PatternKind::Range(PatternRange {
lo,
hi,
ty: ty.fold_with(folder),
end,
}),
PatternKind::Range(range) => PatternKind::Range(range),
PatternKind::Slice {
ref prefix,
ref slice,

View File

@ -445,7 +445,6 @@ impl CloneShimBuilder<'tcx> {
let func_ty = tcx.mk_fn_def(self.def_id, substs);
let func = Operand::Constant(box Constant {
span: self.span,
ty: func_ty,
user_ty: None,
literal: ty::Const::zero_sized(tcx, func_ty),
});
@ -505,7 +504,6 @@ impl CloneShimBuilder<'tcx> {
fn make_usize(&self, value: u64) -> Box<Constant<'tcx>> {
box Constant {
span: self.span,
ty: self.tcx.types.usize,
user_ty: None,
literal: ty::Const::from_usize(self.tcx, value),
}
@ -745,7 +743,6 @@ fn build_call_shim<'tcx>(
let ty = tcx.type_of(def_id);
(Operand::Constant(box Constant {
span,
ty,
user_ty: None,
literal: ty::Const::zero_sized(tcx, ty),
}),

View File

@ -539,7 +539,6 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
Operand::Constant(Box::new(
Constant {
span,
ty,
user_ty: None,
literal: self.tcx.mk_const(*ty::Const::from_scalar(
self.tcx,

View File

@ -527,7 +527,6 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
fn constant_bool(&self, span: Span, val: bool) -> Rvalue<'tcx> {
Rvalue::Use(Operand::Constant(Box::new(Constant {
span,
ty: self.tcx.types.bool,
user_ty: None,
literal: ty::Const::from_bool(self.tcx, val),
})))

View File

@ -975,7 +975,6 @@ fn insert_panic_block<'tcx>(
let term = TerminatorKind::Assert {
cond: Operand::Constant(box Constant {
span: body.span,
ty: tcx.types.bool,
user_ty: None,
literal: ty::Const::from_bool(tcx, false),
}),

View File

@ -328,7 +328,7 @@ impl Inliner<'tcx> {
}
TerminatorKind::Call {func: Operand::Constant(ref f), .. } => {
if let ty::FnDef(def_id, _) = f.ty.sty {
if let ty::FnDef(def_id, _) = f.literal.ty.sty {
// Don't give intrinsics the extra penalty for calls
let f = tcx.fn_sig(def_id);
if f.abi() == Abi::RustIntrinsic || f.abi() == Abi::PlatformIntrinsic {

View File

@ -97,8 +97,7 @@ impl Visitor<'tcx> for OptimizationFinder<'b, 'tcx> {
let place_ty = place.ty(&self.body.local_decls, self.tcx).ty;
if let ty::Array(_, len) = place_ty.sty {
let span = self.body.source_info(location).span;
let ty = self.tcx.types.usize;
let constant = Constant { span, ty, literal: len, user_ty: None };
let constant = Constant { span, literal: len, user_ty: None };
self.optimizations.arrays_lengths.insert(location, constant);
}
}

View File

@ -249,7 +249,7 @@ trait Qualif {
if let ConstValue::Unevaluated(def_id, _) = constant.literal.val {
// Don't peek inside trait associated constants.
if cx.tcx.trait_of_item(def_id).is_some() {
Self::in_any_value_of_ty(cx, constant.ty).unwrap_or(false)
Self::in_any_value_of_ty(cx, constant.literal.ty).unwrap_or(false)
} else {
let (bits, _) = cx.tcx.at(constant.span).mir_const_qualif(def_id);
@ -258,7 +258,7 @@ trait Qualif {
// Just in case the type is more specific than
// the definition, e.g., impl associated const
// with type parameters, take it into account.
qualif && Self::mask_for_ty(cx, constant.ty)
qualif && Self::mask_for_ty(cx, constant.literal.ty)
}
} else {
false

View File

@ -224,7 +224,7 @@ fn is_rustc_peek<'a, 'tcx>(
if let Some(mir::Terminator { ref kind, source_info, .. }) = *terminator {
if let mir::TerminatorKind::Call { func: ref oper, ref args, .. } = *kind {
if let mir::Operand::Constant(ref func) = *oper {
if let ty::FnDef(def_id, _) = func.ty.sty {
if let ty::FnDef(def_id, _) = func.literal.ty.sty {
let abi = tcx.fn_sig(def_id).abi();
let name = tcx.item_name(def_id);
if abi == Abi::RustIntrinsic && name == sym::rustc_peek {

View File

@ -970,7 +970,6 @@ where
fn constant_usize(&self, val: u16) -> Operand<'tcx> {
Operand::Constant(box Constant {
span: self.source_info.span,
ty: self.tcx().types.usize,
user_ty: None,
literal: ty::Const::from_usize(self.tcx(), val.into()),
})

View File

@ -397,10 +397,9 @@ impl ExtraComments<'tcx> {
impl Visitor<'tcx> for ExtraComments<'tcx> {
fn visit_constant(&mut self, constant: &Constant<'tcx>, location: Location) {
self.super_constant(constant, location);
let Constant { span, ty, user_ty, literal } = constant;
let Constant { span, user_ty, literal } = constant;
self.push("mir::Constant");
self.push(&format!("+ span: {:?}", span));
self.push(&format!("+ ty: {:?}", ty));
if let Some(user_ty) = user_ty {
self.push(&format!("+ user_ty: {:?}", user_ty));
}