From b0f860d90fefc89219c1e2bc9f8815cde07a7b7c Mon Sep 17 00:00:00 2001 From: Neelesh Chandola Date: Sun, 9 Dec 2018 10:25:04 +0530 Subject: [PATCH 1/5] Fix enum regression --- compiler/semtypes.nim | 2 ++ 1 file changed, 2 insertions(+) diff --git a/compiler/semtypes.nim b/compiler/semtypes.nim index a011a8fc83..294cabe477 100644 --- a/compiler/semtypes.nim +++ b/compiler/semtypes.nim @@ -90,6 +90,8 @@ proc semEnum(c: PContext, n: PNode, prev: PType): PType = if sonsLen(v) == 2: strVal = v.sons[1] # second tuple part is the string value if skipTypes(strVal.typ, abstractInst).kind in {tyString, tyCString}: + if not isOrdinalType(v.sons[0].typ): + localError(c.config, v.sons[0].info, errOrdinalTypeExpected) x = getOrdValue(v.sons[0]) # first tuple part is the ordinal else: localError(c.config, strVal.info, errStringLiteralExpected) From 93503c18aa8d649a9a128d80d40430d7f9e6aa21 Mon Sep 17 00:00:00 2001 From: Neelesh Chandola Date: Sun, 9 Dec 2018 10:33:20 +0530 Subject: [PATCH 2/5] Add test --- tests/errmsgs/t9908.nim | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 tests/errmsgs/t9908.nim diff --git a/tests/errmsgs/t9908.nim b/tests/errmsgs/t9908.nim new file mode 100644 index 0000000000..ca135ed50c --- /dev/null +++ b/tests/errmsgs/t9908.nim @@ -0,0 +1,10 @@ +discard """ +errormsg: "Error: ordinal type expected" +line: 10 +""" + +# https://github.com/nim-lang/Nim/issues/9908 + +type + X = enum + a = ("a", "b") From d977fc6f320ea7aed97de0d883b6da27f7de61c4 Mon Sep 17 00:00:00 2001 From: Neelesh Chandola Date: Sun, 9 Dec 2018 11:20:52 +0530 Subject: [PATCH 3/5] Fix test case expected output --- tests/errmsgs/t9908.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/errmsgs/t9908.nim b/tests/errmsgs/t9908.nim index ca135ed50c..fa086517f7 100644 --- a/tests/errmsgs/t9908.nim +++ b/tests/errmsgs/t9908.nim @@ -1,5 +1,5 @@ discard """ -errormsg: "Error: ordinal type expected" +errormsg: "ordinal type expected" line: 10 """ From e44641b6cb025b689298584418e91844b1ac5a4a Mon Sep 17 00:00:00 2001 From: Neelesh Chandola Date: Sun, 9 Dec 2018 14:20:01 +0530 Subject: [PATCH 4/5] Float values are invalid in enum --- compiler/semtypes.nim | 2 ++ tests/errmsgs/{t9908.nim => t9908_01.nim} | 4 ++-- tests/errmsgs/t9908_02.nim | 10 ++++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) rename tests/errmsgs/{t9908.nim => t9908_01.nim} (75%) create mode 100644 tests/errmsgs/t9908_02.nim diff --git a/compiler/semtypes.nim b/compiler/semtypes.nim index 294cabe477..4cc759e529 100644 --- a/compiler/semtypes.nim +++ b/compiler/semtypes.nim @@ -100,6 +100,8 @@ proc semEnum(c: PContext, n: PNode, prev: PType): PType = of tyString, tyCString: strVal = v x = counter + of tyFloat..tyFloat128: + localError(c.config, v.info, errOrdinalTypeExpected) else: x = getOrdValue(v) if i != 1: diff --git a/tests/errmsgs/t9908.nim b/tests/errmsgs/t9908_01.nim similarity index 75% rename from tests/errmsgs/t9908.nim rename to tests/errmsgs/t9908_01.nim index fa086517f7..b9d37b67b7 100644 --- a/tests/errmsgs/t9908.nim +++ b/tests/errmsgs/t9908_01.nim @@ -6,5 +6,5 @@ line: 10 # https://github.com/nim-lang/Nim/issues/9908 type - X = enum - a = ("a", "b") + X = enum + a = ("a", "b") diff --git a/tests/errmsgs/t9908_02.nim b/tests/errmsgs/t9908_02.nim new file mode 100644 index 0000000000..7ff3d1ff73 --- /dev/null +++ b/tests/errmsgs/t9908_02.nim @@ -0,0 +1,10 @@ +discard """ +errormsg: "ordinal type expected" +line: 10 +""" + +# https://github.com/nim-lang/Nim/pull/9909#issuecomment-445519287 + +type + E = enum + myVal = 80.9 From 47c38cb98e6cde435fd38565cd374050955610eb Mon Sep 17 00:00:00 2001 From: Neelesh Chandola Date: Tue, 11 Dec 2018 10:34:10 +0530 Subject: [PATCH 5/5] Better fix --- compiler/semtypes.nim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/semtypes.nim b/compiler/semtypes.nim index 4cc759e529..eed82adeb5 100644 --- a/compiler/semtypes.nim +++ b/compiler/semtypes.nim @@ -100,9 +100,9 @@ proc semEnum(c: PContext, n: PNode, prev: PType): PType = of tyString, tyCString: strVal = v x = counter - of tyFloat..tyFloat128: - localError(c.config, v.info, errOrdinalTypeExpected) else: + if not isOrdinalType(v.typ): + localError(c.config, v.info, errOrdinalTypeExpected) x = getOrdValue(v) if i != 1: if x != counter: incl(result.flags, tfEnumHasHoles)