Repository navigation
Incorrect lifetime bound check in async + impl_trait_in_assoc_type #114572
Copy link
Copy link
Closed
Labels
C-bugCategory: This is a bug.Category: This is a bug.F-impl_trait_in_assoc_type`#![feature(impl_trait_in_assoc_type)]``#![feature(impl_trait_in_assoc_type)]`F-type_alias_impl_trait`#[feature(type_alias_impl_trait)]``#[feature(type_alias_impl_trait)]`WG-asyncWorking group: Async & awaitWorking group: Async & await
Description
I tried this code:
#![feature(type_alias_impl_trait)]
#![feature(impl_trait_in_assoc_type)]
use std::{fmt::Display, future::Future};
pub trait SharedDisplay: Display + Send + 'static {}
pub struct Message<T: AsRef<dyn SharedDisplay>> {
inner: T,
}
pub trait AsyncPrinter: Send + Sync {
type AsyncOutput<'a>: Future<Output = String> + Send
where
Self: 'a;
fn to_string(&self, msg: Message<Box<dyn SharedDisplay>>) -> Self::AsyncOutput<'_>;
}
struct FooPrinter;
impl AsyncPrinter for FooPrinter {
type AsyncOutput<'a> = impl Future<Output = String> + Send + 'a where Self: 'a;
fn to_string(&self, msg: Message<Box<dyn SharedDisplay>>) -> Self::AsyncOutput<'_> {
async move { msg.inner.to_string() }
}
}I expected to see this happen: The code correctly compiles
Instead, this happened: The code fails to compile with lifetime bound check errors.
More information that might be helpful
I did a bisect and found that the above code compiles in nightly-2023-03-14 but fails in nightly-2023-03-15, possibly due to #108909 but I didn't dig deep to find the root cause.
Also, by changing the implementation of the to_string function of FooPrinter by wrapping the async block with a smart pointer (see below), the code successfully compiles
fn to_string(&self, msg: Message<Box<dyn SharedDisplay>>) -> Self::AsyncOutput<'_> {
Box::pin(async move { msg.inner.to_string() })
}Let me know if more info is needed, or actually my codes fails to compile by design.
Meta
rustc --version --verbose:
rustc 1.73.0-nightly (474709a9a 2023-08-03)
binary: rustc
commit-hash: 474709a9a2a74a8bcf0055fadb335d0ca0d2d939
commit-date: 2023-08-03
host: x86_64-apple-darwin
release: 1.73.0-nightly
LLVM version: 16.0.5
Backtrace
error[E0478]: lifetime bound not satisfied
--> src/lib.rs:23:28
|
23 | type AsyncOutput<'a> = impl Future<Output = String> + Send + 'a where Self: 'a;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: lifetime parameter instantiated with the lifetime `'a` as defined here
--> src/lib.rs:23:22
|
23 | type AsyncOutput<'a> = impl Future<Output = String> + Send + 'a where Self: 'a;
| ^^
= note: but lifetime parameter must outlive the static lifetime
Metadata
Metadata
Assignees
Labels
C-bugCategory: This is a bug.Category: This is a bug.F-impl_trait_in_assoc_type`#![feature(impl_trait_in_assoc_type)]``#![feature(impl_trait_in_assoc_type)]`F-type_alias_impl_trait`#[feature(type_alias_impl_trait)]``#[feature(type_alias_impl_trait)]`WG-asyncWorking group: Async & awaitWorking group: Async & await
Activity
compiler-errors commentedon Aug 8, 2023
This is
impl_trait_in_assoc_type, not RPITIT. This doesn't have to do with #108909 -- I'll do a full bisection later.[-]Incorrect lifetime bound check in RPITIT[/-][+]Incorrect lifetime bound check in async + impl_trait_in_assoc_type[/+]compiler-errors commentedon Aug 8, 2023
searched nightlies: from nightly-2023-02-01 to nightly-2023-04-01
regressed nightly: nightly-2023-03-15
searched commit range: 22f247c...1716932
regressed commit: 669e751
bisected with cargo-bisect-rustc v0.6.5
Host triple: aarch64-unknown-linux-gnu
Reproduce with:
This regressed in #104833. Strange, but perhaps a case that #105300 didn't end up fixing? cc @aliemjay
lqd commentedon Aug 8, 2023
Note for bisection,
impl_trait_in_assoc_typewas added a month after the expectednightly-2023-03-15and points to #104833 locally.(dang, too slow)
aliemjay commentedon Aug 9, 2023
Minimized:
The hidden type is inferred to be
Output<'a> = Static<&'a str>, but that's not a well-formed type. This is not a problem for RPIT only because we skip the well-formedness check of the hidden type.Auto merge of rust-lang#121679 - lcnr:opaque-wf-check-2, r=<try>
Auto merge of rust-lang#121679 - lcnr:opaque-wf-check-2, r=oli-obk
Auto merge of #121679 - lcnr:opaque-wf-check-2, r=oli-obk
Auto merge of #121679 - lcnr:opaque-wf-check-2, r=oli-obk
Auto merge of #121679 - lcnr:opaque-wf-check-2, r=oli-obk