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

Submitted by Ian Taylor on Sept. 25, 2012, 11:19 p.m.

Details

Message ID mcrehlpofmi.fsf@google.com
State New
Headers show

Commit Message

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 hide | download patch | download mbox

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())