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 cd3cf3a20e)
This commit is contained in:
ringabout
2024-04-24 18:47:05 +08:00
committed by narimiran
parent efcb6b4b11
commit e3f4c3d417
3 changed files with 39 additions and 20 deletions

View File

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

View File

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

View File

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