From 8cf5643621600aaa869935721227fc3b7ee5f881 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Sun, 7 May 2023 03:38:17 +0800 Subject: [PATCH] fixes #21280; Enum with int64.high() value crashes compiler (#21285) * fixes #21280; Enum with int64.high() value crashes compiler * Update tests/enum/tenum.nim * Update tests/enum/tenum.nim * fixes tests * Update tests/enum/tenum.nim --------- Co-authored-by: Andreas Rumpf --- compiler/semtypes.nim | 7 ++++++- tests/enum/tenum.nim | 8 ++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/compiler/semtypes.nim b/compiler/semtypes.nim index e54de80a8241..38d040946f08 100644 --- a/compiler/semtypes.nim +++ b/compiler/semtypes.nim @@ -16,6 +16,7 @@ const errIntLiteralExpected = "integer literal expected" errWrongNumberOfVariables = "wrong number of variables" errInvalidOrderInEnumX = "invalid order in enum '$1'" + errOverflowInEnumX = "The enum '$1' exceeds its maximum value ($2)" errOrdinalTypeExpected = "ordinal type expected; given: $1" errSetTooBig = "set is too large; use `std/sets` for ordinal types with more than 2^16 elements" errBaseTypeMustBeOrdinal = "base type of a set must be an ordinal" @@ -147,7 +148,11 @@ proc semEnum(c: PContext, n: PNode, prev: PType): PType = declarePureEnumField(c, e) if (let conflict = strTableInclReportConflict(symbols, e); conflict != nil): wrongRedefinition(c, e.info, e.name.s, conflict.info) - inc(counter) + if counter == high(typeof(counter)): + if i > 1 and result.n[i-2].sym.position == high(int): + localError(c.config, n[i].info, errOverflowInEnumX % [e.name.s, $high(typeof(counter))]) + else: + inc(counter) if isPure and sfExported in result.sym.flags: addPureEnum(c, LazySym(sym: result.sym)) if tfNotNil in e.typ.flags and not hasNull: diff --git a/tests/enum/tenum.nim b/tests/enum/tenum.nim index 88d85ddcc437..8046c658907f 100644 --- a/tests/enum/tenum.nim +++ b/tests/enum/tenum.nim @@ -176,3 +176,11 @@ block: # bug #12589 when not defined(gcRefc): doAssert $typ() == "wkbPoint25D" + + block: # bug #21280 + type + Test = enum + B = 19 + A = int64.high() + + doAssert ord(A) == int64.high()