Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow multiple parameters using an underscore as name #1493

Draft
wants to merge 5 commits into
base: devel
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions compiler/ast/astalgo.nim
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ proc getNamedParamFromList*(list: PNode, ident: PIdent): PSym =
## if c.instLines: actual.info else: templ.info)
for i in 1..<list.len:
let it = list[i].sym
if it.name.id == ident.id or
sameIgnoreBacktickGensymInfo(it.name.s, ident.s): return it
if it.name.s != "_" and (it.name.id == ident.id or
sameIgnoreBacktickGensymInfo(it.name.s, ident.s)): return it

proc hashNode(p: RootRef): Hash =
result = hash(cast[pointer](p))
Expand Down
6 changes: 5 additions & 1 deletion compiler/backend/ccgtypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ proc mangleParamName(c: ConfigRef; s: PSym): Rope =
## we cannot use 'sigConflicts' here since we don't have access to a BProc.
## Fortunately C's scoping rules are sane enough so that that doesn't
## cause any trouble.
if true:
if s.name.s == "_":
# multiple parameters can use an underscore as the name. Construct a
# locally unique C name by appending the parameter position
result = "_p" & $s.position
else:
var res = s.name.s.mangle
if isKeyword(s.name) or c.cppDefines.contains(res):
res.add "_0"
Expand Down
1 change: 1 addition & 0 deletions compiler/sem/sem.nim
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,7 @@ proc semConstBoolExpr(c: PContext, n: PNode): PNode =

proc semGenericStmt(c: PContext, n: PNode): PNode
proc semConceptBody(c: PContext, n: PNode): PNode
proc isDiscardUnderscore(v: PSym): bool

include semtypes, semtempl, semgnrc, semstmts, semexprs

Expand Down
4 changes: 1 addition & 3 deletions compiler/sem/seminst.nim
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,7 @@ proc addParamOrResult(c: PContext, param: PSym)

proc instantiateBody(c: PContext, n, params: PNode, result, orig: PSym) =
if n[bodyPos].kind != nkEmpty:
let procParams = result.typ.n
for i in 1..<procParams.len:
addDecl(c, procParams[i].sym)
addParams(c, result.typ.n)
maybeAddResult(c, result, result.ast)

inc c.inGenericInst
Expand Down
5 changes: 4 additions & 1 deletion compiler/sem/semtypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1589,6 +1589,9 @@ proc semProcTypeNode(c: PContext, n, genericParams: PNode,
if argNode.kind == nkError:
localReport(c.config, argNode)

# mark underscore parameters as gensyms:
discard isDiscardUnderscore(arg)

if a[j].kind == nkPragmaExpr:
a[j][1] = pragmaDecl(c, arg, a[j][1], paramPragmas)
# check if we got any errors and if so report them
Expand All @@ -1614,7 +1617,7 @@ proc semProcTypeNode(c: PContext, n, genericParams: PNode,
inc(counter)
if def != nil and def.kind != nkEmpty:
arg.ast = copyTree(def)
if containsOrIncl(check, arg.name.id):
if arg.name.s != "_" and containsOrIncl(check, arg.name.id):
localReport(c.config, a[j], reportSym(
rsemParameterRedefinition, arg))

Expand Down
19 changes: 19 additions & 0 deletions tests/lang/s02_core/s02_procedures/t99_underscore_parameter.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
discard """
description: '''
Multiple parameters can use an underscore as the name, making the
parameters inaccessible
'''
"""

proc test1(_: int) = discard
test1(1)

proc test2(_: int, _: int) = discard
test2(1, 2)

proc mixed(a: int, _: int) = discard
mixed(1, 2)

## The same is true for parameters of anonymous procedures.
let anon = proc(_, _, _: int) = discard
anon(1, 2, 3)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
discard """
description: '''
An underscore parameter cannot be reference in named parameter passing.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
An underscore parameter cannot be reference in named parameter passing.
An underscore parameter cannot be referenced in named parameter passing.

'''
action: reject
"""

proc test(_: int) = discard
test(_ = 1)