From 623e0763c2df1aa49a516775ba5e43fa4e258ba2 Mon Sep 17 00:00:00 2001 From: Matthew Baulch Date: Thu, 11 Aug 2016 21:09:34 +1000 Subject: [PATCH 1/3] Tidy up isOrdinalType --- compiler/types.nim | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/compiler/types.nim b/compiler/types.nim index c56382b896..442db8ec94 100644 --- a/compiler/types.nim +++ b/compiler/types.nim @@ -148,10 +148,11 @@ proc skipGeneric(t: PType): PType = proc isOrdinalType(t: PType): bool = assert(t != nil) - # caution: uint, uint64 are no ordinal types! - result = t.kind in {tyChar,tyInt..tyInt64,tyUInt8..tyUInt32,tyBool,tyEnum} or - (t.kind in {tyRange, tyOrdinal, tyConst, tyMutable, tyGenericInst}) and - isOrdinalType(t.sons[0]) + const + # caution: uint, uint64 are no ordinal types! + baseKinds = {tyChar,tyInt..tyInt64,tyUInt8..tyUInt32,tyBool,tyEnum} + parentKinds = {tyRange, tyOrdinal, tyConst, tyMutable, tyGenericInst} + t.kind in baseKinds or (t.kind in parentKinds and isOrdinalType(t.sons[0])) proc enumHasHoles(t: PType): bool = var b = t From 8ebce3ce2b4210125bc8bf16aba1323d719adaf7 Mon Sep 17 00:00:00 2001 From: Matthew Baulch Date: Thu, 11 Aug 2016 21:11:06 +1000 Subject: [PATCH 2/3] Distinct types derived from ordinal types are ordinal. --- compiler/types.nim | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/compiler/types.nim b/compiler/types.nim index 442db8ec94..ff60730f0b 100644 --- a/compiler/types.nim +++ b/compiler/types.nim @@ -151,7 +151,8 @@ proc isOrdinalType(t: PType): bool = const # caution: uint, uint64 are no ordinal types! baseKinds = {tyChar,tyInt..tyInt64,tyUInt8..tyUInt32,tyBool,tyEnum} - parentKinds = {tyRange, tyOrdinal, tyConst, tyMutable, tyGenericInst} + parentKinds = {tyRange, tyOrdinal, tyConst, tyMutable, tyGenericInst, + tyDistinct} t.kind in baseKinds or (t.kind in parentKinds and isOrdinalType(t.sons[0])) proc enumHasHoles(t: PType): bool = From 674a1110f0ad8b95bb6d7332c87aa1c684cd0973 Mon Sep 17 00:00:00 2001 From: Matthew Baulch Date: Thu, 11 Aug 2016 21:13:18 +1000 Subject: [PATCH 3/3] Require ordinal or set argument to system.ord --- compiler/semmagic.nim | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/compiler/semmagic.nim b/compiler/semmagic.nim index d9fec62759..cbe9bc1768 100644 --- a/compiler/semmagic.nim +++ b/compiler/semmagic.nim @@ -114,8 +114,12 @@ proc semTypeTraits(c: PContext, n: PNode): PNode = proc semOrd(c: PContext, n: PNode): PNode = result = n - result.typ = makeRangeType(c, firstOrd(n.sons[1].typ), - lastOrd(n.sons[1].typ), n.info) + let parType = n.sons[1].typ + if isOrdinalType(parType) or parType.kind == tySet: + result.typ = makeRangeType(c, firstOrd(parType), lastOrd(parType), n.info) + else: + localError(n.info, errOrdinalTypeExpected) + result.typ = errorType(c) proc semBindSym(c: PContext, n: PNode): PNode = result = copyNode(n)