Repository navigation
fix: parenthesise a struct literal operand in a Go condition [patch] - #165
Merged
Merged
Conversation
GoGenerator.WriteCondition drops the clause's outer parentheses to match
gofmt, which left a struct literal bare in the header of an if:
`if p == Point{1, 2} {` is a Go syntax error, because Go reads `Point{` there
as the start of the block. Each direct operand of the condition now goes
through WriteConditionOperand, which parenthesises a brace-spelled
construction, as the Go spec prescribes and gofmt keeps.
IsCompositeLiteral now holds the rule GenerateConstructionExpression already
used to choose braces over a conversion, so the two cannot disagree.
Fixes #105
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NfxXtpcxX8hkLYrePfHkV4
Contributor
Author
|
Locally the full suite passes on this branch: 1063 tests, with Go 1.24 and gcc on the path. I'll re-run the failed job once, as soon as the macOS and Windows legs of this run finish. Generated by Claude Code |
|
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 #105
What was wrong
To match gofmt,
GoGenerator.WriteConditionwrites the clause of anifwithout its outer parentheses. That left a struct literal operand bare.if p == Point{1, 2} {is a Go syntax error, because Go readsPoint{in anifheader as the start of the block (the composite-literal parsing ambiguity in the Go spec).Change
WriteConditionOperand. It wraps any construction Go spells with braces in parentheses:if p == (Pair{1, 2}) {. gofmt keeps that form.IsCompositeLiteralholds the brace-or-conversion rule thatGenerateConstructionExpressionalready used. Both paths now share it, so they can't disagree.ifgo throughWriteCondition, so all three are fixed: return, assignment/variable initialiser, and the called function literal inside another expression.Tests
New test in
GoGeneratedSourceCompilesTests:ConditionComparingAgainstAStructLiteral_Compiles.Pairstruct and three functions:!=;+, which lowers to the called function literal.p == (Pair{1, 2}), runsgo build ./..., and checks thatgofmt -lreports nothing.With the generator change reverted, the test fails. The full suite passes: 1063 tests, 0 skipped, with Go 1.24 and gcc on the path.
🤖 Generated with Claude Code
https://claude.ai/code/session_01NfxXtpcxX8hkLYrePfHkV4
Generated by Claude Code