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

E0495 Diagnostic regression - missing actual source of error #100290

Open
RustyYato opened this issue Aug 8, 2022 · 0 comments
Open

E0495 Diagnostic regression - missing actual source of error #100290

RustyYato opened this issue Aug 8, 2022 · 0 comments
Labels
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.

Comments

@RustyYato
Copy link
Contributor

RustyYato commented Aug 8, 2022

Given the following code: playground on nightly 2022-08-07

use std::marker::PhantomData;

struct Invariant<'ctx>(&'ctx mut &'ctx ());
#[derive(Clone, Copy)]
struct Token<'ctx>(PhantomData<Invariant<'ctx>>);

pub struct Builder<'ctx> {
    _ctx: PhantomData<Invariant<'ctx>>,
}

#[derive(Clone, Copy)]
pub struct State<'ctx> {
    _ctx: PhantomData<Invariant<'ctx>>,
}

impl<'ctx> Builder<'ctx> {
    pub fn with(f: impl FnOnce(&mut Builder<'_>)) {
        let mut builder = Builder {
            _ctx: PhantomData,
        };

        f(&mut builder);
    }

    pub fn new_state(&mut self) -> State<'ctx> {
        State { _ctx: PhantomData }
    }

    pub fn accept(&mut self, _state: State<'ctx>) {}
}

fn test() {
    Builder::with(|b1| {
        Builder::with(|b2| {
            let state = b2.new_state();
            b1.accept(state);
        });
    });
}

The current output is as follows:

Note how the actual erroneous function call is completely missing! (a.check_same(b.0))

Compiling playground v0.0.1 (/playground)
error[[E0521]](https://doc.rust-lang.org/nightly/error-index.html#E0521): borrowed data escapes outside of closure
  --> src/lib.rs:35:25
   |
33 |     Builder::with(|b1| {
   |                    -- `b1` declared here, outside of the closure body
34 |         Builder::with(|b2| {
   |                        -- `b2` is a reference that is only valid in the closure body
35 |             let state = b2.new_state();
   |                         ^^^^^^^^^^^^^^ `b2` escapes the closure body here
   |
   = note: requirement occurs because of a mutable reference to `Builder<'_>`
   = note: mutable references are invariant over their type parameter
   = help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance

error[[E0521]](https://doc.rust-lang.org/nightly/error-index.html#E0521): borrowed data escapes outside of closure
  --> src/lib.rs:35:25
   |
33 |     Builder::with(|b1| {
   |                    --
   |                    |
   |                    `b1` is a reference that is only valid in the closure body
   |                    has type `&mut Builder<'1>`
34 |         Builder::with(|b2| {
35 |             let state = b2.new_state();
   |                         ^^^^^^^^^^^^^^
   |                         |
   |                         `b1` escapes the closure body here
   |                         argument requires that `'1` must outlive `'static`

For more information about this error, try `rustc --explain E0521`.
error: could not compile `playground` due to 2 previous errors

Ideally the output should look like: (NOTE: this is the output of the last stable release, 1.62.1)

Note how the actual erroneous function call listed as the source of the error.

Compiling playground v0.0.1 (/playground)
error[[E0495]](https://doc.rust-lang.org/stable/error-index.html#E0495): cannot infer an appropriate lifetime for lifetime parameter `'ctx` due to conflicting requirements
  --> src/lib.rs:35:28
   |
35 |             let state = b2.new_state();
   |                            ^^^^^^^^^
   |
note: first, the lifetime cannot outlive the anonymous lifetime #2 defined here...
  --> src/lib.rs:33:19
   |
33 |       Builder::with(|b1| {
   |  ___________________^
34 | |         Builder::with(|b2| {
35 | |             let state = b2.new_state();
36 | |             b1.accept(state);
37 | |         });
38 | |     });
   | |_____^
note: ...but the lifetime must also be valid for the anonymous lifetime #2 defined here...
  --> src/lib.rs:34:23
   |
34 |           Builder::with(|b2| {
   |  _______________________^
35 | |             let state = b2.new_state();
36 | |             b1.accept(state);
37 | |         });
   | |_________^
note: ...so that the types are compatible
  --> src/lib.rs:36:16
   |
36 |             b1.accept(state);
   |                ^^^^^^
   = note: expected `&mut Builder<'_>`
              found `&mut Builder<'_>`

For more information about this error, try `rustc --explain E0495`.
error: could not compile `playground` due to previous error
@RustyYato RustyYato 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 Aug 8, 2022
@RustyYato RustyYato reopened this Aug 8, 2022
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 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

1 participant