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>
(cherry picked from commit 9c1e3bf8fb)
This commit is contained in:
Emre Şafak
2025-07-14 17:15:02 -04:00
committed by narimiran
parent ce69b31309
commit 1a4a1ab747
2 changed files with 14 additions and 1 deletions

View File

@@ -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

View 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