summary refs log tree commit diff stats
diff options
context:
space:
mode:
authormetagn <metagngn@gmail.com>2024-09-27 10:33:40 +0300
committerGitHub <noreply@github.com>2024-09-27 09:33:40 +0200
commitc21bf7f41babcdb049c54d4e148883606764947e (patch)
tree23dc16a4ffcf327d9c0e570dcbe31d812cadd4cd
parent62a5bb4d0acfea39797b8b5207786c1d61e12b1b (diff)
downloadNim-c21bf7f41babcdb049c54d4e148883606764947e.tar.gz
make default values typed in proc AST same as param sym AST (#24184)
fixes #12942, fixes #19118

This is the exact same as #20735 but maybe the situation has improved
after #24065.
-rw-r--r--compiler/semtypes.nim2
-rw-r--r--tests/errmsgs/tunknown_named_parameter.nim4
-rw-r--r--tests/proc/tdefaultvalueprocast.nim50
3 files changed, 54 insertions, 2 deletions
diff --git a/compiler/semtypes.nim b/compiler/semtypes.nim
index 450b81dbf..6fe921e81 100644
--- a/compiler/semtypes.nim
+++ b/compiler/semtypes.nim
@@ -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")
diff --git a/tests/errmsgs/tunknown_named_parameter.nim b/tests/errmsgs/tunknown_named_parameter.nim
index d3dd6cd2d..9ec8bc9cd 100644
--- a/tests/errmsgs/tunknown_named_parameter.nim
+++ b/tests/errmsgs/tunknown_named_parameter.nim
@@ -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
 
diff --git a/tests/proc/tdefaultvalueprocast.nim b/tests/proc/tdefaultvalueprocast.nim
new file mode 100644
index 000000000..cc5c4df25
--- /dev/null
+++ b/tests/proc/tdefaultvalueprocast.nim
@@ -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)