Repository navigation
unused_lifetimes lint has false positives with GATs #94307
Description
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
jackh726 commentedon Feb 24, 2022
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::Binderlint_unused_lifetimesset to true everywhere except here.Then
check_uses_for_lifetimes_defined_by_scopecan just bail if that is false.GATs issue triage: not blocking. This is more of an
unused_lifetimelint issue than a GATs issue.chansuke commentedon Feb 24, 2022
@rustbot claim
nikomatsakis commentedon Apr 13, 2022
I agree with the assessment of not blocking, would be good to fix though.
@chansuke, are you still working on this?
chansuke commentedon Apr 13, 2022
@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.
chansuke commentedon Jun 17, 2022
@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 commentedon Jun 17, 2022
I think it's fixed, I was having trouble reproducing it on nightly as well...