From 5e8f9b11c9f993a22c9820da38de978c959a046a Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 1 Sep 2022 14:22:37 +0800 Subject: [PATCH] checker: check casting voidptr to generic struct (fix #15618) (#15626) --- vlib/crypto/ed25519/internal/ed25519_test.v | 2 +- vlib/sync/pool/README.md | 2 +- vlib/v/checker/checker.v | 6 +++--- .../tests/cast_voidptr_to_struct_err.out | 14 ++++++++++++++ .../tests/cast_voidptr_to_struct_err.vv | 19 +++++++++++++++++++ vlib/v/gen/c/cgen.v | 2 +- vlib/v/tests/generic_interface_infer_test.v | 2 +- 7 files changed, 40 insertions(+), 7 deletions(-) create mode 100644 vlib/v/checker/tests/cast_voidptr_to_struct_err.out create mode 100644 vlib/v/checker/tests/cast_voidptr_to_struct_err.vv diff --git a/vlib/crypto/ed25519/internal/ed25519_test.v b/vlib/crypto/ed25519/internal/ed25519_test.v index adb2f088a..52da3963b 100644 --- a/vlib/crypto/ed25519/internal/ed25519_test.v +++ b/vlib/crypto/ed25519/internal/ed25519_test.v @@ -140,7 +140,7 @@ fn worker_for_string_content(p &pool.PoolProcessor, idx int, worker_id int) &Sig return sr } -struct SignResult { +pub struct SignResult { mut: item string result bool diff --git a/vlib/sync/pool/README.md b/vlib/sync/pool/README.md index bdea5b37c..fa0b68043 100644 --- a/vlib/sync/pool/README.md +++ b/vlib/sync/pool/README.md @@ -12,7 +12,7 @@ for each input item. Example: ```v import sync.pool -struct SResult { +pub struct SResult { s string } diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index bc0150b5f..27baec9a7 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -2367,7 +2367,7 @@ pub fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type { from_sym := c.table.sym(from_type) final_from_sym := c.table.final_sym(from_type) - mut to_type := node.typ + mut to_type := c.unwrap_generic(node.typ) mut to_sym := c.table.sym(to_type) // type to be used as cast mut final_to_sym := c.table.final_sym(to_type) @@ -2597,8 +2597,8 @@ pub fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type { } } } - node.typname = c.table.sym(to_type).name - return to_type + node.typname = c.table.sym(node.typ).name + return node.typ } fn (mut c Checker) at_expr(mut node ast.AtExpr) ast.Type { diff --git a/vlib/v/checker/tests/cast_voidptr_to_struct_err.out b/vlib/v/checker/tests/cast_voidptr_to_struct_err.out new file mode 100644 index 000000000..da1a8e0e9 --- /dev/null +++ b/vlib/v/checker/tests/cast_voidptr_to_struct_err.out @@ -0,0 +1,14 @@ +vlib/v/checker/tests/cast_voidptr_to_struct_err.vv:6:9: error: cannot cast `voidptr` to struct + 4 | + 5 | fn unwrap_concrete(ptr voidptr) Foo { + 6 | return Foo(ptr) + | ~~~~~~~~ + 7 | } + 8 | +vlib/v/checker/tests/cast_voidptr_to_struct_err.vv:10:9: error: cannot cast `voidptr` to struct + 8 | + 9 | fn unwrap_generic(ptr voidptr) T { + 10 | return T(ptr) + | ~~~~~~ + 11 | } + 12 | diff --git a/vlib/v/checker/tests/cast_voidptr_to_struct_err.vv b/vlib/v/checker/tests/cast_voidptr_to_struct_err.vv new file mode 100644 index 000000000..52b681bf4 --- /dev/null +++ b/vlib/v/checker/tests/cast_voidptr_to_struct_err.vv @@ -0,0 +1,19 @@ +struct Foo { + x int +} + +fn unwrap_concrete(ptr voidptr) Foo { + return Foo(ptr) +} + +fn unwrap_generic(ptr voidptr) T { + return T(ptr) +} + +pub fn main() { + foo1 := unwrap_concrete(voidptr(0)) + foo2 := unwrap_generic(voidptr(0)) + + println(foo1) + println(foo2) +} diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index b481945fc..733ff4655 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -43,7 +43,7 @@ fn string_array_to_map(a []string) map[string]bool { return res } -struct Gen { +pub struct Gen { pref &pref.Preferences field_data_type ast.Type // cache her to avoid map lookups module_built string diff --git a/vlib/v/tests/generic_interface_infer_test.v b/vlib/v/tests/generic_interface_infer_test.v index 44efe985d..75300da28 100644 --- a/vlib/v/tests/generic_interface_infer_test.v +++ b/vlib/v/tests/generic_interface_infer_test.v @@ -14,7 +14,7 @@ interface Interface { fn test_infer_generic_interface() { s := Struct{7, 5} println(s) - i := Interface(s) + i := Interface(s) println(i) assert i.method() == 10 } -- 2.30.2