[vala/0.48] codegen: Properly compare string if binary-expression contains string-literal



commit 7854aefca66d6ac51b54dd24090ae0c6cfb4fa5f
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Sat Jul 11 18:29:42 2020 +0200

    codegen: Properly compare string if binary-expression contains string-literal
    
    Found by -Werror=address

 codegen/valaccodebasemodule.vala              |  9 +++++----
 tests/Makefile.am                             |  1 +
 tests/generics/string-literal-comparison.vala | 10 ++++++++++
 3 files changed, 16 insertions(+), 4 deletions(-)
---
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index a29762663..c4f9647f9 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -5542,10 +5542,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
diff --git a/tests/Makefile.am b/tests/Makefile.am
index dd8ddfca3..99e4d5f6f 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -590,6 +590,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
index 000000000..b1e6c2e9a
--- /dev/null
+++ b/tests/generics/string-literal-comparison.vala
@@ -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);
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]