Skip to content

Commit

Permalink
parser: fix compiler error when match returns reference (#18728) (#18783
Browse files Browse the repository at this point in the history
)
  • Loading branch information
shove70 committed Jul 5, 2023
1 parent 072364f commit cd6330e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
3 changes: 2 additions & 1 deletion vlib/v/parser/if_match.v
Expand Up @@ -240,7 +240,8 @@ fn (mut p Parser) match_expr() ast.MatchExpr {
&& (((ast.builtin_type_names_matcher.matches(p.tok.lit) || p.tok.lit[0].is_capital())
&& p.peek_tok.kind != .lpar) || (p.peek_tok.kind == .dot && p.peek_token(2).lit.len > 0
&& p.peek_token(2).lit[0].is_capital()))) || p.is_only_array_type()
|| p.tok.kind == .key_fn || p.peek_token(2).kind == .amp {
|| p.tok.kind == .key_fn
|| (p.tok.kind == .lsbr && p.peek_token(2).kind == .amp) {
mut types := []ast.Type{}
for {
// Sum type match
Expand Down
29 changes: 29 additions & 0 deletions vlib/v/tests/match_return_reference_test.v
@@ -0,0 +1,29 @@
struct Foo {}

fn test_assign_ref_from_match() {
var := match 0 {
0 {
&Foo{}
}
else {
unsafe { nil }
}
}
assert var != unsafe { nil }
}

fn return_ref_from_match() &Foo {
return match 0 {
0 {
&Foo{}
}
else {
unsafe { nil }
}
}
}

fn test_return_ref_from_match() {
var := return_ref_from_match()
assert var != unsafe { nil }
}

0 comments on commit cd6330e

Please sign in to comment.