Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip single use lifetime lint for generated opaque types #88650

Merged
merged 2 commits into from
Sep 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion compiler/rustc_resolve/src/late/lifetimes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
}
}
}
}

Expand Down
19 changes: 19 additions & 0 deletions src/test/ui/lifetimes/issue-77175.rs
Original file line number Diff line number Diff line change
@@ -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() {}