From 82f1f50335e114071fea6f5b873871b66e9c4372 Mon Sep 17 00:00:00 2001 From: Stefano Buliani Date: Sat, 4 Sep 2021 11:47:52 -0700 Subject: [PATCH 1/2] 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. --- compiler/rustc_resolve/src/late/lifetimes.rs | 23 +++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_resolve/src/late/lifetimes.rs b/compiler/rustc_resolve/src/late/lifetimes.rs index 4c1d537d55fa3..a5dbbffeaa86b 100644 --- a/compiler/rustc_resolve/src/late/lifetimes.rs +++ b/compiler/rustc_resolve/src/late/lifetimes.rs @@ -2024,7 +2024,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { // ensure that we issue lints in a repeatable order def_ids.sort_by_cached_key(|&def_id| self.tcx.def_path_hash(def_id)); - for def_id in def_ids { + 'lifetimes: for def_id in def_ids { debug!("check_uses_for_lifetimes_defined_by_scope: def_id = {:?}", def_id); let lifetimeuseset = self.lifetime_uses.remove(&def_id); @@ -2067,6 +2067,27 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { { continue; } + + // opaque types generated when desugaring an async function can have a single + // use lifetime even if it is explicitly denied (Issue #77175) + if let hir::Node::Item(hir::Item { + kind: hir::ItemKind::OpaqueTy(ref opaque), + .. + }) = self.tcx.hir().get(parent_hir_id) + { + if opaque.origin != hir::OpaqueTyOrigin::AsyncFn { + continue 'lifetimes; + } + // We want to do this only if the liftime identifier is already defined + // in the async function that generated this. Otherwise it could be + // an opaque type defined by the developer and we still want this + // lint to fail compilation + for p in opaque.generics.params { + if defined_by.contains_key(&p.name) { + continue 'lifetimes; + } + } + } } } From 0696c28430c58508e40e027f565f48ff8e0c0b34 Mon Sep 17 00:00:00 2001 From: Stefano Buliani Date: Sat, 4 Sep 2021 11:49:13 -0700 Subject: [PATCH 2/2] test for issue #77175 --- src/test/ui/lifetimes/issue-77175.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/test/ui/lifetimes/issue-77175.rs diff --git a/src/test/ui/lifetimes/issue-77175.rs b/src/test/ui/lifetimes/issue-77175.rs new file mode 100644 index 0000000000000..2282752b6c1fe --- /dev/null +++ b/src/test/ui/lifetimes/issue-77175.rs @@ -0,0 +1,19 @@ +#[deny(single_use_lifetimes)] +// edition:2018 +// check-pass + +// Prior to the fix, the compiler complained that the 'a lifetime was only used +// once. This was obviously wrong since the lifetime is used twice: For the s3 +// parameter and the return type. The issue was caused by the compiler +// desugaring the async function into a generator that uses only a single +// lifetime, which then the validator complained about becauase of the +// single_use_lifetimes constraints. +async fn bar<'a>(s1: String, s2: &'_ str, s3: &'a str) -> &'a str { + s3 +} + +fn foo<'a>(s1: String, s2: &'_ str, s3: &'a str) -> &'a str { + s3 +} + +fn main() {}