Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow typed/untyped params in magic procs (even if not in stdlib) #12911

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions compiler/semtypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,10 @@ proc newProcType(c: PContext; info: TLineInfo; prev: PType = nil): PType =
# usual we desperately try to save memory:
result.n.add newNodeI(nkEffectList, info)

proc isMagic(sym: PSym): bool =
let nPragmas = sym.ast[pragmasPos]
return hasPragma(nPragmas, wMagic)

proc semProcTypeNode(c: PContext, n, genericParams: PNode,
prev: PType, kind: TSymKind; isType=false): PType =
# for historical reasons (code grows) this is invoked for parameter
Expand Down Expand Up @@ -1148,11 +1152,13 @@ proc semProcTypeNode(c: PContext, n, genericParams: PNode,

if hasType:
typ = semParamType(c, a[^2], constraint)
var owner = getCurrOwner(c).owner
let sym = getCurrOwner(c)
var owner = sym.owner
# TODO: Disallow typed/untyped in procs in the compiler/stdlib
if (owner.kind != skModule or owner.owner.name.s != "stdlib") and
kind == skProc and (typ.kind == tyTyped or typ.kind == tyUntyped):
localError(c.config, a[^2].info, "'" & typ.sym.name.s & "' is only allowed in templates and macros")
if kind == skProc and (typ.kind == tyTyped or typ.kind == tyUntyped):
if not isMagic(sym):
if (owner.kind != skModule or (owner.owner.name.s != "stdlib")):
localError(c.config, a[^2].info, "'" & typ.sym.name.s & "' is only allowed in templates and macros or magic procs")

if hasDefault:
def = a[^1]
Expand Down
12 changes: 10 additions & 2 deletions tests/proc/untyped.nim
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
discard """
errormsg: "'untyped' is only allowed in templates and macros"
line: 6
errormsg: "'untyped' is only allowed in templates and macros or magic procs"
line: 14
"""

# magic procs are allowed with `untyped`
proc declaredInScope2*(x: untyped): bool {.magic: "DefinedInScope", noSideEffect, compileTime.}
proc bar(): bool =
var x = 1
declaredInScope2(x)
static: doAssert bar()

# but not non-magic procs
proc fun(x:untyped)=discard
fun(10)