From 876f6d69a81eb5cff04af2f8be473c567622d82a Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Fri, 2 Jun 2023 01:02:56 +0800 Subject: [PATCH] fixes #21974; fixes sameConstant fieldDefect (#21981) * fixes #21974; fixes sameConstant fieldDefect * add a test case (cherry picked from commit 8e35b3d577cb0a6b216b16668ff5f34a86cfbbab) --- compiler/injectdestructors.nim | 2 +- tests/arc/tarc_orc.nim | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/compiler/injectdestructors.nim b/compiler/injectdestructors.nim index 3160e8abfe..50f4607daf 100644 --- a/compiler/injectdestructors.nim +++ b/compiler/injectdestructors.nim @@ -997,7 +997,7 @@ proc p(n: PNode; c: var Con; s: var Scope; mode: ProcessMode): PNode = proc sameLocation*(a, b: PNode): bool = proc sameConstant(a, b: PNode): bool = - a.kind in nkLiterals and a.intVal == b.intVal + a.kind in nkLiterals and b.kind in nkLiterals and a.intVal == b.intVal const nkEndPoint = {nkSym, nkDotExpr, nkCheckedFieldExpr, nkBracketExpr} if a.kind in nkEndPoint and b.kind in nkEndPoint: diff --git a/tests/arc/tarc_orc.nim b/tests/arc/tarc_orc.nim index 981567350e..5a283c03ba 100644 --- a/tests/arc/tarc_orc.nim +++ b/tests/arc/tarc_orc.nim @@ -45,3 +45,33 @@ proc main() = # todo bug with templates doAssert b() == @[] static: main() main() + +block: # bug #21974 + type Test[T] = ref object + values : seq[T] + counter: int + + proc newTest[T](): Test[T] = + result = new(Test[T]) + result.values = newSeq[T](16) + result.counter = 0 + + proc push[T](self: Test[T], value: T) = + self.counter += 1 + if self.counter >= self.values.len: + self.values.setLen(self.values.len * 2) + self.values[self.counter - 1] = value + + proc pop[T](self: Test[T]): T = + result = self.values[0] + self.values[0] = self.values[self.counter - 1] # <--- This line + self.counter -= 1 + + + type X = tuple + priority: int + value : string + + var a = newTest[X]() + a.push((1, "One")) + doAssert a.pop.value == "One"