mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-22 15:25:22 +00:00
Improve error message for keywords as parameters (#25052)
A function with an illegal parameter name like ```nim proc myproc(type: int) = echo type ``` would uninformatively fail like so: ```nim tkeywordparam.nim(1, 13) Error: expected closing ')' ``` This commit makes it return the following error: ```nim tkeywordparam.nim(1, 13) Error: 'type' is a keyword and cannot be used as a parameter name ``` --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> Co-authored-by: Emre Şafak <esafak@users.noreply.github.com> Co-authored-by: Andreas Rumpf <araq4k@proton.me>
This commit is contained in:
@@ -1156,7 +1156,10 @@ proc parseParamList(p: var Parser, retColon = true): PNode =
|
||||
parMessage(p, errGenerated, "the syntax is 'parameter: var T', not 'var parameter: T'")
|
||||
break
|
||||
else:
|
||||
parMessage(p, "expected closing ')'")
|
||||
if p.tok.tokType in tokKeywordLow..tokKeywordHigh:
|
||||
parMessage(p, errGenerated, "'" & $p.tok.ident.s & "' is a keyword and cannot be used as a parameter name")
|
||||
else:
|
||||
parMessage(p, "expected closing ')'")
|
||||
break
|
||||
result.add(a)
|
||||
if p.tok.tokType notin {tkComma, tkSemiColon}: break
|
||||
|
||||
10
tests/errmsgs/tkeywordparam.nim
Normal file
10
tests/errmsgs/tkeywordparam.nim
Normal file
@@ -0,0 +1,10 @@
|
||||
discard """
|
||||
cmd: "nim check $file"
|
||||
errormsg: "'type' is a keyword and cannot be used as a parameter name"
|
||||
nimout: '''
|
||||
tkeywordparam.nim(1, 13) Error: 'type' is a keyword and cannot be used as a parameter name
|
||||
'''
|
||||
"""
|
||||
|
||||
proc myproc(type: int) =
|
||||
echo type
|
||||
Reference in New Issue
Block a user