Skip to content

immutable dyn references are broken #119

Description

@matejcik

given these definitions

#[stabby::stabby(checked)]
pub trait Test {
    extern "C" fn test(&self);
}

#[stabby::stabby]
pub struct TestImpl(u32);

impl Test for TestImpl {
    extern "C" fn test(&self) {
        println!("test: {}", self.0);
    }
}

the following works fine:

type DynTest<'a> = stabby::dynptr!(&'a mut dyn Test);

fn main() {
    println!("Hello, world!");
    let mut test = TestImpl(42);
    let dyn_test: DynTest = (&mut test).into();
}

but the following is broken:

type DynTest<'a> = stabby::dynptr!(&'a dyn Test);

fn main() {
    println!("Hello, world!");
    let test = TestImpl(42);
    let dyn_test: DynTest = (&test).into();
}

compiler error:

error[E0597]: `test` does not live long enough
  --> src/main.rs:27:29
   |
26 |     let test = TestImpl(42);
   |         ---- binding `test` declared here
27 |     let dyn_test: DynTest = (&test).into();
   |                             ^^^^^^^-------
   |                             |
   |                             borrowed value does not live long enough
   |                             argument requires that `test` is borrowed for `'static`
28 | }
   | - `test` dropped here while still borrowed

speaking of, it's rather unexpected that one must always specify a lifetime or it's 'static

let dyn_test = <stabby::dynptr!(&mut dyn Test)>::from(&mut test);

should implicitly use '_ lifetime, no?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions