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

Returning upvar captured by mutable reference doesn't work #40510

Closed
Zoxc opened this issue Mar 14, 2017 · 5 comments
Closed

Returning upvar captured by mutable reference doesn't work #40510

Zoxc opened this issue Mar 14, 2017 · 5 comments
Labels
C-bug Category: This is a bug. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.

Comments

@Zoxc
Copy link
Contributor

Zoxc commented Mar 14, 2017

This works:

fn main() {
    let a = Box::new(8u8);

    || {
        &a
    };
}

This does not and gives a confusing error:

fn main() {
    let mut a = Box::new(8u8);

    || {
        &mut a
    };
}

The error:

error[E0495]: cannot infer an appropriate lifetime for borrow expression due to conflicting requirements
 --> <anon>:5:9
  |
5 |         &mut a
  |         ^^^^^^
  |
note: first, the lifetime cannot outlive the lifetime  as defined on the body at 4:7...
 --> <anon>:4:8
  |
4 |       || {
  |  ________^ starting here...
5 | |         &mut a
6 | |     };
  | |_____^ ...ending here
note: ...so that closure can access `a`
 --> <anon>:5:9
  |
5 |         &mut a
  |         ^^^^^^
note: but, the lifetime must be valid for the scope of call-site for function at 4:7...
 --> <anon>:4:8
  |
4 |       || {
  |  ________^ starting here...
5 | |         &mut a
6 | |     };
  | |_____^ ...ending here
note: ...so that return value is valid for the call
 --> <anon>:4:5
  |
4 |       || {
  |  _____^ starting here...
5 | |         &mut a
6 | |     };
  | |_____^ ...ending here
@Zoxc
Copy link
Contributor Author

Zoxc commented Apr 11, 2017

This also gives a confusing error:

fn main() {
	let mut a = Vec::<bool>::new();

	|| {
		|| {
			a.push(true)
		}
	};
}
rustc 1.16.0 (30cf806ef 2017-03-10)
error: lifetime of `a` is too short to guarantee its contents can be safely reborrowed
 --> <anon>:5:3
  |
5 | 		|| {
  | 		^^
  |
note: `a` would have to be valid for the scope of call-site for function at 4:4...
 --> <anon>:4:5
  |
4 |   	|| {
  |  _____^ starting here...
5 | | 		|| {
6 | | 			a.push(true)
7 | | 		}
8 | | 	};
  | |__^ ...ending here
note: ...but `a` is only valid for the lifetime  as defined on the body at 4:4
 --> <anon>:4:5
  |
4 |   	|| {
  |  _____^ starting here...
5 | | 		|| {
6 | | 			a.push(true)
7 | | 		}
8 | | 	};
  | |__^ ...ending here

error: aborting due to previous error

The following works:

fn main() {
	let a = Vec::<bool>::new();

	|| {
		|| {
			a.len()
		}
	};
}

@Mark-Simulacrum
Copy link
Member

None of these examples don't compile today, so I'm presuming this is fixed and marking E-needstest.

@Mark-Simulacrum Mark-Simulacrum added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Jun 20, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-bug Category: This is a bug. label Jul 27, 2017
@dsprenkels
Copy link
Contributor

Can I take this issue and put this into a run-pass test?

@Mark-Simulacrum
Copy link
Member

Sounds good to me, though compile-pass would probably be sufficient - grep for rustc_error.

@dsprenkels
Copy link
Contributor

Ok. Will do. Btw, it looks like this commit was fixed by 9809fe0.

dsprenkels pushed a commit to dsprenkels/rust that referenced this issue Aug 2, 2017
dsprenkels pushed a commit to dsprenkels/rust that referenced this issue Aug 3, 2017
bors added a commit that referenced this issue Aug 3, 2017
Add regression test for #40510

This pull request adds a test case for issue #40510.

Fixes #40510.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.
Projects
None yet
Development

No branches or pull requests

3 participants