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

Empty struct lifetime bug #11681

Closed
angdev opened this issue Jan 20, 2014 · 11 comments · Fixed by #11776 or #11929
Closed

Empty struct lifetime bug #11681

angdev opened this issue Jan 20, 2014 · 11 comments · Fixed by #11776 or #11929
Labels
A-destructors Area: Destructors (`Drop`, …) A-lifetimes Area: Lifetimes / regions E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.
Milestone

Comments

@angdev
Copy link
Contributor

angdev commented Jan 20, 2014

I asked stackoverflow about lifetime of variable. Original link is this : http://stackoverflow.com/questions/21226942/rust-lifetime-and-calling-member-function

** Question from link **

I have a question about lifetime of varable in Rust programming language.

createTest function creates and returns r-value reference. and when it returns a reference, testValue is destroyed. But test.print() doesn't lead to crash. Why?

(Is Test::print function called as static function?)

Code

struct Test;                                                         
impl Drop for Test {                                                       
  fn drop (&mut self) {                                                    
    println("Dropped.");                                                   
  }                                                                        
}                                                                          

impl Test {                                                                
  fn print(&self) { println!("Print!"); }                                  
}                                                                          

fn createTest() -> &Test {                                                 
  let testValue = &Test;                                                   
  return testValue;                                                        
}                                                                          

fn main() {                                                                
  let test = createTest();                                             
  test.print();                                                            
  println("Test");                                                         
}

Result

Dropped.
Print!
Test

It happened with empty struct only. Is it a really bug?

@huonw
Copy link
Member

huonw commented Jan 20, 2014

Thanks for the report! I'm closing this as a dupe of #8309 and #10800: the latter means that testValue is of type &'static Test, and the former means that it is legal to return this via an anonymous lifetime.

@huonw huonw closed this as completed Jan 20, 2014
@huonw
Copy link
Member

huonw commented Jan 20, 2014

Oh, my apologies, I missed the Drop implementation. This does seem like a bug with how Drop interacts with those two bugs I referenced above.

@huonw huonw reopened this Jan 20, 2014
@alexcrichton
Copy link
Member

Nominating, this sounds bad.

@pnkfelix
Copy link
Member

Accepted for 1.0, P-backcompat-lang.

Also, cc @nikomatsakis

@flaper87
Copy link
Contributor

I'm digging into this. @nikomatsakis if you've any pointers that would help debugging the issue, please let me know 😄

@flaper87
Copy link
Contributor

I dug a bit into this. If what I looked into is correct, testValue is being treated as a static item instead of being treated as an rvalue given the fact that it's no-Pod. This doesn't seem wrong for PODs but it should be for non-PODs.

This is the borrowck log for the &Test section:

add_assignment[var](lp=$(local (id=97, name=testValue)), assignment=0, path_index=middle::borrowck::move_data::MovePathIndex(0u))
gather_loans_in_expr(expr=99u32/&Test)
guarantee_valid(borrow_id=99u32, cmt={cat_static_item id:98 m:McImmutable ty:Test}, req_mutbl=ImmutableMutability, loan_region=ReFree(middle::ty::FreeRegion{scope_id: 103u32, bound_region: BrAnon(0u)}))
guarantee_lifetime(cmt={cat_static_item id:98 m:McImmutable ty:Test}, loan_region=ReFree(103, BrAnon(0)))
guarantee_lifetime.check(cmt={cat_static_item id:98 m:McImmutable ty:Test}, loan_region=ReFree(103, BrAnon(0)))

@nikomatsakis
Copy link
Contributor

@flaper87 you are correct, and it's just wrong all the time. We should treat unary structs and nullary enum variants as rvalues, not static items.

@flaper87
Copy link
Contributor

@nikomatsakis r?

@huonw
Copy link
Member

huonw commented Jan 30, 2014

@flaper87 is this actually fixed by #11776? I don't see a test case with a Drop impl in that pull request.

@pnkfelix
Copy link
Member

reopening to ensure a test gets added.

@pnkfelix pnkfelix reopened this Jan 30, 2014
@flaper87
Copy link
Contributor

@huonw it is fixed. The issue had nothing to do w/ the Drop but the fact that unary structs were treated as static_items. I'll add a cfail test for this specific case, anyway.

flip1995 pushed a commit to flip1995/rust that referenced this issue Jan 25, 2024
…_is_some, r=llogiq

Improve help message for `search_is_some` lint

Fixes rust-lang#11681.

Like mentioned in the issue, we tend to use the formulation "consider using", which we didn't in this case. I think it clears both the confusion and also makes help message more coherent overall.

r? `@llogiq`

changelog: Improve help message for `search_is_some` lint
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-destructors Area: Destructors (`Drop`, …) A-lifetimes Area: Lifetimes / regions E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.
Projects
None yet
6 participants