Skip to content

Commit db3bae0

Browse files
authored
Rollup merge of rust-lang#63495 - eddyb:mir-constant-ty, r=oli-obk
Remove redundant `ty` fields from `mir::Constant` and `hair::pattern::PatternRange`. Fixes rust-lang#56137. As a side-effect, associated const literals have the correct type now, which should make things easier for rust-lang#61041. r? @oli-obk / @matthewjasper cc @davidtwco @varkor
2 parents aec047e + 45980e8 commit db3bae0

35 files changed

+90
-153
lines changed

src/librustc/mir/mod.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -2197,7 +2197,6 @@ impl<'tcx> Operand<'tcx> {
21972197
let ty = tcx.type_of(def_id).subst(tcx, substs);
21982198
Operand::Constant(box Constant {
21992199
span,
2200-
ty,
22012200
user_ty: None,
22022201
literal: ty::Const::zero_sized(tcx, ty),
22032202
})
@@ -2476,7 +2475,6 @@ impl<'tcx> Debug for Rvalue<'tcx> {
24762475
#[derive(Copy, Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable, HashStable)]
24772476
pub struct Constant<'tcx> {
24782477
pub span: Span,
2479-
pub ty: Ty<'tcx>,
24802478

24812479
/// Optional user-given type: for something like
24822480
/// `collect::<Vec<_>>`, this would be present and would
@@ -3385,12 +3383,11 @@ impl<'tcx> TypeFoldable<'tcx> for Constant<'tcx> {
33853383
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
33863384
Constant {
33873385
span: self.span.clone(),
3388-
ty: self.ty.fold_with(folder),
33893386
user_ty: self.user_ty.fold_with(folder),
33903387
literal: self.literal.fold_with(folder),
33913388
}
33923389
}
33933390
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
3394-
self.ty.visit_with(visitor) || self.literal.visit_with(visitor)
3391+
self.literal.visit_with(visitor)
33953392
}
33963393
}

src/librustc/mir/tcx.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ impl<'tcx> Operand<'tcx> {
252252
match self {
253253
&Operand::Copy(ref l) |
254254
&Operand::Move(ref l) => l.ty(local_decls, tcx).ty,
255-
&Operand::Constant(ref c) => c.ty,
255+
&Operand::Constant(ref c) => c.literal.ty,
256256
}
257257
}
258258
}

src/librustc/mir/visit.rs

-2
Original file line numberDiff line numberDiff line change
@@ -782,13 +782,11 @@ macro_rules! make_mir_visitor {
782782
location: Location) {
783783
let Constant {
784784
span,
785-
ty,
786785
user_ty,
787786
literal,
788787
} = constant;
789788

790789
self.visit_span(span);
791-
self.visit_ty(ty, TyContext::Location(location));
792790
drop(user_ty); // no visit method for this
793791
self.visit_const(literal, location);
794792
}

src/librustc_codegen_ssa/mir/analyze.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ impl<'mir, 'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> Visitor<'tcx>
221221
mir::TerminatorKind::Call {
222222
func: mir::Operand::Constant(ref c),
223223
ref args, ..
224-
} => match c.ty.sty {
224+
} => match c.literal.ty.sty {
225225
ty::FnDef(did, _) => Some((did, args)),
226226
_ => None,
227227
},

src/librustc_codegen_ssa/mir/block.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
651651
let (llval, ty) = self.simd_shuffle_indices(
652652
&bx,
653653
constant.span,
654-
constant.ty,
654+
constant.literal.ty,
655655
c,
656656
);
657657
return OperandRef {

src/librustc_codegen_ssa/mir/operand.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
466466
}
467467

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

src/librustc_mir/borrow_check/nll/type_check/mod.rs

