Skip to content

Struct{x:y, .. old.method_taking_self()} doesn't treat old as a moved value #18567

Closed
@brandonson

Description

@brandonson

In this code, the call to start.make_string_bar() should move start, but start can still be used by the println:

struct Mine{
  test: String,
  other_val: int
}

impl Mine{
  fn make_string_bar(mut self) -> Mine{
    self.test = "Bar".to_string();
    self
  }
}

fn main(){
  let start = Mine{test:"Foo".to_string(), other_val:0};
  let end = Mine{other_val:1, ..start.make_string_bar()};
  println!("{}", start.test);
}

This specific example of the bug doesn't do anything terrible for me, it just doesn't print anything. However, this appears to trample variable values, as I first ran into this with more complex code where it caused a segfault.

Changing start.test in the println to end.test prints bar, as expected. Changing Mine{other_val:1, ..start.make_string_bar()} to Mine{other_val:1, ..start} causes compiler errors at the println saying start has moved, which is also correct.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions