diff options
author | Rico Tzschichholz <ricotz@ubuntu.com> | 2020-07-11 18:29:42 +0200 |
---|---|---|
committer | Rico Tzschichholz <ricotz@ubuntu.com> | 2020-07-26 10:45:12 +0200 |
commit | 3bbf1ec226da6daf8b57407293a79ed02d38cd18 (patch) | |
tree | e0d98d0e5efaffa356eafb26e71604eb6003c15b | |
parent | 8d2cb0377652428e413bfb55e5081c88fa2d97bd (diff) | |
download | vala-3bbf1ec226da6daf8b57407293a79ed02d38cd18.tar.gz |
codegen: Properly compare string if binary-expression contains string-literal
Found by -Werror=address
-rw-r--r-- | codegen/valaccodebasemodule.vala | 9 | ||||
-rw-r--r-- | tests/Makefile.am | 1 | ||||
-rw-r--r-- | 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 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<G> { + public Foo (G g) { + assert (g == "foo"); + } +} + +void main () { + var s = "foo"; + var foo = new Foo<string> (s); +} |