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
E0621 when converting dyn reference to dyn pointer #81474
Comments
I think the problem here is that trait object lifetimes elision rules are different for references and pointers.
The pointed types don't match, and so the compiler gives an error. However the hint it gives is wrong, you don't need to add |
It's kind of surprising (to me, at least) that even a pointer cast can't launder the lifetime, given that you'd have to do an unsafe dereference to actually obtain a value with the laundered lifetime, but this appears to be the case in general, not just for Alternatively, you can make #![allow(unused)]
trait Empty {
}
fn launder<'a>(t:*mut (dyn Empty + 'a)) -> *mut (dyn Empty + 'a) {
t
}
fn test(e: &mut dyn Empty) {
launder(e as *mut dyn Empty);
} |
I forgot the the modified version, I guess it works because It's a bit confusing how you can omit the lifetime from the cast though. |
I don't know if it's the same issue, but I would like the following to work, without putting a lifetime on struct A. #![allow(unused)]
trait Empty {}
struct Dep<'a> (&'a mut u8);
impl Empty for Dep<'_>{}
struct A {
deps: Vec<*const dyn Empty>,
}
impl A {
pub fn inject(&mut self, dep: *const dyn Empty) {
self.deps.push(dep);
}
}
fn test(dep: &Dep<'_>) {
let mut a = A{deps: vec![]};
a.inject(dep);
} |
The current output doesn't contain such a suggestion (anymore?):
Classifying this as a diagnostic issue: We should (1) add a hint explaining that the lifetime requirement was introduced by a default object lifetime and (2, optionally) suggest relaxing the object lifetime (if the relevant trait object type comes from the local crate etc.). |
I tried this code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=fa659e7e2652bb05a7e407a9925b1344
I expected to see this happen: The reference should convert to a pointer, losing all lifetime information.
Instead, this happened: Rust askes that the reference be given a static lifetime. This doesn't occur with non-dyn references. In addition, if you make a small modification to the code then it works: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=1257118244ee5aef365f12906c3b52fc
Meta
rustc --version --verbose
:The text was updated successfully, but these errors were encountered: