Skip to content

Commit e8a9120

Browse files
committed
handle the active field index in unions
1 parent c87fe69 commit e8a9120

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

src/librustc/mir/mod.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -1376,10 +1376,14 @@ pub enum AggregateKind<'tcx> {
13761376
/// The type is of the element
13771377
Array(Ty<'tcx>),
13781378
Tuple,
1379-
/// The second field is variant number (discriminant), it's equal to 0
1380-
/// for struct and union expressions. The fourth field is active field
1381-
/// number and is present only for union expressions.
1379+
1380+
/// The second field is variant number (discriminant), it's equal
1381+
/// to 0 for struct and union expressions. The fourth field is
1382+
/// active field number and is present only for union expressions
1383+
/// -- e.g. for a union expression `SomeUnion { c: .. }`, the
1384+
/// active field index would identity the field `c`
13821385
Adt(&'tcx AdtDef, usize, &'tcx Substs<'tcx>, Option<usize>),
1386+
13831387
Closure(DefId, ClosureSubsts<'tcx>),
13841388
Generator(DefId, ClosureSubsts<'tcx>, GeneratorInterior<'tcx>),
13851389
}

src/librustc_mir/transform/type_check.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -1031,14 +1031,16 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
10311031
fn aggregate_field_ty(
10321032
&mut self,
10331033
ak: &Box<AggregateKind<'tcx>>,
1034-
field: usize,
1034+
field_index: usize,
10351035
location: Location,
10361036
) -> Result<Ty<'tcx>, FieldAccessError> {
10371037
let tcx = self.tcx();
10381038

10391039
match **ak {
1040-
AggregateKind::Adt(def, variant, substs, _) => {
1041-
if let Some(field) = def.variants[variant].fields.get(field) {
1040+
AggregateKind::Adt(def, variant_index, substs, active_field_index) => {
1041+
let variant = &def.variants[variant_index];
1042+
let adj_field_index = active_field_index.unwrap_or(field_index);
1043+
if let Some(field) = variant.fields.get(adj_field_index) {
10421044
Ok(self.normalize(&field.ty(tcx, substs), location))
10431045
} else {
10441046
Err(FieldAccessError::OutOfRange {
@@ -1047,18 +1049,18 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
10471049
}
10481050
}
10491051
AggregateKind::Closure(def_id, substs) => {
1050-
match substs.upvar_tys(def_id, tcx).nth(field) {
1052+
match substs.upvar_tys(def_id, tcx).nth(field_index) {
10511053
Some(ty) => Ok(ty),
10521054
None => Err(FieldAccessError::OutOfRange {
10531055
field_count: substs.upvar_tys(def_id, tcx).count(),
10541056
}),
10551057
}
10561058
}
10571059
AggregateKind::Generator(def_id, substs, _) => {
1058-
if let Some(ty) = substs.upvar_tys(def_id, tcx).nth(field) {
1059-
Ok(ty);
1060+
if let Some(ty) = substs.upvar_tys(def_id, tcx).nth(field_index) {
1061+
Ok(ty)
10601062
} else {
1061-
match substs.field_tys(def_id, tcx).nth(field) {
1063+
match substs.field_tys(def_id, tcx).nth(field_index) {
10621064
Some(ty) => Ok(ty),
10631065
None => Err(FieldAccessError::OutOfRange {
10641066
field_count: substs.field_tys(def_id, tcx).count() + 1,

0 commit comments

Comments
 (0)