Skip to content

Commit

Permalink
also add tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
metagn committed Dec 3, 2024
1 parent f1048be commit 3f6ca89
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
27 changes: 19 additions & 8 deletions compiler/semexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2956,14 +2956,20 @@ proc semTuplePositionsConstr(c: PContext, n: PNode, flags: TExprFlags; expectedT
var typ = newTypeS(tyTuple, c) # leave typ.n nil!
for i in 0..<n.len:
let expectedElemType = if expected != nil: expected[i] else: nil
n[i] = semExprWithType(c, n[i], {}, expectedElemType)
if expectedElemType != nil and
(expectedElemType.kind != tyNil and not hasEmpty(expectedElemType)):
# hasEmpty/nil check is to not break existing code like
# `const foo = [(1, {}), (2, {false})]`,
# `const foo = if true: (0, nil) else: (1, new(int))`
n[i] = fitNode(c, expectedElemType, n[i], n[i].info)
addSonSkipIntLit(typ, n[i].typ.skipTypes({tySink}), c.idgen)
if typ.kind == tyFromExpr:
n[i] = semGenericStmt(c, n[i])
else:
n[i] = semExprWithType(c, n[i], {}, expectedElemType)
if n[i].typ != nil and n[i].typ.kind == tyFromExpr and c.inGenericContext > 0:
typ = makeTypeFromExpr(c, n.copyTree)
else:
if expectedElemType != nil and
(expectedElemType.kind != tyNil and not hasEmpty(expectedElemType)):
# hasEmpty/nil check is to not break existing code like
# `const foo = [(1, {}), (2, {false})]`,
# `const foo = if true: (0, nil) else: (1, new(int))`
n[i] = fitNode(c, expectedElemType, n[i], n[i].info)
addSonSkipIntLit(typ, n[i].typ.skipTypes({tySink}), c.idgen)
result.typ() = typ

include semobjconstr
Expand Down Expand Up @@ -3054,6 +3060,11 @@ proc semExport(c: PContext, n: PNode): PNode =

proc semTupleConstr(c: PContext, n: PNode, flags: TExprFlags; expectedType: PType = nil): PNode =
var tupexp = semTuplePositionsConstr(c, n, flags, expectedType)
# convert `tupexp` to typedesc if necessary:
if tupexp.typ.kind == tyFromExpr:
# tyFromExpr is already ambivalent between types and values
result = tupexp
return
var isTupleType: bool = false
if tupexp.len > 0: # don't interpret () as type
isTupleType = tupexp[0].typ.kind == tyTypeDesc
Expand Down
10 changes: 10 additions & 0 deletions tests/proc/tgenericdefaultparam.nim
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,13 @@ block: # issue #24484, array version
doAssert t == [T(0), 5]
bar[uint8]()

block: # issue #24484, tuple version
type E = enum A
proc foo[T](t = (T.A,)) =
discard
foo[E]()

proc bar[T](t: (T, int) = (T(0), 5)) =
doAssert t == (T(0), 5)
bar[uint8]()

0 comments on commit 3f6ca89

Please sign in to comment.