Skip to content

Commit

Permalink
fix #13524: magics with untyped params (eg astToStr) now work inside …
Browse files Browse the repository at this point in the history
…generics
  • Loading branch information
timotheecour committed Feb 28, 2020
1 parent 1056f9e commit 148818e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
13 changes: 12 additions & 1 deletion compiler/semgnrc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,14 @@ proc semGenericStmt(c: PContext, n: PNode,
var mixinContext = false
if s != nil:
incl(s.flags, sfUsed)
mixinContext = s.magic in {mDefined, mDefinedInScope, mCompiles}
# more robust/future proof than:
# mixinContext = s.magic in {mDefined, mDefinedInScope, mCompiles, mAstToStr}
if s.magic != mNone and s.typ != nil:
for i in 1..<s.typ.len:
if s.typ[i].kind == tyUntyped:
mixinContext = true
break

let sc = symChoice(c, fn, s, if s.isMixedIn: scForceOpen else: scOpen)
case s.kind
of skMacro:
Expand Down Expand Up @@ -283,6 +290,10 @@ proc semGenericStmt(c: PContext, n: PNode,
# is not exported and yet the generic 'threadProcWrapper' works correctly.
let flags = if mixinContext: flags+{withinMixin} else: flags
for i in first..<result.safeLen:
let flags = if mixinContext: flags+{withinMixin} else: flags
# instead, would be better to only set `withinMixin` for arguments of
# kind tyUntyped, eg `if s.typ[i].kind == tyUntyped:`, however, this
# currentl doesn't work
result[i] = semGenericStmt(c, result[i], flags, ctx)
of nkCurlyExpr:
result = newNodeI(nkCall, n.info)
Expand Down
2 changes: 1 addition & 1 deletion lib/system.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1629,7 +1629,7 @@ else:
result[i+1] = y[i]


proc astToStr*[T](x: T): string {.magic: "AstToStr", noSideEffect.}
proc astToStr*(x: untyped): string {.magic: "AstToStr", noSideEffect.}
## Converts the AST of `x` into a string representation. This is very useful
## for debugging.

Expand Down
8 changes: 8 additions & 0 deletions tests/generics/t13525.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# https://github.com/nim-lang/Nim/issues/13524
template fun(field): untyped = astToStr(field)
proc test1(): string =
fun(nonexistant1) # works
proc test2[T](): string =
fun(nonexistant2) # used to cause: Error: undeclared identifier: 'nonexistant2'
doAssert test1() == "nonexistant1"
doAssert test2[int]() == "nonexistant2"

0 comments on commit 148818e

Please sign in to comment.