Patchwork Go patch committed: Better error for switch on non-comparable type

login
register
mail settings
Submitter Ian Taylor
Date Sept. 25, 2012, 11:19 p.m.
Message ID <mcrehlpofmi.fsf@google.com>
Download mbox | patch
Permalink /patch/186918/
State New
Headers show

Comments

Ian Taylor - Sept. 25, 2012, 11:19 p.m.
This patch to the Go frontend gives a better error message for a switch
on a non-comparable type.  Bootstrapped and ran Go testsuite on
x86_64-unknown-linux-gnu.  Committed to mainline and 4.7 branch.

Ian

Patch

diff -r 74d2d7d217d8 -r f47f5449a663 go/statements.cc
--- a/go/statements.cc	Sat Sep 22 00:17:10 2012 -0700
+++ b/go/statements.cc	Mon Sep 24 15:20:45 2012 -0700
@@ -3846,6 +3846,16 @@ 
     return new Constant_switch_statement(this->val_, this->clauses_,
 					 this->break_label_, loc);
 
+  if (this->val_ != NULL
+      && !this->val_->type()->is_comparable()
+      && !Type::are_compatible_for_comparison(true, this->val_->type(),
+					      Type::make_nil_type(), NULL))
+    {
+      error_at(this->val_->location(),
+	       "cannot switch on value whose type that may not be compared");
+      return Statement::make_error_statement(loc);
+    }
+
   Block* b = new Block(enclosing, loc);
 
   if (this->clauses_->empty())