Skip to content

Commit

Permalink
fix typed void values
Browse files Browse the repository at this point in the history
  • Loading branch information
metagn committed Oct 3, 2024
1 parent d6f93ca commit 5e42966
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
8 changes: 7 additions & 1 deletion compiler/semtypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1372,6 +1372,7 @@ proc semProcTypeNode(c: PContext, n, genericParams: PNode,
"either use ';' (semicolon) or explicitly write each default value")
message(c.config, a.info, warnImplicitDefaultValue, msg)
block determineType:
var canBeVoid = false
if kind == skTemplate:
if typ != nil and typ.kind == tyUntyped:
# don't do any typechecking or assign a type for
Expand All @@ -1382,9 +1383,14 @@ proc semProcTypeNode(c: PContext, n, genericParams: PNode,
# don't do any typechecking
def.typ = makeTypeFromExpr(c, def.copyTree)
break determineType
elif typ != nil and typ.kind == tyTyped:
canBeVoid = true
let isGeneric = isCurrentlyGeneric()
inc c.inGenericContext, ord(isGeneric)
def = semExprWithType(c, def, {efDetermineType, efAllowSymChoice}, typ)
if canBeVoid:
def = semExpr(c, def, {efDetermineType, efAllowSymChoice}, typ)
else:
def = semExprWithType(c, def, {efDetermineType, efAllowSymChoice}, typ)
dec c.inGenericContext, ord(isGeneric)
if def.referencesAnotherParam(getCurrOwner(c)):
def.flags.incl nfDefaultRefsParam
Expand Down
22 changes: 22 additions & 0 deletions tests/template/tdefaultparam.nim
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,25 @@ block: # untyped params with default value
test2:
s.add "d"
doAssert s == "abcde"
template test3(body: untyped = willNotCompile) =
discard
test3()

block: # typed params with `void` default value
macro foo(x: typed): untyped =
result = x
template test(body: untyped, alt: typed = (;), maxTries = 3): untyped {.foo.} =
body
alt
var s = "a"
test:
s.add "b"
do:
s.add "c"
doAssert s == "abc"
template test2(body: untyped, alt: typed = s.add("e"), maxTries = 3): untyped =
body
alt
test2:
s.add "d"
doAssert s == "abcde"

0 comments on commit 5e42966

Please sign in to comment.