zls icon indicating copy to clipboard operation
zls copied to clipboard

Support for type detection for if expression

Open sgwong opened this issue 1 year ago • 0 comments

zls version: 0.11.0-dev.599+ed85a30

the zls version above doesn't support for certain type detection for if expression, check the sample below.

const a: u32 = 5;
const b: u32 = a + 5;
const at = true;
const bt = false;
const StA = struct {
    a: u32,
    b: bool,
    pub fn init() StA {
        return StA{ .a = 5, .b = true };
    }
};
fn StB() type {
    return struct { a: u32, b: bool };
}
const StC = struct {
    a: u32,
    b: bool,

    pub fn init() StC {
        return StC{ .a = 5, .b = true };
    }

    pub fn init2() StC {
        return StC{ .a = 6, .b = true };
    }
};

const strA = "abc";
const strB = "abc";

const f = if (b > 5) a else b; //this works: f:u32

const c = if (b > 5) true else false; //not working

const d = if (b > 5) a else 5; //not working

const e = if (b > 5) strA else strB; //not working

const ct = if (b > 5) at else bt; //not working

const cst = if (b > 5) StA{ .a = 5, .b = true } else StA{ .a = 6, .b = false }; //this works: Cst: StA

const dst = if (b > 5) StA.init() else StA{ .a = 6, .b = false }; //this works: dst: StA

const est = if (b > 5) StC{ .a = 6, .b = false } else StC.init(); //this works: est: StC

const fst = if (b > 5) StC.init() else StC.init2(); //this works: fst: StC

const gst = StC.init();
const hst = StC.init2();

const ist = if (b > 5) StC.init() else gst; //this works: ist: StC

const jst = if (b > 5) StB(){ .a = 5, .b = true } else StB(){ .a = 6, .b = false }; //this works: jst: StB()

pub fn main() void {
    _ = f;
    _ = c;
    _ = d;
    _ = e;
    _ = ct;
    _ = cst;
    _ = dst;
    _ = est;
    _ = fst;
    _ = gst;
    _ = hst;
    _ = ist;
    _ = jst;
}

sgwong avatar Jul 21 '23 05:07 sgwong