mirror of
https://github.com/nim-lang/Nim.git
synced 2026-07-19 15:31:28 +00:00
Don't consider tyAnd/tyNot/tyOr/tyAnything as generic (#8700)
* Don't consider tyAnd/tyNot/tyOr/tyAnything as generic `containsGenericType` was too shallow and didn't check all the branches. The resulting half-processed nodes are often simplified by the constant folding pass but when that's not possible we get a nasty error during codegen. Fixes #8693 * Move the blame onto the semFold pass Slightly better evaluation of `is` forms.
This commit is contained in:
@@ -352,6 +352,11 @@ proc isOpImpl(c: PContext, n: PNode, flags: TExprFlags): PNode =
|
||||
res = t.kind == tyProc and
|
||||
t.callConv == ccClosure and
|
||||
tfIterator notin t.flags
|
||||
of "iterator":
|
||||
let t = skipTypes(t1, abstractRange)
|
||||
res = t.kind == tyProc and
|
||||
t.callConv == ccClosure and
|
||||
tfIterator in t.flags
|
||||
else:
|
||||
res = false
|
||||
else:
|
||||
|
||||
@@ -173,32 +173,41 @@ proc makeRangeF(typ: PType, first, last: BiggestFloat; g: ModuleGraph): PType =
|
||||
result.n = n
|
||||
addSonSkipIntLit(result, skipTypes(typ, {tyRange}))
|
||||
|
||||
proc evalIs(n, a: PNode): PNode =
|
||||
proc evalIs(n: PNode, lhs: PSym, g: ModuleGraph): PNode =
|
||||
# XXX: This should use the standard isOpImpl
|
||||
#internalAssert a.kind == nkSym and a.sym.kind == skType
|
||||
#internalAssert n.sonsLen == 3 and
|
||||
# n[2].kind in {nkStrLit..nkTripleStrLit, nkType}
|
||||
internalAssert g.config,
|
||||
n.sonsLen == 3 and
|
||||
lhs.typ != nil and
|
||||
n[2].kind in {nkStrLit..nkTripleStrLit, nkType}
|
||||
|
||||
let t1 = a.sym.typ
|
||||
var
|
||||
res = false
|
||||
t1 = lhs.typ
|
||||
t2 = n[2].typ
|
||||
|
||||
if t1.kind == tyTypeDesc and t2.kind != tyTypeDesc:
|
||||
t1 = t1.base
|
||||
|
||||
if n[2].kind in {nkStrLit..nkTripleStrLit}:
|
||||
case n[2].strVal.normalize
|
||||
of "closure":
|
||||
let t = skipTypes(t1, abstractRange)
|
||||
result = newIntNode(nkIntLit, ord(t.kind == tyProc and
|
||||
t.callConv == ccClosure and
|
||||
tfIterator notin t.flags))
|
||||
res = t.kind == tyProc and
|
||||
t.callConv == ccClosure and
|
||||
tfIterator notin t.flags
|
||||
of "iterator":
|
||||
let t = skipTypes(t1, abstractRange)
|
||||
result = newIntNode(nkIntLit, ord(t.kind == tyProc and
|
||||
t.callConv == ccClosure and
|
||||
tfIterator in t.flags))
|
||||
else: discard
|
||||
res = t.kind == tyProc and
|
||||
t.callConv == ccClosure and
|
||||
tfIterator in t.flags
|
||||
else:
|
||||
res = false
|
||||
else:
|
||||
# XXX semexprs.isOpImpl is slightly different and requires a context. yay.
|
||||
let t2 = n[2].typ
|
||||
var match = sameType(t1, t2)
|
||||
result = newIntNode(nkIntLit, ord(match))
|
||||
res = sameType(t1, t2)
|
||||
|
||||
result = newIntNode(nkIntLit, ord(res))
|
||||
result.typ = n.typ
|
||||
|
||||
proc evalOp(m: TMagic, n, a, b, c: PNode; g: ModuleGraph): PNode =
|
||||
@@ -584,6 +593,9 @@ proc getConstExpr(m: PSym, n: PNode; g: ModuleGraph): PNode =
|
||||
result = copyTree(s.ast)
|
||||
of skProc, skFunc, skMethod:
|
||||
result = n
|
||||
of skParam:
|
||||
if s.typ != nil and s.typ.kind == tyTypeDesc:
|
||||
result = newSymNodeTypeDesc(s, n.info)
|
||||
of skType:
|
||||
# XXX gensym'ed symbols can come here and cannot be resolved. This is
|
||||
# dirty, but correct.
|
||||
@@ -651,9 +663,9 @@ proc getConstExpr(m: PSym, n: PNode; g: ModuleGraph): PNode =
|
||||
of mConStrStr:
|
||||
result = foldConStrStr(m, n, g)
|
||||
of mIs:
|
||||
let a = getConstExpr(m, n[1], g)
|
||||
if a != nil and a.kind == nkSym and a.sym.kind == skType:
|
||||
result = evalIs(n, a)
|
||||
let lhs = getConstExpr(m, n[1], g)
|
||||
if lhs != nil and lhs.kind == nkSym:
|
||||
result = evalIs(n, lhs.sym, g)
|
||||
else:
|
||||
result = magicCall(m, n, g)
|
||||
except OverflowError:
|
||||
|
||||
29
tests/magics/t8693.nim
Normal file
29
tests/magics/t8693.nim
Normal file
@@ -0,0 +1,29 @@
|
||||
discard """
|
||||
output: '''true
|
||||
false
|
||||
true
|
||||
false
|
||||
false
|
||||
true
|
||||
true
|
||||
false
|
||||
true
|
||||
true
|
||||
'''
|
||||
"""
|
||||
|
||||
type Foo = int | float
|
||||
|
||||
proc bar(t1, t2: typedesc): bool =
|
||||
echo (t1 is t2)
|
||||
(t2 is t1)
|
||||
|
||||
proc bar[T](x: T, t2: typedesc): bool =
|
||||
echo (T is t2)
|
||||
(t2 is T)
|
||||
|
||||
echo bar(int, Foo)
|
||||
echo bar(4, Foo)
|
||||
echo bar(any, int)
|
||||
echo bar(int, any)
|
||||
echo bar(Foo, Foo)
|
||||
Reference in New Issue
Block a user