+4-40
Original file line numberDiff line numberDiff line change
@@ -272,12 +272,11 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> {
272272

273273
fn visit_constant(&mut self, constant: &Constant<'tcx>, location: Location) {
274274
self.super_constant(constant, location);
275-
self.sanitize_constant(constant, location);
276-
self.sanitize_type(constant, constant.ty);
275+
self.sanitize_type(constant, constant.literal.ty);
277276

278277
if let Some(annotation_index) = constant.user_ty {
279278
if let Err(terr) = self.cx.relate_type_and_user_type(
280-
constant.ty,
279+
constant.literal.ty,
281280
ty::Variance::Invariant,
282281
&UserTypeProjection { base: annotation_index, projs: vec![], },
283282
location.to_locations(),
@@ -289,7 +288,7 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> {
289288
constant,
290289
"bad constant user type {:?} vs {:?}: {:?}",
291290
annotation,
292-
constant.ty,
291+
constant.literal.ty,
293292
terr,
294293
);
295294
}
@@ -299,7 +298,7 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> {
299298
location.to_locations(),
300299
ConstraintCategory::Boring,
301300
self.cx.param_env.and(type_op::ascribe_user_type::AscribeUserType::new(
302-
constant.ty, def_id, UserSubsts { substs, user_self_ty: None },
301+
constant.literal.ty, def_id, UserSubsts { substs, user_self_ty: None },
303302
)),
304303
) {
305304
span_mirbug!(
@@ -403,41 +402,6 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
403402
}
404403
}
405404

406-
/// Checks that the constant's `ty` field matches up with what would be
407-
/// expected from its literal. Unevaluated constants and well-formed
408-
/// constraints are checked by `visit_constant`.
409-
fn sanitize_constant(&mut self, constant: &Constant<'tcx>, location: Location) {
410-
debug!(
411-
"sanitize_constant(constant={:?}, location={:?})",
412-
constant, location
413-
);
414-
415-
let literal = constant.literal;
416-
417-
if let ConstValue::Unevaluated(..) = literal.val {
418-
return;
419-
}
420-
421-
debug!("sanitize_constant: expected_ty={:?}", literal.ty);
422-
423-
if let Err(terr) = self.cx.eq_types(
424-
literal.ty,
425-
constant.ty,
426-
location.to_locations(),
427-
ConstraintCategory::Boring,
428-
) {
429-
span_mirbug!(
430-
self,
431-
constant,
432-
"constant {:?} should have type {:?} but has {:?} ({:?})",
433-
constant,
434-
literal.ty,
435-
constant.ty,
436-
terr,
437-
);
438-
}
439-
}
440-
441405
/// Checks that the types internal to the `place` match up with
442406
/// what would be expected.
443407
fn sanitize_place(

src/librustc_mir/build/expr/as_constant.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
3838
inferred_ty: ty,
3939
})
4040
});
41+
assert_eq!(literal.ty, ty);
4142
Constant {
4243
span,
43-
ty,
4444
user_ty,
4545
literal,
4646
}

src/librustc_mir/build/expr/as_rvalue.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
591591
let n = (!0u128) >> (128 - bits);
592592
let literal = ty::Const::from_bits(self.hir.tcx(), n, param_ty);
593593

594-
self.literal_operand(span, ty, literal)
594+
self.literal_operand(span, literal)
595595
}
596596

597597
// Helper to get the minimum value of the appropriate type
@@ -602,6 +602,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
602602
let n = 1 << (bits - 1);
603603
let literal = ty::Const::from_bits(self.hir.tcx(), n, param_ty);
604604

605-
self.literal_operand(span, ty, literal)
605+
self.literal_operand(span, literal)
606606
}
607607
}

src/librustc_mir/build/expr/into.rs

-2
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
114114
destination,
115115
Constant {
116116
span: expr_span,
117-
ty: this.hir.bool_ty(),
118117
user_ty: None,
119118
literal: this.hir.true_literal(),
120119
},
@@ -126,7 +125,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
126125
destination,
127126
Constant {
128127
span: expr_span,
129-
ty: this.hir.bool_ty(),
130128
user_ty: None,
131129
literal: this.hir.false_literal(),
132130
},

src/librustc_mir/build/matches/simplify.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
108108
Err(match_pair)
109109
}
110110

