Fix constant folding of len() with concept type (#8143)

Fixes #7952
This commit is contained in:
LemonBoy 2018-06-30 13:43:55 +02:00 • committed by Andreas Rumpf
commit eec239e851
2 changed files with 18 additions and 6 deletions

View file

@ -623,7 +623,7 @@ proc firstOrd*(conf: ConfigRef; t: PType): BiggestInt =
assert(t.n.sons[0].kind == nkSym) assert(t.n.sons[0].kind == nkSym)
result = t.n.sons[0].sym.position result = t.n.sons[0].sym.position
of tyGenericInst, tyDistinct, tyTypeDesc, tyAlias, tySink, of tyGenericInst, tyDistinct, tyTypeDesc, tyAlias, tySink,
tyStatic, tyInferred, tyUserTypeClassInst: tyStatic, tyInferred, tyUserTypeClasses:
result = firstOrd(conf, lastSon(t)) result = firstOrd(conf, lastSon(t))
of tyOrdinal: of tyOrdinal:
if t.len > 0: result = firstOrd(conf, lastSon(t)) if t.len > 0: result = firstOrd(conf, lastSon(t))
@ -642,7 +642,7 @@ proc firstFloat*(t: PType): BiggestFloat =
getFloatValue(t.n.sons[0]) getFloatValue(t.n.sons[0])
of tyVar: firstFloat(t.sons[0]) of tyVar: firstFloat(t.sons[0])
of tyGenericInst, tyDistinct, tyTypeDesc, tyAlias, tySink, of tyGenericInst, tyDistinct, tyTypeDesc, tyAlias, tySink,
tyStatic, tyInferred: tyStatic, tyInferred, tyUserTypeClasses:
firstFloat(lastSon(t)) firstFloat(lastSon(t))
else: else:
internalError(newPartialConfigRef(), "invalid kind for firstFloat(" & $t.kind & ')') internalError(newPartialConfigRef(), "invalid kind for firstFloat(" & $t.kind & ')')
@ -679,7 +679,7 @@ proc lastOrd*(conf: ConfigRef; t: PType; fixedUnsigned = false): BiggestInt =
assert(t.n.sons[sonsLen(t.n) - 1].kind == nkSym) assert(t.n.sons[sonsLen(t.n) - 1].kind == nkSym)
result = t.n.sons[sonsLen(t.n) - 1].sym.position result = t.n.sons[sonsLen(t.n) - 1].sym.position
of tyGenericInst, tyDistinct, tyTypeDesc, tyAlias, tySink, of tyGenericInst, tyDistinct, tyTypeDesc, tyAlias, tySink,
tyStatic, tyInferred: tyStatic, tyInferred, tyUserTypeClasses:
result = lastOrd(conf, lastSon(t)) result = lastOrd(conf, lastSon(t))
of tyProxy: result = 0 of tyProxy: result = 0
of tyOrdinal: of tyOrdinal:
@ -699,7 +699,7 @@ proc lastFloat*(t: PType): BiggestFloat =
assert(t.n.kind == nkRange) assert(t.n.kind == nkRange)
getFloatValue(t.n.sons[1]) getFloatValue(t.n.sons[1])
of tyGenericInst, tyDistinct, tyTypeDesc, tyAlias, tySink, of tyGenericInst, tyDistinct, tyTypeDesc, tyAlias, tySink,
tyStatic, tyInferred: tyStatic, tyInferred, tyUserTypeClasses:
lastFloat(lastSon(t)) lastFloat(lastSon(t))
else: else:
internalError(newPartialConfigRef(), "invalid kind for lastFloat(" & $t.kind & ')') internalError(newPartialConfigRef(), "invalid kind for lastFloat(" & $t.kind & ')')
@ -707,7 +707,7 @@ proc lastFloat*(t: PType): BiggestFloat =
proc lengthOrd*(conf: ConfigRef; t: PType): BiggestInt = proc lengthOrd*(conf: ConfigRef; t: PType): BiggestInt =
case t.kind case t.skipTypes(tyUserTypeClasses).kind
of tyInt64, tyInt32, tyInt: result = lastOrd(conf, t) of tyInt64, tyInt32, tyInt: result = lastOrd(conf, t)
of tyDistinct: result = lengthOrd(conf, t.sons[0]) of tyDistinct: result = lengthOrd(conf, t.sons[0])
else: else:
@ -717,7 +717,7 @@ proc lengthOrd*(conf: ConfigRef; t: PType): BiggestInt =
if last == high(BiggestInt) and first <= 0: if last == high(BiggestInt) and first <= 0:
result = last result = last
else: else:
result = lastOrd(conf, t) - firstOrd(conf, t) + 1 result = last - first + 1
# -------------- type equality ----------------------------------------------- # -------------- type equality -----------------------------------------------

12
tests/concepts/t7952.nim Normal file
View file

@ -0,0 +1,12 @@
discard """
output: 5
"""
type
HasLen = concept iter
len(iter) is int
proc echoLen(x: HasLen) =
echo len(x)
echoLen([1, 2, 3, 4, 5])