From 405635338181596b76d2389f7ae2191e589fe748 Mon Sep 17 00:00:00 2001 From: Turiiya <34311583+ttytm@users.noreply.github.com> Date: Sun, 7 Apr 2024 16:05:13 +0200 Subject: [PATCH] parser, checker: improve the error message for an unknown type (#21207) --- vlib/v/checker/tests/is_type_not_exist.out | 41 +++++++++++++++------- vlib/v/checker/tests/is_type_not_exist.vv | 19 ++++++++++ vlib/v/parser/parse_type.v | 8 ++--- 3 files changed, 52 insertions(+), 16 deletions(-) diff --git a/vlib/v/checker/tests/is_type_not_exist.out b/vlib/v/checker/tests/is_type_not_exist.out index 3803649e8..8c7dd944b 100644 --- a/vlib/v/checker/tests/is_type_not_exist.out +++ b/vlib/v/checker/tests/is_type_not_exist.out @@ -1,14 +1,31 @@ -vlib/v/checker/tests/is_type_not_exist.vv:8:10: error: is: type `SomethingThatDontExist` does not exist - 6 | - 7 | fn fn_with_sum_type_param(i Integer) { - 8 | if i is SomethingThatDontExist { +vlib/v/checker/tests/is_type_not_exist.vv:10:10: error: is: type `testmod.SomethingThatDontExist` does not exist + 8 | + 9 | fn fn_with_sum_type_param(i Integer) { + 10 | if i is SomethingThatDontExist { | ~~~~~~~~~~~~~~~~~~~~~~ - 9 | println('It should fail !') - 10 | } -vlib/v/checker/tests/is_type_not_exist.vv:8:10: error: `Integer` has no variant `SomethingThatDontExist` - 6 | - 7 | fn fn_with_sum_type_param(i Integer) { - 8 | if i is SomethingThatDontExist { + 11 | println('It should fail !') + 12 | } +vlib/v/checker/tests/is_type_not_exist.vv:10:10: error: `testmod.Integer` has no variant `testmod.SomethingThatDontExist` + 8 | + 9 | fn fn_with_sum_type_param(i Integer) { + 10 | if i is SomethingThatDontExist { | ~~~~~~~~~~~~~~~~~~~~~~ - 9 | println('It should fail !') - 10 | } + 11 | println('It should fail !') + 12 | } +vlib/v/checker/tests/is_type_not_exist.vv:29:11: error: is: type `testmod.empty_foo` does not exist + 27 | _ := testmod.empty_foo + 28 | fb := Foobar(Foo{foo: 5}) + 29 | if fb is testmod.empty_foo{} + | ~~~~~~~ + 30 | } +vlib/v/checker/tests/is_type_not_exist.vv:29:11: error: `testmod.Foobar` has no variant `testmod.empty_foo` + 27 | _ := testmod.empty_foo + 28 | fb := Foobar(Foo{foo: 5}) + 29 | if fb is testmod.empty_foo{} + | ~~~~~~~ + 30 | } +vlib/v/checker/tests/is_type_not_exist.vv:1:1: error: project must include a `main` module or be a shared library (compile with `v -shared`) + 1 | module testmod + | ^ + 2 | + 3 | type Integer = i8 | i16 | int | i64 diff --git a/vlib/v/checker/tests/is_type_not_exist.vv b/vlib/v/checker/tests/is_type_not_exist.vv index e50859b34..6575f437c 100644 --- a/vlib/v/checker/tests/is_type_not_exist.vv +++ b/vlib/v/checker/tests/is_type_not_exist.vv @@ -1,3 +1,5 @@ +module testmod + type Integer = i8 | i16 | int | i64 fn main() { @@ -9,3 +11,20 @@ fn fn_with_sum_type_param(i Integer) { println('It should fail !') } } + +struct Foo { + foo int +} +struct Bar { + bar i8 +} + +type Foobar = Foo | Bar + +const empty_foo = Foo{foo: 0} + +fn fn_with_module_const_type_is_expr() { + _ := testmod.empty_foo + fb := Foobar(Foo{foo: 5}) + if fb is testmod.empty_foo{} +} diff --git a/vlib/v/parser/parse_type.v b/vlib/v/parser/parse_type.v index 845aca777..b3f1d50f2 100644 --- a/vlib/v/parser/parse_type.v +++ b/vlib/v/parser/parse_type.v @@ -614,13 +614,13 @@ fn (mut p Parser) parse_any_type(language ast.Language, is_ptr bool, check_dot b if mod in p.imports { p.register_used_import(mod) mod = p.imports[mod] + if p.tok.lit.len > 0 && !p.tok.lit[0].is_capital() { + p.error('imported types must start with a capital letter') + return 0 + } } // prefix with full module name = '${mod}.${p.tok.lit}' - if p.tok.lit.len > 0 && !p.tok.lit[0].is_capital() { - p.error('imported types must start with a capital letter') - return 0 - } } else if p.expr_mod != '' && !p.inside_generic_params { // p.expr_mod is from the struct and not from the generic parameter name = p.expr_mod + '.' + name -- 2.39.5