Patchwork Go patch committed: Fix comparisons of string and interface type

login
register
mail settings
Submitter Ian Taylor
Date Dec. 14, 2010, 11:45 p.m.
Message ID <mcrhbef7wb3.fsf@google.com>
Download mbox | patch
Permalink /patch/75587/
State New
Headers show

Comments

Ian Taylor - Dec. 14, 2010, 11:45 p.m.
This patch fixes comparisons of string and interface types.  The code
was assuming that a string type could only be compared with another
string type, which is not correct.  Bootstrapped and ran Go testsuite on
x86_64-unknown-linux-gnu.  Committed to mainline.

Ian

Patch

diff -r 440aea85b07b go/expressions.cc
--- a/go/expressions.cc	Tue Dec 14 15:32:56 2010 -0800
+++ b/go/expressions.cc	Tue Dec 14 15:42:01 2010 -0800
@@ -5938,9 +5938,8 @@ 
       gcc_unreachable();
     }
 
-  if (left_type->is_string_type())
-    {
-      gcc_assert(right_type->is_string_type());
+  if (left_type->is_string_type() && right_type->is_string_type())
+    {
       tree string_type = Type::make_string_type()->get_tree(context->gogo());
       static tree string_compare_decl;
       left_tree = Gogo::call_builtin(&string_compare_decl,
@@ -5954,13 +5953,12 @@ 
 				     right_tree);
       right_tree = build_int_cst_type(integer_type_node, 0);
     }
-
-  if ((left_type->interface_type() != NULL
-       && right_type->interface_type() == NULL
-       && !right_type->is_nil_type())
-      || (left_type->interface_type() == NULL
-	  && !left_type->is_nil_type()
-	  && right_type->interface_type() != NULL))
+  else if ((left_type->interface_type() != NULL
+	    && right_type->interface_type() == NULL
+	    && !right_type->is_nil_type())
+	   || (left_type->interface_type() == NULL
+	       && !left_type->is_nil_type()
+	       && right_type->interface_type() != NULL))
     {
       // Comparing an interface value to a non-interface value.
       if (left_type->interface_type() == NULL)