Compile time safety for case object fields

2022-10-21 Thread Araq
https://github.com/nim-lang/Nim/pull/20614

Compile time safety for case object fields

2022-10-21 Thread deech
I am able to reproduce with: nim c -r --warning:ProveField:on /tmp/test.nim Run Yes that is odd. I'm not sure why the warning triggers in the loop but not above. FWIW I can get the warning to go away with: for node in list: let n = node[] case

Compile time safety for case object fields

2022-10-21 Thread axyz
I'm on version 1.6.8 I can try updating anyway if I run `nim c -r --forceBuild --verbosity\:0 --hint\[Processing\]\:off --excessiveStackTrace\:on test.nim` or even just `nim c test.nim` it compiles without warning and outputs foo hello Run if I run with `nim c -

Compile time safety for case object fields

2022-10-21 Thread deech
I do not see a warning when compiled with: nim c -r --forceBuild --verbosity\:0 --hint\[Processing\]\:off --excessiveStackTrace\:on /tmp/test.nim Run I also tried it with the `--warning:ProveField:on` flag, still no warning. My nim version: Nim Compiler

Compile time safety for case object fields

2022-10-21 Thread axyz
not sure if I got the issue right. If the problem is related to partial initialization, why it affects also accessors? e.g. # node is correctly initialized with all fields let n = Node(kind: nkChild, name: "foo") case n.kind of nkChild: echo $n.name #using case statement

Compile time safety for case object fields

2022-10-21 Thread deech
This is unfortunate but happens because you are initializing a `ref` object which allows unsafe partial initialization, making a `NodeObj` first errors the way it should without adding any warning flags: type NodeKind = enum nkParent, nkChild Node

Compile time safety for case object fields

2022-10-21 Thread axyz
the problem seems to happen inside loops: let list = @[Node(kind: nkParent), Node(kind: nkChild)] for node in list: case node.kind of nkParent: node.children = @[] # inside the loop results in warning, if not in a loop is fine as in previous post of

Compile time safety for case object fields

2022-10-21 Thread axyz
ok, this is working: let a = Node(kind: nkParent) case a.kind of nkParent: a.children = @[] else: discard Run I guess it makes sense as the kind could change at runtime so the compiler needs a case or if to infer it. Still on a more complex code I ha

Compile time safety for case object fields

2022-10-21 Thread axyz
sorry if the code is not too clean, when I do let a = Node(kind: nkParent, children: @[]) # this is fine (now warnings) echo $a.children # this throws a warning (cannot prove that field is accessible) Run Myabe I am missing something, but was expecting that once `a

Compile time safety for case object fields

2022-10-21 Thread Araq
`--warning[ProveField]:on` works but depending on your shell you can also write it as `--warning:ProveField:on`

Compile time safety for case object fields

2022-10-21 Thread axyz
cool, I have 2 questions/issues though: 1\. I was able to enable the warning using a pragma like `{.warning[ProveField]: on.} ` on top of the file, but not sure how to pass it as a compilation argument. I tried nim c or nimble build with that argument and it says `no matches found: --warning[Pr

Compile time safety for case object fields

2022-10-20 Thread Araq
And yeah, this should have become the default for Nim v2 but v2 is already late and overwhelming.

Compile time safety for case object fields

2022-10-20 Thread Araq
Use `--warning[ProveField]:on`.

Compile time safety for case object fields

2022-10-20 Thread axyz
Given a simple "kinded" object like: type NodeKind = enum nkParent, nkChild Node = ref NodeObj NodeObj {.acyclic.} = object case kind: NodeKind of nkParent: children: seq[Node] of nkChild: name: strin