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

fix #13443 Underscore cannot be defined more than once in proc #17581

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
3 changes: 3 additions & 0 deletions compiler/ccgtypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ proc mangleParamName(m: BModule; s: PSym): Rope =
## we cannot use 'sigConflicts' here since we have a BModule, not a BProc.
## Fortunately C's scoping rules are sane enough so that that doesn't
## cause any trouble.
if s.name.s == "_":
Copy link
Member

Choose a reason for hiding this comment

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

can there be something similar for js? or what prevents this?

Copy link
Member Author

@ringabout ringabout Mar 31, 2021

Choose a reason for hiding this comment

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

It just didn't work for JS VM I guess a known bug

Copy link
Member Author

Choose a reason for hiding this comment

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

tunderscore.nim(9, 27) Error: cannot evaluate at compile time: x

Copy link
Member

Choose a reason for hiding this comment

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

in this case we should leave #13443 open (with a note) until we can address this :)

Copy link
Member Author

Choose a reason for hiding this comment

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

why, that's the bug of js vm, not related to my PR?

Copy link
Member

Choose a reason for hiding this comment

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

fine, I'll just open a new bug for js+vm then

Copy link
Contributor

Choose a reason for hiding this comment

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

This shouldn't be handled in the backend IMO.

Copy link
Member

@timotheecour timotheecour Mar 31, 2021

Choose a reason for hiding this comment

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

maybe, what do you suggest concretely?
(see also the items i added in future work)

Copy link
Collaborator

@metagn metagn Oct 28, 2022

Choose a reason for hiding this comment

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

Can't we just mangle if sfGensym in s.flags?

Suggested change
if s.name.s == "_":
if sfGensym in s.flags:

result = getTempName(m)
return
result = s.loc.r
if result == nil:
var res = s.name.s.mangle
Expand Down
16 changes: 13 additions & 3 deletions compiler/semtypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ const
errNoGenericParamsAllowedForX = "no generic parameters allowed for $1"
errInOutFlagNotExtern = "the '$1' modifier can be used only with imported types"


proc isDiscardUnderscoreV(v: PSym): bool =
if v.name.s == "_":
v.flags.incl(sfGenSym)
timotheecour marked this conversation as resolved.
Show resolved Hide resolved
result = true

proc newOrPrevType(kind: TTypeKind, prev: PType, c: PContext): PType =
if prev == nil:
result = newTypeS(kind, c)
Expand Down Expand Up @@ -984,7 +990,9 @@ proc addParamOrResult(c: PContext, param: PSym, kind: TSymKind) =
# bug #XXX, fix the gensym'ed parameters owner:
if param.owner == nil:
param.owner = getCurrOwner(c)
else: addDecl(c, param)
else:
if not isDiscardUnderscoreV(param):
addDecl(c, param)

template shouldHaveMeta(t) =
internalAssert c.config, tfHasMeta in t.flags
Expand Down Expand Up @@ -1295,8 +1303,10 @@ 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):
localError(c.config, a[j].info, "attempt to redefine: '" & arg.name.s & "'")
# addDecl
if not isDiscardUnderscoreV(arg):
if containsOrIncl(check, arg.name.id):
localError(c.config, a[j].info, "attempt to redefine: '" & arg.name.s & "'")
result.n.add newSymNode(arg)
rawAddSon(result, finalType)
addParamOrResult(c, arg, kind)
Expand Down
28 changes: 28 additions & 0 deletions tests/proc/tunderscore.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
discard """
targets: "c cpp js"
"""

ringabout marked this conversation as resolved.
Show resolved Hide resolved
ringabout marked this conversation as resolved.
Show resolved Hide resolved
proc main() =
var x = 0

block:
proc foo(_, _: int) = x += 5

foo(1, 2)
doAssert x == 5

block:
proc foo(_: int, _: float) = x += 5

foo(1, 2)
doAssert x == 10

block:
proc foo(_: int, _: float, _: string) = x += 5

foo(1, 2, "5")
doAssert x == 15

ringabout marked this conversation as resolved.
Show resolved Hide resolved

static: main()
main()