fixes #23422; card regression (#23437)

fixes #23422

ref https://github.com/nim-lang/Nim/issues/20997
https://github.com/nim-lang/Nim/pull/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
This commit is contained in:
ringabout
2024-03-28 18:04:47 +08:00
committed by GitHub
parent a24990bd8c
commit 4b6a9e4add
2 changed files with 39 additions and 1 deletions

View File

@@ -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])

View File

@@ -1,3 +1,7 @@
discard """
target: "c cpp"
"""
# Test builtin sets
# xxx these tests are not very good, this should be revisited.
@@ -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)