From e3f4c3d417bd7b59388c4cd867c29bc895cc30fa Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Wed, 24 Apr 2024 18:47:05 +0800 Subject: [PATCH] fixes #23524; global variables cannot be analysed when injecting `move` (#23529) fixes #23524 ```nim proc isAnalysableFieldAccess*(orig: PNode; owner: PSym): bool = ... result = n.kind == nkSym and n.sym.owner == owner and {sfGlobal, sfThread, sfCursor} * n.sym.flags == {} and (n.sym.kind != skParam or isSinkParam(n.sym)) ``` In `isAnalysableFieldAccess`, globals, cursors are already rejected (cherry picked from commit cd3cf3a20e7bf6a115cd836ccc0c55ab07bcf3e9) --- lib/system.nim | 8 ++++--- tests/arc/tarcmisc.nim | 16 +++++++++++++ tests/destructor/tmove_objconstr.nim | 35 ++++++++++++++-------------- 3 files changed, 39 insertions(+), 20 deletions(-) diff --git a/lib/system.nim b/lib/system.nim index b151fecd53..6c49ee3f5a 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -161,9 +161,11 @@ when defined(nimHasEnsureMove): ## Ensures that `x` is moved to the new location, otherwise it gives ## an error at the compile time. runnableExamples: - var x = "Hello" - let y = ensureMove(x) - doAssert y == "Hello" + proc foo = + var x = "Hello" + let y = ensureMove(x) + doAssert y == "Hello" + foo() discard "implemented in injectdestructors" type diff --git a/tests/arc/tarcmisc.nim b/tests/arc/tarcmisc.nim index 7320b32b3f..f6adf223b3 100644 --- a/tests/arc/tarcmisc.nim +++ b/tests/arc/tarcmisc.nim @@ -700,3 +700,19 @@ block: # bug #23505 C(value: addr tmp[]) discard init(C) + +block: # bug #23524 + type MyType = object + a: int + + proc `=destroy`(typ: MyType) = discard + + var t1 = MyType(a: 100) + var t2 = t1 # Should be a copy? + + proc main() = + t2 = t1 + doAssert t1.a == 100 + doAssert t2.a == 100 + + main() diff --git a/tests/destructor/tmove_objconstr.nim b/tests/destructor/tmove_objconstr.nim index a583a1704b..cdc1eb1c0d 100644 --- a/tests/destructor/tmove_objconstr.nim +++ b/tests/destructor/tmove_objconstr.nim @@ -137,28 +137,29 @@ doAssert seq3[0] == 1.0 var seq4, seq5: MySeqNonCopyable (seq4, i, seq5) = myfunc2(2, 3) -seq4 = block: - var tmp = newMySeq(4, 1.0) - tmp[0] = 3.0 - tmp +proc foo = + seq4 = block: + var tmp = newMySeq(4, 1.0) + tmp[0] = 3.0 + tmp -doAssert seq4[0] == 3.0 + doAssert seq4[0] == 3.0 -import macros -seq4 = - if i > 0: newMySeq(2, 5.0) - elif i < -100: raise newException(ValueError, "Parse Error") - else: newMySeq(2, 3.0) + seq4 = + if i > 0: newMySeq(2, 5.0) + elif i < -100: raise newException(ValueError, "Parse Error") + else: newMySeq(2, 3.0) -seq4 = - case (char) i: - of 'A', {'W'..'Z'}: newMySeq(2, 5.0) - of 'B': quit(-1) - else: - let (x1, x2, x3) = myfunc2(2, 3) - x3 + seq4 = + case (char) i: + of 'A', {'W'..'Z'}: newMySeq(2, 5.0) + of 'B': quit(-1) + else: + let (x1, x2, x3) = myfunc2(2, 3) + x3 +foo() #------------------------------------------------------------ #-- Move into array constructor