Skip to content

Fix how we handle niche optimization with ZST #1205

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

Merged
merged 6 commits into from
May 20, 2022

Conversation

celinval
Copy link
Contributor

@celinval celinval commented May 17, 2022

Description of changes:

The compiler uses niche optimization when there is only one variant that contains nonzero sized fields. This variant is represented as the dataful variant. However, other variants may have one or more ZST fields that can be deconstructed and referenced in the code.

Before this change, we were not generating code for them and it generates type mismatch issues when the user tried to access them. In the testcase I added, it also uncovered an issue that would crash the compiler when the names of the fields of the ZST variant did not match the dataful one.

This change ensures that we represent all variants that have any field, so their access is valid.

Resolved issues:

Resolves #95.
Resolves #277.
Resolves #729

Call-outs:

I enable a debug_assert inside projection mismatch for now. I am planning to turn this into an assert and revert the logic added by #1057. But this is a much larger change that I prefer creating a separate PR for.

Testing:

  • How is this change tested? New test + fixme test.

  • Is this a refactor change? Nope

Checklist

  • Each commit message has a non-empty body, explaining why the change was made
  • Methods or procedures are documented
  • Regression or unit tests are included, or existing tests cover the modified code
  • My PR is restricted to a single feature or bugfix

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.

celinval added 3 commits May 17, 2022 14:19
The compiler uses niche optimization when there is only one variant that
contains nonzero sized fields. This variant is represented as the
dataful variant. However, other variants may have one or more ZST fields
that can be deconstructed and referenced in the code.

Before this change, we were not generating code for them and it generate
type mismatch issues when the user tried to access them.

This change ensures that we represent all variants that have any field,
so their access is valid.
We should probably turn this into an assert and revert the logic added
by model-checking#1057. But this is a much
larger change that I prefer creating a separate PR for.
@celinval celinval requested a review from a team as a code owner May 17, 2022 21:27
Copy link
Contributor

@zhassan-aws zhassan-aws left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice to finally get rid of this warning! Thanks!

subst,
variants,
initial_offset,
ctx.ensure_union(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment that explains what this is doing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure!

.iter_enumerated()
.filter_map(|(i, case)| {
if case.fields.is_empty() {
// Skip variant types that cannot be referenced.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@celinval celinval merged commit 849b4e6 into model-checking:main May 20, 2022
@tedinski
Copy link
Contributor

This PR rules. Just wanted to say that. Nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants