Skip to content

Commit 45f6027

Browse files
committed
break type-checking of aggregate-kind out into helper function
1 parent 31d61f1 commit 45f6027

File tree

1 file changed

+52
-45
lines changed

1 file changed

+52
-45
lines changed

src/librustc_mir/transform/type_check.rs

Lines changed: 52 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,13 +1031,13 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
10311031

10321032
fn aggregate_field_ty(
10331033
&mut self,
1034-
ak: &Box<AggregateKind<'tcx>>,
1034+
ak: &AggregateKind<'tcx>,
10351035
field_index: usize,
10361036
location: Location,
10371037
) -> Result<Ty<'tcx>, FieldAccessError> {
10381038
let tcx = self.tcx();
10391039

1040-
match **ak {
1040+
match *ak {
10411041
AggregateKind::Adt(def, variant_index, substs, active_field_index) => {
10421042
let variant = &def.variants[variant_index];
10431043
let adj_field_index = active_field_index.unwrap_or(field_index);
@@ -1069,56 +1069,17 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
10691069
}
10701070
}
10711071
}
1072-
AggregateKind::Array(ty) => {
1073-
Ok(ty)
1074-
}
1072+
AggregateKind::Array(ty) => Ok(ty),
10751073
AggregateKind::Tuple => {
10761074
unreachable!("This should have been covered in check_rvalues");
10771075
}
10781076
}
10791077
}
10801078

1081-
fn check_rvalue(&mut self, mir: &Mir<'tcx>, rv: &Rvalue<'tcx>, location: Location) {
1082-
let tcx = self.tcx();
1083-
match rv {
1079+
fn check_rvalue(&mut self, mir: &Mir<'tcx>, rvalue: &Rvalue<'tcx>, location: Location) {
1080+
match rvalue {
10841081
Rvalue::Aggregate(ak, ops) => {
1085-
match **ak {
1086-
// tuple rvalue field type is always the type of the op. Nothing to check here.
1087-
AggregateKind::Tuple => {}
1088-
_ => {
1089-
for (i, op) in ops.iter().enumerate() {
1090-
let field_ty = match self.aggregate_field_ty(ak, i, location) {
1091-
Ok(field_ty) => field_ty,
1092-
Err(FieldAccessError::OutOfRange { field_count }) => {
1093-
span_mirbug!(
1094-
self,
1095-
rv,
1096-
"accessed field #{} but variant only has {}",
1097-
i,
1098-
field_count
1099-
);
1100-
continue;
1101-
}
1102-
};
1103-
let op_ty = op.ty(mir, tcx);
1104-
if let Err(terr) = self.sub_types(
1105-
op_ty,
1106-
field_ty,
1107-
location.at_successor_within_block(),
1108-
)
1109-
{
1110-
span_mirbug!(
1111-
self,
1112-
rv,
1113-
"{:?} is not a subtype of {:?}: {:?}",
1114-
op_ty,
1115-
field_ty,
1116-
terr
1117-
);
1118-
}
1119-
}
1120-
}
1121-
}
1082+
self.check_aggregate_rvalue(mir, rvalue, ak, ops, location)
11221083
}
11231084
// FIXME: These other cases have to be implemented in future PRs
11241085
Rvalue::Use(..) |
@@ -1134,6 +1095,52 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
11341095
}
11351096
}
11361097

1098+
fn check_aggregate_rvalue(
1099+
&mut self,
1100+
mir: &Mir<'tcx>,
1101+
rvalue: &Rvalue<'tcx>,
1102+
aggregate_kind: &AggregateKind<'tcx>,
1103+
operands: &[Operand<'tcx>],
1104+
location: Location,
1105+
) {
1106+
match aggregate_kind {
1107+
// tuple rvalue field type is always the type of the op. Nothing to check here.
1108+
AggregateKind::Tuple => return,
1109+
_ => {}
1110+
}
1111+
1112+
let tcx = self.tcx();
1113+
1114+
for (i, operand) in operands.iter().enumerate() {
1115+
let field_ty = match self.aggregate_field_ty(aggregate_kind, i, location) {
1116+
Ok(field_ty) => field_ty,
1117+
Err(FieldAccessError::OutOfRange { field_count }) => {
1118+
span_mirbug!(
1119+
self,
1120+
rvalue,
1121+
"accessed field #{} but variant only has {}",
1122+
i,
1123+
field_count
1124+
);
1125+
continue;
1126+
}
1127+
};
1128+
let operand_ty = operand.ty(mir, tcx);
1129+
if let Err(terr) =
1130+
self.sub_types(operand_ty, field_ty, location.at_successor_within_block())
1131+
{
1132+
span_mirbug!(
1133+
self,
1134+
rvalue,
1135+
"{:?} is not a subtype of {:?}: {:?}",
1136+
operand_ty,
1137+
field_ty,
1138+
terr
1139+
);
1140+
}
1141+
}
1142+
}
1143+
11371144
fn typeck_mir(&mut self, mir: &Mir<'tcx>) {
11381145
self.last_span = mir.span;
11391146
debug!("run_on_mir: {:?}", mir.span);

0 commit comments

Comments
 (0)