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

Go compiler produces uncompilable code

VotersWatch issueWatchersLinkCloneUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 0.9.2
    • 0.9.2
    • Go - Compiler
    • None
    • OSX 10.10

    • Patch Available

    Description

      The Thrift Go compiler produces wrong code for the following code:

      struct A { 1: list<B> b }
      struct B { 1: i64 id }
      

      The abbreviated reader method creates a []B instead of a []*B but still tries to add a &B{}.

       func (p *A) ReadField1(iprot thrift.TProtocol) error {
           ...
           tSlice := make([]B, 0, size)
           ...
           for i := 0; i < size; i++ {
               _elem0 := &B{}
               ...
               p.B = append(p.B, _elem0)
           }
           ...
       }
      

      The Go compiler message is:

      $ go version
      go version go1.3.3 darwin/amd64
      $ go build
      ./ttypes.go:74: cannot use _elem0 (type *B) as type B in append
      

      Moving struct B above struct A produces correct code:

      struct B { 1: i64 id }
      struct A { 1: list<B> b }
      

      The abbreviated reader method:

       func (p *A) ReadField1(iprot thrift.TProtocol) error {
           ...
           tSlice := make([]*B, 0, size)
           ...
           for i := 0; i < size; i++ {
               _elem0 := &B{}
               ...
               p.B = append(p.B, _elem0)
           }
           ...
       }
      

      This problem does not occurr with enums since they are aliases for int64. So both versions generate correct code:

      struct A { 1: list<B> b }
      enum B { X }
      

      and

      enum B { X }
      struct A { 1: list<B> b }
      

      Tested with version 902b7af4c84b8f716668d4d4f10612c16109c09a from https://git-wip-us.apache.org/repos/asf/thrift.git.
      I've built the thrift compiler with the following flags:

      PATH=/opt/boxen/homebrew/Cellar/bison27/2.7.1/bin:$PATH ./configure \
          --without-cpp \
          --without-qt4 \
          --without-c_glib \
          --without-csharp \
          --without-java \
          --without-erlang \
          --without-nodejs \
          --without-lua \
          --without-python \
          --without-perl \
          --without-php \
          --without-php_extension \
          --without-ruby \
          --without-haskell \
          --with-go
      

      and generate the thrift stubs as follows:

      ../compiler/cpp/thrift --gen go -out . model.thrift
      

      I'll try to dig a bit through the source of the generator to see whether I can produce a patch.

      Attachments

        1. Thrift2793.thrift
          0.2 kB
          Jens Geyer
        2. THRIFT-2793.patch
          2 kB
          Frank Schroeder

        Issue Links

        Activity

          This comment will be Viewable by All Users Viewable by All Users
          Cancel

          People

            jensg Jens Geyer
            magiconair Frank Schroeder
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Slack

                Issue deployment