From e8556b45f58b3b231c3720189bad9a4eeb6ca51f Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Wed, 24 Aug 2022 00:38:12 +0800 Subject: [PATCH] fixes #19967; reset does not work on set [backport: 1.2] (#19968) * fixes #19967 * use case * add testcase * fix typos * explictly specify other branches Co-authored-by: xflywind <43030857+xflywind@users.noreply.github.com> --- compiler/ccgreset.nim | 15 ++++++++++++++- tests/stdlib/tsystem.nim | 27 +++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/compiler/ccgreset.nim b/compiler/ccgreset.nim index fc7370d81..93b1652f9 100644 --- a/compiler/ccgreset.nim +++ b/compiler/ccgreset.nim @@ -87,7 +87,20 @@ proc specializeResetT(p: BProc, accessor: Rope, typ: PType) = lineCg(p, cpsStmts, "$1 = 0;$n", [accessor]) of tyCstring, tyPointer, tyPtr, tyVar, tyLent: lineCg(p, cpsStmts, "$1 = NIM_NIL;$n", [accessor]) - else: + of tySet: + case mapSetType(p.config, typ) + of ctArray: + lineCg(p, cpsStmts, "#nimZeroMem($1, sizeof($2));$n", + [accessor, getTypeDesc(p.module, typ)]) + of ctInt8, ctInt16, ctInt32, ctInt64: + lineCg(p, cpsStmts, "$1 = 0;$n", [accessor]) + else: + doAssert false, "unexpected set type kind" + of {tyNone, tyEmpty, tyNil, tyUntyped, tyTyped, tyGenericInvocation, + tyGenericParam, tyOrdinal, tyRange, tyOpenArray, tyForward, tyVarargs, + tyUncheckedArray, tyProxy, tyBuiltInTypeClass, tyUserTypeClass, + tyUserTypeClassInst, tyCompositeTypeClass, tyAnd, tyOr, tyNot, + tyAnything, tyStatic, tyFromExpr, tyConcept, tyVoid, tyIterable}: discard proc specializeReset(p: BProc, a: TLoc) = diff --git a/tests/stdlib/tsystem.nim b/tests/stdlib/tsystem.nim index 00be16275..a90a1d1d9 100644 --- a/tests/stdlib/tsystem.nim +++ b/tests/stdlib/tsystem.nim @@ -74,3 +74,30 @@ template main = static: main() main() + +# bug #19967 +block: + type + X = object + a: string + b: set[char] + + var y = X(b: {'a'}) + + reset(y) + + doAssert y.b == {} + +block: + type + Color = enum + Red, Blue, Yellow + X = object + a: string + b: set[Color] + + var y = X(b: {Red, Blue}) + + reset(y) + doAssert y.b == {} +