fix: emit implicit enum selectors in struct field defaults - #2225
Closed
mvanhorn wants to merge 1 commit into
Closed
fix: emit implicit enum selectors in struct field defaults#2225mvanhorn wants to merge 1 commit into
mvanhorn wants to merge 1 commit into
Conversation
Member
|
Good to see ya back contributing @mvanhorn 🚀 Will review this when I can later today |
SchoolyB
requested changes
Jul 31, 2026
SchoolyB
left a comment
Member
There was a problem hiding this comment.
@mvanhorn The fix for struct literals is solid 👍🏾 , I verified
One thing though — the same bug exists in the new(Struct) path and this PR doesn't
cover it. If you do something like:
const Task struct {
status Status = .PENDING
}
const Status enum {
PENDING
}
do main() {
mut t = new(Task)
println("${t^.status}")
}
It still emits _np->status = ; and leaks a C compiler error. The default emission
loop in the new() codegen path needs the same current_var_type save/restore you
applied to the struct literal path. Would be good to add a new(Task) case to the
integration test as well.
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.
Related issue
Closes #
Summary
Update the existing struct-default initializer path in
grayc/src/codegen/codegen.cso it emits an implicit enum selector using the field's declared enum type as context. Reuse the production expression/codegen behavior that already handles implicit enum selectors in struct literal fields and other contextually typed positions, keeping explicit enum selectors and non-enum defaults unchanged. Add a focused passing integration program that declares an enum-typed field with an implicit-selector default, constructs the struct without overriding that field, and verifies the resulting value; include an explicit-selector default or override in the same test as a parity guard.The typechecker accepts an implicit enum selector such as
.PENDINGas the default value of an enum-typed struct field, but the code generator emits no C expression for that default. Constructing the struct with the field omitted therefore produces an initializer like.status = }and leaks a C compiler error. The equivalent explicit selector,Status.PENDING, works, and implicit selectors already work in other expression contexts. There are no assignees, competing pull requests, comments claiming the work, or closed prior attempts in the supplied issue bundle.Closes #2223
Type of change
Breaking changes
None.
Checklist
make buildcompiles with zero warningsNot run: no test command resolved in this workspace, so nothing was executed to pass.
@before module names in any text (it tags GitHub users)Not run: no test command resolved in this workspace, so nothing was executed to pass.
If adding user-facing stdlib/builtin/type
grayc/src/stdlib/<module>.hand.c(with@manblock)Not run: no test command resolved in this workspace, so nothing was executed to pass.
stdlib_arg_table[],stdlib_arg_type_table[],_using_funcs[]Not run: no test command resolved in this workspace, so nothing was executed to pass.
emit_<module>_call()Not run: no test command resolved in this workspace, so nothing was executed to pass.
STANDARD.mdupdatedNot run: no test command resolved in this workspace, so nothing was executed to pass.
./scripts/generate_stdlib_man.shrun and output committedNot run: no test command resolved in this workspace, so nothing was executed to pass.
AI was used for assistance.