Uploaded image for project: 'Thrift'
  1. Thrift
  2. THRIFT-5601

Typedef after first use causes incorrect go code

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Reopened
    • Major
    • Resolution: Unresolved
    • 0.17.0, 0.18.1
    • None
    • Go - Compiler
    • None

    Description

      Currently if a typedef happens after it's first used, the compiler will generate incorrect (uncompilable) go code.

      For example, for this thrift file:

       

      struct Bar {
        1: optional Foo foo,
      }
      
      typedef i32 Foo 

      The generated go code would be:

       

       

      ...
      
      type Foo int32
      
      ...
      
      type Bar struct {
        Foo *int32 `thrift:"foo,1" db:"foo" json:"foo,omitempty"`
      }
      
      ...
      
      func (p *Bar)  ReadField1(ctx context.Context, iprot thrift.TProtocol) error {
        if v, err := iprot.ReadI32(ctx); err != nil {
        return thrift.PrependError("error reading field 1: ", err)
      } else {          
        temp := Foo(v)
        p.Foo = &temp                     
      }
        return nil
      }

      If we move the typedef to be before it's used, then generated go code would be:

       

       

      ...
      type Bar struct {
        Foo *Foo `thrift:"foo,1" db:"foo" json:"foo,omitempty"`
      }
      ...

      instead.

      Interestingly the compiler knows that the field should be of type int32 (if we change the typedef to string it would be string instead), so it's not like the compiler don't know about the typedef at all, it's just not using the correct typedef in the go code.

       

      Attachments

        Issue Links

          Activity

            People

              fishywang Yuxuan Wang
              fishywang Yuxuan Wang
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 20m
                  20m