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

When passing too many lifetimes to a struct, there are tons of unrelated errors about needing explicit lifetime annotations #125604

Closed
RalfJung opened this issue May 27, 2024 · 2 comments · Fixed by #125608
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@RalfJung
Copy link
Member

RalfJung commented May 27, 2024

To reproduce, check out commit cdc509f of rustc and then edit this line:

dcx: &mut DiagnosticCx<'_, '_, '_, 'tcx>,

Specifically, add another '_, before the 'tcx. Then run ./x.py check miri. This results in some surprising errors:

error[E0107]: struct takes 4 lifetime arguments but 5 lifetime arguments were supplied
   --> src/tools/miri/src/borrow_tracker/stacked_borrows/mod.rs:278:19
    |
278 |         dcx: &mut DiagnosticCx<'_, '_, '_, '_,'tcx>,
    |                   ^^^^^^^^^^^^                ---- help: remove this lifetime argument
    |                   |
    |                   expected 4 lifetime arguments
    |
note: struct defined here, with 4 lifetime parameters: `'history`, `'ecx`, `'mir`, `'tcx`
   --> src/tools/miri/src/borrow_tracker/stacked_borrows/diagnostics.rs:123:12
    |
123 | pub struct DiagnosticCx<'history, 'ecx, 'mir, 'tcx> {
    |            ^^^^^^^^^^^^ --------  ----  ----  ----

error[E0621]: explicit lifetime required in the type of `dcx`
   --> src/tools/miri/src/borrow_tracker/stacked_borrows/mod.rs:285:13
    |
278 |         dcx: &mut DiagnosticCx<'_, '_, '_, '_,'tcx>,
    |              -------------------------------------- help: add explicit lifetime `'tcx` to the type of `dcx`: `&mut DiagnosticCx<'_, '_, '_, 'tcx>`
...
285 |             self.find_granting(access, tag, exposed_tags).map_err(|()| dcx.access_error(self))?;
    |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime `'tcx` required

error[E0621]: explicit lifetime required in the type of `dcx`
   --> src/tools/miri/src/borrow_tracker/stacked_borrows/mod.rs:305:17
    |
278 |         dcx: &mut DiagnosticCx<'_, '_, '_, '_,'tcx>,
    |              -------------------------------------- help: add explicit lifetime `'tcx` to the type of `dcx`: `&mut DiagnosticCx<'_, 'tcx, '_, '_>`
...
305 |                 dcx.log_invalidation(item.tag());
    |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime `'tcx` required

The first error is what I expected, but the other two are not useful: they are referring to an argument type that's not even a valid type, so they should not be shown. When doing refactoring, it can be quite hard to spot the actual errors in this sea of false errors.

So far I have not managed to reproduce this with a smaller example.

Cc @oli-obk is this a consequence of your recent work to keep compiling in more cases?

@RalfJung RalfJung added the A-diagnostics Area: Messages for errors, warnings, and lints label May 27, 2024
@oli-obk
Copy link
Contributor

oli-obk commented May 27, 2024

is this a consequence of your recent work to keep compiling in more cases?

probably ^^

I'm gonna guess due to the error all the lifetimes become something else than their default, instead of just continueing as if the extra argument was removed and the rest was normal

@oli-obk
Copy link
Contributor

oli-obk commented May 27, 2024

Not minimal, but small:

struct Foo<'a, 'b, 'c, 'd>(&'a(), &'b(), &'c(), &'d());

impl<'a, 'b, 'c, 'd>  Foo<'a, 'b, 'c, 'd> {
    fn acc(&mut self, _bar: &Bar) -> &'d() {
        todo!()
    }
}

struct Bar;

impl<'a> Bar {
    fn boom(&self, foo: &mut Foo<'_, '_, '_, '_, 'a>) -> Result<(), &'a ()> {
        self.bar().map_err(|()| foo.acc(self))?;
        Ok(())
    }
    fn bar(&self) -> Result<(), &'a ()> { todo!() }
}

fn main() {}

@oli-obk oli-obk self-assigned this May 27, 2024
@fmease fmease added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. labels May 27, 2024
Noratrieb added a commit to Noratrieb/rust that referenced this issue Jun 4, 2024
…, r=BoxyUwU

Avoid follow-up errors if the number of generic parameters already doesn't match

fixes rust-lang#125604

best reviewed commit-by-commit
@bors bors closed this as completed in 0dc6550 Jun 4, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jun 4, 2024
Rollup merge of rust-lang#125608 - oli-obk:subsequent_lifetime_errors, r=BoxyUwU

Avoid follow-up errors if the number of generic parameters already doesn't match

fixes rust-lang#125604

best reviewed commit-by-commit
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 D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

3 participants