]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codegen: Properly compare string if binary-expression contains string-literal
authorRico Tzschichholz <ricotz@ubuntu.com>
Sat, 11 Jul 2020 16:29:42 +0000 (18:29 +0200)
committerRico Tzschichholz <ricotz@ubuntu.com>
Sat, 11 Jul 2020 16:29:42 +0000 (18:29 +0200)
Found by -Werror=address

codegen/valaccodebasemodule.vala
tests/Makefile.am
tests/generics/string-literal-comparison.vala [new file with mode: 0644]

index a159ecfcfccfb8c4cf9b2679c030be4d03781b96..5d70d5b495e6630a0c1bbe70e1fee1771f2bc3e1 100644 (file)
@@ -5569,10 +5569,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)) {
                        switch (expr.operator) {
                        case BinaryOperator.PLUS:
                                // string concatenation
index 3867108caf91ef54cd949bf2c1b110f7169431f1..9980e94bbe5a6e464825336ba2528ab3e4722c61 100644 (file)
@@ -594,6 +594,7 @@ TESTS = \
        generics/integer-type-cast.vala \
        generics/parameter-sizeof-initializer.vala \
        generics/member-dup-destroy.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 (file)
index 0000000..b1e6c2e
--- /dev/null
@@ -0,0 +1,10 @@
+class Foo<G> {
+       public Foo (G g) {
+               assert (g == "foo");
+       }
+}
+
+void main () {
+       var s = "foo";
+       var foo = new Foo<string> (s);
+}