-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
type-alias-impl-trait implicitly captures lifetime params #96996
Comments
Another place where lifetime parameters are captured implicitly is in type aliases: #![feature(type_alias_impl_trait)]
type StrRef<'a> = impl AsRef<str>;
fn define(s: &str) -> StrRef<'_> { s } This shouldn't pass as it violates the RFC. I can see several examples in the wild that rely on this behavior (disclosure: I do!). And I expect this fix to be painful for some. The main reason is that there's no easy way to indicate the an opaque type captures a lifetime paramter. The usual way to do so - type StrRef<'a> = impl AsRef<str>;
+ type StrRef<'a> = impl AsRef<str> + 'a; is just wrong; it changes the semantics and adds a probably unnecessary bounds on other captured lifetime/type parameters. See how this is a problem with return-position-impl-trait #49431. It's only solution now is to use the impl<'r, S, State, Res, Err> Service<WebRequest<'r, State>> for MiddlewareService<S>
where
S: for<'r2> Service<WebRequest<'r2, State>, Response = Res>,
{
type Response = Res;
type Future<'f> = impl Future<Output = Res> + Captures<'r> + Captures<'f> where Self: 'f;
fn call(&self, mut req: WebRequest<'r, State>) -> Self::Future<'_> {
async move { self.0.call(req).await }
}
} |
I'm wondering if we could change the meaning of |
It's not a no-op, as the implicit capturing is intersection based and type FutureT<'iter, 'future> = impl Future<Output = It<'iter>> /*+ 'future */;
fn get_iter<'iter, 'future>(this: &'iter FutIt, arg: &'future ()) -> FutureT<'iter, 'future> {
async move {
let _a = &arg;
It { _inner: this }
}
} Adapted from this forum post. |
Require lifetime bounds for opaque types in order to allow hidden types to capture said lifetimes fixes rust-lang#96996 cc `@aliemjay`
This compiles now but it shouldn't. Explicit annotation for captured lifetimes in the opaque type
impl Sized + 'a
is required by the RFC.I guess this is the root cause of #91601.
@rustbot label F-type_alias_impl_trait T-compiler
The text was updated successfully, but these errors were encountered: