Skip to content

Commit 6a11bf7

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 b2a39d5 + d30f481 commit 6a11bf7

File tree

28 files changed

+58
-121
lines changed

28 files changed

+58
-121
lines changed

src/librustc/mir/mod.rs

Lines changed: 1 addition & 4 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 4 additions & 40 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 0 additions & 2 deletions
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
},

0 commit comments

Comments
 (0)