support struct field assignment obj.field = x#202
Merged
Conversation
assigning to a struct field silently wrote a local instead of the field: ir_emit_compound_assignment handled index targets (m[k]=v) then fell back to treating the target as a bare identifier, so obj.field = x and self.field = x were lost. now a field-access target lowers to the existing sstore op (the same one struct construction emits), for plain and compound (+=, -=, ...) forms. because structs are shared heap handles, mutations through a function or a method's implicit self are visible to the caller. the checker already type-checked field-access targets, so no checker change was needed.
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.
summary
assigning to a struct field silently did the wrong thing:
ir_emit_compound_assignmenthandled index targets (
m[k] = v) and then fell back to treating the assignment target as abare identifier, so
obj.field = x(andself.field = xinside a method) wrote a local namedafter the field instead of the field itself.
now a field-access target lowers to the existing
sstoreop — the same one struct constructionalready emits — for both plain and compound (
+=,-=, …) forms:because structs are shared heap handles, mutations made through a function parameter or a
method's
selfare visible to the caller (matchingtest_shared_collection_aliasing).this is the missing peer to the struct features that already exist (named-positional
construction and field defaults), and it's the prerequisite for stateful iterators in the
upcoming iterator-protocol work.
how
ir_emitter_core.pith: newir_emit_field_assignmentandir_emit_field_compound_assignment,invoked from
ir_emit_compound_assignmentbefore the identifier fallback (mirroring theexisting
ir_emit_index_assignment). they resolve the field index viair_struct_field_index_lookupand emitsstore.check_assignmentalready type-checks a field-access target (the field'stype vs the value).
what was tested
tests/cases/test_struct_field_assign.pith: assign a field on a local, compound+=, a string field, mutation through a function (reference semantics), mutation throughmethods via implicit
self, andp.y = p.x + 1(bootstrap fixed-point left to CI.)