Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iterator declaration zeros variable on ARC/ORC #19795

Closed
levovix0 opened this issue May 16, 2022 · 5 comments · Fixed by #21379
Closed

iterator declaration zeros variable on ARC/ORC #19795

levovix0 opened this issue May 16, 2022 · 5 comments · Fixed by #21379

Comments

@levovix0
Copy link

Example

type Node = ref object
  kind: Node

var n1 = Node()
var n2 = Node()

n2.kind = n1

iterator iter(x: Node): Node =
  discard

echo cast[int](n1)

Current Output

0

Expected Output

some non-zero address

Additional Information

without --gc:orc or --gc:arc works fine
also if add echo cast[int](n1) before iterator declaration, works fine

$ nim -v
Nim Compiler Version 1.7.1 [Linux: amd64]
Compiled at 2022-05-14
Copyright (c) 2006-2022 by Andreas Rumpf

git hash: 19001c070bbe7645ff45fcbd66ab221235715302
active boot switches: -d:release

on 1.6.6 this bug also exists

@ringabout
Copy link
Member

Or

type Node = ref object
  kind: Node

var n1 = Node()
var n2 = Node()
n2.kind = n1

proc foo =
  echo cast[int](n1)

foo()

@ringabout
Copy link
Member

It looks like dfa analysis cannot pass the scope boundry. Nim compiler thinks n2.kind = n1 is lastRead for n1 and sinks it. I'm still looking at dfa and injectdestructors.

@ringabout
Copy link
Member

Yeah, the program splits into three parts.

var
  n1
  n2
type
  Node = ref object
    kind: Node

n1 = Node()
n2 = Node()
`=sink`(n2.kind, n1)
wasMoved(n1)
iterator iter(x: Node): Node =
  discard
var :tmpD_452984865
try:
  echo [
    :tmpD_452984865 = cast[int](n1)
    :tmpD_452984865]
finally:
  `=destroy`(:tmpD_452984865)

@ringabout
Copy link
Member

ringabout commented Jun 30, 2022

It seems to caused by how the parser parses the top level statement: read everything until the next proc declaration etc. The top level statements splits into different parts. Dfa can only analyse them seperately. So dfa can only get partial infos regarding symbols.

@ringabout
Copy link
Member

ringabout commented Jul 29, 2022

It breaks zero-functional tests.

import zero_functional, options, lists


type
  SimpleIter = ref object
    items: seq[int]

proc initSimpleIter(): SimpleIter =
  SimpleIter(items: @[1, 2, 3])

proc len(si: SimpleIter): int =
  si.items.len()

iterator items(si: SimpleIter): int =
  for i in 0..<len(si.items):
    yield(si.items[i])

let si = initSimpleIter()

var d = si --> to(list)
d --> foreach(it = it * 2)
echo d --> to(seq)
let e: DoublyLinkedList[int] = d
discard(e)
echo d --> to(seq)
doAssert(d --> to(seq) == @[2, 4, 6])

--> EXPANDS TO

proc (): auto =
  {.push, hint[XDeclaredButNotUsed]: false.}
  proc `__iterType__`(): auto =
    var __itList__ = d.head
    var idx = 0
    while (
      not (__itList__ == nil)):
      let __it0 = __itList__.value
      let _itListNext = __itList__.next
      let __it1 = __it0
      result = (__it0, __it1)

  {.pop.}
  var res`gensym87: seq[type(__iterType__()[1])]
  result = zfInit(res`gensym87)
  var __itList__ = d.head
  var idx = 0
  while (
    not (__itList__ == nil)):
    let __it0 = __itList__.value
    let _itListNext = __itList__.next
    let __it1 = __it0
    zfAddItem(result, idx, __it1)
    __itList__ = _itListNext
    idx += 1()

@ringabout ringabout mentioned this issue Jul 29, 2022
6 tasks
bung87 added a commit to bung87/Nim that referenced this issue Nov 8, 2022
ringabout added a commit that referenced this issue Jan 3, 2023
Araq pushed a commit that referenced this issue Feb 22, 2023
…m now parses the whole module at one time (#21379)

* fixes #19795; remove parse pipeline

* isScript

* fixes nimscriptapi

* don't touch reorder

* check script

* fixes tests

* it seems implicit imports of system cause troubles

* access the first child of `nkStmtList`

* ignore comments

* minor messages

* perhaps increases hloLoopDetector

* the module is a stmtList, which changes the errors

* fixes nimdoc

* fixes tlinter

* fixes nim  secret tests

* fixes arc_misc

* fixes nim secret tests again

* safe; fixes one more test

* GlobalError is the root cause too

* fixes parsing errors

* put emit types to the cfsForwardTypes section

* fixes #11852; `{.push checks:off}` now works in procs

* disable navigator

* fixes nimdoc

* add tests for JS

* fixes nimsuggest
survivorm pushed a commit to survivorm/Nim that referenced this issue Feb 28, 2023
…ove parsing pipeline, Nim now parses the whole module at one time (nim-lang#21379)

* fixes nim-lang#19795; remove parse pipeline

* isScript

* fixes nimscriptapi

* don't touch reorder

* check script

* fixes tests

* it seems implicit imports of system cause troubles

* access the first child of `nkStmtList`

* ignore comments

* minor messages

* perhaps increases hloLoopDetector

* the module is a stmtList, which changes the errors

* fixes nimdoc

* fixes tlinter

* fixes nim  secret tests

* fixes arc_misc

* fixes nim secret tests again

* safe; fixes one more test

* GlobalError is the root cause too

* fixes parsing errors

* put emit types to the cfsForwardTypes section

* fixes nim-lang#11852; `{.push checks:off}` now works in procs

* disable navigator

* fixes nimdoc

* add tests for JS

* fixes nimsuggest
capocasa pushed a commit to capocasa/Nim that referenced this issue Mar 31, 2023
…ove parsing pipeline, Nim now parses the whole module at one time (nim-lang#21379)

* fixes nim-lang#19795; remove parse pipeline

* isScript

* fixes nimscriptapi

* don't touch reorder

* check script

* fixes tests

* it seems implicit imports of system cause troubles

* access the first child of `nkStmtList`

* ignore comments

* minor messages

* perhaps increases hloLoopDetector

* the module is a stmtList, which changes the errors

* fixes nimdoc

* fixes tlinter

* fixes nim  secret tests

* fixes arc_misc

* fixes nim secret tests again

* safe; fixes one more test

* GlobalError is the root cause too

* fixes parsing errors

* put emit types to the cfsForwardTypes section

* fixes nim-lang#11852; `{.push checks:off}` now works in procs

* disable navigator

* fixes nimdoc

* add tests for JS

* fixes nimsuggest
bung87 pushed a commit to bung87/Nim that referenced this issue Jul 29, 2023
…ove parsing pipeline, Nim now parses the whole module at one time (nim-lang#21379)

* fixes nim-lang#19795; remove parse pipeline

* isScript

* fixes nimscriptapi

* don't touch reorder

* check script

* fixes tests

* it seems implicit imports of system cause troubles

* access the first child of `nkStmtList`

* ignore comments

* minor messages

* perhaps increases hloLoopDetector

* the module is a stmtList, which changes the errors

* fixes nimdoc

* fixes tlinter

* fixes nim  secret tests

* fixes arc_misc

* fixes nim secret tests again

* safe; fixes one more test

* GlobalError is the root cause too

* fixes parsing errors

* put emit types to the cfsForwardTypes section

* fixes nim-lang#11852; `{.push checks:off}` now works in procs

* disable navigator

* fixes nimdoc

* add tests for JS

* fixes nimsuggest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment