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
{.requiresInit.}
has several bugs
#20253
Comments
The only bug here is that proc initMeow(): Meow =
discard
does compile. Everything else is you confusing control flow and data flow. ;-) |
The documentation for requiresInit reads as such:
I would argue that My first example that's "confusing data flow for control flow" is a simplified version of the example in the manual type
MyObject = object {.requiresInit.}
proc p() =
# the following is valid:
var x: MyObject
if someCondition():
x = a()
else:
x = a()
# use x And if we adapt that example as follows (to fill in the missing bits and make it compile) type
MyObject {.requiresInit.} = object
a: int
proc a(): MyObject =
result = MyObject(a: 4)
proc someCondition(): bool = false
proc p() =
# the following is valid:
var x: MyObject
if someCondition():
x = a()
else:
x = a() we get
I don't really see how my example could be legitimately invalid while the example in the manual is valid. Perhaps the documentation is wrong. The documentation goes on to immediately imply that the above example should not compile, so it's likely it's just wrong. |
* fix nim-lang#20253 * change NimbleStableCommit * Update koch.nim Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
* fix nim-lang#20253 * change NimbleStableCommit * Update koch.nim Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
* fix nim-lang#20253 * change NimbleStableCommit * Update koch.nim Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
What happened?
this compiles fine, even though it should probably error when compiling initMeow
on the other hand the following does not compile, even though the manual says requiresInit "does a control flow analysis to prove the variable has been initialized and does not rely on syntactic properties"
Also new and setLen both bypass requiresInit
this last bug is the same as #19139 but the issue isn't that it crashes and it shouldn't crash but that the example compiles at all, new should be considered to default initialize things, not initialize them fully.
setLen is even more nasty, it doesn't have a way to initialize things at all.
Nim Version
Nim Compiler Version 1.7.1 [Windows: amd64]
Compiled at 2022-08-20
Copyright (c) 2006-2022 by Andreas Rumpf
active boot switches: -d:release
Current Standard Output Logs
No response
Expected Standard Output Logs
No response
Possible Solution
given all these issues requiresInit is pretty useless, and doesn't manage to do what the various "required to initialize" constructs in other languages do. Personally I think it might be better to make requiresInit just require an explicit initializer and expand it to flow based checks later, given that the flow based checks don't really work now anyway.
Additionally the gaps in what's considered initialization should be fixed. Ironically for returning an uninitialized result the "can not prove result is initialized" warning fires off, but the requiresInit checks still pass.
Additional Information
No response
The text was updated successfully, but these errors were encountered: