From: Rico Tzschichholz Date: Sat, 11 Jul 2020 16:29:42 +0000 (+0200) Subject: codegen: Properly compare string if binary-expression contains string-literal X-Git-Tag: 0.46.12~28 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3bbf1ec226da6daf8b57407293a79ed02d38cd18;p=thirdparty%2Fvala.git codegen: Properly compare string if binary-expression contains string-literal Found by -Werror=address --- diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala index 1610dad0b..0628807fd 100644 --- a/codegen/valaccodebasemodule.vala +++ b/codegen/valaccodebasemodule.vala @@ -5694,10 +5694,11 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator { } } - if (!(expr.left.value_type is NullType) - && expr.left.value_type.compatible (string_type) - && !(expr.right.value_type is NullType) - && expr.right.value_type.compatible (string_type)) { + bool is_string_comparison = !(expr.left.value_type is NullType) && expr.left.value_type.compatible (string_type) + && !(expr.right.value_type is NullType) && expr.right.value_type.compatible (string_type); + bool has_string_literal = (expr.left is StringLiteral || expr.right is StringLiteral); + + if (is_string_comparison || (has_string_literal && expr.operator != BinaryOperator.PLUS)) { if (expr.operator == BinaryOperator.PLUS) { // string concatenation if (expr.left.is_constant () && expr.right.is_constant ()) { diff --git a/tests/Makefile.am b/tests/Makefile.am index dc386d635..4f12d128b 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -565,6 +565,7 @@ TESTS = \ generics/inference-static-function.vala \ generics/integer-type-cast.vala \ generics/parameter-sizeof-initializer.vala \ + generics/string-literal-comparison.vala \ generics/type-parameter-properties.vala \ generics/bug640330.test \ generics/bug640330.vala \ diff --git a/tests/generics/string-literal-comparison.vala b/tests/generics/string-literal-comparison.vala new file mode 100644 index 000000000..b1e6c2e9a --- /dev/null +++ b/tests/generics/string-literal-comparison.vala @@ -0,0 +1,10 @@ +class Foo { + public Foo (G g) { + assert (g == "foo"); + } +} + +void main () { + var s = "foo"; + var foo = new Foo (s); +}