Skip to content

Commit

Permalink
reimplement errors in the checker
Browse files Browse the repository at this point in the history
  • Loading branch information
spytheman committed Mar 28, 2024
1 parent c5a0662 commit 85c87ab
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
9 changes: 9 additions & 0 deletions vlib/v/checker/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) {
}
}
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) {
c.error('the fn returns ${c.error_type_name(node.return_type)}, but ${c.error_type_name(node.return_type.clear_flag(.option))} is a Result alias, you can not mix them',
node.return_type_pos)
}
if node.return_type.has_flag(.result) && return_type_unaliased.has_flag(.option) {
c.error('the fn returns ${c.error_type_name(node.return_type)}, but ${c.error_type_name(node.return_type.clear_flag(.result))} is an Option alias, you can not mix them',
node.return_type_pos)
}
if node.return_type != ast.void_type {
if ct_attr_idx := node.attrs.find_comptime_define() {
sexpr := node.attrs[ct_attr_idx].ct_expr.str()
Expand Down
21 changes: 20 additions & 1 deletion vlib/v/checker/return.v
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import v.pref
// ? => Option type
// ! => Result type
// others => type `name`
@[inline]
fn (mut c Checker) error_type_name(exp_type ast.Type) string {
return if exp_type == ast.void_type.set_flag(.result) {
'Result type'
Expand All @@ -20,6 +19,11 @@ fn (mut c Checker) error_type_name(exp_type ast.Type) string {
}
}

@[inline]
fn (mut c Checker) error_unaliased_type_name(exp_type ast.Type) string {
return c.error_type_name(c.table.unaliased_type(exp_type))
}

// TODO: non deferred
fn (mut c Checker) return_stmt(mut node ast.Return) {
if c.table.cur_fn == unsafe { nil } {
Expand Down Expand Up @@ -135,6 +139,21 @@ fn (mut c Checker) return_stmt(mut node ast.Return) {
} else if exp_is_result && got_types_0_idx == ast.none_type_idx {
c.error('Option and Result types have been split, use `?` to return none', node.pos)
}
expected_fn_return_type_has_option := c.table.cur_fn.return_type.has_flag(.option)
expected_fn_return_type_has_result := c.table.cur_fn.return_type.has_flag(.result)
if exp_is_option && expected_fn_return_type_has_result {
if got_types_0_idx == ast.none_type_idx {
c.error('cannot use `none` as ${c.error_type_name(c.table.unaliased_type(c.table.cur_fn.return_type))} in return argument',
node.pos)
return
}
return
}
if exp_is_result && expected_fn_return_type_has_option {
c.error('expecting to return a ?Type, but you are returning ${c.error_type_name(expected_type)} instead',
node.pos)
return
}
if (exp_is_option
&& got_types_0_idx in [ast.none_type_idx, ast.error_type_idx, option_type_idx])
|| (exp_is_result && got_types_0_idx in [ast.error_type_idx, result_type_idx]) {
Expand Down
16 changes: 15 additions & 1 deletion vlib/v/checker/tests/option_alias_result_type_err.out
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
vlib/v/parser/tests/option_alias_result_type_err.vv:13:13: error: cannot use `!Bar`, as parent type `?Foo` is an Option
vlib/v/checker/tests/option_alias_result_type_err.vv:13:13: error: the fn returns type `!Bar`, but type `Bar` is an Option alias, you can not mix them
11 | }
12 |
13 | fn f(b Baz) !Bar {
| ~~~~
14 | match b {
15 | .nothing {
vlib/v/checker/tests/option_alias_result_type_err.vv:16:4: error: cannot use `none` as type `?Foo` in return argument
14 | match b {
15 | .nothing {
16 | return none
| ~~~~~~~~~~~
17 | }
18 | .value {
vlib/v/checker/tests/option_alias_result_type_err.vv:24:4: error: cannot use `none` as type `?Foo` in return argument
22 | }
23 | else {
24 | return none
| ~~~~~~~~~~~
25 | }
26 | }
3 changes: 0 additions & 3 deletions vlib/v/checker/tests/option_alias_result_type_err.vv
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,3 @@ fn f(b Baz) !Bar {
}
}
}

fn main() {
}

0 comments on commit 85c87ab

Please sign in to comment.