slachiewicz opened a new pull request, #3812:
URL: https://github.com/apache/thrift/pull/3812

   ## Summary
   
   A typedef of a struct was emitted as a **defined type** (`type Alias 
Struct`). In Go a defined type is a distinct type with its own (empty) method 
set. Because Thrift structs are always used through a pointer, the declared 
type was actually `type Alias *Inner`, meaning:
   
   - The alias had no `Read`/`Write`/`Equals` methods (`*Inner` methods were 
not promoted).
   - Every field access required a double pointer.
   - The `-remote` stub called `NewAlias()` instead of the struct's own 
`NewInner()`, which didn't exist.
   
   **Fix:** emit a Go **type alias** (`type Alias = Struct`) when the typedef's 
resolved type is a struct or exception. A Go alias shares the named type's 
identity and method set entirely — which is exactly the Thrift typedef 
semantics.
   
   A secondary bug is also fixed: the forward-typedef unwrapping used a single 
`if` that jumped directly to the true type, silently replacing user-declared 
typedef names in field types with their underlying types. The fix changes it to 
a `while` loop that stops at the first non-forward typedef.
   
   ## Root causes closed by this PR
   
   | Ticket | Description |
   |---|---|
   | THRIFT-3037 | Go: typedef of struct from included file broken |
   | THRIFT-3491 | Go: typedef of struct in service signature broken |
   | THRIFT-4901 | Go: typedef of struct generates wrong type |
   | THRIFT-5489 | Go: forward typedef loses declared name |
   | THRIFT-5601 | Go: forward typedef fix (reverted in THRIFT-5685 due to side 
effects now addressed here) |
   | THRIFT-5685 | Revert of THRIFT-5601 |
   
   ## Changes
   
   - **`compiler/cpp/src/thrift/generate/t_go_generator.cc`**
     - `generate_typedef`: emit `= Struct` alias for struct/exception typedefs.
     - `type_to_go_type_with_opt`: forward-typedef unwrapping changed from `if` 
to `while` (stop at first declared typedef); struct typedefs now return 
`*TypeName` as fields expect.
     - `generate_service_remote`: use the resolved struct's name/module for 
constructor; skip the typedef-conversion cast for struct typedefs (alias = same 
type).
   
   - **`lib/go/test/TypedefStructTest.thrift`** — new: covers forward typedef, 
typedef of struct, typedef of typedef, typedef of exception, service with 
typedef'd parameter/return.
   - **`lib/go/test/TypedefIncludeTest.thrift`** — new: covers typedef of a 
struct from an included file.
   - **`lib/go/test/tests/typedef_struct_test.go`** — new: compile-time 
alias-identity assertions + round-trip tests over binary/compact/JSON for each 
shape.
   - **`lib/go/test/Makefile.am`** — registers the two new Thrift files.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to