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 · 6 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.

@metagn
Copy link
Collaborator

metagn commented Aug 29, 2024

This is an instantiation bug, the type of static T is inferred as typeof(nil) with value nil, related to #21331, #12552, #4858

Nope, just static not allowing conversions

Araq pushed a commit that referenced this issue Oct 3, 2024
fixes #23010, split from #24195

When resemming bracket nodes, the compiler currently unconditionally
makes a new node with an array type based on the node. However the VM
can generate bracket nodes with `seq` types, which this erases. To fix
this, if a bracket node already has a type, we still resem the bracket
node, but don't construct a new type for it, instead using the type of
the original node.

A version of this was rejected that didn't resem the node at all if it
was typed, but I can't find it. The difference with this one is that the
individual elements are still resemmed.

This should fix the break caused by #24184 so we could redo it after
this PR but it might still have issues, not to mention the related
pre-existing issues like #22793, #12559 etc.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants