Repository navigation
Keep Go constant-field values and apply instance-field initialisers - #187
Merged
Merged
Conversation
GoGenerator placed a field by IsStatic alone, so a constant that was not static became a struct member and its value was dropped, and an instance field's initialiser was lost from the generated constructor. A constant now goes to package-level storage, as static fields already did and as Rust does, and each constructor sets the initialised instance fields it does not set itself. The struct member carries a note saying where its default is applied, since a bare composite literal still starts it at zero. Fixes #111 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01JgoVAth3nGZUgbKrSgVCYX
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01JgoVAth3nGZUgbKrSgVCYX
|
3 tasks
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.



Fixes #111
What changed
GoGeneratordecided where a field goes fromIsStaticalone. Two things went wrong as a result:InitialValuewas lost: the constructor returnedCfg{}.The fix:
IsPackageLevelFieldreturnsfield.IsStatic || field.IsConstant, matchingRustGenerator.IsAssociatedField. Both placement predicates now use it, soRatiois written asconst CfgRatio float64 = 0.5, the same path static fields already take.Cfg{Misses: 1, Hits: 5}: the constructor's ownMisses: 1wins over the field's default, and no key is written twice, since Go rejects duplicate keys.// defaults to 5: Go has no field initialisers, so only a constructor sets it, because a bareCfg{}literal still starts the field at zero.Tests
GoGeneratedSourceCompilesTests.ConstantAndInitialisedFields_KeepTheirValuesWhenRunruns the issue's reproduction with a real Go toolchain.go run .now prints0.5 5; before the fix it printed0 0. The test also checks thatgofmtwould leave the generated file unchanged.GoGeneratorTests.Constructor_SetsInstanceFieldDefaultsItDoesNotSetItselfcovers a constructor that sets one field itself, plus the note on the field.GoGeneratorTests.ConstantField_IsPackageLevelEvenWhenNotStaticcovers a non-static constant.With
GoGenerator.csreverted, all three fail; with the fix, they pass. The full suite passes locally (1091 tests, 0 failed, 0 skipped, with Go on the path).🤖 Generated with Claude Code
https://claude.ai/code/session_01JgoVAth3nGZUgbKrSgVCYX
Generated by Claude Code