nim-works / nimskull

An in development statically typed systems programming language; with sustainability at its core. We, the community of users, maintain it.
https://nim-works.github.io/nimskull/index.html
Other
280 stars 39 forks source link

fix wrong cursor inference in the context of loops #1388

Closed zerbina closed 4 months ago

zerbina commented 4 months ago

Summary

Fixes https://github.com/nim-works/nimskull/issues/1385

Details

For var/let bindings defined in loops, the aliveEnd is now only updated during the first analysis of the loop. This ensures that variables defined outside the loop, but only assigned to within the loop, always have longer alive times than variables defined within loops, thus preventing outer variables borrowing from inner variables (cursorfication cannot happen when a variable outlives its assignment source).

This fix also renders the isConditionallyReassigned heuristic obsolete, which was used to fix a more specific case of the same bug. The heuristic disabled cursorfication for all variables of which re-assignments are enclosed by a loop and if/else/elif/of, which also applied to, e.g.:

while cond:
  if cond:
    var a = newString(...)
    var b = newString(...)
    var x: string
    x = a
    x = b # this assignment disabled `x` being turned into a cursor
    discard a

With the heuristic removed, in the above example, x is now turned into a cursor, matching what would happen when there is no enclosing loop or if.

saem commented 4 months ago

/merge

github-actions[bot] commented 4 months ago

Merge requested by: @saem

Contents after the first section break of the PR description has been removed and preserved below: