Skip to content

Commit ccf569f

Browse files
committed
clean up handling of variant type
1 parent 43f9467 commit ccf569f

File tree

24 files changed

+638
-793
lines changed

24 files changed

+638
-793
lines changed

Diff for: compiler/rustc_const_eval/src/interpret/operand.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -683,20 +683,17 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
683683
let discr_val = self.cast_from_scalar(tag_bits, tag_layout, discr_layout.ty);
684684
let discr_bits = discr_val.assert_bits(discr_layout.size);
685685
// Convert discriminant to variant index, and catch invalid discriminants.
686-
let index = match *op.layout.ty.kind() {
686+
let index = match *op.layout.ty.strip_variant_type().kind() {
687687
ty::Adt(adt, _) => {
688688
adt.discriminants(*self.tcx).find(|(_, var)| var.val == discr_bits)
689689
}
690-
ty::Variant(ty, _) => match ty.kind() {
691-
ty::Adt(adt, _) => adt.discriminants(*self.tcx).find(|(_, var)| var.val == discr_bits),
692-
_ => bug!("unexpected type: {:?}", ty.kind()),
693-
}
694690
ty::Generator(def_id, substs, _) => {
695691
let substs = substs.as_generator();
696692
substs
697693
.discriminants(def_id, *self.tcx)
698694
.find(|(_, var)| var.val == discr_bits)
699695
}
696+
ty::Variant(..) => unreachable!(),
700697
_ => span_bug!(self.cur_span(), "tagged layout for non-adt non-generator"),
701698
}
702699
.ok_or_else(|| err_ub!(InvalidTag(Scalar::from_uint(tag_bits, tag_layout.size))))?;

Diff for: compiler/rustc_const_eval/src/interpret/validity.rs

+8-38
Original file line numberDiff line numberDiff line change
@@ -217,12 +217,8 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
217217
match layout.variants {
218218
Variants::Multiple { tag_field, .. } => {
219219
if tag_field == field {
220-
return match layout.ty.kind() {
220+
return match layout.ty.strip_variant_type().kind() {
221221
ty::Adt(def, ..) if def.is_enum() => PathElem::EnumTag,
222-
ty::Variant(ty, ..) => match ty.kind() {
223-
ty::Adt(def, ..) if def.is_enum() => PathElem::EnumTag,
224-
_ => bug!("non-variant type {:?}", layout.ty),
225-
},
226222
ty::Generator(..) => PathElem::GeneratorTag,
227223
_ => bug!("non-variant type {:?}", layout.ty),
228224
};
@@ -232,7 +228,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
232228
}
233229

234230
// Now we know we are projecting to a field, so figure out which one.
235-
match layout.ty.kind() {
231+
match layout.ty.strip_variant_type().kind() {
236232
// generators and closures.
237233
ty::Closure(def_id, _) | ty::Generator(def_id, _, _) => {
238234
let mut name = None;
@@ -276,20 +272,6 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
276272
}
277273
}
278274

279-
ty::Variant(ty, ..) => match ty.kind() {
280-
ty::Adt(def, ..) if def.is_enum() => {
281-
// we might be projecting *to* a variant, or to a field *in* a variant.
282-
match layout.variants {
283-
Variants::Single { index } => {
284-
// Inside a variant
285-
PathElem::Field(def.variants[index].fields[field].ident.name)
286-
}
287-
Variants::Multiple { .. } => bug!("we handled variants above"),
288-
}
289-
}
290-
_ => bug!("unexpected type: {:?}", ty.kind()),
291-
},
292-
293275
// other ADTs
294276
ty::Adt(def, _) => PathElem::Field(def.non_enum_variant().fields[field].ident.name),
295277

@@ -513,7 +495,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
513495
) -> InterpResult<'tcx, bool> {
514496
// Go over all the primitive types
515497
let ty = value.layout.ty;
516-
match ty.kind() {
498+
match ty.strip_variant_type().kind() {
517499
ty::Bool => {
518500
let value = self.read_scalar(value)?;
519501
try_validation!(
@@ -585,17 +567,6 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
585567
self.check_safe_pointer(value, "box")?;
586568
Ok(true)
587569
}
588-
ty::Variant(ty, _) => match ty.kind() {
589-
ty::Adt(def, _) => {
590-
if def.is_box() {
591-
self.check_safe_pointer(value, "box")?;
592-
Ok(true)
593-
} else {
594-
Ok(false)
595-
}
596-
}
597-
_ => bug!("unexpected type: {:?}", ty.kind()),
598-
},
599570
ty::FnPtr(_sig) => {
600571
let value = try_validation!(
601572
self.ecx.read_immediate(value),
@@ -641,6 +612,8 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
641612
| ty::Opaque(..)
642613
| ty::Projection(..)
643614
| ty::GeneratorWitness(..) => bug!("Encountered invalid type {:?}", ty),
615+
616+
ty::Variant(..) => unreachable!(),
644617
}
645618
}
646619

@@ -756,15 +729,12 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
756729
variant_id: VariantIdx,
757730
new_op: &OpTy<'tcx, M::PointerTag>,
758731
) -> InterpResult<'tcx> {
759-
let name = match old_op.layout.ty.kind() {
732+
let ty = old_op.layout.ty.strip_variant_type();
733+
let name = match ty.kind() {
760734
ty::Adt(adt, _) => PathElem::Variant(adt.variants[variant_id].ident.name),
761-
ty::Variant(ty, ..) => match ty.kind() {
762-
ty::Adt(adt, ..) => PathElem::Variant(adt.variants[variant_id].ident.name),
763-
_ => bug!("unexpected type {:?}", ty.kind()),
764-
},
765735
// Generators also have variants
766736
ty::Generator(..) => PathElem::GeneratorState(variant_id),
767-
_ => bug!("Unexpected type with variant: {:?}", old_op.layout.ty),
737+
_ => bug!("Unexpected type with variant: {:?}", ty),
768738
};
769739
self.with_elem(name, move |this| this.visit_value(new_op))
770740
}

Diff for: compiler/rustc_infer/src/infer/combine.rs

-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ impl<'infcx, 'tcx> InferCtxt<'infcx, 'tcx> {
7272
{
7373
let a_is_expected = relation.a_is_expected();
7474

75-
debug!("super_combine_tys: {:?} | {:?}", a.kind(), b.kind());
7675
match (a.kind(), b.kind()) {
7776
// Relate integral variables to other types
7877
(&ty::Infer(ty::IntVar(a_id)), &ty::Infer(ty::IntVar(b_id))) => {

Diff for: compiler/rustc_middle/src/ty/cast.rs

+1-10
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,7 @@ impl<'tcx> CastTy<'tcx> {
5959
ty::Uint(u) => Some(CastTy::Int(IntTy::U(u))),
6060
ty::Float(_) => Some(CastTy::Float),
6161
ty::Adt(d, _) if d.is_enum() && d.is_payloadfree() => Some(CastTy::Int(IntTy::CEnum)),
62-
ty::Variant(ty, _) => match ty.kind() {
63-
ty::Adt(d, _) => {
64-
if d.is_enum() && d.is_payloadfree() {
65-
Some(CastTy::Int(IntTy::CEnum))
66-
} else {
67-
None
68-
}
69-
}
70-
_ => bug!("unexpected type: {:?}", ty.kind()),
71-
}
62+
ty::Variant(ty, _) => Self::from_ty(ty),
7263
ty::RawPtr(mt) => Some(CastTy::Ptr(mt)),
7364
ty::FnPtr(..) => Some(CastTy::FnPtr),
7465
_ => None,

Diff for: compiler/rustc_middle/src/ty/error.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -244,10 +244,7 @@ impl<'tcx> ty::TyS<'tcx> {
244244
ty::Tuple(ref tys) if tys.is_empty() => format!("`{}`", self).into(),
245245

246246
ty::Adt(def, _) => format!("{} `{}`", def.descr(), tcx.def_path_str(def.did)).into(),
247-
ty::Variant(ty, _) => match ty.kind() {
248-
ty::Adt(def, _) => format!("{} `{}`", def.descr(), tcx.def_path_str(def.did)).into(),
249-
_ => bug!("unexpected type: {:?}", ty.kind()),
250-
}
247+
ty::Variant(ty, _) => ty.sort_string(tcx),
251248
ty::Foreign(def_id) => format!("extern type `{}`", tcx.def_path_str(def_id)).into(),
252249
ty::Array(t, n) => {
253250
if t.is_simple_ty() {
@@ -322,7 +319,7 @@ impl<'tcx> ty::TyS<'tcx> {
322319
ty::Variant(ty, _) => match ty.kind() {
323320
ty::Adt(def, _) => format!("{} variant", def.descr()).into(),
324321
_ => bug!("unexpected type: {:?}", ty.kind()),
325-
}
322+
},
326323
ty::Foreign(_) => "extern type".into(),
327324
ty::Array(..) => "array".into(),
328325
ty::Slice(_) => "slice".into(),

Diff for: compiler/rustc_middle/src/ty/fast_reject.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,7 @@ pub fn simplify_type(
6666
ty::Uint(uint_type) => Some(UintSimplifiedType(uint_type)),
6767
ty::Float(float_type) => Some(FloatSimplifiedType(float_type)),
6868
ty::Adt(def, _) => Some(AdtSimplifiedType(def.did)),
69-
ty::Variant(ref ty, _) => match ty.kind() {
70-
ty::Adt(def, _) => Some(AdtSimplifiedType(def.did)),
71-
_ => bug!("unexpected type: {:?}", ty.kind()),
72-
}
69+
ty::Variant(ty, _) => simplify_type(tcx, ty, can_simplify_params),
7370
ty::Str => Some(StrSimplifiedType),
7471
ty::Array(..) | ty::Slice(_) => Some(ArraySimplifiedType),
7572
ty::RawPtr(_) => Some(PtrSimplifiedType),

Diff for: compiler/rustc_middle/src/ty/flags.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,7 @@ impl FlagComputation {
161161
self.add_substs(substs);
162162
}
163163

164-
&ty::Variant(ty, _) => match ty.kind() {
165-
ty::Adt(_, substs) => self.add_substs(substs),
166-
_ => bug!("unexpected type: {:?}", ty.kind()),
167-
}
164+
&ty::Variant(ty, _) => self.add_kind(ty.kind()),
168165

169166
&ty::Projection(data) => {
170167
self.add_flags(TypeFlags::HAS_TY_PROJECTION);

0 commit comments

Comments
 (0)