Open
Description
I notice that the use of a local variable to hold a function (e.g. for brevity) doesn't prevent the compiler from treating the variable as an alias for the function, and dispatching calls statically. However, if the variable is referenced from an inner closure, it is treated as a true variable, resulting in a dynamic call:
func f() any {
escape := html.EscapeString
println(escape("foo")) // static call
return func() {
println(escape("foo")) // dynamic call
}
}
The closure may be latent, in the case of a range-over-func:
func f(seq iter.Seq[string]) {
escape := html.EscapeString
println(escape("foo")) // static call
for s := range seq {
println(escape(s)) // dynamic call
}
}
This is another facet of #73524 (comment), where closures defeat constant propagation of outer single-assignment variables.
Metadata
Metadata
Assignees
Labels
Type
Projects
Status
Todo