kurtlawrence / papyrus

(Rust) repl
MIT License
439 stars 14 forks source link

Refactor typing system to fix &mut types #21

Closed kurtlawrence closed 5 years ago

kurtlawrence commented 5 years ago

Mutably borrow types will not work with the current type system. This is because an eval() call will return a Repl with a type parameter of Data. If this type parameter is &mut then the reference lifetime will follow through, such that once given some data Data, rust thinks the lifetime is still valid for the Read return value (as it is captures Data as well).

I will have to move back to typing the reference type, NoRef, Brw, and BrwMut.

kurtlawrence commented 5 years ago

I believe I can use the current macro implementation. See the example below.

macro_rules! tst {
    (&mut $type:ty) => {{
        println!("a mutable reference type");
        stringify!($type)
        }};
    (&$type:ty) => {{
        println!("a reference type");
        stringify!($type)
        }};
    ($type:ty) => {{
        println!("just a type");
        stringify!($type)
        }};
}

fn main() {
    dbg!(tst!(String));
    dbg!(tst!(&String));
    dbg!(tst!(&mut String));

    dbg!(tst!(&mut String));
    dbg!(tst!(&&String));
    dbg!(tst!(&mut &mut String));
}

Rust's macros can give me the information about reference type, by matching a pattern of &mut Type or & Type above just Type. The system doesn't hold up well with double references. Might have to test that later.

kurtlawrence commented 5 years ago

fixed on dev branch