Skip to content

Commit 23afad6

Browse files
committed
Auto merge of #88650 - sapessi:issue-77175-fix, r=estebank
Skip single use lifetime lint for generated opaque types Fix: #77175 The opaque type generated by the desugaring process of an async function uses the lifetimes defined by the originating function. The DefId for the lifetimes in the opaque type are different from the ones in the originating async function - as they should be, as far as I understand, and could therefore be considered a single use lifetimes, this causes the single_use_lifetimes lint to fail compilation if explicitly denied. This fix skips the lint for lifetimes used only once in generated opaque types for an async function that are declared in the parent async function definition. More info in the comments on the original issue: 1 and 2
2 parents 6cdd42f + 0696c28 commit 23afad6

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

Diff for: compiler/rustc_resolve/src/late/lifetimes.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -2024,7 +2024,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
20242024
// ensure that we issue lints in a repeatable order
20252025
def_ids.sort_by_cached_key(|&def_id| self.tcx.def_path_hash(def_id));
20262026

2027-
for def_id in def_ids {
2027+
'lifetimes: for def_id in def_ids {
20282028
debug!("check_uses_for_lifetimes_defined_by_scope: def_id = {:?}", def_id);
20292029

20302030
let lifetimeuseset = self.lifetime_uses.remove(&def_id);
@@ -2067,6 +2067,27 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
20672067
{
20682068
continue;
20692069
}
2070+
2071+
// opaque types generated when desugaring an async function can have a single
2072+
// use lifetime even if it is explicitly denied (Issue #77175)
2073+
if let hir::Node::Item(hir::Item {
2074+
kind: hir::ItemKind::OpaqueTy(ref opaque),
2075+
..
2076+
}) = self.tcx.hir().get(parent_hir_id)
2077+
{
2078+
if opaque.origin != hir::OpaqueTyOrigin::AsyncFn {
2079+
continue 'lifetimes;
2080+
}
2081+
// We want to do this only if the liftime identifier is already defined
2082+
// in the async function that generated this. Otherwise it could be
2083+
// an opaque type defined by the developer and we still want this
2084+
// lint to fail compilation
2085+
for p in opaque.generics.params {
2086+
if defined_by.contains_key(&p.name) {
2087+
continue 'lifetimes;
2088+
}
2089+
}
2090+
}
20702091
}
20712092
}
20722093

Diff for: src/test/ui/lifetimes/issue-77175.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#[deny(single_use_lifetimes)]
2+
// edition:2018
3+
// check-pass
4+
5+
// Prior to the fix, the compiler complained that the 'a lifetime was only used
6+
// once. This was obviously wrong since the lifetime is used twice: For the s3
7+
// parameter and the return type. The issue was caused by the compiler
8+
// desugaring the async function into a generator that uses only a single
9+
// lifetime, which then the validator complained about becauase of the
10+
// single_use_lifetimes constraints.
11+
async fn bar<'a>(s1: String, s2: &'_ str, s3: &'a str) -> &'a str {
12+
s3
13+
}
14+
15+
fn foo<'a>(s1: String, s2: &'_ str, s3: &'a str) -> &'a str {
16+
s3
17+
}
18+
19+
fn main() {}

0 commit comments

Comments
 (0)