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

Cloning &strs doesn't trigger borrow checker #19704

Closed
Manishearth opened this issue Dec 10, 2014 · 5 comments
Closed

Cloning &strs doesn't trigger borrow checker #19704

Manishearth opened this issue Dec 10, 2014 · 5 comments
Labels
A-lifetimes Area: Lifetimes / regions A-type-system Area: Type system

Comments

@Manishearth
Copy link
Member

MWE:

fn get() -> Option<String> {
 Some("hi".to_string())
}

fn main() {
 let y = match get() {
  Some(x) => x.as_slice().clone(),
  None => "hello"
 };
 let mut z = "something".to_string();
 for i in range(1,1000u) {
   z = format!("{}", i);
   println!("{}", z)
 }
 println!("{}", y)
}

The last thing it prints out should be "hi". Instead it prints out "99" -- it it looks like the String x is deallocated in the match, but the pointer to the freed memory lives on.

@Manishearth
Copy link
Member Author

Changing "hi" to a longer string leads to varying results (I guess it's allocating different-sized Strings in different areas, which sounds logical)

@pcwalton pcwalton added A-type-system Area: Type system A-lifetimes Area: Lifetimes / regions labels Dec 10, 2014
@pcwalton
Copy link
Contributor

cc @nikomatsakis

@Manishearth
Copy link
Member Author

More minimal MWE:

fn main() {
 let y = {
  // "hello world" allocated
  "hello world".to_string().as_slice().clone()
  // "hello world" deallocated
 };
 // new string allocated in same spot
 let z = "something".to_string();
 println!("{}", y)
 // prints "somethingld"
}

I suspect results may vary depending on your system, but fiddling with the string length works.

@sfackler
Copy link
Member

Looks like a dup of #19261

@Manishearth
Copy link
Member Author

Seems correct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lifetimes Area: Lifetimes / regions A-type-system Area: Type system
Projects
None yet
Development

No branches or pull requests

3 participants