1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: fix missing check for wrong assignment: non-option to option type (#17628)

This commit is contained in:
Felipe Pena 2023-03-15 15:24:36 -03:00 committed by GitHub
parent d290f432d1
commit 39e80afab0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 0 deletions

View File

@ -208,6 +208,10 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
} else {
// Make sure the variable is mutable
c.fail_if_immutable(left)
if !is_blank_ident && !left_type.has_flag(.option) && right_type.has_flag(.option) {
c.error('cannot assign an Option value to a non-option variable', right.pos())
}
// left_type = c.expr(left)
// if right is ast.None && !left_type.has_flag(.option) {
// println(left_type)

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/option_var_assign_err.vv:7:12: error: cannot assign an Option value to a non-option variable
5 |
6 | if value == 1 {
7 | result = trying_to_change_non_opt_to_option(0)
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 | }
9 | return result

View File

@ -0,0 +1,14 @@
module main
fn trying_to_change_non_opt_to_option(value int) ?int {
mut result := 122
if value == 1 {
result = trying_to_change_non_opt_to_option(0)
}
return result
}
fn main() {
println(trying_to_change_non_opt_to_option(0))
}