compiler: use bool for comma-ok if not already boolean

If a comma-ok variable already has a type, and that type is not a
boolean type, then set the type of the temporary variable to bool.
Otherwise we may try to convert an unnamed bool type to an interface
type, which will fail. But we don't want to always use bool, because
the type of the comma-ok variable may be a named bool type, in
which case the assignment would fail (or need an explicit conversion).

The test case is https://go.dev/cl/404496.

Fixes golang/go#52535

Change-Id: I15eb64824952ed8650eb6ae273c1abefb10443e3
Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/413894
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Than McIntosh <thanm@google.com>
diff --git a/go/statements.cc b/go/statements.cc
index b3db843..b442830 100644
--- a/go/statements.cc
+++ b/go/statements.cc
@@ -1594,9 +1594,9 @@
 
   // var present_temp bool
   Temporary_statement* present_temp =
-    Statement::make_temporary((this->present_->type()->is_sink_type())
-			      ? Type::make_boolean_type()
-			      : this->present_->type(),
+    Statement::make_temporary((this->present_->type()->is_boolean_type()
+			       ? this->present_->type()
+			       : Type::lookup_bool_type()),
 			      NULL, loc);
   b->add_statement(present_temp);
 
@@ -1789,9 +1789,9 @@
 
   // var closed_temp bool
   Temporary_statement* closed_temp =
-    Statement::make_temporary((this->closed_->type()->is_sink_type())
-			      ? Type::make_boolean_type()
-			      : this->closed_->type(),
+    Statement::make_temporary((this->closed_->type()->is_boolean_type()
+			       ? this->closed_->type()
+			       : Type::lookup_bool_type()),
 			      NULL, loc);
   b->add_statement(closed_temp);
 
@@ -1965,6 +1965,8 @@
       b->add_statement(s);
 
       res = Expression::make_call_result(call, 1);
+      if (!this->ok_->type()->is_boolean_type())
+	res = Expression::make_cast(Type::lookup_bool_type(), res, loc);
       s = Statement::make_assignment(this->ok_, res, loc);
       b->add_statement(s);
     }
@@ -2001,7 +2003,9 @@
   Temporary_statement* ok_temp = NULL;
   if (!this->ok_->is_sink_expression())
     {
-      ok_temp = Statement::make_temporary(this->ok_->type(),
+      ok_temp = Statement::make_temporary((this->ok_->type()->is_boolean_type()
+					   ? this->ok_->type()
+					   : Type::lookup_bool_type()),
 					  NULL, loc);
       b->add_statement(ok_temp);
     }