diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index d30a2b6b9b..ca5627b26e 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -2776,7 +2776,7 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) { } right_sym := g.table.get_type_symbol(node.right_type) unaliased_right := if right_sym.kind == .alias { (right_sym.info as table.Alias).parent_type } else { node.right_type } - if left_type == table.ustring_type_idx && node.op != .key_in && node.op != .not_in { + if unaliased_left == table.ustring_type_idx && node.op != .key_in && node.op != .not_in { fn_name := match node.op { .plus { 'ustring_add(' @@ -2809,7 +2809,7 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) { g.write(', ') g.expr(node.right) g.write(')') - } else if left_type == table.string_type_idx && node.op !in [.key_in, .not_in] { + } else if unaliased_left == table.string_type_idx && node.op !in [.key_in, .not_in] { // `str == ''` -> `str.len == 0` optimization if node.op in [.eq, .ne] && node.right is ast.StringLiteral && (node.right as ast.StringLiteral).val == '' { diff --git a/vlib/v/tests/string_alias_test.v b/vlib/v/tests/string_alias_test.v new file mode 100644 index 0000000000..09cc485c53 --- /dev/null +++ b/vlib/v/tests/string_alias_test.v @@ -0,0 +1,17 @@ +type StrAlias = string + +fn test_alias_left_eq() { + assert StrAlias('test') == 'test' +} + +fn test_alias_right_eq() { + assert 'test' == StrAlias('test') +} + +fn test_alias_left_ne() { + assert StrAlias('test') != 'test2' +} + +fn test_alias_right_ne() { + assert 'test' != StrAlias('test2') +}