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_lifetimes lint has false positives with GATs #94307

Closed
Tracked by #94038
kahomayo opened this issue Feb 23, 2022 · 6 comments
Closed
Tracked by #94038

unused_lifetimes lint has false positives with GATs #94307

kahomayo opened this issue Feb 23, 2022 · 6 comments
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-GATs Area: Generic associated types (GATs) C-bug Category: This is a bug. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs F-lint-single_use_lifetimes `single_use_lifetimes` lint GATs-triaged Issues using the `generic_associated_types` feature that have been triaged

Comments

@kahomayo
Copy link

kahomayo commented Feb 23, 2022

The unused_lifetimes lint can be incorrectly triggered both by declaring and by implementing GATs.

Declaring a GAT with an unconstrained lifetime causes the lint to fire (Playground):

#![feature(generic_associated_types)]
#![warn(unused_lifetimes)]

trait Foo {
    // This GAT triggers the lint:
    type X<'x>;
    // Using the lifetime in an associated function does not avoid the lint.
    fn make_x<'a>(_x: &'a()) -> Self::X<'a>;
    fn make_x_elide(_x: &()) -> Self::X<'_>;
}

This code should be accepted (or maybe produce a lint about a lack of constraints on 'x). Right now, it produces warnings about unused lifetimes. Note that there is no way to elide the lifetime (as the lint suggests) and that removing the lifetime breaks the API for implementors and users.

warning: lifetime parameter `'x` never used
 --> src/lib.rs:6:12
  |
6 |     type X<'x>;
  |           -^^- help: elide the unused lifetime
  |

Implementing a GAT without using a lifetime causes the lint to fire:

impl Foo for () {
    type X<'x> = u8;
    fn make_x<'a>(_x: &'a ()) -> u8 { 0 }
    fn make_x_elide(_x: &()) -> u8 { 0 }
}

This code should be accepted, as there is no way for the implementor to elide the lifetime here ('_ is reserved and omitting the <'x> is a lifetime mismatch). Using the lifetime in the type would alter the meaning of the code and may break consumers.

warning: lifetime parameter `'x` never used
  --> src/lib.rs:13:12
   |
13 |     type X<'x> = u8;
   |           -^^- help: elide the unused lifetime

Meta

rustc --version --verbose:

rustc 1.61.0-nightly (68369a041 2022-02-22)
binary: rustc
commit-hash: 68369a041cea809a87e5bd80701da90e0e0a4799
commit-date: 2022-02-22
host: x86_64-pc-windows-msvc
release: 1.61.0-nightly
LLVM version: 14.0.0
@kahomayo kahomayo added the C-bug Category: This is a bug. label Feb 23, 2022
@workingjubilee workingjubilee added F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs A-diagnostics Area: Messages for errors, warnings, and lints labels Feb 23, 2022
@jackh726
Copy link
Member

This lint gets emitted in this function. This was actually mentioned in this comment, so it's good that this was filed.

To fix this, I think we probably want to add another field on Scope::Binder lint_unused_lifetimes set to true everywhere except here.

Then check_uses_for_lifetimes_defined_by_scope can just bail if that is false.

GATs issue triage: not blocking. This is more of an unused_lifetime lint issue than a GATs issue.

@jackh726 jackh726 added E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. GATs-triaged Issues using the `generic_associated_types` feature that have been triaged labels Feb 24, 2022
@chansuke
Copy link
Contributor

@rustbot claim

@nikomatsakis
Copy link
Contributor

I agree with the assessment of not blocking, would be good to fix though.

@chansuke, are you still working on this?

@chansuke
Copy link
Contributor

@nikomatsakis Sorry, I couldn't take time for this and not working now. I will take a look at this weekend but if it's urgent, I will unassign myself.

@cjgillot cjgillot added the F-lint-single_use_lifetimes `single_use_lifetimes` lint label Jun 12, 2022
@chansuke
Copy link
Contributor

@jackh726 Thank you so much for the explanation. I am working on this issue now and am trying to reproduce the warning, is it already fixed in latest version((rustc 1.63.0-nightly (b31f9cc 2022-06-15)?

@compiler-errors
Copy link
Member

I think it's fixed, I was having trouble reproducing it on nightly as well...

@fmease fmease added the A-GATs Area: Generic associated types (GATs) label Nov 2, 2024
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 A-GATs Area: Generic associated types (GATs) C-bug Category: This is a bug. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs F-lint-single_use_lifetimes `single_use_lifetimes` lint GATs-triaged Issues using the `generic_associated_types` feature that have been triaged
Projects
None yet
Development

No branches or pull requests

8 participants