]> 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>
Sun, 26 Jul 2020 08:45:12 +0000 (10:45 +0200)
Found by -Werror=address

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

index 1610dad0b30b757975ab0c024247e1ff5070b4a2..0628807fd24abe566c81ee895be7e3c29c21cf6b 100644 (file)
@@ -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 ()) {
index dc386d6354cf519f21365474041c285e70565aa5..4f12d128ba0fe8c14cd35956bac4c4b789024aee 100644 (file)
@@ -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 (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);
+}