error message for accidental use of macro (#10490)
* error message for accidental use of macro
This commit is contained in:
parent
257965e105
commit
6b9a139f7f
3 changed files with 27 additions and 2 deletions
|
|
@ -448,10 +448,14 @@ proc semVarOrLet(c: PContext, n: PNode, symkind: TSymKind): PNode =
|
||||||
var def: PNode = c.graph.emptyNode
|
var def: PNode = c.graph.emptyNode
|
||||||
if a.sons[length-1].kind != nkEmpty:
|
if a.sons[length-1].kind != nkEmpty:
|
||||||
def = semExprWithType(c, a.sons[length-1], {efAllowDestructor})
|
def = semExprWithType(c, a.sons[length-1], {efAllowDestructor})
|
||||||
if def.typ.kind == tyTypeDesc and c.p.owner.kind != skMacro:
|
if def.typ.kind == tyProc and def.kind == nkSym and def.sym.kind == skMacro:
|
||||||
|
localError(c.config, def.info, "cannot assign macro symbol to variable here. Forgot to invoke the macro with '()'?")
|
||||||
|
def.typ = errorType(c)
|
||||||
|
elif def.typ.kind == tyTypeDesc and c.p.owner.kind != skMacro:
|
||||||
# prevent the all too common 'var x = int' bug:
|
# prevent the all too common 'var x = int' bug:
|
||||||
localError(c.config, def.info, "'typedesc' metatype is not valid here; typed '=' instead of ':'?")
|
localError(c.config, def.info, "'typedesc' metatype is not valid here; typed '=' instead of ':'?")
|
||||||
def.typ = errorType(c)
|
def.typ = errorType(c)
|
||||||
|
|
||||||
if typ != nil:
|
if typ != nil:
|
||||||
if typ.isMetaType:
|
if typ.isMetaType:
|
||||||
def = inferWithMetatype(c, typ, def)
|
def = inferWithMetatype(c, typ, def)
|
||||||
|
|
@ -583,7 +587,10 @@ proc semConst(c: PContext, n: PNode): PNode =
|
||||||
localError(c.config, a.sons[length-1].info, errConstExprExpected)
|
localError(c.config, a.sons[length-1].info, errConstExprExpected)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if def.typ.kind == tyTypeDesc and c.p.owner.kind != skMacro:
|
if def.typ.kind == tyProc and def.kind == nkSym and def.sym.kind == skMacro:
|
||||||
|
localError(c.config, def.info, "cannot assign macro symbol to constant here. Forgot to invoke the macro with '()'?")
|
||||||
|
def.typ = errorType(c)
|
||||||
|
elif def.typ.kind == tyTypeDesc and c.p.owner.kind != skMacro:
|
||||||
# prevent the all too common 'const x = int' bug:
|
# prevent the all too common 'const x = int' bug:
|
||||||
localError(c.config, def.info, "'typedesc' metatype is not valid here; typed '=' instead of ':'?")
|
localError(c.config, def.info, "'typedesc' metatype is not valid here; typed '=' instead of ':'?")
|
||||||
def.typ = errorType(c)
|
def.typ = errorType(c)
|
||||||
|
|
|
||||||
9
tests/errmsgs/t10489_a.nim
Normal file
9
tests/errmsgs/t10489_a.nim
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
discard """
|
||||||
|
errormsg: "cannot assign macro symbol to variable here"
|
||||||
|
line: 9
|
||||||
|
"""
|
||||||
|
|
||||||
|
macro m(body: untyped): untyped =
|
||||||
|
body
|
||||||
|
|
||||||
|
let x1 = m
|
||||||
9
tests/errmsgs/t10489_b.nim
Normal file
9
tests/errmsgs/t10489_b.nim
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
discard """
|
||||||
|
errormsg: "cannot assign macro symbol to constant here"
|
||||||
|
line: 9
|
||||||
|
"""
|
||||||
|
|
||||||
|
macro m(body: untyped): untyped =
|
||||||
|
body
|
||||||
|
|
||||||
|
const x2 = m
|
||||||
Loading…
Add table
Add a link
Reference in a new issue