Skip to content

Copy assignment of std::vector is mistranslated #670

Open

Description

@lucic71

Copy assignment of a std::vector<T> (a = b) is translated to a = b.clone(). This results in the following wrong sequence of operations:

Step C++ Output Rust translation Output
Before a = {Foo 1, Foo 2}, b = {Foo 3} a = {Foo 1, Foo 2}, b = {Foo 3}
1 a[0] = b[0]: copy assignment onto the existing Foo 1 copy= 3 b.clone(): copy-constructs a new Foo 3 in a new Vec copy 3
2 a[1].~Foo(): the surplus element is destroyed ~Foo 2 a = <new Vec>: the old Vec is dropped, Foo 1 and Foo 2 go away with no destructor call
After a = {Foo 3}, a[0] is the same object as before a = {Foo 3}, a[0] is a different object

Activity

  1. lucic71 commented on Nov 20, 2026

    @lucic71
    ContributorAuthor

    Calling a = b.clone() is also broken for std::vector<std::pair<T1, T2>>.

    std::pair<T1, T2> is (Value<T1>, Value<T2>), calling Clone on each element copies the Rc's of the pair. The correct operation is to deep copy the pair, not copy the Rc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions