Skip to content

Kotlin: escape keyword-named oneofs in toString() - #3675

Merged
oldergod merged 1 commit into
masterfrom
bquenaudon.2026-08-14.tostring-escape-oneof-keyword
Aug 14, 2026
Merged

Kotlin: escape keyword-named oneofs in toString()#3675
oldergod merged 1 commit into
masterfrom
bquenaudon.2026-08-14.tostring-escape-oneof-keyword

Conversation

@oldergod

@oldergodoldergod commented Aug 14, 2026

Copy link
Copy Markdown
Member

Fixes#3674.

With escapeKotlinKeywords = true, a oneof whose name is a Kotlin keyword produced a toString() that doesn't compile:

if (`in` !=null) result +="""in=$in"""

generateToStringMethod's OneOf branch emitted the interpolated value as a raw add(fieldName) rather than add("%N", fieldName). %N is what makes KotlinPoet backtick-escape a keyword. The null check directly above it already used %N, as do both usages in the Field branch — only the oneof interpolation was raw, hence the asymmetric output in the issue.

Now:

if (`in` !=null) result +="""in=$`in`"""

The label text on either side (add(fieldName) for the in= prefix) stays unescaped — that's literal content of the output string, not an identifier.

cc @bjoernmayer@adityaanikam

With escapeKotlinKeywords, generateToStringMethod emitted the oneof's
interpolated value as a raw name instead of going through KotlinPoet's
%N. The null check right above it and both usages in the Field branch
already used %N, so only the oneof interpolation went unescaped:
if (`in` != null) result += """in=$in"""
which doesn't compile. Emit it with %N like everywhere else:
if (`in` != null) result += """in=$`in`"""
Closes#3674
@oldergod
oldergodforce-pushed the bquenaudon.2026-08-14.tostring-escape-oneof-keyword branch from 0c15429 to d2a87d8CompareAugust 14, 2026 13:42
@oldergod
oldergod marked this pull request as ready for review August 14, 2026 13:43
@oldergod
oldergod requested a review from dnkoutsoAugust 14, 2026 19:26
@oldergod
oldergod merged commit 60c04b9 into masterAug 14, 2026
15 checks passed
@oldergod
oldergod deleted the bquenaudon.2026-08-14.tostring-escape-oneof-keyword branch August 14, 2026 21:08
Sign up for freeto 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.

escapeKotlinKeywords = true not using right property name in toString

2 participants

@oldergod@staktrace