1
Fork 0

Rollup merge of #131909 - clubby789:enum-overflow-cast, r=compiler-errors

Prevent overflowing enum cast from ICEing

Fixes #131902
This commit is contained in:
Stuart Cook 2024-10-24 14:19:56 +11:00 committed by GitHub
commit 4b02d642dd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
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 });