Skip to content

Commit 10b8fac

Browse files
committed
handle the active field index in unions
1 parent b3a10db commit 10b8fac

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

src/librustc/mir/mod.rs

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

src/librustc_mir/transform/type_check.rs

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