1
Fork 0

Prevent overflowing enum cast from ICEing

This commit is contained in:
clubby789 2024-10-18 22:05:31 +00:00
parent f7b5e5471b
commit ab4222ad97
3 changed files with 77 additions and 4 deletions

View file

@ -281,7 +281,14 @@ impl<'tcx> Cx<'tcx> {
.unwrap_or_else(|e| panic!("could not compute layout for {param_env_ty:?}: {e:?}"))
.size;
let lit = ScalarInt::try_from_uint(discr_offset as u128, size).unwrap();
let (lit, overflowing) = ScalarInt::truncate_from_uint(discr_offset as u128, size);
if overflowing {
// An erroneous enum with too many variants for its repr will emit E0081 and E0370
self.tcx.dcx().span_delayed_bug(
source.span,
"overflowing enum wasn't rejected by hir analysis",
);
}
let kind = ExprKind::NonHirLiteral { lit, user_ty: None };
let offset = self.thir.exprs.push(Expr { temp_lifetime, ty: discr_ty, span, kind });