diff --git a/vlib/v/checker/match.v b/vlib/v/checker/match.v index 15cf38b143..d9f670428c 100644 --- a/vlib/v/checker/match.v +++ b/vlib/v/checker/match.v @@ -236,7 +236,11 @@ fn (mut c Checker) match_exprs(mut node ast.MatchExpr, cond_type_sym ast.TypeSym if expr_type !in cond_type_sym.info.variants { expr_str := c.table.type_to_str(expr_type) expect_str := c.table.type_to_str(node.cond_type) - c.error('`$expect_str` has no variant `$expr_str`', expr.pos()) + sumtype_variant_names := cond_type_sym.info.variants.map(c.table.type_to_str_using_aliases(it, + {})) + suggestion := util.new_suggestion(expr_str, sumtype_variant_names) + c.error(suggestion.say('`$expect_str` has no variant `$expr_str`'), + expr.pos()) } } else if cond_type_sym.info is ast.Alias && expr_type_sym.info is ast.Struct { expr_str := c.table.type_to_str(expr_type) diff --git a/vlib/v/checker/tests/amod/amod.v b/vlib/v/checker/tests/amod/amod.v new file mode 100644 index 0000000000..5392fc0f2b --- /dev/null +++ b/vlib/v/checker/tests/amod/amod.v @@ -0,0 +1,5 @@ +module amod + +pub struct Xyz {} + +pub struct Bcg {} diff --git a/vlib/v/checker/tests/match_invalid_type.out b/vlib/v/checker/tests/match_invalid_type.out index 4c6b98b836..fc7f5f8a57 100644 --- a/vlib/v/checker/tests/match_invalid_type.out +++ b/vlib/v/checker/tests/match_invalid_type.out @@ -1,4 +1,5 @@ -vlib/v/checker/tests/match_invalid_type.vv:5:3: error: `IoS` has no variant `byte` +vlib/v/checker/tests/match_invalid_type.vv:5:3: error: `IoS` has no variant `byte`. +2 possibilities: `int`, `string`. 3 | fn sum() { 4 | match IoS(1) { 5 | byte { diff --git a/vlib/v/checker/tests/sumtype_has_no_variant_suggestion.out b/vlib/v/checker/tests/sumtype_has_no_variant_suggestion.out new file mode 100644 index 0000000000..6697412807 --- /dev/null +++ b/vlib/v/checker/tests/sumtype_has_no_variant_suggestion.out @@ -0,0 +1,8 @@ +vlib/v/checker/tests/sumtype_has_no_variant_suggestion.vv:14:5: error: `Abc` has no variant `amod.NonExisting`. +5 possibilities: `amod.Bcg`, `amod.Xyz`, `AnotherStruct`, `Struct1`, `ThirdStruct`. + 12 | a := Abc(Struct1{}) + 13 | match a { + 14 | x.NonExisting { println('----') } + | ~~~~~~~~~~~ + 15 | else {} + 16 | } diff --git a/vlib/v/checker/tests/sumtype_has_no_variant_suggestion.vv b/vlib/v/checker/tests/sumtype_has_no_variant_suggestion.vv new file mode 100644 index 0000000000..240c1fa415 --- /dev/null +++ b/vlib/v/checker/tests/sumtype_has_no_variant_suggestion.vv @@ -0,0 +1,17 @@ +import v.checker.tests.amod as x + +struct Struct1 {} + +struct AnotherStruct {} + +struct ThirdStruct {} + +type Abc = AnotherStruct | Struct1 | ThirdStruct | x.Bcg | x.Xyz + +fn main() { + a := Abc(Struct1{}) + match a { + x.NonExisting { println('----') } + else {} + } +}