diff --git a/vlib/v/checker/match.v b/vlib/v/checker/match.v index 10c61a6230..7b1d5b7c7f 100644 --- a/vlib/v/checker/match.v +++ b/vlib/v/checker/match.v @@ -198,6 +198,10 @@ fn (mut c Checker) match_exprs(mut node ast.MatchExpr, cond_type_sym ast.TypeSym c.error('struct instances cannot be matched by type name, they can only be matched to other instances of the same struct type', branch.pos) } + if mut expr is ast.TypeNode && cond_sym.is_primitive() { + c.error('matching by type can only be done for sum types, generics, interfaces, `${node.cond}` is none of those', + branch.pos) + } if mut expr is ast.RangeExpr { // Allow for `match enum_value { 4..5 { } }`, even though usually int and enum values, // are considered incompatible outside unsafe{}, and are not allowed to be compared directly diff --git a/vlib/v/checker/tests/match_type_node_with_non_sum_type_err.out b/vlib/v/checker/tests/match_type_node_with_non_sum_type_err.out new file mode 100644 index 0000000000..8e2608d250 --- /dev/null +++ b/vlib/v/checker/tests/match_type_node_with_non_sum_type_err.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/match_type_node_with_non_sum_type_err.vv:5:3: error: matching by type can only be done for sum types, generics, interfaces, `b` is none of those + 3 | + 4 | match b { + 5 | i64 { println('i64') } + | ~~~ + 6 | 100 { println(100) } + 7 | else {} diff --git a/vlib/v/checker/tests/match_type_node_with_non_sum_type_err.vv b/vlib/v/checker/tests/match_type_node_with_non_sum_type_err.vv new file mode 100644 index 0000000000..6e8570e623 --- /dev/null +++ b/vlib/v/checker/tests/match_type_node_with_non_sum_type_err.vv @@ -0,0 +1,9 @@ +fn main() { + b := i64(100) + + match b { + i64 { println('i64') } + 100 { println(100) } + else {} + } +}