From 7ebb2baa6c0d3e0717bb79f746cf36a6095d31d7 Mon Sep 17 00:00:00 2001 From: Neelesh Chandola Date: Mon, 9 Dec 2019 13:49:40 +0530 Subject: [PATCH] Assigning template to var/let/const gives a proper error (#12851) * Assigning template to var/let/const gives a proper error * Fix style --- compiler/semstmts.nim | 14 +++++++++----- tests/errmsgs/t10489_a.nim | 2 +- tests/errmsgs/t10489_b.nim | 2 +- tests/errmsgs/t12844.nim | 13 +++++++++++++ 4 files changed, 24 insertions(+), 7 deletions(-) create mode 100644 tests/errmsgs/t12844.nim diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim index 3abee2faf7..e8a5ae0387 100644 --- a/compiler/semstmts.nim +++ b/compiler/semstmts.nim @@ -36,7 +36,7 @@ const errRecursiveDependencyX = "recursive dependency: '$1'" errRecursiveDependencyIteratorX = "recursion is not supported in iterators: '$1'" errPragmaOnlyInHeaderOfProcX = "pragmas are only allowed in the header of a proc; redefinition of $1" - errCannotAssignMacroSymbol = "cannot assign macro symbol to $1 here. Forgot to invoke the macro with '()'?" + errCannotAssignMacroSymbol = "cannot assign $1 '$2' to '$3'. Did you mean to call the $1 with '()'?" errInvalidTypeDescAssign = "'typedesc' metatype is not valid here; typed '=' instead of ':'?" proc semDiscard(c: PContext, n: PNode): PNode = @@ -449,8 +449,10 @@ proc semVarOrLet(c: PContext, n: PNode, symkind: TSymKind): PNode = if a[^1].kind != nkEmpty: def = semExprWithType(c, a[^1], {efAllowDestructor}) if def.typ.kind == tyProc and def.kind == nkSym: - if def.sym.kind == skMacro: - localError(c.config, def.info, errCannotAssignMacroSymbol % "variable") + if def.sym.kind in {skMacro, skTemplate}: + localError(c.config, def.info, errCannotAssignMacroSymbol % [ + if def.sym.kind == skMacro: "macro" else: "template", + def.sym.name.s, a[0].ident.s]) def.typ = errorType(c) elif def.typ.kind == tyTypeDesc and c.p.owner.kind != skMacro: # prevent the all too common 'var x = int' bug: @@ -590,8 +592,10 @@ proc semConst(c: PContext, n: PNode): PNode = # don't evaluate here since the type compatibility check below may add a converter var def = semExprWithType(c, a[^1]) if def.typ.kind == tyProc and def.kind == nkSym: - if def.sym.kind == skMacro: - localError(c.config, def.info, errCannotAssignMacroSymbol % "constant") + if def.sym.kind in {skMacro, skTemplate}: + localError(c.config, def.info, errCannotAssignMacroSymbol % [ + if def.sym.kind == skMacro: "macro" else: "template", + def.sym.name.s, a[0].ident.s]) def.typ = errorType(c) elif def.typ.kind == tyTypeDesc and c.p.owner.kind != skMacro: # prevent the all too common 'const x = int' bug: diff --git a/tests/errmsgs/t10489_a.nim b/tests/errmsgs/t10489_a.nim index 7cfe0176cf..86659f7acd 100644 --- a/tests/errmsgs/t10489_a.nim +++ b/tests/errmsgs/t10489_a.nim @@ -1,5 +1,5 @@ discard """ -errormsg: "cannot assign macro symbol to variable here" +errormsg: "cannot assign macro 'm' to 'x1'. Did you mean to call the macro with '()'?" line: 9 """ diff --git a/tests/errmsgs/t10489_b.nim b/tests/errmsgs/t10489_b.nim index e690f028a7..1182b45126 100644 --- a/tests/errmsgs/t10489_b.nim +++ b/tests/errmsgs/t10489_b.nim @@ -1,5 +1,5 @@ discard """ -errormsg: "cannot assign macro symbol to constant here" +errormsg: "cannot assign macro 'm' to 'x2'. Did you mean to call the macro with '()'?" line: 9 """ diff --git a/tests/errmsgs/t12844.nim b/tests/errmsgs/t12844.nim new file mode 100644 index 0000000000..999d26255f --- /dev/null +++ b/tests/errmsgs/t12844.nim @@ -0,0 +1,13 @@ +discard """ +cmd: "nim check $file" +errormsg: "cannot assign template 'z' to 'y'. Did you mean to call the template with '()'?" +nimout: ''' +t12844.nim(11, 11) Error: cannot assign template 'z' to 'x'. Did you mean to call the template with '()'? +t12844.nim(12, 9) Error: cannot assign template 'z' to 'y'. Did you mean to call the template with '()'?''' +""" + +template z*(args: varargs[string, `$`]) = + discard +const x = z +var y = z +