From 4897d7860e96c0324442ba87d711c9faab97e670 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Wed, 27 Nov 2024 03:00:21 +0200 Subject: [PATCH] checker: prevent a compiler panic, while running `v -check file.v` on files with parser errors (fix #22981) (#22982) --- vlib/v/checker/fn.v | 4 ++ .../with_check_option/v_import_const.out | 59 +++++++++++++++++++ .../tests/with_check_option/v_import_const.vv | 5 ++ 3 files changed, 68 insertions(+) create mode 100644 vlib/v/checker/tests/with_check_option/v_import_const.out create mode 100644 vlib/v/checker/tests/with_check_option/v_import_const.vv diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index cc5228a9ce3b9a..57fa7b7bd6191e 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -118,6 +118,10 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) { } } } + if node.return_type == ast.no_type { + c.error('invalid return type in fn `${node.name}`', node.pos) + return + } c.fn_return_type = node.return_type return_type_unaliased := c.table.unaliased_type(node.return_type) if node.return_type.has_flag(.option) && return_type_unaliased.has_flag(.result) { diff --git a/vlib/v/checker/tests/with_check_option/v_import_const.out b/vlib/v/checker/tests/with_check_option/v_import_const.out new file mode 100644 index 00000000000000..5943161a9a08ac --- /dev/null +++ b/vlib/v/checker/tests/with_check_option/v_import_const.out @@ -0,0 +1,59 @@ +vlib/v/checker/tests/with_check_option/v_import_const.vv:1:25: notice: script mode started here + 1 | import abc.def { const, global } + | ~~~~~~ + 2 | import xyz.qwe + 3 | +vlib/v/checker/tests/with_check_option/v_import_const.vv:1:18: error: import syntax error, please specify a valid fn or type name + 1 | import abc.def { const, global } + | ~~~~~ + 2 | import xyz.qwe + 3 | +vlib/v/checker/tests/with_check_option/v_import_const.vv:1:23: error: cannot import multiple modules at a time + 1 | import abc.def { const, global } + | ^ + 2 | import xyz.qwe + 3 | +vlib/v/checker/tests/with_check_option/v_import_const.vv:1:25: error: `global` evaluated but not used + 1 | import abc.def { const, global } + | ~~~~~~ + 2 | import xyz.qwe + 3 | +vlib/v/checker/tests/with_check_option/v_import_const.vv:1:32: error: invalid expression: unexpected token `}` + 1 | import abc.def { const, global } + | ^ + 2 | import xyz.qwe + 3 | +vlib/v/checker/tests/with_check_option/v_import_const.vv:1:32: error: expression evaluated but not used + 1 | import abc.def { const, global } + | ^ + 2 | import xyz.qwe + 3 | +vlib/v/checker/tests/with_check_option/v_import_const.vv:4:4: error: all definitions must occur before code in script mode + 2 | import xyz.qwe + 3 | + 4 | fn abc() { + | ~~~ + 5 | } +vlib/v/checker/tests/with_check_option/v_import_const.vv:2:8: error: undefined ident: `xyz` + 1 | import abc.def { const, global } + 2 | import xyz.qwe + | ~~~ + 3 | + 4 | fn abc() { +vlib/v/checker/tests/with_check_option/v_import_const.vv:2:12: error: `xyz` does not return a value + 1 | import abc.def { const, global } + 2 | import xyz.qwe + | ~~~ + 3 | + 4 | fn abc() { +vlib/v/checker/tests/with_check_option/v_import_const.vv:1:1: error: invalid return type in fn `` + 1 | import abc.def { const, global } + | ^ + 2 | import xyz.qwe + 3 | +vlib/v/checker/tests/with_check_option/v_import_const.vv:4:8: error: unknown function: + 2 | import xyz.qwe + 3 | + 4 | fn abc() { + | ^ + 5 | } diff --git a/vlib/v/checker/tests/with_check_option/v_import_const.vv b/vlib/v/checker/tests/with_check_option/v_import_const.vv new file mode 100644 index 00000000000000..41273f92eef15a --- /dev/null +++ b/vlib/v/checker/tests/with_check_option/v_import_const.vv @@ -0,0 +1,5 @@ +import abc.def { const, global } +import xyz.qwe + +fn abc() { +}