111-
PatternKind::Range(PatternRange { lo, hi, ty, end }) => {
112-
let (range, bias) = match ty.sty {
111+
PatternKind::Range(PatternRange { lo, hi, end }) => {
112+
let (range, bias) = match lo.ty.sty {
113113
ty::Char => {
114114
(Some(('\u{0000}' as u128, '\u{10FFFF}' as u128, Size::from_bits(32))), 0)
115115
}

src/librustc_mir/build/matches/test.rs

+14-12
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
6363
}
6464

6565
PatternKind::Range(range) => {
66-
assert!(range.ty == match_pair.pattern.ty);
66+
assert_eq!(range.lo.ty, match_pair.pattern.ty);
67+
assert_eq!(range.hi.ty, match_pair.pattern.ty);
6768
Test {
6869
span: match_pair.pattern.span,
6970
kind: TestKind::Range(range),
@@ -270,22 +271,23 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
270271
);
271272
} else {
272273
if let [success, fail] = *make_target_blocks(self) {
274+
assert_eq!(value.ty, ty);
275+
let expect = self.literal_operand(test.span, value);
273276
let val = Operand::Copy(place.clone());
274-
let expect = self.literal_operand(test.span, ty, value);
275277
self.compare(block, success, fail, source_info, BinOp::Eq, expect, val);
276278
} else {
277279
bug!("`TestKind::Eq` should have two target blocks");
278280
}
279281
}
280282
}
281283

282-
TestKind::Range(PatternRange { ref lo, ref hi, ty, ref end }) => {
284+
TestKind::Range(PatternRange { ref lo, ref hi, ref end }) => {
283285
let lower_bound_success = self.cfg.start_new_block();
284286
let target_blocks = make_target_blocks(self);
285287

286288
// Test `val` by computing `lo <= val && val <= hi`, using primitive comparisons.
287-
let lo = self.literal_operand(test.span, ty, lo);
288-
let hi = self.literal_operand(test.span, ty, hi);
289+
let lo = self.literal_operand(test.span, lo);
290+
let hi = self.literal_operand(test.span, hi);
289291
let val = Operand::Copy(place.clone());
290292

291293
if let [success, fail] = *target_blocks {
@@ -387,7 +389,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
387389
) {
388390
use rustc::middle::lang_items::EqTraitLangItem;
389391

390-
let mut expect = self.literal_operand(source_info.span, value.ty, value);
392+
let mut expect = self.literal_operand(source_info.span, value);
391393
let mut val = Operand::Copy(place.clone());
392394

393395
// If we're using `b"..."` as a pattern, we need to insert an
@@ -440,7 +442,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
440442
};
441443

442444
let eq_def_id = self.hir.tcx().require_lang_item(EqTraitLangItem);
443-
let (mty, method) = self.hir.trait_method(eq_def_id, sym::eq, deref_ty, &[deref_ty.into()]);
445+
let method = self.hir.trait_method(eq_def_id, sym::eq, deref_ty, &[deref_ty.into()]);
444446

445447
let bool_ty = self.hir.bool_ty();
446448
let eq_result = self.temp(bool_ty, source_info.span);
@@ -449,7 +451,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
449451
self.cfg.terminate(block, source_info, TerminatorKind::Call {
450452
func: Operand::Constant(box Constant {
451453
span: source_info.span,
452-
ty: mty,
453454

454455
// FIXME(#54571): This constant comes from user input (a
455456
// constant in a pattern). Are there forms where users can add
@@ -656,8 +657,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
656657

657658
let tcx = self.hir.tcx();
658659

659-
let lo = compare_const_vals(tcx, test.lo, pat.hi, self.hir.param_env, test.ty)?;
660-
let hi = compare_const_vals(tcx, test.hi, pat.lo, self.hir.param_env, test.ty)?;
660+
let test_ty = test.lo.ty;
661+
let lo = compare_const_vals(tcx, test.lo, pat.hi, self.hir.param_env, test_ty)?;
662+
let hi = compare_const_vals(tcx, test.hi, pat.lo, self.hir.param_env, test_ty)?;
661663

662664
match (test.end, pat.end, lo, hi) {
663665
// pat < test
@@ -774,8 +776,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
774776

775777
let tcx = self.hir.tcx();
776778

777-
let a = compare_const_vals(tcx, range.lo, value, self.hir.param_env, range.ty)?;
778-
let b = compare_const_vals(tcx, value, range.hi, self.hir.param_env, range.ty)?;
779+
let a = compare_const_vals(tcx, range.lo, value, self.hir.param_env, range.lo.ty)?;
780+
let b = compare_const_vals(tcx, value, range.hi, self.hir.param_env, range.lo.ty)?;
779781

780782
match (b, range.end) {
781783
(Less, _) |

src/librustc_mir/build/misc.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
2626
/// without any user type annotation.
2727
pub fn literal_operand(&mut self,
2828
span: Span,
29-
ty: Ty<'tcx>,
3029
literal: &'tcx ty::Const<'tcx>)
3130
-> Operand<'tcx> {
3231
let constant = box Constant {
3332
span,
34-
ty,
3533
user_ty: None,
3634
literal,
3735
};
@@ -47,7 +45,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
4745
pub fn zero_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {
4846
let literal = ty::Const::from_bits(self.hir.tcx(), 0, ty::ParamEnv::empty().and(ty));
4947

50-
self.literal_operand(span, ty, literal)
48+
self.literal_operand(span, literal)
5149
}
5250

5351
pub fn push_usize(&mut self,
@@ -61,7 +59,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
6159
block, source_info, &temp,
6260
Constant {
6361
span: source_info.span,
64-
ty: self.hir.usize_ty(),
6562
user_ty: None,
6663
literal: self.hir.usize_literal(value),
6764
});

src/librustc_mir/hair/cx/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,7 @@ fn convert_path_expr<'a, 'tcx>(
927927
ExprKind::Literal {
928928
literal: cx.tcx.mk_const(ty::Const {
929929
val: ConstValue::Unevaluated(def_id, substs),
930-
ty: cx.tcx.type_of(def_id),
930+
ty: cx.tables().node_type(expr.hir_id),
931931
}),
932932
user_ty,
933933
}

src/librustc_mir/hair/cx/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,13 @@ impl<'a, 'tcx> Cx<'a, 'tcx> {
170170
method_name: Symbol,
171171
self_ty: Ty<'tcx>,
172172
params: &[Kind<'tcx>])
173-
-> (Ty<'tcx>, &'tcx ty::Const<'tcx>) {
173+
-> &'tcx ty::Const<'tcx> {
174174
let substs = self.tcx.mk_substs_trait(self_ty, params);
175175
for item in self.tcx.associated_items(trait_def_id) {
176176
if item.kind == ty::AssocKind::Method && item.ident.name == method_name {
177177
let method_ty = self.tcx.type_of(item.def_id);
178178
let method_ty = method_ty.subst(self.tcx, substs);
179-
return (method_ty, ty::Const::zero_sized(self.tcx, method_ty));
179+
return ty::Const::zero_sized(self.tcx, method_ty);
180180
}
181181
}
182182

0 commit comments

Comments
 (0)