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

checker: check option and result handling in type-casted aliases (#16988)

This commit is contained in:
Swastik Baranwal 2023-01-16 02:39:30 +05:30 committed by GitHub
parent 5f30110e2c
commit e8108f21e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 34 additions and 2 deletions

View File

@ -992,6 +992,8 @@ fn (mut c Checker) check_expr_opt_call(expr ast.Expr, ret_type ast.Type) ast.Typ
if expr.or_expr.kind != .absent {
c.check_or_expr(expr.or_expr, ret_type, ret_type.set_flag(.result))
}
} else if expr is ast.CastExpr {
c.check_expr_opt_call(expr.expr, ret_type)
}
return ret_type
}

View File

@ -0,0 +1,13 @@
vlib/v/checker/tests/alias_type_cast_option_result_unhandled_err.vv:15:12: error: ret_abc_result() returns a result, so it should have either an `or {}` block, or `!` at the end
13 | }
14 |
15 | a := Alias(ret_abc_result())
| ~~~~~~~~~~~~~~~~
16 | b := Alias(ret_abc_option())
17 | println('${a}${b}')
vlib/v/checker/tests/alias_type_cast_option_result_unhandled_err.vv:16:12: error: ret_abc_option() returns an option, so it should have either an `or {}` block, or `?` at the end
14 |
15 | a := Alias(ret_abc_result())
16 | b := Alias(ret_abc_option())
| ~~~~~~~~~~~~~~~~
17 | println('${a}${b}')

View File

@ -0,0 +1,17 @@
struct Abc {
a string
}
type Alias = Abc
fn ret_abc_result() !Abc {
return Abc{'a'}
}
fn ret_abc_option() ?Abc {
return Abc{'a'}
}
a := Alias(ret_abc_result())
b := Alias(ret_abc_option())
println('${a}${b}')

View File

@ -1,7 +1,7 @@
vlib/v/checker/tests/cast_array_to_number_err.vv:5:8: error: cannot cast array `[]u8` to `i16`
3 | fn main() {
4 | bytes := '010000150107120508d37445a0d7e5c5071980710c64310d9e12043000777369ff0424ab78b91a05164b00e50034003300'
5 | yr := i16(hx.decode(bytes.substr(6,8))?)
5 | yr := i16(hx.decode(bytes.substr(6,8))!)
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6 | println(bytes)
7 | println('yr: $yr')

View File

@ -2,7 +2,7 @@ import encoding.hex as hx
fn main() {
bytes := '010000150107120508d37445a0d7e5c5071980710c64310d9e12043000777369ff0424ab78b91a05164b00e50034003300'
yr := i16(hx.decode(bytes.substr(6,8))?)
yr := i16(hx.decode(bytes.substr(6,8))!)
println(bytes)
println('yr: $yr')
}