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

Unused generic parameter E0392 not emitted in recursive type that is behind Cell #92561

Closed
Noratrieb opened this issue Jan 4, 2022 · 3 comments
Labels
A-variance Area: Variance (https://doc.rust-lang.org/nomicon/subtyping.html) C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users.

Comments

@Noratrieb
Copy link
Member

When you have an unused lifetime (or type) parameter, it is a hard error. If you "use" that parameter by having a recursive type,
rustc still notices that it's unused and gives the error. But if that recursion goes through Cell (also works with RefCell), the error is not emitted and the program works fine.

Maybe this should be a diagnostics issue, but I put this under bug since it causes programs to compile that shouldn't under the normal rules.

I tried this code:

playground

use std::cell::Cell;

struct GivesAnError<'a> {
    same: Box<GivesAnError<'a>>,
}

#[derive(Debug, Default)]
struct NoError<'a> {
    same: Box<Cell<NoError<'a>>>,
}

I expected to see this happen:
E0392, because the lifetime is actually not used

Instead, this happened:
No error, and the program compiled

Meta

This happens on the latest nightly-2022-01-04 on the playground

@Noratrieb Noratrieb added the C-bug Category: This is a bug. label Jan 4, 2022
@Aaron1011 Aaron1011 added the A-variance Area: Variance (https://doc.rust-lang.org/nomicon/subtyping.html) label Jan 4, 2022
@Aaron1011
Copy link
Member

This is a very subtle issue. The compiler is not actually checking if the lifetime 'a get used - it's checking if has its variance inferred. With Box<GivesAnError<'a>>, there are no constraints placed in the variance of 'a (Box is covariant). However, with Box<Cell<NoError<'a>>>, Cell ends up forcing 'a to be invariant, allowing the code to compile.

@Aaron1011 Aaron1011 added D-confusing Diagnostics: Confusing error or lint that should be reworked. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. labels Jan 4, 2022
@SNCPlay42
Copy link
Contributor

Duplicate of #74165

@Noratrieb
Copy link
Member Author

Ah, so the normal error is not really a lint but the compiler being forced to abort, that makes sense. I guess it makes sense to close this, since it's a duplicate, I couldn't find the duplicate when searching for duplicates 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-variance Area: Variance (https://doc.rust-lang.org/nomicon/subtyping.html) C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users.
Projects
None yet
Development

No branches or pull requests

3 participants