Skip to content

Escape keyword-named identifiers in the Go generator [patch] - #445

Merged
matt-edmondson merged 2 commits into
mainfrom
claude/go-keyword-identifiers
Nov 11, 2026
Merged

matt-edmondson merged 2 commits into
mainfrom
claude/go-keyword-identifiers

Conversation

@matt-edmondson

@matt-edmondson matt-edmondson commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Part of #106 (Go target). C is covered separately by #446, and the issue stays open for JavaScript.

What was wrong

The Go generator wrote identifiers named after Go keywords as they were. A struct field called type or range, a parameter called chan or select, a local called go or defer, a method called switch, a function called func, or a package-level var map was enough to make the package fail to parse:

./holder.go:4:2: syntax error: unexpected keyword type, expected field name or embedded type

Change

  • GoGenerator now declares ReservedWords: the 25 keywords in the Go spec. It uses the base EscapeIdentifier, which appends _.
  • The escape is applied where each of these is declared: struct fields, package-level fields and variables, parameters, locals (including the _ = name line and the conditional-lowering form), and methods and free functions.
  • References, callees and designated members (Holder{type_: 3}) already go through EscapeReference, SpellCallee and WriteListElement, so they come out the same as the declarations. One Go-only call path, a call whose receiver is a composite literal, wrote call.Callee directly; it now goes through SpellCallee too.
  • Predeclared identifiers (nil, true, len, int) are not keywords, and Go lets any declaration shadow them, so they are not escaped.
  • Exported names, and names joined to their type's (HolderMake), can never be keywords, because every Go keyword is lower case.

Tests

  • Go_KeywordNamesCompileAndResolve generates a package full of keyword-named members. It runs it with go run against a driver that reads each escaped name back, then checks that gofmt -l reports nothing. It failed before this change with the syntax errors above.
  • Go_ReferenceKeepsTheReceiverAndPredeclaredNamesAndEscapesTheMember checks that self.chan becomes self.chan_ while self and true stay as written.
  • Full suite: 1666 passed, 0 failed (dotnet test --project Coder.Test/Coder.Test.csproj, with Go installed).

🤖 Generated with Claude Code

https://claude.ai/code/session_017vfJW3mc54mR7dMPMARSVv

A field, parameter, local, method, function, package-level variable or
designated member named after a Go keyword came out verbatim, so the
package failed to parse: a field called type or range, or a parameter
called chan or select, was enough. Go now appends an underscore at each
declaration, and references and calls go through the same hook so they
agree. The predeclared identifiers (nil, true, len, int) are names Go
lets any declaration shadow, so they are not escaped. Exported names and
names joined to their type's (HolderMake) are never keywords, since
every Go keyword is lower case.

Part of #106.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW3mc54mR7dMPMARSVv
matt-edmondson pushed a commit that referenced this pull request Nov 7, 2026
Keeps ReservedWordIdentifierTests.cs untouched, so this branch and the Go
keyword PR (#445) merge in either order.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW3ect9msbMq1zFsRLD
matt-edmondson added a commit that referenced this pull request Nov 10, 2026
* Escape keyword-named bindings in the JavaScript generator [patch]

JavaScript wrote keyword-named parameters, locals, functions, classes and
top-level names verbatim, so `total(in, typeof)` or `function return(...)`
failed to parse. These bindings now get an underscore (`in_`, `return_`)
at the declaration and at every use.

Members keep their names. JavaScript reads a keyword after a `.` and as a
field, accessor or method name, so `holder.default` and `this.switch()`
stay as written. A new `EscapesMemberNames` hook on LanguageGeneratorBase
tells EscapeReference to escape only a reference's leading segment, and
SpellCallee to leave a call with a receiver alone and to count only free
functions.

Part of #106.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW3ect9msbMq1zFsRLD

* Move the JavaScript keyword tests into their own file

Keeps ReservedWordIdentifierTests.cs untouched, so this branch and the Go
keyword PR (#445) merge in either order.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW3ect9msbMq1zFsRLD

---------

Co-authored-by: Claude <noreply@anthropic.com>
Keeps both the Go reserved words and main's SharesNameWithGetter and
MemberNameKey overrides, which were added at the same spot.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW3mc54mR7dMPMARSVv
@sonarqubecloud

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit aa05f48 into main Nov 11, 2026
16 checks passed
@matt-edmondson
matt-edmondson deleted the claude/go-keyword-identifiers branch November 11, 2026 01:51
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.

2 participants