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

Avoid generating conflicting struct names in Rust code

VotersStop watchingWatchersLinkCloneUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 0.12.0
    • Component/s: Rust - Compiler
    • Labels:
      None

      Description

      If a thrift definition file contains multiple services, with functions having the same name, the generated code is not valid. There are conflicting definitions of *Args and *Result structs.

      service backend {
        i32 send(
          1: i32 arg
        );
      }
      
      service other_backend {
        string send(
          1: i32 arg
        )
      }
      

      Will generate the following code in the same file (I only pasted the interesting bits):

      struct SendResult {
        result_value: Option<i32>,
      }
      // ...
      struct SendResult {
        result_value: Option<String>,
      }
      

      And rustc would complain with errors like:

      error[E0428]: the name `SendResult` is defined multiple times
         --> src/test.rs:496:1
          |
      234 | struct SendResult {
          | ----------------- previous definition of the type `SendResult` here
      ...
      496 | struct SendResult {
          | ^^^^^^^^^^^^^^^^^ `SendResult` redefined here
          |
          = note: `SendResult` must be defined only once in the type namespace of this module
      

      Another (very similar) issue occurs if a user-defined struct happens to be called (keeping the same example as before) SendResult:

      struct SendResult {
          1: i32 resultCode,
          2: optional string errorCause,
      }
      
      service backend {
        SendResult send(
          1: i32 arg
        );
      }
      

      Will generate the following code in the same file (I only pasted the interesting bits)::

      pub struct SendResult {
        pub result_code: Option<i32>,
        pub error_cause: Option<String>,
      }
      // ...
      struct SendResult {
        result_value: Option<SendResult>,
      }
      

      Again, conflicting definitions of structs and rustc would complain.


      The approach taken by the Go generator (I haven't looked at other languages too much, some other generators probably do the same) for generating internal structs related to a service call is to prepend the service name to each of the structs.
      Using the same mechanism would solve both the issues cited above.

      I will give it a try and open a PR, but am also definitely open to any feedback on the idea

        Attachments

          Activity

            People

            • Assignee:
              allengeorge Allen George
              Reporter:
              flavr Flavien Raynaud

              Dates

              • Created:
                Updated:
                Resolved:

                Issue deployment