Skip to content

Commit a06c1ca

Browse files
committed
Auto merge of #156839 - qaijuang:fix-single-use-lifetimes-impl-bounds, r=mu001999
[single_use_lifetimes]: Account for lifetime bounds This PR makes `single_use_lifetimes` account for bounds on lifetime parameters, so a lifetime such as `'b` in `<'a, 'b: 'a>` is not treated as single-use just because its bound is stored separately from ordinary lifetime uses. Fixes #153836
2 parents 06b13d5 + ccb9e9e commit a06c1ca

7 files changed

Lines changed: 57 additions & 47 deletions

File tree

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3630,6 +3630,9 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
36303630
};
36313631
match use_set {
36323632
Some(LifetimeUseSet::Many) => {}
3633+
// A lifetime bound is a real use of that lifetime parameter, even
3634+
// though visiting a bound like `'b: 'a` only records a use of `'a`.
3635+
Some(LifetimeUseSet::One { .. }) if !param.bounds.is_empty() => {}
36333636
Some(LifetimeUseSet::One { use_span, use_ctxt }) => {
36343637
let param_ident = param.ident;
36353638
let deletion_span =

tests/ui/single-use-lifetime/issue-104440.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//@ check-pass
2+
13
#![feature(decl_macro, rustc_attrs)]
24
#![deny(single_use_lifetimes)]
35

@@ -35,7 +37,7 @@ mod type_params {
3537

3638
mod lifetime_params {
3739
macro m($a:lifetime) {
38-
fn f<'b, 'c, $a: 'b, 'a: 'c>(t1: &$a(), t2: &'a ()) -> (&'b (), &'c ()) { //~ ERROR lifetime parameter `'a` only used once
40+
fn f<'b, 'c, $a: 'b, 'a: 'c>(t1: &$a(), t2: &'a ()) -> (&'b (), &'c ()) {
3941
(t1, t2)
4042
}
4143
}
@@ -60,7 +62,7 @@ mod lifetime_params {
6062
}
6163
}
6264

63-
m!('a); //~ ERROR lifetime parameter `'a` only used once
65+
m!('a);
6466
n!('a);
6567
p!('a);
6668
}

tests/ui/single-use-lifetime/issue-104440.stderr

Lines changed: 0 additions & 28 deletions
This file was deleted.

tests/ui/single-use-lifetime/issue-117965.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//@ check-pass
2+
13
#![deny(single_use_lifetimes)]
24

35
pub enum Data<'a> {
@@ -7,7 +9,6 @@ pub enum Data<'a> {
79

810
impl<'a> Data<'a> {
911
pub fn get<'b: 'a>(&'b self) -> &'a str {
10-
//~^ ERROR lifetime parameter `'b` only used once
1112
match &self {
1213
Self::Borrowed(val) => val,
1314
Self::Owned(val) => &val,

tests/ui/single-use-lifetime/issue-117965.stderr

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Regression test for https://github.com/rust-lang/rust/issues/153836.
2+
#![deny(single_use_lifetimes)]
3+
#![allow(dead_code)]
4+
#![allow(unused_variables)]
5+
6+
struct Foo<'a>(&'a i32);
7+
struct Bar<'b>(&'b i32);
8+
9+
fn function<'a, 'b: 'a>(_: Foo<'a>, _: Bar<'b>) {}
10+
11+
type FnPtr = for<'a, 'b: 'a> fn(Foo<'a>, Bar<'b>);
12+
//~^ ERROR bounds cannot be used in this context
13+
14+
trait WhereBound<'a, 'b> {}
15+
16+
fn where_bound<T>()
17+
where
18+
T: for<'a, 'b: 'a> WhereBound<'a, 'b>,
19+
//~^ ERROR bounds cannot be used in this context
20+
{
21+
}
22+
23+
trait ImplTrait<'a> {
24+
fn foo(self, foo: Foo<'a>);
25+
}
26+
27+
impl<'a, 'b: 'a> ImplTrait<'a> for Bar<'b> {
28+
fn foo(self, foo: Foo<'a>) {
29+
let _: &'a i32 = self.0;
30+
let _: Foo<'a> = foo;
31+
}
32+
}
33+
34+
fn main() {}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: bounds cannot be used in this context
2+
--> $DIR/lifetime-bounds.rs:11:26
3+
|
4+
LL | type FnPtr = for<'a, 'b: 'a> fn(Foo<'a>, Bar<'b>);
5+
| ^^
6+
7+
error: bounds cannot be used in this context
8+
--> $DIR/lifetime-bounds.rs:18:20
9+
|
10+
LL | T: for<'a, 'b: 'a> WhereBound<'a, 'b>,
11+
| ^^
12+
13+
error: aborting due to 2 previous errors
14+

0 commit comments

Comments
 (0)