Skip to content

Commit 1d9b32a

Browse files
Rollup merge of #155346 - GrigorenkoPV:single-use-lifetimes, r=jdonszelmann
`single_use_lifetimes`: respect `anonymous_lifetime_in_impl_trait` Tracking issue: #44752 (for the `single_use_lifetimes` lint; the `anonymous_lifetime_in_impl_trait` feature seems to have no tracking issue). Closes #129255 Closes #135550 @rustbot label A-diagnostics A-lifetimes A-lints D-incorrect L-single_use_lifetimes
2 parents d839949 + 103923f commit 1d9b32a

4 files changed

Lines changed: 80 additions & 3 deletions

File tree

compiler/rustc_resolve/src/late.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,9 @@ enum LifetimeRibKind {
365365

366366
/// This rib acts as a barrier to forbid reference to lifetimes of a parent item.
367367
Item,
368+
369+
/// Lifetimes cannot be elided in `impl Trait` types without `#![feature(anonymous_lifetime_in_impl_trait)]`.
370+
ImplTrait,
368371
}
369372

370373
#[derive(Copy, Clone, Debug)]
@@ -941,7 +944,7 @@ impl<'ast, 'ra, 'tcx> Visitor<'ast> for LateResolutionVisitor<'_, 'ast, 'ra, 'tc
941944
}
942945
TyKind::ImplTrait(..) => {
943946
let candidates = self.lifetime_elision_candidates.take();
944-
visit::walk_ty(self, ty);
947+
self.with_lifetime_rib(LifetimeRibKind::ImplTrait, |this| visit::walk_ty(this, ty));
945948
self.lifetime_elision_candidates = candidates;
946949
}
947950
TyKind::TraitObject(bounds, ..) => {
@@ -1353,6 +1356,7 @@ impl<'ast, 'ra, 'tcx> Visitor<'ast> for LateResolutionVisitor<'_, 'ast, 'ra, 'tc
13531356
LifetimeRibKind::AnonymousCreateParameter { .. }
13541357
| LifetimeRibKind::AnonymousReportError
13551358
| LifetimeRibKind::StaticIfNoLifetimeInScope { .. }
1359+
| LifetimeRibKind::ImplTrait
13561360
| LifetimeRibKind::Elided(_)
13571361
| LifetimeRibKind::ElisionFailure
13581362
| LifetimeRibKind::ConcreteAnonConst(_)
@@ -1780,6 +1784,15 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
17801784
LifetimeRibKind::ConcreteAnonConst(_) => {
17811785
span_bug!(ident.span, "unexpected rib kind: {:?}", rib.kind)
17821786
}
1787+
1788+
LifetimeRibKind::ImplTrait => {
1789+
if self.r.tcx.features().anonymous_lifetime_in_impl_trait()
1790+
{
1791+
None
1792+
} else {
1793+
Some(LifetimeUseSet::Many)
1794+
}
1795+
}
17831796
})
17841797
.unwrap_or(LifetimeUseSet::Many);
17851798
debug!(?use_ctxt, ?use_set);
@@ -1819,6 +1832,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
18191832
| LifetimeRibKind::Generics { .. }
18201833
| LifetimeRibKind::ElisionFailure
18211834
| LifetimeRibKind::AnonymousReportError
1835+
| LifetimeRibKind::ImplTrait
18221836
| LifetimeRibKind::StaticIfNoLifetimeInScope { .. } => {}
18231837
}
18241838
}
@@ -2000,7 +2014,9 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
20002014
return;
20012015
}
20022016
LifetimeRibKind::Item => break,
2003-
LifetimeRibKind::Generics { .. } | LifetimeRibKind::ConstParamTy => {}
2017+
LifetimeRibKind::Generics { .. }
2018+
| LifetimeRibKind::ConstParamTy
2019+
| LifetimeRibKind::ImplTrait => {}
20042020
LifetimeRibKind::ConcreteAnonConst(_) => {
20052021
// There is always an `Elided(LifetimeRes::Infer)` inside an `AnonConst`.
20062022
span_bug!(lifetime.ident.span, "unexpected rib kind: {:?}", rib.kind)
@@ -2323,7 +2339,9 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
23232339
}
23242340
break;
23252341
}
2326-
LifetimeRibKind::Generics { .. } | LifetimeRibKind::ConstParamTy => {}
2342+
LifetimeRibKind::Generics { .. }
2343+
| LifetimeRibKind::ConstParamTy
2344+
| LifetimeRibKind::ImplTrait => {}
23272345
LifetimeRibKind::ConcreteAnonConst(_) => {
23282346
// There is always an `Elided(LifetimeRes::Infer)` inside an `AnonConst`.
23292347
span_bug!(elided_lifetime_span, "unexpected rib kind: {:?}", rib.kind)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//@ revisions: with-gate without-gate
2+
//@ [with-gate] run-rustfix
3+
//@ [without-gate] check-pass
4+
5+
#![cfg_attr(with_gate, feature(anonymous_lifetime_in_impl_trait))]
6+
7+
#![deny(single_use_lifetimes)]
8+
9+
// https://github.com/rust-lang/rust/issues/153836
10+
11+
fn foo<'a>(x: impl IntoIterator<Item = &'a i32>) {
12+
//[with-gate]~^ ERROR: lifetime parameter `'a` only used once [single_use_lifetimes]
13+
for i in x {
14+
dbg!(i);
15+
}
16+
}
17+
18+
fn main() {
19+
foo(&[1, 2, 3]);
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//@ revisions: with-gate without-gate
2+
//@ [with-gate] run-rustfix
3+
//@ [without-gate] check-pass
4+
5+
#![cfg_attr(with_gate, feature(anonymous_lifetime_in_impl_trait))]
6+
7+
#![deny(single_use_lifetimes)]
8+
9+
// https://github.com/rust-lang/rust/issues/153836
10+
11+
fn foo(x: impl IntoIterator<Item = &i32>) {
12+
//[with-gate]~^ ERROR: lifetime parameter `'a` only used once [single_use_lifetimes]
13+
for i in x {
14+
dbg!(i);
15+
}
16+
}
17+
18+
fn main() {
19+
foo(&[1, 2, 3]);
20+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error: lifetime parameter `'a` only used once
2+
--> $DIR/anonymous_lifetime_in_impl_trait.rs:11:8
3+
|
4+
LL | fn foo<'a>(x: impl IntoIterator<Item = &'a i32>) {
5+
| ^^ this lifetime... -- ...is used only here
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/anonymous_lifetime_in_impl_trait.rs:7:9
9+
|
10+
LL | #![deny(single_use_lifetimes)]
11+
| ^^^^^^^^^^^^^^^^^^^^
12+
help: elide the single-use lifetime
13+
|
14+
LL - fn foo<'a>(x: impl IntoIterator<Item = &'a i32>) {
15+
LL + fn foo(x: impl IntoIterator<Item = &i32>) {
16+
|
17+
18+
error: aborting due to 1 previous error
19+

0 commit comments

Comments
 (0)