1
0
mirror of https://github.com/golang/go synced 2024-11-22 20:04:47 -07:00
go/test/fixedbugs/issue54159.go
David Chase cc258e6785 cmd/compile: add "deadlocals" pass to remove unused locals
This CL adds a "deadlocals" pass, which runs after inlining and before
escape analysis, to prune any unneeded local variables and
assignments. In particular, this helps avoid unnecessary Addrtaken
markings from unreachable closures.

Deadlocals is sensitive to "_ = ..." as a signal of explicit
use for testing.  This signal occurs only if the entire
left-hand-side is "_" targets; if it is
  `_, ok := someInlinedFunc(args)`
then the first return value is eligible for dead code elimination.

Use this (`_ = x`) to fix tests broken by deadlocals elimination.

Includes a test, based on one of the tests that required modification.

Matthew Dempsky wrote this, changing ownership to allow rebases, commits, tweaks.

Fixes #65158.

Old-Change-Id: I723fb69ccd7baadaae04d415702ce6c8901eaf4e
Change-Id: I1f25f4293b19527f305c18c3680b214237a7714c
Reviewed-on: https://go-review.googlesource.com/c/go/+/600498
Reviewed-by: Keith Randall <khr@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@golang.org>
Auto-Submit: David Chase <drchase@google.com>
Commit-Queue: David Chase <drchase@google.com>
2024-07-30 15:46:27 +00:00

24 lines
593 B
Go

// errorcheck -0 -m=2
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
func run() { // ERROR "cannot inline run: recursive"
f := func() { // ERROR "can inline run.func1 with cost .* as:.*" "func literal does not escape"
g() // ERROR "inlining call to g"
}
f() // ERROR "inlining call to run.func1" "inlining call to g"
_ = f
run()
}
func g() { // ERROR "can inline g with cost .* as:.*"
}
func main() { // ERROR "can inline main with cost .* as:.*"
run()
}