lambdaclass / concrete

Concrete is a simple programming language specifically crafted for creating highly scalable systems that are reliable, efficient, and easy to maintain.
Apache License 2.0
123 stars 11 forks source link

Found a case where compiler should throw an Error but panics #134

Open kenarab opened 4 months ago

kenarab commented 4 months ago
mod LinearExampleStub {

    struct Linear {
        x: i32,
        y: i32,
    }

    fn main() -> i32 {
        let mut xy: Linear = 
                    Linear {
                        x: 0,
                        y: 1,
                    };
        // linear value is written/consumed
        consume_x(&xy);
        return xy.x;
    }

    fn consume_x(value: & mut Linear) {
        value.x = value.x + 1;
    }

}
$cargo r build linearExample02.con --ir
   Compiling linearExample02 (linearExample02.con)
thread 'main' panicked at crates/concrete_ir/src/lib.rs:475:38:
not yet implemented
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Lines 474-475 of lib.rs

            TyKind::Param { .. } => todo!(),
            TyKind::Struct { .. } => todo!(),

Changing parameter as non mutable yields an expected error conducting to make the parameter mutable. So this new feature (checking mutable types) should implement mutable parameters for being able to cover borrow checking part of LinearityCheck

mod LinearExampleStub {

    struct Linear {
        x: i32,
        y: i32,
    }

    fn main() -> i32 {
        let mut xy: Linear = 
                    Linear {
                        x: 0,
                        y: 1,
                    };
        // linear value is written/consumed
        consume_x(&xy);
        return xy.x;
    }

    fn consume_x(value: & Linear) {
        value.x = value.x + 1;
    }

}
$cargo r build linearExample02.con --ir
   Compiling linearExample02 (linearExample02.con)
[NotMutable] Error: 
    ╭─[linearExample02.con:22:3]
    │
 21 │     fn consume_x(value: & Linear) {
    │                  ──┬──  
    │                    ╰──── variable declared here
 22 │        value.x = value.x + 1;
    │        ──────────┬──────────  
    │                  ╰──────────── can't mutate this variable because it's not mutable
────╯
kenarab commented 4 months ago

This requirement was a consequence of PR #124

edg-l commented 4 months ago

Can you try with &mut on the function call and definition?

kenarab commented 4 months ago

You are correct, it runs when rewriting to

consume_x(&mut xy);

The first example panics the compiler, it is better if it gives a compilation error than panic.

kenarab commented 4 months ago

Changed issue title. Will convert the panic case into a testcase