diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index fcf03d3138..423914ff75 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -2126,8 +2126,8 @@ fn (mut g Gen) expr_with_cast(expr ast.Expr, got_type_raw ast.Type, expected_typ g.expr(expr) return } - if exp_sym.info is ast.Interface && got_type.idx() != expected_type.idx() - && !expected_type.has_flag(.optional) { + if got_sym.info !is ast.Interface && exp_sym.info is ast.Interface + && got_type.idx() != expected_type.idx() && !expected_type.has_flag(.optional) { if expr is ast.StructInit && !got_type.is_ptr() { g.inside_cast_in_heap++ got_styp := g.cc_type(got_type.ref(), true) diff --git a/vlib/v/tests/generics_interface_with_non_generic_method_test.v b/vlib/v/tests/generics_interface_with_non_generic_method_test.v index 3d95b533bf..4a2e4567c8 100644 --- a/vlib/v/tests/generics_interface_with_non_generic_method_test.v +++ b/vlib/v/tests/generics_interface_with_non_generic_method_test.v @@ -13,7 +13,7 @@ enum NodeType { struct EnumTypeFactory {} -fn (f EnumTypeFactory) get_type(type_name string) NodeType { +fn (f &EnumTypeFactory) get_type(type_name string) NodeType { return match type_name { 'expression' { NodeType.expression } 'statement' { NodeType.statement } @@ -22,24 +22,34 @@ fn (f EnumTypeFactory) get_type(type_name string) NodeType { } } +struct RawNode { + type_name string +} + struct Node { factory TypeFactory type_name NodeType + raw_node RawNode } -fn new_node(type_name string, factory TypeFactory) Node { +fn new_node(factory TypeFactory, raw_node RawNode) ?Node { return Node{ factory: factory - type_name: factory.get_type(type_name) + type_name: factory.get_type(raw_node.type_name) + raw_node: raw_node } } -fn test_generic_interface_with_non_generic_method() { - root1 := new_node('literal', EnumTypeFactory{}) +fn program(factory TypeFactory) ? { + root1 := new_node(factory, RawNode{'literal'})? println(root1) assert root1.type_name == .literal - root2 := new_node('expression', root1.factory) + root2 := new_node(root1.factory, RawNode{'expression'})? println(root2) assert root2.type_name == .expression } + +fn test_generic_interface_with_non_generic_method() ? { + program(&EnumTypeFactory{})? +}