fix: reject a compound assignment whose target is an optional - #1003
Merged
Conversation
`x += 3` on an `Int?` holding 5 answered 0, and nothing reported anything. A compound assignment reads its target, applies an operator and writes it back, so an optional target has to be unwrapped first. The check ran two acceptances before it looked at the operator: one that accepts a value an optional target can be built from, and one for collection literals. Both are a plain assignment's question, and taking either skipped the numeric check entirely, so the operator was emitted against the shell's address. The reported symptom was a segfault on a none-valued target. The quieter and worse one is the case above, where a held value reads back as zero. Those two acceptances now apply to `=` alone, which leaves every compound form reaching the numeric check and reporting E217 against the optional. Unwrapping and rewrapping instead would be a language decision rather than a fix, and it is not one this change should make on its own. ## what was tested An invalid-corpus case pins the rejection for a held value and for none. A golden covers what must keep working: compound assignment on integers and floats, and plain assignment into an optional, which is a different question and still allowed. Regressions 430/430 on both compilers, colocated std tests 114/114, pithgen 150 seeds clean, the leak gate flat, the invalid corpus 70/70, both bootstrap seed checks, formatting clean, seed regenerated.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #998.
x += 3on anInt?holding 5 answered 0, and nothing reported anything.A compound assignment reads its target, applies an operator and writes it back, so an optional target has to be unwrapped first. The check ran two acceptances before it ever looked at the operator — one that accepts a value an optional target can be built from, one for collection literals. Both answer a plain assignment's question, and taking either skipped the numeric check entirely, leaving the operator to be emitted against the shell's address.
The symptom on the issue was a segfault on a none-valued target. The quieter and worse one is the case above: a held value reads back as zero, silently.
Those two acceptances now apply to
=alone, so every compound form reaches the numeric check and reportsE217: operator += requires numeric type, got optional.Unwrapping and rewrapping instead would be a language decision rather than a fix, and not one this change should make on its own.
what was tested
An invalid-corpus case pins the rejection for both a held value and
none. A golden covers what must keep working: compound assignment on integers and floats, and plain assignment into an optional — a different question, still allowed.Regressions 430/430 on both compilers, colocated std tests 114/114, pithgen 150 seeds clean, leak gate flat, invalid corpus 70/70, both bootstrap seed checks, formatting clean, seed regenerated.