Skip to content

Commit d82c1c5

Browse files
committed
Avoid unused lifetime warning for lifetimes introduced when desugering async.
1 parent b505208 commit d82c1c5

File tree

3 files changed

+81
-1
lines changed

3 files changed

+81
-1
lines changed

src/librustc/middle/resolve_lifetime.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -709,15 +709,22 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
709709
match param.kind {
710710
GenericParamKind::Lifetime { .. } => {
711711
let (name, reg) = Region::early(&self.tcx.hir(), &mut index, &param);
712+
let def_id = if let Region::EarlyBound(_ ,def_id , _) = reg {
713+
def_id
714+
} else {
715+
bug!();
716+
};
712717
if let hir::ParamName::Plain(param_name) = name {
713718
if param_name.name == kw::UnderscoreLifetime {
714719
// Pick the elided lifetime "definition" if one exists
715720
// and use it to make an elision scope.
721+
self.lifetime_uses.insert(def_id.clone(), LifetimeUseSet::Many);
716722
elision = Some(reg);
717723
} else {
718724
lifetimes.insert(name, reg);
719725
}
720726
} else {
727+
self.lifetime_uses.insert(def_id.clone(), LifetimeUseSet::Many);
721728
lifetimes.insert(name, reg);
722729
}
723730
}
@@ -1623,7 +1630,6 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
16231630
_ => None,
16241631
} {
16251632
debug!("id = {:?} span = {:?} name = {:?}", id, span, name);
1626-
16271633
if name.name == kw::UnderscoreLifetime {
16281634
continue;
16291635
}
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// edition:2018
2+
3+
// Avoid spurious warnings of unused lifetime. The below async functions
4+
// are desugered to have an unused lifetime
5+
// but we don't want to warn about that as there's nothing they can do about it.
6+
7+
#![deny(unused_lifetimes)]
8+
#![allow(dead_code)]
9+
10+
pub async fn october(s: &str) {
11+
println!("{}", s);
12+
}
13+
14+
pub async fn async_fn(&mut ref s: &mut[i32]) {
15+
println!("{:?}", s);
16+
}
17+
18+
macro_rules! foo_macro {
19+
() => {
20+
pub async fn async_fn_in_macro(&mut ref _s: &mut[i32]) {}
21+
};
22+
}
23+
24+
foo_macro!();
25+
26+
pub async fn func_with_unused_lifetime<'a>(s: &'a str) {
27+
//~^ ERROR lifetime parameter `'a` never used
28+
println!("{}", s);
29+
}
30+
31+
pub async fn func_with_two_unused_lifetime<'a, 'b>(s: &'a str, t: &'b str) {
32+
//~^ ERROR lifetime parameter `'a` never used
33+
//~^^ ERROR lifetime parameter `'b` never used
34+
println!("{}", s);
35+
}
36+
37+
pub async fn func_with_unused_lifetime_in_two_params<'c>(s: &'c str, t: &'c str) {
38+
//~^ ERROR lifetime parameter `'c` never used
39+
println!("{}", s);
40+
}
41+
42+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
error: lifetime parameter `'a` never used
2+
--> $DIR/unused-lifetime.rs:26:40
3+
|
4+
LL | pub async fn func_with_unused_lifetime<'a>(s: &'a str) {
5+
| ^^
6+
|
7+
note: lint level defined here
8+
--> $DIR/unused-lifetime.rs:7:9
9+
|
10+
LL | #![deny(unused_lifetimes)]
11+
| ^^^^^^^^^^^^^^^^
12+
13+
error: lifetime parameter `'a` never used
14+
--> $DIR/unused-lifetime.rs:31:44
15+
|
16+
LL | pub async fn func_with_two_unused_lifetime<'a, 'b>(s: &'a str, t: &'b str) {
17+
| ^^
18+
19+
error: lifetime parameter `'b` never used
20+
--> $DIR/unused-lifetime.rs:31:48
21+
|
22+
LL | pub async fn func_with_two_unused_lifetime<'a, 'b>(s: &'a str, t: &'b str) {
23+
| ^^
24+
25+
error: lifetime parameter `'c` never used
26+
--> $DIR/unused-lifetime.rs:37:54
27+
|
28+
LL | pub async fn func_with_unused_lifetime_in_two_params<'c>(s: &'c str, t: &'c str) {
29+
| ^^
30+
31+
error: aborting due to 4 previous errors
32+

0 commit comments

Comments
 (0)