Skip to content

Commit

Permalink
checker: disallow option or result return type, for infix operato…
Browse files Browse the repository at this point in the history
…r overloading (#20494)
  • Loading branch information
Delta456 committed Jan 11, 2024
1 parent 69b0d1c commit 554f21a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions vlib/v/checker/fn.v
Expand Up @@ -346,6 +346,8 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) {
sptype := c.table.type_to_str(param_type)
c.error('the receiver type `${srtype}` should be the same type as the operand `${sptype}`',
node.pos)
} else if node.return_type.has_option_or_result() {
c.error('return type cannot be Option or Result', node.return_type_pos)
}
}
}
Expand Down
@@ -0,0 +1,14 @@
vlib/v/checker/tests/operator_overloading_return_type_option_or_result.vv:3:22: error: return type cannot be Option or Result
1 | type Vec = []int
2 |
3 | fn (v Vec) + (u Vec) !Vec {
| ~~~~
4 | if v.len != u.len {
5 | return error("Operations require dim(v) == dim(u)")
vlib/v/checker/tests/operator_overloading_return_type_option_or_result.vv:10:22: error: return type cannot be Option or Result
8 | }
9 |
10 | fn (v Vec) - (u Vec) ?Vec {
| ~~~~
11 | if v.len != u.len {
12 | return none
@@ -0,0 +1,22 @@
type Vec = []int

fn (v Vec) + (u Vec) !Vec {
if v.len != u.len {
return error("Operations require dim(v) == dim(u)")
}
return Vec([v[0] + u[0], v[1] + u[1], v[2] + u[2]])
}

fn (v Vec) - (u Vec) ?Vec {
if v.len != u.len {
return none
}
return Vec([v[0] + u[0], v[1] + u[1], v[2] + u[2]])
}



fn main() {
vec := Vec([1, 2, 3])
dump(vec + vec)
}

0 comments on commit 554f21a

Please sign in to comment.