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

opcLdDeref (eg via addr) gives wrong results in inside static block #13887

Open
timotheecour opened this issue Apr 6, 2020 · 2 comments · May be fixed by timotheecour/Nim#795
Open

opcLdDeref (eg via addr) gives wrong results in inside static block #13887

timotheecour opened this issue Apr 6, 2020 · 2 comments · May be fixed by timotheecour/Nim#795
Labels
const `const x=expr` or `static: stmt`

Comments

@timotheecour
Copy link
Member

timotheecour commented Apr 6, 2020

opcLdDeref gives wrong results in VM inside static block

Example 1

when true: # D20200405T172240
  # proc fun() = # would work
  template fun() = # fails
    var s = @[10,11,12]
    let z = s[0].addr
    doAssert z[] == 10
    z[] = 100
    doAssert z[] == 100
    doAssert s[0] == 100 # fails here
  static: fun() # fails
  fun() # would work

Current Output

doAssert s[0] == 100 fails

Expected Output

works

Example 2

shows that pointer is conflated for value:

  template fun() =
    var s = @[10,11,12]
    echo cast[int](s[0].addr) # 4323682360
    let z = s[0].addr 
    echo cast[int](z) # 10 => BUG
  static: fun() # fails

Example 3

when true: # D20200405T172849:here
  template fun() =
  # proc fun() = # would work
    var s = @[10.1,11.2,12.3]
    echo cast[int](s[0].addr)
    let z = s[0].addr
    echo cast[int](z)
  static: fun() # fails

Error: unhandled exception: 'intVal' is not accessible using discriminant 'kind' of type 'TNode' [FieldError]

Possible Solution

Additional Information

@timotheecour timotheecour changed the title opcLdDeref (eg via addr) gives wrong results in VM inside static block opcLdDeref (eg via addr) gives wrong results in inside static block Apr 6, 2020
@recloser
Copy link
Contributor

recloser commented Jul 9, 2020

No need to get fancy with seqs, even the simplest code fails:

static:
  var x = 5
  var y = addr(x)
  y[] += 10
  doAssert x == y[]

@ghost ghost added the VM see also `const` label label Jul 26, 2020
@timotheecour timotheecour added const `const x=expr` or `static: stmt` and removed VM see also `const` label labels Nov 13, 2020
@timotheecour
Copy link
Member Author

as many other issues, this would be fixed by nim-lang/RFCs#276

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
const `const x=expr` or `static: stmt`
Projects
None yet
2 participants