8000 cmd/compile: stronger constant propagation around closures · Issue #73601 · golang/go · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
cmd/compile: stronger constant propagation around closures #73601
Open
@adonovan

Description

@adonovan

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

FeatureRequestIssues asking for a new feature that does not need a proposal.NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.Performancecompiler/runtimeIssues related to the Go compiler and/or runtime.

Type

No type

Projects

Status

Todo

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    0