Repository navigation
fix: write a C# const local only for a compile-time value [patch] - #172
Merged
Merged
Conversation
CSharpGenerator wrote `const` for every constant local with a named type, so a constant computed from a parameter or a call came out as `const int doubled = (n * 2);`, which is error CS0133. GoGenerator's IsCompileTimeValue moves to LanguageGeneratorBase so both generators share one rule, and C# now writes `const` only when the initial value is a literal. Otherwise the local is a plain declaration, which is already how an inferred constant was written, since C# has no readonly local. Fixes #110 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017USYdQkEy3VLP48Dd4JadP
|
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
CSharpGenerator.GenerateVariableDeclarationwroteconstfor everyIsConstantlocal with a named type. When the initial value was computed from a parameter or a call, the output wasconst int doubled = (n * 2);, which fails with CS0133.GoGenerator.IsCompileTimeValuemoves toLanguageGeneratorBaseas aprotected staticmethod, so C# and Go use one rule. Go behaves the same as before.constonly whenIsCompileTimeValue(InitialValue)is true. Otherwise the local is a plain declaration (int doubled = (n * 2);). That matches how the generator already writes an inferred (var) constant, since C# has no readonly locals.Tests
CSharpConstantLocalTests:NonLiteralConstant_IsAPlainLocaluses the repro from the issue. It fails onmainwithconst int doubled = (n * 2);and passes with this change.LiteralConstant_IsConstchecks that a literal constant still comes out asconst int max = 7;.Coder.Testsuite: 1073 passed, 0 failed.Fixes #110
🤖 Generated with Claude Code
https://claude.ai/code/session_017USYdQkEy3VLP48Dd4JadP
Generated by Claude Code