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

Stronger enforcement of not nil and requiresInit #13808

Merged
merged 21 commits into from
Apr 1, 2020
Merged

Stronger enforcement of not nil and requiresInit #13808

merged 21 commits into from
Apr 1, 2020

Conversation

zah
Copy link
Member

@zah zah commented Mar 30, 2020

This plugs a great number of holes in the object construction and sequence growing logic preventing the creation of values that violate not nil or requiresInit 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 the requiresInit 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, because newSeq and seq.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.

compiler/semexprs.nim Outdated Show resolved Hide resolved
zah added 18 commits April 1, 2020 04:08
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).
@zah zah force-pushed the fix-requiresInit branch from 35543c4 to 805447b Compare April 1, 2020 01:09
@Araq Araq closed this Apr 1, 2020
@Araq Araq reopened this Apr 1, 2020
@timotheecour timotheecour mentioned this pull request Apr 1, 2020
1 task
@@ -80,7 +80,7 @@ block thashes:
# Test with range
block:
type
R = range[1..10]
R = range[0..9]
Copy link
Member

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?

Copy link
Member

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.

Copy link
Member

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.

@Araq Araq added the merge_when_passes_CI mergeable once green label Apr 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merge_when_passes_CI mergeable once green
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants