Skip to content

fix: parenthesise a struct literal operand in a Go condition [patch] - #165

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/105-go-struct-literal-condition
Oct 6, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/105-go-struct-literal-condition

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #105

What was wrong

To match gofmt, GoGenerator.WriteCondition writes the clause of an if without its outer parentheses. That left a struct literal operand bare. if p == Point{1, 2} { is a Go syntax error, because Go reads Point{ in an if header as the start of the block (the composite-literal parsing ambiguity in the Go spec).

Change

  • Each direct operand of a binary or unary condition now goes through a new WriteConditionOperand. It wraps any construction Go spells with braces in parentheses: if p == (Pair{1, 2}) {. gofmt keeps that form.
    • Everything else, and the gofmt-style clause, is unchanged.
  • A new IsCompositeLiteral holds the brace-or-conversion rule that GenerateConstructionExpression already used. Both paths now share it, so they can't disagree.
  • All three positions where a conditional becomes an if go through WriteCondition, so all three are fixed: return, assignment/variable initialiser, and the called function literal inside another expression.

Tests

New test in GoGeneratedSourceCompilesTests: ConditionComparingAgainstAStructLiteral_Compiles.

  • It builds a Pair struct and three functions:
    • a return comparing against a positional literal;
    • an assignment comparing a field-named literal on the left with !=;
    • a conditional nested inside +, which lowers to the called function literal.
  • It pins p == (Pair{1, 2}), runs go build ./..., and checks that gofmt -l reports nothing.
  • It is inconclusive when no Go toolchain is on the path, like the other tests in this file.

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

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

Copy link
Copy Markdown
Contributor Author

ci / .NET / Test on ubuntu-latest failed on f8851d2 because the runner went away, not because of a test. The log shows The runner has received a shutdown signal about 50 seconds into the test run, followed by Test run failed: A task was canceled. No test reported a failure.

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

@sonarqubecloud

sonarqubecloud Bot commented Oct 6, 2026

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit 501ddad into main Oct 6, 2026
21 of 22 checks passed
@matt-edmondson
matt-edmondson deleted the fix/105-go-struct-literal-condition branch October 6, 2026 02:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Go: a condition comparing against a struct literal generates if p == Point{1, 2} {, which is a Go syntax error

2 participants