Skip to content

Commit 9c1e3bf

Browse files
esafakgoogle-labs-jules[bot]Araq
authored
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>
1 parent 30d4f77 commit 9c1e3bf

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

compiler/parser.nim

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1156,7 +1156,10 @@ proc parseParamList(p: var Parser, retColon = true): PNode =
11561156
parMessage(p, errGenerated, "the syntax is 'parameter: var T', not 'var parameter: T'")
11571157
break
11581158
else:
1159-
parMessage(p, "expected closing ')'")
1159+
if p.tok.tokType in tokKeywordLow..tokKeywordHigh:
1160+
parMessage(p, errGenerated, "'" & $p.tok.ident.s & "' is a keyword and cannot be used as a parameter name")
1161+
else:
1162+
parMessage(p, "expected closing ')'")
11601163
break
11611164
result.add(a)
11621165
if p.tok.tokType notin {tkComma, tkSemiColon}: break

tests/errmsgs/tkeywordparam.nim

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
discard """
2+
cmd: "nim check $file"
3+
errormsg: "'type' is a keyword and cannot be used as a parameter name"
4+
nimout: '''
5+
tkeywordparam.nim(1, 13) Error: 'type' is a keyword and cannot be used as a parameter name
6+
'''
7+
"""
8+
9+
proc myproc(type: int) =
10+
echo type

0 commit comments

Comments
 (0)