-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Stronger enforcement of not nil
and requiresInit
#13808
Conversation
New issue: since `Table[A, B]` allocates its backing storage with `newSeq[KeyValuePair[A, B]]`, it's no longer legal to create a table with `not nil` types used as either keys or values.
The new mechanism can deal with more complex scenarios such as not nil field appearing in a non-default case object branch or a field within a generic object that may depend on a when branch. The commit also plugs another hole: the user is no longer able to create illegal default values through seq.setLen(N).
@@ -80,7 +80,7 @@ block thashes: | |||
# Test with range | |||
block: | |||
type | |||
R = range[1..10] | |||
R = range[0..9] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why does this test need to change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because it produces a new warning, I think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no this PR actually suppresses the warning instead (which is great); so IMO the test should keep [1..10]
, and the 2 # causes warning, why?
can finally disappear. yay.
This plugs a great number of holes in the object construction and sequence growing logic preventing the creation of values that violate
not nil
orrequiresInit
requirements.As the manual already states, when the
requiresInit
is specified on an object type, all construction expressions needs to initialize all of the fields of the constructed type. You can also set therequiresInit
pragma only on specific fields to enforce a mandatory subset.Please read the commit messages and the provided test cases for a full list of fixes and modified behaviors. I'll appreciate it if this PR is not squashed while merging, because I might need to return to specific commits to perform experiments.
One final remaining issue is that it's no longer possible to instantiate the tables and set types in the standard library with
not nil
types, becausenewSeq
andseq.setLen
are no longer allowed when the element type doesn't have a legal default value.My very last commits includes a demonstration how the
KeyValuePair
type can be modified to restore the full functionality, but this would come at the expense of some optimisation tricks done in tables.nim. There are some alternative solutions that may solve the problem, but require additional work.