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

checker: disallow function cast outside unsafe (#16030)

This commit is contained in:
Swastik Baranwal 2022-10-11 18:16:35 +05:30 committed by GitHub
parent 5047058595
commit 05fc7d3a72
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 0 deletions

View File

@ -2546,6 +2546,12 @@ pub fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type {
c.error('casting numbers to enums, should be done inside `unsafe{}` blocks', node.pos)
}
if final_to_sym.kind == .function && final_from_sym.kind == .function && !(c.inside_unsafe
|| c.file.is_translated) && !c.check_matching_function_symbols(final_from_sym, final_to_sym) {
c.error('casting a function value from one function signature, to another function signature, should be done inside `unsafe{}` blocks',
node.pos)
}
if to_type == ast.string_type {
if from_type in [ast.u8_type, ast.bool_type] {
snexpr := node.expr.str()

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/function_cast_outside_unsafe_err.vv:6:10: error: casting a function value from one function signature, to another function signature, should be done inside `unsafe{}` blocks
4 | fn main(){
5 | f := fn(){}
6 | println(FnB(f)) // not FnA()
| ~~~~~~
7 | }

View File

@ -0,0 +1,7 @@
type FnA = fn()
type FnB = fn(int)
fn main(){
f := fn(){}
println(FnB(f)) // not FnA()
}