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

Referring to a static nil objects's field is impossible, even when it should #12559

Open
dawkot opened this issue Oct 30, 2019 · 5 comments
Open

Comments

@dawkot
Copy link

dawkot commented Oct 30, 2019

Example

type T = ref object
  x: int

proc f(arg: static T) = discard arg.x

f nil

Current Output

/usercode/in.nim(9, 3) template/generic instantiation of `f` from here
/usercode/in.nim(7, 36) Error: undeclared field: 'x' 

Expected Output

The standard error message for when trying to access a nil ref

Additional Information

$ nim -v
Nim Compiler Version 1.0.99 [Windows: amd64]
Compiled at 2019-10-10
@dawkot
Copy link
Author

dawkot commented Oct 30, 2019

It's worse than that:

Example

import sequtils

type T = ref object
  x: int

proc foo(arg: static T) =
  static:
    assert arg == nil or arg.x > 0
 
foo nil

Current Output

/usercode/in.nim(10, 5) template/generic instantiation of foo from here
/usercode/in.nim(8, 29) Error: undeclared field: 'x'

@dawkot dawkot changed the title Confusing error when accessing a static ref object that is nil Referring to a static nil objects's field is impossible, even when it should Oct 30, 2019
@kraptor
Copy link
Contributor

kraptor commented Oct 30, 2019

Isn't static T referring to the type of T, which is not an instance and, thus, it has not x defined? (which is correct, in my opinion).

Also, if instead of a proc, you define a macrom then it is catched correctly:

type 
  T = ref object
    x: int

macro f(arg: static[T]) = 
  discard arg.x

f nil

Then you get:

/usercode/in.nim(6, 14)  f
/usercode/in.nim(8, 3) template/generic instantiation of `f` from here
/usercode/in.nim(6, 14) Error: attempt to access a nil address

Afaik both results (undefined for proc and nil address for macro) are correct, in my humble opninion.

@dawkot
Copy link
Author

dawkot commented Oct 31, 2019

Well, if it's a wont-fix, is there any other way to do compile-time checking on arguments like this?

@Araq
Copy link
Member

Araq commented Nov 4, 2019

It's not a wont-fix, it looks like a real bug to me.

@bung87
Copy link
Collaborator

bung87 commented Nov 9, 2022

should do f cast[T](nil) to make it work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants