Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
brandonson opened this issue Nov 3, 2014 · 3 comments · Fixed by #18672
Closed

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

brandonson opened this issue Nov 3, 2014 · 3 comments · Fixed by #18672
Milestone

Comments

@brandonson
Copy link
Contributor

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.

@brandonson brandonson changed the title Using Struct{x:y, .. old.method_taking_self()} doesn't treat old as a moved value Struct{x:y, .. old.method_taking_self()} doesn't treat old as a moved value Nov 3, 2014
@emberian
Copy link
Member

emberian commented Nov 3, 2014

Nominating for P-high, this is an obvious soundness hole.

@ghost ghost self-assigned this Nov 3, 2014
@brandonson
Copy link
Contributor Author

As a heads up, I'm working on fixing this with a bit of guidance from eddyb on IRC, whoever he is here! Looks like the with expression wasn't being walked by visitors, but fields from the result were consumed.

@ghost ghost removed their assignment Nov 3, 2014
brandonson added a commit to brandonson/rust that referenced this issue Nov 6, 2014
Fixes rust-lang#18567. Struct{x:foo, .. with_expr} did not walk with_expr, which allowed
using moved variables in some cases.  The CFG for structs also built up with
with_expr happening before the fields, which is now reversed. (Fields are now
before the with_expr in the CFG)
@pnkfelix
Copy link
Member

pnkfelix commented Nov 6, 2014

Assigning P-backcompat-lang, 1.0.

@pnkfelix pnkfelix added this to the 1.0 milestone Nov 6, 2014
bors added a commit that referenced this issue Nov 7, 2014
…matsakis

Fixes #18567. `Struct{x:foo, .. with_expr}` did not walk `with_expr`, which allowed
using moved variables in some cases.  The CFG for structs also built up with
`with_expr` happening before the fields, which is now reversed. (Fields are now
before the `with_expr` in the CFG)
lnicola added a commit to lnicola/rust that referenced this issue Dec 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants