Skip to content

Commit 74e1192

Browse files
committed
handle the active field index in unions
1 parent 95b7471 commit 74e1192

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
@@ -1377,10 +1377,14 @@ pub enum AggregateKind<'tcx> {
13771377
/// The type is of the element
13781378
Array(Ty<'tcx>),
13791379
Tuple,
1380-
/// The second field is variant number (discriminant), it's equal to 0
1381-
/// for struct and union expressions. The fourth field is active field
1382-
/// number and is present only for union expressions.
1380+
1381+
/// The second field is variant number (discriminant), it's equal
1382+
/// to 0 for struct and union expressions. The fourth field is
1383+
/// active field number and is present only for union expressions
1384+
/// -- e.g. for a union expression `SomeUnion { c: .. }`, the
1385+
/// active field index would identity the field `c`
13831386
Adt(&'tcx AdtDef, usize, &'tcx Substs<'tcx>, Option<usize>),
1387+
13841388
Closure(DefId, ClosureSubsts<'tcx>),
13851389
Generator(DefId, ClosureSubsts<'tcx>, GeneratorInterior<'tcx>),
13861390
}

src/librustc_mir/transform/type_check.rs

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

10361036
match **ak {
1037-
AggregateKind::Adt(def, variant, substs, _) => {
1038-
if let Some(field) = def.variants[variant].fields.get(field) {
1037+
AggregateKind::Adt(def, variant_index, substs, active_field_index) => {
1038+
let variant = &def.variants[variant_index];
1039+
let adj_field_index = active_field_index.unwrap_or(field_index);
1040+
if let Some(field) = variant.fields.get(adj_field_index) {
10391041
Ok(self.normalize(&field.ty(tcx, substs), location))
10401042
} else {
10411043
Err(FieldAccessError::OutOfRange {
@@ -1044,18 +1046,18 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
10441046
}
10451047
}
10461048
AggregateKind::Closure(def_id, substs) => {
1047-
match substs.upvar_tys(def_id, tcx).nth(field) {
1049+
match substs.upvar_tys(def_id, tcx).nth(field_index) {
10481050
Some(ty) => Ok(ty),
10491051
None => Err(FieldAccessError::OutOfRange {
10501052
field_count: substs.upvar_tys(def_id, tcx).count(),
10511053
}),
10521054
}
10531055
}
10541056
AggregateKind::Generator(def_id, substs, _) => {
1055-
if let Some(ty) = substs.upvar_tys(def_id, tcx).nth(field) {
1056-
Ok(ty);
1057+
if let Some(ty) = substs.upvar_tys(def_id, tcx).nth(field_index) {
1058+
Ok(ty)
10571059
} else {
1058-
match substs.field_tys(def_id, tcx).nth(field) {
1060+
match substs.field_tys(def_id, tcx).nth(field_index) {
10591061
Some(ty) => Ok(ty),
10601062
None => Err(FieldAccessError::OutOfRange {
10611063
field_count: substs.field_tys(def_id, tcx).count() + 1,

0 commit comments

Comments
 (0)