Opened 4 years ago

Last modified 4 years ago

#197 new defect

Generated constructor calls copy ctor twice on tuple elements

Reported by: f37yu Owned by:
Priority: major Component: cfa-cc
Version: 1.0 Keywords: tuple, copy, constructor
Cc:

Description (last modified by f37yu)

Compiler generated copy constructor for struct with fields of tuple type does a redundant copy.

The tuple elements are first copied to generated temporary variables, then to the actual destination.

Code that creates the problem:

struct A {};
void ?{}(A& a, A src) { sout | 'A'; }
struct B { [A, A] elem; };

int main() {
  B b;
  B b1 = b;
}

Correct behavior is the program prints twice. Currently on B b1 = b; it prints four times.

compile this snippet with -n -P resolver, should find the following (note the "maybe constructed"):

?{}: autogenerated cfa static inline function
... with parameters
  _dst: reference to instance of struct B with body 1
  _src: instance of struct B with body 1
... returning nothing
... with body
  CompoundStmt
    Implicit Ctor Dtor Statement
    ... with Ctor/Dtor: Expression Statement:
        Tuple Assignment Expression, with stmt expr:
          Statement Expression: 
            CompoundStmt
              Declaration of __multassign_L0: reference to instance of struct A with body 1 with initializer (not constructed)
                Simple Initializer: Generated Cast of:
                  Tuple Index Expression, with tuple:
                    Member Expression, with field:
                      elem: tuple of types
                        instance of struct A with body 1
                        instance of struct A with body 1

                    ... from aggregate:
                      Generated Cast of:
                        Variable Expression: _dst: reference to instance of struct B with body 1
                      ... to:
                        instance of struct B with body 1                    with index: 0

                ... to:
                  reference to instance of struct A with body 1

              Declaration of __multassign_L1: reference to instance of struct A with body 1 with initializer (not constructed)
                Simple Initializer: Generated Cast of:
                  Tuple Index Expression, with tuple:
                    Member Expression, with field:
                      elem: tuple of types
                        instance of struct A with body 1
                        instance of struct A with body 1

                    ... from aggregate:
                      Generated Cast of:
                        Variable Expression: _dst: reference to instance of struct B with body 1
                      ... to:
                        instance of struct B with body 1                    with index: 1

                ... to:
                  reference to instance of struct A with body 1

              Declaration of __multassign_R0: instance of struct A with body 1 with initializer (maybe constructed)
                Constructor initializer: 
                ... initially constructed with Implicit Ctor Dtor Statement
                  ... with Ctor/Dtor: Expression Statement:
                      Application of
                        Variable Expression: ?{}: static inline function
                        ... with parameters
                          _dst: reference to instance of struct A with body 1
                          _src: instance of struct A with body 1
                        ... returning nothing

                      ... to arguments
                        Generated Cast of:
                          Variable Expression: __multassign_R0: instance of struct A with body 1
                        ... to:
                          reference to instance of struct A with body 1
                        Tuple Index Expression, with tuple:
                          Member Expression, with field:
                            elem: tuple of types
                              instance of struct A with body 1
                              instance of struct A with body 1

                          ... from aggregate:
                            Variable Expression: _src: instance of struct B with body 1                          with index: 0


                ... destructed with Implicit Ctor Dtor Statement
                  ... with Ctor/Dtor: Expression Statement:
                      Application of
                        Variable Expression: ^?{}: static inline function
                        ... with parameters
                          _dst: reference to instance of struct A with body 1
                        ... returning nothing

                      ... to arguments
                        Generated Cast of:
                          Variable Expression: __multassign_R0: instance of struct A with body 1
                        ... to:
                          reference to instance of struct A with body 1



              Declaration of __multassign_R1: instance of struct A with body 1 with initializer (maybe constructed)
                Constructor initializer: 
                ... initially constructed with Implicit Ctor Dtor Statement
                  ... with Ctor/Dtor: Expression Statement:
                      Application of
                        Variable Expression: ?{}: static inline function
                        ... with parameters
                          _dst: reference to instance of struct A with body 1
                          _src: instance of struct A with body 1
                        ... returning nothing

                      ... to arguments
                        Generated Cast of:
                          Variable Expression: __multassign_R1: instance of struct A with body 1
                        ... to:
                          reference to instance of struct A with body 1
                        Tuple Index Expression, with tuple:
                          Member Expression, with field:
                            elem: tuple of types
                              instance of struct A with body 1
                              instance of struct A with body 1

                          ... from aggregate:
                            Variable Expression: _src: instance of struct B with body 1                          with index: 1


                ... destructed with Implicit Ctor Dtor Statement
                  ... with Ctor/Dtor: Expression Statement:
                      Application of
                        Variable Expression: ^?{}: static inline function
                        ... with parameters
                          _dst: reference to instance of struct A with body 1
                        ... returning nothing

                      ... to arguments
                        Generated Cast of:
                          Variable Expression: __multassign_R1: instance of struct A with body 1
                        ... to:
                          reference to instance of struct A with body 1



              Expression Statement:
                Tuple:
                  Application of
                    Variable Expression: ?{}: static inline function
                    ... with parameters
                      _dst: reference to instance of struct A with body 1
                      _src: instance of struct A with body 1
                    ... returning nothing

                  ... to arguments
                    Variable Expression: __multassign_L0: reference to instance of struct A with body 1
                    Variable Expression: __multassign_R0: instance of struct A with body 1

                  Application of
                    Variable Expression: ?{}: static inline function
                    ... with parameters
                      _dst: reference to instance of struct A with body 1
                      _src: instance of struct A with body 1
                    ... returning nothing

                  ... to arguments
                    Variable Expression: __multassign_L1: reference to instance of struct A with body 1
                    Variable Expression: __multassign_R1: instance of struct A with body 1

Change History (2)

comment:1 Changed 4 years ago by f37yu

Description: modified (diff)

comment:2 Changed 4 years ago by f37yu

Description: modified (diff)
Note: See TracTickets for help on using tickets.