mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker: fix sumtype cast of comptime var (#17287)
This commit is contained in:
parent
fbcb1f3711
commit
16344cfc28
@ -2688,12 +2688,26 @@ pub fn (mut c Checker) expr(node_ ast.Expr) ast.Type {
|
||||
// return ast.void_type
|
||||
// }
|
||||
|
||||
fn (mut c Checker) get_comptime_selector_type(node ast.ComptimeSelector, default_type ast.Type) ast.Type {
|
||||
if node.field_expr is ast.SelectorExpr
|
||||
&& c.check_comptime_is_field_selector(node.field_expr as ast.SelectorExpr)
|
||||
&& (node.field_expr as ast.SelectorExpr).field_name == 'name' {
|
||||
return c.comptime_fields_default_type
|
||||
}
|
||||
return default_type
|
||||
}
|
||||
|
||||
fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type {
|
||||
// Given: `Outside( Inside(xyz) )`,
|
||||
// node.expr_type: `Inside`
|
||||
// node.typ: `Outside`
|
||||
node.expr_type = c.expr(node.expr) // type to be casted
|
||||
|
||||
if node.expr is ast.ComptimeSelector {
|
||||
node.expr_type = c.get_comptime_selector_type(node.expr as ast.ComptimeSelector,
|
||||
node.expr_type)
|
||||
}
|
||||
|
||||
mut from_type := c.unwrap_generic(node.expr_type)
|
||||
from_sym := c.table.sym(from_type)
|
||||
final_from_sym := c.table.final_sym(from_type)
|
||||
|
29
vlib/v/tests/comptime_sumtype_cast_test.v
Normal file
29
vlib/v/tests/comptime_sumtype_cast_test.v
Normal file
@ -0,0 +1,29 @@
|
||||
pub type Any = bool | int | map[string]Any | string
|
||||
|
||||
struct StructType {
|
||||
mut:
|
||||
val string
|
||||
}
|
||||
|
||||
fn test_main() {
|
||||
struct_to_map_string()
|
||||
}
|
||||
|
||||
fn map_from[T](t T) {
|
||||
$if T is $Struct {
|
||||
$for field in T.fields {
|
||||
assert Any('string').str() == "Any('string')"
|
||||
assert t.$(field.name).str() == 'true'
|
||||
a := t.$(field.name)
|
||||
assert Any(a).str() == "Any('true')"
|
||||
assert Any(t.$(field.name)).str() == "Any('true')"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn struct_to_map_string() {
|
||||
struct_type := StructType{
|
||||
val: 'true'
|
||||
}
|
||||
map_from(struct_type)
|
||||
}
|
Loading…
Reference in New Issue
Block a user