Skip to content

unused_lifetimes lint has false positives with GATs #94307

Closed

Description

@kahomayo

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

Activity

  1. jackh726 commented on Feb 24, 2022

    @jackh726
    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.

  2. added
    E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
    E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.
    on Feb 24, 2022
  3. chansuke commented on Feb 24, 2022

    @chansuke
    Contributor

    @rustbot claim

  4. nikomatsakis commented on Apr 13, 2022

    @nikomatsakis
    Contributor

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

    @chansuke, are you still working on this?

  5. chansuke commented on Apr 13, 2022

    @chansuke
    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.

  6. chansuke commented on Jun 17, 2022

    @chansuke
    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)?

  7. compiler-errors commented on Jun 17, 2022

    @compiler-errors
    Contributor

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

  8. added
    A-GATsArea: Generic associated types (GATs)
    on Nov 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-GATsArea: Generic associated types (GATs)A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.E-mentorCall 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. GATsL-single_use_lifetimesLint: single_use_lifetimes

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions