What is the difference with ctor generated and my emulation?

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;                                                                                                                                                                                          
    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() }                                                                                                                                                                                                                
    #[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();                                                                                                                                                                                                                                                                                                                                                                                                                                                                     

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);


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

The ctor code isn't performing any deref coercion at compile time. optxx is undergoing unsized coercion to a dyn Deref<Target = &'static OptVal> trait object.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.