-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Cannot coerce dyn Trait<'short> + 'long
to dyn Trait<'long> + 'long
#139457
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
Comments
Is this suggesting that I should be able to turn a trait Trait<'a, 'b> {
fn turn(&self, x: &'a str) -> &'b str;
}
impl<'a> Trait<'a, 'a> for () {
fn turn(&self, x: &'a str) -> &'a str {
x
}
}
/// Emulate the requested upcasting behavior.
///
/// For obvious reasons this is impl'd via unsafe.
fn upcast<'a, 'b>(x: Box<dyn Trait<'a, 'b> + 'static>) -> Box<dyn Trait<'a, 'static> + 'static> {
unsafe { std::mem::transmute(x) }
}
fn longer<'a>(x: &'a str) -> &'static str {
let o: Box<dyn Trait<'a, 'a>> = Box::new(());
upcast(o).turn(x)
} If this is a behavior that would only work for one lifetime for some reason, then it doesn't really feel principled. |
That’s a good example, thanks! But you could turn trait Trait<'a, 'b, 'c> {}
fn convert<'short, 'short2, 'long: 'short + 'short2, 'longer: 'long>(
obj: Box<dyn Trait<'short, 'short2, 'longer> + 'long>,
) -> Box<dyn Trait<'long, 'long, 'longer> + 'long> {
obj
} All lifetime parameters not already known to outlive |
That doesn't seem like something we can implement given that the trait system doesn't know anything about lifetime relationships (especially so in NLL), and I'm not convinced the new strategy is sound either. |
Even if it could be sound for safe code, |
I'm gonna close this b/c it doesn't seem actionable. |
Nice example, thanks! @RalfJung I think this is another soundness conflict for rust-lang/unsafe-code-guidelines#379? |
Hm, maybe? That issue was originally meant for two libraries conflicting with each other, this here is about a language feature... but I guess we already have other language features there. |
You could emulate the language feature with a library function that does a |
I tried this code:
I expected to see this happen: It compiles. The concrete type of the
dyn Trait<'short> + 'long
cannot mention'short
(as it lives for all of'long
), so it must implementTrait<'long>
also.Instead, this happened:
Meta
rustc --version
:@rustbot label A-lifetimes A-trait-objects T-types
The text was updated successfully, but these errors were encountered: