Repository navigation
Widen the operands of a Go if-clause comparison [patch] - #459
Open
matt-edmondson wants to merge 1 commit into
Open
matt-edmondson wants to merge 1 commit into
matt-edmondson wants to merge 1 commit into
Conversation
WriteCondition writes a top-level comparison itself to leave off the outer parentheses gofmt removes, which bypassed the operand widening GenerateBinary does. It now widens each operand to the type the two meet at, except for shifts and logical operators, and still parenthesises a struct literal operand. Fixes #395 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017vfJW4VpqLXsf6UDQnX46h
|
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 #395
What changed
GoGenerator.WriteConditionwrites a top-level comparison itself, so it can leave off the outer parentheses that gofmt removes from anifclause. That skipped the operand wideningStandardLanguageGenerator.GenerateBinarydoes everywhere else. Son < limit ? … : …with anintand alongproducedif n < limit {, which failsgo buildwith mismatched types.The non-bitwise
BinaryExpressioncase now computesPromoted(StaticType(left), StaticType(right)), except for shifts and logical operators, whichGenerateBinaryalso excludes. Each operand goes through a newWriteWidenedConditionOperand, which callsGenerateWidenedTo. A struct-literal operand, or a call chain that starts with one, is not a number, so it still goes toWriteConditionOperand. That keeps the #105/#185 parenthesising, and gofmt still has no outer parentheses to remove.int n, long limit:n < limit ? …if n < limit {if int64(n) < limit {int n:n < 0.5 ? …if n < 0.5 {if float64(n) < 0.5 {float n, double limit:n < limit ? …if n < limit {if float64(n) < limit {Tests
GoGeneratedSourceCompilesTests.ConditionComparingMixedNumericWidths_Compileschecks the three spellings above, then runsgo buildandgofmt -lon the output.Generated by Claude Code