Skip to content

Commit ef812f1

Browse files
committed
Use let chains instead of let else
This makes it more obvious that we're looking at a special case.
1 parent f9989d0 commit ef812f1

File tree

1 file changed

+13
-19
lines changed
  • compiler/rustc_mir_build/src/thir/cx

1 file changed

+13
-19
lines changed

compiler/rustc_mir_build/src/thir/cx/expr.rs

+13-19
Original file line numberDiff line numberDiff line change
@@ -201,33 +201,23 @@ impl<'tcx> Cx<'tcx> {
201201
source: self.mirror_expr(source),
202202
cast: PointerCast::ArrayToPointer,
203203
}
204-
} else {
205-
// check whether this is casting an enum variant discriminant
206-
// to prevent cycles, we refer to the discriminant initializer
204+
} else if let hir::ExprKind::Path(ref qpath) = source.kind
205+
&& let res = self.typeck_results().qpath_res(qpath, source.hir_id)
206+
&& let ty = self.typeck_results().node_type(source.hir_id)
207+
&& let ty::Adt(adt_def, substs) = ty.kind()
208+
&& let Res::Def(DefKind::Ctor(CtorOf::Variant, CtorKind::Const), variant_ctor_id) = res
209+
{
210+
// Check whether this is casting an enum variant discriminant.
211+
// To prevent cycles, we refer to the discriminant initializer,
207212
// which is always an integer and thus doesn't need to know the
208-
// enum's layout (or its tag type) to compute it during const eval
213+
// enum's layout (or its tag type) to compute it during const eval.
209214
// Example:
210215
// enum Foo {
211216
// A,
212217
// B = A as isize + 4,
213218
// }
214219
// The correct solution would be to add symbolic computations to miri,
215220
// so we wouldn't have to compute and store the actual value
216-
217-
let hir::ExprKind::Path(ref qpath) = source.kind else {
218-
return ExprKind::Cast { source: self.mirror_expr(source)};
219-
};
220-
221-
let res = self.typeck_results().qpath_res(qpath, source.hir_id);
222-
let ty = self.typeck_results().node_type(source.hir_id);
223-
let ty::Adt(adt_def, substs) = ty.kind() else {
224-
return ExprKind::Cast { source: self.mirror_expr(source)};
225-
};
226-
227-
let Res::Def(DefKind::Ctor(CtorOf::Variant, CtorKind::Const), variant_ctor_id) = res else {
228-
return ExprKind::Cast { source: self.mirror_expr(source)};
229-
};
230-
231221
let idx = adt_def.variant_index_with_ctor_id(variant_ctor_id);
232222
let (discr_did, discr_offset) = adt_def.discriminant_def_for_variant(idx);
233223

@@ -266,6 +256,10 @@ impl<'tcx> Cx<'tcx> {
266256
};
267257

268258
ExprKind::Cast { source }
259+
} else {
260+
// Default to `ExprKind::Cast` for all explicit casts.
261+
// MIR building then picks the right MIR casts based on the types.
262+
ExprKind::Cast { source: self.mirror_expr(source) }
269263
}
270264
}
271265

0 commit comments

Comments
 (0)