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

master
yuyi 2022-05-27 23:51:40 +08:00 committed by GitHub
parent f3e7f24ee6
commit a971b9a99a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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_for || p.inside_select) && !known_var {
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
&& language == .v {
// if a == Foo{} {...}
} else if p.peek_tok.kind == .lcbr
&& ((p.inside_if && lit0_is_capital && !known_var && language == .v)
|| (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)
} else if p.peek_tok.kind == .dot && (lit0_is_capital && !known_var && language == .v) {
// 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'
}