[
https://issues.apache.org/jira/browse/THRIFT-6340?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Sylwester Lachiewicz updated THRIFT-6340:
-----------------------------------------
Description:
For a required field,
[t_kotlin_generator.cc:1129|https://github.com/apache/thrift/blob/master/compiler/cpp/src/thrift/generate/t_kotlin_generator.cc#L1129]
declares the private backing property as {{_}} plus the raw field name, and so
does every reader of it except the getter at [line
1163|https://github.com/apache/thrift/blob/master/compiler/cpp/src/thrift/generate/t_kotlin_generator.cc#L1163],
which references {{_}} plus {{kotlin_safe_name(name)}}, and that
backtick-quotes a keyword. For a field named after a Kotlin keyword the two
names differ and the class does not compile.
{code}
struct S { 1: required i32 val }
{code}
{noformat}
$ thrift --gen kotlin -out kt repro.thrift && grep -n "_val\|_\`val\`" kt/S.kt
9: private var _val: kotlin.Int? = null
11: val `val`: kotlin.Int get() = _`val`!!
{noformat}
{{_`val`}} is not a valid identifier either. The getter should use {{_}} plus
the raw name like the rest, since the underscore prefix already avoids the
keyword. Reproduced on master at 3af0cfff1.
was:
For a required field,
[t_kotlin_generator.cc:1129|https://github.com/apache/thrift/blob/master/compiler/cpp/src/thrift/generate/t_kotlin_generator.cc#L1129]
declares the private backing property as {{_}} plus the raw field name, while
the getter at [line
1163|https://github.com/apache/thrift/blob/master/compiler/cpp/src/thrift/generate/t_kotlin_generator.cc#L1163]
and the other readers reference {{_}} plus {{kotlin_safe_name(name)}}, which
backtick-quotes a keyword. For a field named after a Kotlin keyword the two
names differ and the class does not compile.
{code}
struct S { 1: required i32 val }
{code}
{noformat}
$ thrift --gen kotlin -out kt repro.thrift && grep -n "_val\|_\`val\`" kt/S.kt
9: private var _val: kotlin.Int? = null,
12: val `val`: kotlin.Int get() = _`val`!!
{noformat}
{{_`val`}} is not a valid identifier either. Both sides should use one name,
for example {{_}} plus the raw name, since the underscore prefix already avoids
the keyword. Reproduced on master at 3af0cfff1.
> Kotlin generator names a required field's backing property inconsistently
> when the field name is a Kotlin keyword
> -----------------------------------------------------------------------------------------------------------------
>
> Key: THRIFT-6340
> URL: https://issues.apache.org/jira/browse/THRIFT-6340
> Project: Thrift
> Issue Type: Bug
> Components: Kotlin - Compiler
> Affects Versions: 0.25.0
> Reporter: Sylwester Lachiewicz
> Priority: Major
>
> For a required field,
> [t_kotlin_generator.cc:1129|https://github.com/apache/thrift/blob/master/compiler/cpp/src/thrift/generate/t_kotlin_generator.cc#L1129]
> declares the private backing property as {{_}} plus the raw field name, and
> so does every reader of it except the getter at [line
> 1163|https://github.com/apache/thrift/blob/master/compiler/cpp/src/thrift/generate/t_kotlin_generator.cc#L1163],
> which references {{_}} plus {{kotlin_safe_name(name)}}, and that
> backtick-quotes a keyword. For a field named after a Kotlin keyword the two
> names differ and the class does not compile.
> {code}
> struct S { 1: required i32 val }
> {code}
> {noformat}
> $ thrift --gen kotlin -out kt repro.thrift && grep -n "_val\|_\`val\`" kt/S.kt
> 9: private var _val: kotlin.Int? = null
> 11: val `val`: kotlin.Int get() = _`val`!!
> {noformat}
> {{_`val`}} is not a valid identifier either. The getter should use {{_}} plus
> the raw name like the rest, since the underscore prefix already avoids the
> keyword. Reproduced on master at 3af0cfff1.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)