Repository navigation
Conversation
A struct literal could only set an array field from an array literal.
C brace initializers cannot copy an array, so Box{nums: arr} failed
to compile. Users had to declare the struct and assign the field.
The generator now emits such a literal as a statement expression. The
expression fills a temporary and copies each array with memcpy.
Three cases still require an array literal: a package-level
initializer, an anonymous struct literal, and an array literal
element. Taking the address of such a literal is an error, because a
statement expression has no address.
Collaborator
|
Sorry, I'm against making the compiler more complicated in regards to array, slice, and struct handling — it's already complex enough. You can just use |
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.
What
A struct literal can now set an array field from an array value:
Why
C brace initializers cannot copy an array. The generator therefore rejected any array field value that was not an array literal. Users had to declare the struct first and then assign the field.
How
The generator emits such a literal as a GNU statement expression. The expression fills a temporary with the other fields and copies each array with
memcpy:({ box _lit1 = {}; memcpy(_lit1.nums, arr, sizeof(so_int[3])); _lit1; })The temporary initializer uses designators for every field, because the copied array fields are left out of it.
Limits
Three cases still require an array literal, and each case keeps the existing error:
Taking the address of such a literal (
&Box{nums: arr}) is a new error. A statement expression produces a value with no address.Testing
testdata/arrayscovers keyed and positional fields, two array fields in one literal, array parameters, a call result as the value, a slice literal element, and a method call on the literal.testdata/badcases cover the package-level, anonymous struct, and address-of errors.go test ./...passes.doc/spec.mddescribes the new rule and its limits.