Skip to content

Commit 82f1f50

Browse files
committed
Skip single use lifetime lint for generated opaque types
As reported in issue #77175, the opaque type generated by the desugaring process of an async function uses the lifetimes defined by the originating function. The definition ID for the lifetimes in the opaque method is different from the one in the originating async function and it could therefore be considered a single use of the lifetimne, 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.
1 parent 226e181 commit 82f1f50

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-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

0 commit comments

Comments
 (0)