The following code expand from a macro which use crate ctor and compiles,
pub struct optv(pub &'static dyn Deref<Target = &'static OptVal>);
static V3: flags::optv = ::comn::flags::optv({
static mut optxx___rust_ctor___storage: Option<&'static ::comn::flags::OptVal> = None;
#[doc(hidden)]
#[allow(non_camel_case_types)]
struct optxx<T> {
_data: core::marker::PhantomData<T>,
}
static optxx: optxx<&'static ::comn::flags::OptVal> = optxx {
_data: core::marker::PhantomData::<&'static ::comn::flags::OptVal>,
};
impl core::ops::Deref for optxx<&'static ::comn::flags::OptVal> {
type Target = &'static ::comn::flags::OptVal;
fn deref(&self) -> &'static &'static ::comn::flags::OptVal {
unsafe { optxx___rust_ctor___storage.as_ref().unwrap() }
}
}
#[used]
#[allow(non_upper_case_globals)]
#[link_section = ".init_array"]
static optxx___rust_ctor___ctor: unsafe extern "C" fn() = {
#[link_section = ".text.startup"]
unsafe extern "C" fn initer() {
optxx___rust_ctor___storage = Some({
static x1: ::comn::flags::OptVal = ::comn::flags::OptVal::new();
&x1
});
};
initer
};
&optxx
});
However, I emulate it to the following, it can't be compiled:
struct y(&'static i32);
struct x(i32);
impl Deref for x {
type Target = i32;
fn deref(&self) ->&'static i32 { &9 }
}
static i: y = y({
static z: x = x(0);
&z
});
reporting
error[E0015]: cannot perform deref coercion on `x` in statics
--> src/main.rs:16:5
|
16 | &z
| ^^
|
= note: attempting to deref into `i32`
While, I can't found the difference.....
Why the first deref &optxx to &'static OptVal is allowed while the latter is forbidden