microsoft/thrifty

Generated code for union with field named 'result' does not compile

luqasn opened this issue · 6 comments

(with builderless)

Because a val $name = [...] will be generated by generateReadCall and there exists a var result = [...] on the outer scope that will conflict with val $name if $name is 'result'.

See these two lines:
https://github.com/microsoft/thrifty/blob/master/thrifty-kotlin-codegen/src/main/kotlin/com/microsoft/thrifty/kgen/KotlinCodeGenerator.kt#L1150
https://github.com/microsoft/thrifty/blob/master/thrifty-kotlin-codegen/src/main/kotlin/com/microsoft/thrifty/kgen/KotlinCodeGenerator.kt#L1370

Example thrift file that produces uncompilable code (type of field does not matter):

namespace kt test.coro

union Union {
  1: i32 result;
}

Ahh, good catch. This will be a simple fix, I think (hope).

I think the simplest fix would be renaming the var to _internal_result or something (I think that naming is already used elsewhere). That does not eliminate the possibility of a clash but makes it unlikely enough.
The second possibility (that would never fail) would be to prefix the val names with an underscore or something like that.
Option 3 would be to inline the whole thing and get rid of the val completely.
Do you have a preference? Mine would probably be 3, 2, 1

The right thing to do is to use the NameAllocator to choose a name that is guaranteed not to be in use. It's simple, there's support in the existing code for this, I just need to make some time for it.

Fixed in master; I can't really predict when this will be shipped in a proper release. I'll update here when that happens.

Thanks!