parser: fix match expr case with struct init (#14538)

yuyi 2022-05-27 23:51:40 +08:00 committed by Chewing_Bever
parent 31efb48fc5
commit 740a862dcd
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
2 changed files with 33 additions and 3 deletions

View File

@ -2381,9 +2381,10 @@ pub fn (mut p Parser) name_expr() ast.Expr {
&& !p.inside_match_case && (!p.inside_if || p.inside_select) && !p.inside_match_case && (!p.inside_if || p.inside_select)
&& (!p.inside_for || p.inside_select) && !known_var { && (!p.inside_for || p.inside_select) && !known_var {
return p.struct_init(p.mod + '.' + p.tok.lit, false) // short_syntax: false return p.struct_init(p.mod + '.' + p.tok.lit, false) // short_syntax: false
} else if p.peek_tok.kind == .lcbr && p.inside_if && lit0_is_capital && !known_var } else if p.peek_tok.kind == .lcbr
&& language == .v { && ((p.inside_if && lit0_is_capital && !known_var && language == .v)
// if a == Foo{} {...} || (p.inside_match_case && p.tok.kind == .name && p.peek_tok.pos - p.tok.pos == p.tok.len)) {
// `if a == Foo{} {...}` or `match foo { Foo{} {...} }`
return p.struct_init(p.mod + '.' + p.tok.lit, false) return p.struct_init(p.mod + '.' + p.tok.lit, false)
} else if p.peek_tok.kind == .dot && (lit0_is_capital && !known_var && language == .v) { } else if p.peek_tok.kind == .dot && (lit0_is_capital && !known_var && language == .v) {
// T.name // T.name

View File

@ -0,0 +1,29 @@
struct Get {}
struct Post {}
struct Part {
value string
}
type RoutePart = Get | Part | Post
fn test_match_case_with_struct_init() {
route := [RoutePart(Get{}), RoutePart(Part{
value: '/'
})]
status_code := match route {
[RoutePart(Get{}), RoutePart(Part{
value: '/'
})] {
'200'
}
else {
'404'
}
}
print(status_code)
assert status_code == '200'
}