Skip to content

Commit 4c676d9

Browse files
Chris Manghaneianlancetaylor
Chris Manghane
authored andcommitted
compiler: Don't allow builtin function values.
According to the spec, http://golang.org/ref/spec#Built-in_functions: "built-in functions do not have standard Go types, so they can only appear in call expressions; they cannot be used as function values." Fixes golang/go#11570. Change-Id: I398d80e2e6f6a8c23c8f3a2c8b2fb07575c35cf3 Reviewed-on: https://go-review.googlesource.com/12543 Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 9931f2c commit 4c676d9

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

go/gogo.cc

+22
Original file line numberDiff line numberDiff line change
@@ -3154,6 +3154,28 @@ Check_types_traverse::variable(Named_object* named_object)
31543154
reason.c_str());
31553155
var->clear_init();
31563156
}
3157+
else if (init != NULL
3158+
&& init->func_expression() != NULL)
3159+
{
3160+
Named_object* no = init->func_expression()->named_object();
3161+
Function_type* fntype;
3162+
if (no->is_function())
3163+
fntype = no->func_value()->type();
3164+
else if (no->is_function_declaration())
3165+
fntype = no->func_declaration_value()->type();
3166+
else
3167+
go_unreachable();
3168+
3169+
// Builtin functions cannot be used as function values for variable
3170+
// initialization.
3171+
if (fntype->is_builtin())
3172+
{
3173+
error_at(init->location(),
3174+
"invalid use of special builtin function %qs; "
3175+
"must be called",
3176+
no->message_name().c_str());
3177+
}
3178+
}
31573179
else if (!var->is_used()
31583180
&& !var->is_global()
31593181
&& !var->is_parameter()

0 commit comments

Comments
 (0)