Skip to content

VM: nil procvar treated as not nil in VM, causing FieldDefect and further errors #15595

@timotheecour

Description

@timotheecour

VM: FieldDefect error when procvar called in a non-taken branch

Example

when true:
  type Opt = object
    cb: proc()

  proc bar(opt: Opt) =
    if opt.cb != nil: opt.cb()

  static:
    var opt=Opt()
    bar(opt) # BUG

Current Output

/Users/timothee/git_clone/nim/Nim_devel/lib/system/fatal.nim(49) sysFatal
Error: unhandled exception: 'sym' is not accessible using discriminant 'kind' of type 'TNode' [FieldDefect]

Expected Output

works

Additional Information

nim devel c2ba4ef 1.3.7
D20201015T230938

it's blocking a PR

note: no bug if the procvar is not inside some object:

when true:
  type Opt = object
    cb: proc()
  proc bar2(cb: proc()) =
    if cb != nil: cb()
  proc bar(opt: Opt) =
    if opt.cb != nil: opt.cb()

  proc main=
    # bar2(nil) # ok
    var opt=Opt()
    bar(opt) # BUG
  static: main()
  main() # ok

any workaround?

EDIT

the bug seems to be that VM thinks opt.cb!=nil, as seen by insrumenting above code with echo opt.cb != nil, eg:

when true:
  type Opt = object
    cb: proc()
  proc bar2(opt: Opt) =
    echo opt.cb == nil
  proc main=
    var opt = Opt()
    bar2(opt)
  static: main() # false
  main() # true

EDIT: workaround

looks like isNil works here whereas != nil doesn't work

Metadata

Metadata

Assignees

No one assigned

    Labels

    VMsee also `const` label

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions