From 4f14f7714fdb402cf7c19a3a8067b9574fa30cd2 Mon Sep 17 00:00:00 2001 From: Vincenzo Palazzo Date: Sat, 16 Apr 2022 18:29:10 +0200 Subject: [PATCH] checker: ensure that the variant SubType exists (#14053) Signed-off-by: Vincenzo Palazzo --- vlib/v/checker/checker.v | 6 ++++ .../tests/undefined_type_on_sumtype.out | 13 +++++++ .../tests/undefined_type_on_sumtype.vv | 34 +++++++++++++++++++ .../check_combination_of_alias_and_sumtype.vv | 2 ++ 4 files changed, 55 insertions(+) create mode 100644 vlib/v/checker/tests/undefined_type_on_sumtype.out create mode 100644 vlib/v/checker/tests/undefined_type_on_sumtype.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index f817de85d0..8d3fcc3e50 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -4197,6 +4197,12 @@ fn (mut c Checker) ensure_type_exists(typ ast.Type, pos token.Pos) ? { c.ensure_type_exists(info.key_type, pos) ? c.ensure_type_exists(info.value_type, pos) ? } + .sum_type { + info := sym.info as ast.SumType + for concrete_typ in info.concrete_types { + c.ensure_type_exists(concrete_typ, pos) ? + } + } else {} } } diff --git a/vlib/v/checker/tests/undefined_type_on_sumtype.out b/vlib/v/checker/tests/undefined_type_on_sumtype.out new file mode 100644 index 0000000000..e664d3004a --- /dev/null +++ b/vlib/v/checker/tests/undefined_type_on_sumtype.out @@ -0,0 +1,13 @@ +vlib/v/checker/tests/undefined_type_on_sumtype.vv:1:17: error: unknown type `Token`. +Did you mean `Ok<[]Token>`? + 1 | type ParseRes = Result<[]Token, ParseErr> + | ~~~~~~~~~~~~~~~~~~~~~~~~~ + 2 | + 3 | // Token type is unknown +vlib/v/checker/tests/undefined_type_on_sumtype.vv:30:4: error: unused variable: `rx` + 28 | match r { + 29 | Some { + 30 | rx := r.value + | ~~ + 31 | } + 32 | None {} diff --git a/vlib/v/checker/tests/undefined_type_on_sumtype.vv b/vlib/v/checker/tests/undefined_type_on_sumtype.vv new file mode 100644 index 0000000000..51b836d89f --- /dev/null +++ b/vlib/v/checker/tests/undefined_type_on_sumtype.vv @@ -0,0 +1,34 @@ +type ParseRes = Result<[]Token, ParseErr> + +// Token type is unknown +//struct Token {} + +struct ParseErr {} + +type Opt = None | Some + +struct None {} + +struct Some { + value T +} + +type Result = Err | Ok + +struct Ok { + value T +} + +struct Err { + value U +} + +fn test_report() { + r := Opt(None{}) + match r { + Some { + rx := r.value + } + None {} + } +} diff --git a/vlib/v/gen/c/testdata/check_combination_of_alias_and_sumtype.vv b/vlib/v/gen/c/testdata/check_combination_of_alias_and_sumtype.vv index 7fa58f5183..1271e2b459 100644 --- a/vlib/v/gen/c/testdata/check_combination_of_alias_and_sumtype.vv +++ b/vlib/v/gen/c/testdata/check_combination_of_alias_and_sumtype.vv @@ -1,5 +1,7 @@ type ParseRes = Result<[]Token, ParseErr> +struct Token {} + struct ParseErr {} type Opt = None | Some