Skip to content

Commit

Permalink
fixes #23422; card regression (#23437)
Browse files Browse the repository at this point in the history
fixes #23422

ref #20997
#21165

The function `cardSet` is used for large sets that are stored in the
form of arrays. It shouldn't be passed as a pointer
  • Loading branch information
ringabout committed Mar 28, 2024
1 parent a24990b commit 4b6a9e4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion compiler/ccgexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2088,7 +2088,7 @@ proc genSetOp(p: BProc, e: PNode, d: var TLoc, op: TMagic) =
of mExcl: binaryStmtInExcl(p, e, d, "$1[(NU)($2)>>3] &= ~(1U<<($2&7U));$n")
of mCard:
var a: TLoc = initLocExpr(p, e[1])
putIntoDest(p, d, e, ropecg(p.module, "#cardSet($1, $2)", [addrLoc(p.config, a), size]))
putIntoDest(p, d, e, ropecg(p.module, "#cardSet($1, $2)", [rdCharLoc(a), size]))
of mLtSet, mLeSet:
i = getTemp(p, getSysType(p.module.g.graph, unknownLineInfo, tyInt)) # our counter
a = initLocExpr(p, e[1])
Expand Down
38 changes: 38 additions & 0 deletions tests/sets/tsets.nim
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
discard """
target: "c cpp"
"""

# Test builtin sets

# xxx these tests are not very good, this should be revisited.
Expand Down Expand Up @@ -93,3 +97,37 @@ block:

doAssert k99 notin s1
doAssert k99 notin s2

block: # bug #23422
block:
var a: set[uint8] = {1'u8}

proc printLen(x: set[uint8]): int =
doAssert x.len == card(x)
result = card(x)

proc printLenVar(x: var set[uint8]): int =
doAssert x.len == card(x)
result = card(x)

doAssert a.len == 1
doAssert printLen(a) == 1
doAssert printLenVar(a) == card(a)

block:
type Fruit = enum
Apple, Banana, Melon

var a: set[Fruit] = {Apple}

proc printLen(x: set[Fruit]): int =
doAssert x.len == card(x)
result = card(x)

proc printLenVar(x: var set[Fruit]): int =
doAssert x.len == card(x)
result = card(x)

doAssert a.len == 1
doAssert printLen(a) == 1
doAssert printLenVar(a) == card(a)

0 comments on commit 4b6a9e4

Please sign in to comment.