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:
@@ -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)
|
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 to_type == ast.string_type {
|
||||||
if from_type in [ast.u8_type, ast.bool_type] {
|
if from_type in [ast.u8_type, ast.bool_type] {
|
||||||
snexpr := node.expr.str()
|
snexpr := node.expr.str()
|
||||||
|
|||||||
@@ -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 | }
|
||||||
7
vlib/v/checker/tests/function_cast_outside_unsafe_err.vv
Normal file
7
vlib/v/checker/tests/function_cast_outside_unsafe_err.vv
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
type FnA = fn()
|
||||||
|
type FnB = fn(int)
|
||||||
|
|
||||||
|
fn main(){
|
||||||
|
f := fn(){}
|
||||||
|
println(FnB(f)) // not FnA()
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user