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

bugs with var result #13975

Open
timotheecour opened this issue Apr 13, 2020 · 2 comments
Open

bugs with var result #13975

timotheecour opened this issue Apr 13, 2020 · 2 comments
Labels
Code Generation Invalid Code Acceptance Everything related to compiler not complaining about invalid code VM see also `const` label

Comments

@timotheecour
Copy link
Member

timotheecour commented Apr 13, 2020

while working on this fix #13959 I identified 4 other issues with var result

Example 1

  • iterator returning tuple of var in VM gives CT error
when true: # D20200413T140233
  template fun2() =
    block:
      var m = 1
      var m2 = 1
      iterator test3(o: var int): (var int, var int) =
        yield (o, m2)
      for ti in test3(m):
        ti[0]+=3
        ti[1]+=4
      doAssert (m, m2) == (4, 5)
  fun2() # ok
  static: fun2() # BUG: Error: attempt to access a nil address kind: rkInt

Current Output

Error: attempt to access a nil address kind: rkInt

Expected Output

works

Example 2

  • proc returning tuple of var gives SIGSEGV or cgen static error, but should work, just like it does for iterator or for non-tuple
when true: # D20200413T140527
  # nim c: SIGSEGV
  # nim cpp: error: call to implicitly-deleted default constructor of 'tyTuple__oq0LVLj6AbX8hE12f64tQQ'
  template fun2() =
    block:
      var m = 1
      var m2 = 1
      proc test3(o: var int): (var int, var int) =
        (o, m2)
      test3(m)[0] += 3
      echo (m, m2)
  fun2()

Example 3

  • proc returning tuple of var gives CT error in VM
when true: # D20200413T140826
  # Error: attempt to access a nil address kind: rkInt
  template fun2() =
    block:
      var m = 1
      var m2 = 1
      proc test3(o: var int): (var int, var int) =
        (o, m2)
      test3(m)[0] += 3 # BUG here
  static: fun2()

Example 4: accept invalid (un-initialized var result)

  • proc returning var result that isn't initialized should give a semcheck CT error at least for simple cases
when true: # D20200413T141159
  # nim c: SIGSEGV
  # nim cpp: error: declaration of reference variable 'result' requires an initializer
  proc identity(): var int = discard
  let a = identity()

EDIT: at least gives a Warning: Cannot prove that 'result' is initialized. This will become a compile time error in the future. [ProveInit]

Example 5

when true: # D20200711T174301 gives SIGSEGV, should give CT error (1 is not an lvalue)
  proc main(): (var int, int) =
    result[0] = 1
  main()[0].inc

Additional Information

@krux02
Copy link
Contributor

krux02 commented Apr 16, 2020

I don't think Example 4 would be least important. It should be a proper compilation error.

@timotheecour
Copy link
Member Author

timotheecour commented Apr 17, 2020

maybe it's hard to do so in the general case (hence ProveInit being a warning only?) but I'm not sure which cases cause the compiler not to be sure; but yes, when compiler knows for sure that it's not initialized, it should give CT error instead of ProveInit warning

EDIT: maybe something like this could be hard to prove for compiler:

    proc fun(a: var int): var int =
      {.emit: "`result` = `a`;".}

@Araq Araq added the VM see also `const` label label Apr 17, 2020
@timotheecour timotheecour added Invalid Code Acceptance Everything related to compiler not complaining about invalid code Code Generation labels Jul 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Code Generation Invalid Code Acceptance Everything related to compiler not complaining about invalid code VM see also `const` label
Projects
None yet
Development

No branches or pull requests

3 participants