In Go code like
var _ T = F()
(in Go, _ is an unnamed variable which can not be referenced) the
compiler should check whether a value with the return type of F() can be
assigned to a variable of type T. The compiler was not doing this
check--it was just throwing away the type. This patch fixes the
problem. Bootstrapped and ran Go testsuite on
x86_64-unknown-linux-gnu. Committed to mainline.
Ian
diff -r d65e359c1879 go/parse.cc
--- a/go/parse.cc Fri Jan 20 07:30:55 2012 -0800
+++ b/go/parse.cc Fri Jan 20 15:26:29 2012 -0800
@@ -1883,10 +1883,23 @@
{
if (!type_from_init && init != NULL)
{
- if (!this->gogo_->in_global_scope())
+ if (this->gogo_->in_global_scope())
+ return this->create_dummy_global(type, init, location);
+ else if (type == NULL)
this->gogo_->add_statement(Statement::make_statement(init, true));
else
- return this->create_dummy_global(type, init, location);
+ {
+ // With both a type and an initializer, create a dummy
+ // variable so that we will check whether the
+ // initializer can be assigned to the type.
+ Variable* var = new Variable(type, init, false, false, false,
+ location);
+ static int count;
+ char buf[30];
+ snprintf(buf, sizeof buf, "sink$%d", count);
+ ++count;
+ return this->gogo_->add_variable(buf, var);
+ }
}
return this->gogo_->add_sink();
}