Skip to content

Commit

Permalink
make default values typed in proc AST same as param sym AST
Browse files Browse the repository at this point in the history
  • Loading branch information
metagn committed Sep 26, 2024
1 parent 3b85c1a commit 44300ee
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
2 changes: 2 additions & 0 deletions compiler/semtypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1404,6 +1404,8 @@ proc semProcTypeNode(c: PContext, n, genericParams: PNode,
elif typ.kind == tyStatic:
def = semConstExpr(c, def)
def = fitNode(c, typ, def, def.info)
# keep proc AST updated
a[^1] = def

if not hasType and not hasDefault:
if isType: localError(c.config, a.info, "':' expected")
Expand Down
4 changes: 2 additions & 2 deletions tests/errmsgs/tunknown_named_parameter.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ func rsplit(s: string; sep: string; maxsplit: int = -1): seq[string]
first type mismatch at position: 2
required type for sep: string
but expression '{':'}' is of type: set[char]
func rsplit(s: string; seps: set[char] = Whitespace; maxsplit: int = -1): seq[
string]
func rsplit(s: string; seps: set[char] = {' ', '\t', '\v', '\r', '\n', '\f'};
maxsplit: int = -1): seq[string]
first type mismatch at position: 3
unknown named parameter: maxsplits
Expand Down
50 changes: 50 additions & 0 deletions tests/proc/tdefaultvalueprocast.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
discard """
nimout: '''
ProcDef
Sym "foo"
Empty
Empty
FormalParams
Empty
IdentDefs
Sym "x"
Empty
Call
Sym "none"
Sym "Natural"
Empty
Empty
DiscardStmt
Empty
ProcDef
Sym "example"
Empty
Empty
FormalParams
Empty
IdentDefs
Sym "a"
Empty
Sym "thing"
Empty
Empty
DiscardStmt
TupleConstr
Sym "a"
Sym "thing"
'''
"""

import options, macros

macro typedTree(n: typed): untyped =
result = n
echo treeRepr n

# issue #19118
proc foo(x = none(Natural)) {.typedTree.} = discard

# issue #12942
var thing = 2
proc example(a = thing) {.typedTree.} =
discard (a, thing)

0 comments on commit 44300ee

Please sign in to comment.