Skip to content

Commit

Permalink
checker: prevent a compiler panic, while running v -check file.v on…
Browse files Browse the repository at this point in the history
… files with parser errors (fix vlang#22981) (vlang#22982)
  • Loading branch information
spytheman authored Nov 27, 2024
1 parent 8bc94dd commit 4897d78
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
4 changes: 4 additions & 0 deletions vlib/v/checker/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
59 changes: 59 additions & 0 deletions vlib/v/checker/tests/with_check_option/v_import_const.out
Original file line number Diff line number Diff line change
@@ -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 | }
5 changes: 5 additions & 0 deletions vlib/v/checker/tests/with_check_option/v_import_const.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import abc.def { const, global }
import xyz.qwe

fn abc() {
}

0 comments on commit 4897d78

Please sign in to comment.