mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-18 21:40:32 +00:00
* fixes `@[]` type inference in generics
* add issue links
* fixes macros and iterators
* refactor
* add one more test
(cherry picked from commit 64a0355f3f)
This commit is contained in:
@@ -973,7 +973,7 @@ proc afterCallActions(c: PContext; n, orig: PNode, flags: TExprFlags; expectedTy
|
||||
fixAbstractType(c, result)
|
||||
analyseIfAddressTakenInCall(c, result)
|
||||
if callee.magic != mNone:
|
||||
result = magicsAfterOverloadResolution(c, result, flags)
|
||||
result = magicsAfterOverloadResolution(c, result, flags, expectedType)
|
||||
when false:
|
||||
if result.typ != nil and
|
||||
not (result.typ.kind == tySequence and result.typ[0].kind == tyEmpty):
|
||||
|
||||
@@ -146,11 +146,18 @@ proc instantiateBody(c: PContext, n, params: PNode, result, orig: PSym) =
|
||||
if sfGenSym in param.flags:
|
||||
idTablePut(symMap, params[i].sym, result.typ.n[param.position+1].sym)
|
||||
freshGenSyms(c, b, result, orig, symMap)
|
||||
|
||||
|
||||
if sfBorrow notin orig.flags:
|
||||
# We do not want to generate a body for generic borrowed procs.
|
||||
# As body is a sym to the borrowed proc.
|
||||
b = semProcBody(c, b)
|
||||
let resultType = # todo probably refactor it into a function
|
||||
if result.kind == skMacro:
|
||||
sysTypeFromName(c.graph, n.info, "NimNode")
|
||||
elif not isInlineIterator(result.typ):
|
||||
result.typ[0]
|
||||
else:
|
||||
nil
|
||||
b = semProcBody(c, b, resultType)
|
||||
result.ast[bodyPos] = hloBody(c, b)
|
||||
excl(result.flags, sfForward)
|
||||
trackProc(c, result, result.ast[bodyPos])
|
||||
|
||||
@@ -460,7 +460,7 @@ proc semPrivateAccess(c: PContext, n: PNode): PNode =
|
||||
result = newNodeIT(nkEmpty, n.info, getSysType(c.graph, n.info, tyVoid))
|
||||
|
||||
proc magicsAfterOverloadResolution(c: PContext, n: PNode,
|
||||
flags: TExprFlags): PNode =
|
||||
flags: TExprFlags; expectedType: PType = nil): PNode =
|
||||
## This is the preferred code point to implement magics.
|
||||
## ``c`` the current module, a symbol table to a very good approximation
|
||||
## ``n`` the ast like it would be passed to a real macro
|
||||
@@ -583,5 +583,9 @@ proc magicsAfterOverloadResolution(c: PContext, n: PNode,
|
||||
result = n
|
||||
of mPrivateAccess:
|
||||
result = semPrivateAccess(c, n)
|
||||
of mArrToSeq:
|
||||
result = n
|
||||
if result.typ != nil and expectedType != nil and result.typ.kind == tySequence and expectedType.kind == tySequence and result.typ[0].kind == tyEmpty:
|
||||
result.typ = expectedType # type inference for empty sequence # bug #21377
|
||||
else:
|
||||
result = n
|
||||
|
||||
@@ -245,7 +245,7 @@ block: # bug #11777
|
||||
var s: S = {1, 2}
|
||||
doAssert 1 in s
|
||||
|
||||
block: # regression #20807
|
||||
block: # bug #20807
|
||||
var s: seq[string]
|
||||
template fail =
|
||||
s = @[]
|
||||
@@ -255,3 +255,38 @@ block: # regression #20807
|
||||
test: fail()
|
||||
doAssert not (compiles do:
|
||||
let x: seq[int] = `@`[string]([]))
|
||||
|
||||
block: # bug #21377
|
||||
proc b[T](v: T): seq[int] =
|
||||
let x = 0
|
||||
@[]
|
||||
|
||||
doAssert b(0) == @[]
|
||||
|
||||
block: # bug #21377
|
||||
proc b[T](v: T): seq[T] =
|
||||
let x = 0
|
||||
@[]
|
||||
|
||||
doAssert b(0) == @[]
|
||||
|
||||
block: # bug #21377
|
||||
proc b[T](v: T): set[bool] =
|
||||
let x = 0
|
||||
{}
|
||||
|
||||
doAssert b(0) == {}
|
||||
|
||||
block: # bug #21377
|
||||
proc b[T](v: T): array[0, int] =
|
||||
let x = 0
|
||||
[]
|
||||
|
||||
doAssert b(0) == []
|
||||
|
||||
block: # bug #21377
|
||||
proc b[T](v: T): array[0, (string, string)] =
|
||||
let x = 0
|
||||
{:}
|
||||
|
||||
doAssert b(0) == {:}
|
||||
|
||||
Reference in New Issue
Block a user