checker: fix non dereferenced enum in match statements (fixes #10045) (#20591)

This commit is contained in:
Pierre Curto 2024-01-20 06:25:22 +01:00 committed by GitHub
parent 856984aa14
commit d912268e5d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 33 additions and 0 deletions

View File

@ -300,6 +300,7 @@ fn (mut c Checker) get_comptime_number_value(mut expr ast.Expr) ?i64 {
fn (mut c Checker) match_exprs(mut node ast.MatchExpr, cond_type_sym ast.TypeSymbol) {
c.expected_type = node.expected_type
cond_sym := c.table.sym(node.cond_type)
mut enum_ref_checked := false
// branch_exprs is a histogram of how many times
// an expr was used in the match
mut branch_exprs := map[string]int{}
@ -385,6 +386,13 @@ fn (mut c Checker) match_exprs(mut node ast.MatchExpr, cond_type_sym ast.TypeSym
}
ast.EnumVal {
key = expr.val
if !enum_ref_checked {
enum_ref_checked = true
if node.cond_type.is_ptr() {
c.error('missing `*` dereferencing `${node.cond}` in match statement',
node.cond.pos())
}
}
}
else {
key = (*expr).str()

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/match_enum_ref.vv:6:3: error: missing `*` dereferencing `t` in match statement
4 | fn f(t &SomeType) ?int {
5 | return match
6 | t // note the missing asterisk
| ^
7 | {
8 | .a {

View File

@ -0,0 +1,18 @@
enum SomeType {
a
}
fn f(t &SomeType) ?int {
return match
t // note the missing asterisk
{
.a {
panic('This does not happen!')
3
}
}
}
fn main() {
t := SomeType.a
f(&t)?
assert false // should not happen, but does
}