vlang / v

Simple, fast, safe, compiled language for developing maintainable software. Compiles itself in <1s with zero library dependencies. Supports automatic C => V translation. https://vlang.io
MIT License
35.5k stars 2.15k forks source link

cgen: fix sumtype with reference (fix #21721) #21730

Closed yuyi98 closed 4 days ago

yuyi98 commented 4 days ago

This PR fix sumtype with reference (fix #21721).

struct Parse {
mut:
    stack []&Element
}

struct Balise {}

struct RawText {
    s string
}

type Element = Balise | RawText

fn (mut p Parse) process_open_tag() string {
    mut last := &p.stack[0]
    if mut last is RawText {
        println(last)
        return last.s
    } else {
        return ''
    }
}

fn main() {
    mut parse := Parse{
        stack: [&RawText{'raw'}]
    }
    assert parse.process_open_tag() == 'raw'
}

PS D:\Test\v\tt1> v run .
RawText{
    s: 'raw'
}