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

improve error message when returning iterator with reference into stack frame #53882

Open
nikomatsakis opened this issue Sep 1, 2018 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@nikomatsakis
Copy link
Contributor

This example (playground):

use std::rc::Rc;

fn iterate(data: Rc<Vec<u32>>) -> impl Iterator<Item = u32> {
    data.iter().cloned()
}

fn main() {
    for x in iterate(Rc::new(vec![1, 2, 3])) {
        println!("Hi! {}", x);
    }
}

gives this error:

error[E0597]: `data` would be dropped while still borrowed
 --> src/main.rs:4:5
   |
 4 |     data.iter().cloned()
   |     ^^^^ borrowed value does not live long enough
 5 | }
   | - borrowed value only lives until here
   |
   = note: borrowed value must be valid for the static lifetime...

I think we could do better. For example, we don't explain anything about how impl Iterator disallows lifetimes; and I think we can clarify why `data is being dropped.

I don't yet have a concrete suggestion, though, have to think about it.

Related to #52534 -- in some sense a specific instance of that.

@nikomatsakis nikomatsakis added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 1, 2018
@nikomatsakis nikomatsakis changed the title improve error message when improve error message when returning iterator with reference into stack frame Sep 1, 2018
@nikomatsakis
Copy link
Contributor Author

With respect to impl Iterator, one thing I thought of is this

error[E0597]: reference to `data` would be used after `data` is freed
 --> src/main.rs:4:5
   |
 3 | fn iterate(data: Rc<Vec<u32>>) -> impl Iterator<Item = u32> {
   |                                   -------------------------
   |                                   |
   |                                   as written, this type does not permit borrowed data
   |                                   help: try `impl Iterator<Item = u32> + '_`
 4 |     data.iter().cloned()
   |     ^^^^ `data` is borrowed here
 5 | }
   | - borrowed value only lives until here
   |
   = note: borrowed value must be valid for the static lifetime...

But in a way -- in this particular case -- it doesn't matter anyway, as there is nothing you could change that signature too that would work.

@matthewjasper matthewjasper self-assigned this Sep 1, 2018
@matthewjasper
Copy link
Contributor

I've assigned myself since I've been working on things in this area recently, but other things have come up as higher priority, so I might not get around to this for a few weeks.

bors added a commit that referenced this issue Oct 21, 2018
…nikomatsakis

[NLL] Use new region infer errors when explaining borrows

Use the new free region infer errors for explaining borrows

This gives at least some explanation for why a borrow is expected to
last for a certain free region. Also:

* Reports E0373: "closure may outlive the current function" with NLL.
* Special cases the case of returning a reference to (or value referencing) a local variable or temporary (E0515).
* Special case assigning a reference to a local variable in a closure to a captured variable. (E0521)

Closes #51026 - `regions-nested-fns-2.rs` isn't changed to that diagnostic, since that would not be the correct error here.
Closes #51169
cc #53882 - The error is (IMO) better now, but it could be better when we trace lifetimes in these error messages.

r? @nikomatsakis cc @pnkfelix
@matthewjasper matthewjasper removed their assignment Jan 1, 2019
@crlf0710 crlf0710 added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jun 11, 2020
@Nadrieril
Copy link
Member

Nadrieril commented Nov 30, 2023

The current error is:

error[E0597]: `data` does not live long enough
 --> src/main.rs:4:5
  |
3 | fn iterate(data: Rc<Vec<u32>>) -> impl Iterator<Item = u32> {
  |            ---- binding `data` declared here
4 |     data.iter().cloned()
  |     ^^^^----------------
  |     |
  |     borrowed value does not live long enough
  |     opaque type requires that `data` is borrowed for `'static`
5 | }
  | - `data` dropped here while still borrowed

For more information about this error, try `rustc --explain E0597`.

Which is already much better! I think "opaque type requires that" could point to the impl Iterator return type as Niko suggested, and "borrowed value does not live long enough" doesn't explain what it points to. My personal ideal would look like:

error[E0597]: `data` does not live long enough
 --> src/main.rs:4:5
  |
3 | fn iterate(data: Rc<Vec<u32>>) -> impl Iterator<Item = u32> {
  |            ----                   -------------------------
  |            |                      |
  |            |                      as written, this type does not permit borrowed data
  |            |                      help: try `impl Iterator<Item = u32> + '_`
  |            binding `data` declared here
4 |     data.iter().cloned()
  |     ^^^^----------------
  |     |
  |     `data` is borrowed here...
  |     ...and the borrow is captured by this whole expression
5 | }
  | ^ `data` dropped here while still borrowed

For more information about this error, try `rustc --explain E0597`.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants