Skip to content

Commit 3388b75

Browse files
committed
fix rustc_mir_transform validator
1 parent 0b624d0 commit 3388b75

File tree

1 file changed

+45
-43
lines changed

1 file changed

+45
-43
lines changed

compiler/rustc_mir_transform/src/validate.rs

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,55 +1341,57 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
13411341
}
13421342
}
13431343
}
1344-
Rvalue::NullaryOp(NullOp::FieldOffset, ty) => {
1345-
let ty::Field(container, field_path) = ty.kind() else {
1346-
span_bug!(
1347-
self.body.span,
1348-
"FIXME(field_projections): expected ty::Field, found {ty:?}"
1349-
)
1350-
};
1351-
let fail_out_of_bounds = |this: &mut Self, location, field, ty| {
1352-
this.fail(location, format!("Out of bounds field {field:?} for {ty}"));
1353-
};
1354-
1355-
let mut current_ty = *container;
1356-
1357-
for (variant, field) in field_path.iter() {
1358-
match current_ty.kind() {
1359-
ty::Tuple(fields) => {
1360-
if variant != FIRST_VARIANT {
1344+
Rvalue::NullaryOp(NullOp::FieldOffset, ty) => match ty.kind() {
1345+
ty::Alias(..) | ty::Param(_) | ty::Placeholder(_) | ty::Infer(_) => {}
1346+
ty::Field(container, field_path) => {
1347+
let fail_out_of_bounds = |this: &mut Self, location, field, ty| {
1348+
this.fail(location, format!("Out of bounds field {field:?} for {ty}"));
1349+
};
1350+
1351+
let mut current_ty = *container;
1352+
1353+
for (variant, field) in field_path.iter() {
1354+
match current_ty.kind() {
1355+
ty::Tuple(fields) => {
1356+
if variant != FIRST_VARIANT {
1357+
self.fail(
1358+
location,
1359+
format!("tried to get variant {variant:?} of tuple"),
1360+
);
1361+
return;
1362+
}
1363+
let Some(&f_ty) = fields.get(field.as_usize()) else {
1364+
fail_out_of_bounds(self, location, field, current_ty);
1365+
return;
1366+
};
1367+
1368+
current_ty =
1369+
self.tcx.normalize_erasing_regions(self.typing_env, f_ty);
1370+
}
1371+
ty::Adt(adt_def, args) => {
1372+
let Some(field) = adt_def.variant(variant).fields.get(field) else {
1373+
fail_out_of_bounds(self, location, field, current_ty);
1374+
return;
1375+
};
1376+
1377+
let f_ty = field.ty(self.tcx, args);
1378+
current_ty =
1379+
self.tcx.normalize_erasing_regions(self.typing_env, f_ty);
1380+
}
1381+
_ => {
13611382
self.fail(
1362-
location,
1363-
format!("tried to get variant {variant:?} of tuple"),
1364-
);
1383+
location,
1384+
format!("Cannot get offset ({variant:?}, {field:?}) from type {current_ty}"),
1385+
);
13651386
return;
13661387
}
1367-
let Some(&f_ty) = fields.get(field.as_usize()) else {
1368-
fail_out_of_bounds(self, location, field, current_ty);
1369-
return;
1370-
};
1371-
1372-
current_ty = self.tcx.normalize_erasing_regions(self.typing_env, f_ty);
1373-
}
1374-
ty::Adt(adt_def, args) => {
1375-
let Some(field) = adt_def.variant(variant).fields.get(field) else {
1376-
fail_out_of_bounds(self, location, field, current_ty);
1377-
return;
1378-
};
1379-
1380-
let f_ty = field.ty(self.tcx, args);
1381-
current_ty = self.tcx.normalize_erasing_regions(self.typing_env, f_ty);
1382-
}
1383-
_ => {
1384-
self.fail(
1385-
location,
1386-
format!("Cannot get offset ({variant:?}, {field:?}) from type {current_ty}"),
1387-
);
1388-
return;
13891388
}
13901389
}
13911390
}
1392-
}
1391+
_ => {
1392+
span_bug!(self.body.span, "expected `ty::Field` or generic type, found {ty:?}")
1393+
}
1394+
},
13931395
Rvalue::Repeat(_, _)
13941396
| Rvalue::ThreadLocalRef(_)
13951397
| Rvalue::RawPtr(_, _)

0 commit comments

Comments
 (0)