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

checker: check casting voidptr to generic struct (fix #15618) (#15626)

This commit is contained in:
yuyi 2022-09-01 14:22:37 +08:00 committed by GitHub
parent f2f6ea5969
commit 5e8f9b11c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 40 additions and 7 deletions

View File

@ -140,7 +140,7 @@ fn worker_for_string_content(p &pool.PoolProcessor, idx int, worker_id int) &Sig
return sr return sr
} }
struct SignResult { pub struct SignResult {
mut: mut:
item string item string
result bool result bool

View File

@ -12,7 +12,7 @@ for each input item. Example:
```v ```v
import sync.pool import sync.pool
struct SResult { pub struct SResult {
s string s string
} }

View File

@ -2367,7 +2367,7 @@ pub fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type {
from_sym := c.table.sym(from_type) from_sym := c.table.sym(from_type)
final_from_sym := c.table.final_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 to_sym := c.table.sym(to_type) // type to be used as cast
mut final_to_sym := c.table.final_sym(to_type) 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 node.typname = c.table.sym(node.typ).name
return to_type return node.typ
} }
fn (mut c Checker) at_expr(mut node ast.AtExpr) ast.Type { fn (mut c Checker) at_expr(mut node ast.AtExpr) ast.Type {

View File

@ -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<T>(ptr voidptr) T {
10 | return T(ptr)
| ~~~~~~
11 | }
12 |

View File

@ -0,0 +1,19 @@
struct Foo {
x int
}
fn unwrap_concrete(ptr voidptr) Foo {
return Foo(ptr)
}
fn unwrap_generic<T>(ptr voidptr) T {
return T(ptr)
}
pub fn main() {
foo1 := unwrap_concrete(voidptr(0))
foo2 := unwrap_generic<Foo>(voidptr(0))
println(foo1)
println(foo2)
}

View File

@ -43,7 +43,7 @@ fn string_array_to_map(a []string) map[string]bool {
return res return res
} }
struct Gen { pub struct Gen {
pref &pref.Preferences pref &pref.Preferences
field_data_type ast.Type // cache her to avoid map lookups field_data_type ast.Type // cache her to avoid map lookups
module_built string module_built string

View File

@ -14,7 +14,7 @@ interface Interface<T> {
fn test_infer_generic_interface() { fn test_infer_generic_interface() {
s := Struct<u32>{7, 5} s := Struct<u32>{7, 5}
println(s) println(s)
i := Interface(s) i := Interface<u32>(s)
println(i) println(i)
assert i.method() == 10 assert i.method() == 10
} }