Go version

go version devel go1.22-66d34c7d08

Output of go env in your module/workspace:

does not depend on platform, only on `GOEXPERIMENT=rangefunc`

What did you do?

Go play link: https://go.dev/play/p/9faujPo3akR?v=gotip I actually compiled this code GOSSAFUNC=main GOEXPERIMENT=rangefunc go run main.go and then examined ssa.html in a browser.

What did you see happen?

The two interesting places are the genssa column of ssa.html (the rightmost) and the AST column (2nd from left).

in genssa there are references to main.#exit1 and main.i as a stack variable

 v76    00023 (+18) MOVB $0, main.#exit1-137(SP)
v126    00024 (?) NOP
v157    00025 (+21) XORL AX, AX
  b4    00026 (9) JMP 29
v151    00027 (+22) ADDQ CX, main.i-128(SP)
v155    00028 (+9) INCQ AX
v165    00029 (+9) CMPQ AX, $14
  b5    00030 (9) JGE 42
v138    00031 (9) MOVQ main..autotmp_13-112(SP)(AX*8), CX
v139    00032 (+10) XCHGL AX, AX
v119    00033 (+21) CMPB main.#exit1-137(SP), $0
  b6    00034 (21) JEQ 27
v136    00035 (9) MOVQ AX, main..autotmp_26-120(SP)
v116    00036 (9) MOVQ CX, main.x-136(SP)
v143    00037 (21) PCDATA $1, $0
v143    00038 (21) CALL runtime.panicrangeexit(SB)

The cause for this can be seen in the AST column, where both i and #exit1 are marked as Addrtaken, because once-upon-a-time they were locals modified in a closure, but that closure has been inlined and is called nowhere. I understand it cannot quite be completely dead code eliminated because of mentions of the name in debugging information.

. DCL # main.go:20:2
. . NAME-main.i esc(no) Class:PAUTO Offset:0 Addrtaken OnStack Used int tc(1) # main.go:20:2
. AS Def tc(1) # main.go:20:4
. . NAME-main.i esc(no) Class:PAUTO Offset:0 Addrtaken OnStack Used int tc(1) # main.go:20:2
. . LITERAL-0 int tc(1) # main.go:20:7
. DCL
. . NAME-main.#exit1 esc(no) Class:PAUTO Offset:0 Addrtaken OnStack Used bool tc(1) # main.go:21:2
. AS tc(1)
. . NAME-main.#exit1 esc(no) Class:PAUTO Offset:0 Addrtaken OnStack Used bool tc(1) # main.go:21:2

I poked around the compiler a bit, and believe that the deed is done (Addrtaken set) in flowClosure in escape.go

What did you expect to see?

#exit1 and i not marked Addrtaken, allocated to registers, and (in the case of #exit1) completely removed from the code because it is statically evident that it is not needed.

Comment From: gopherbot

Change https://go.dev/cl/587656 mentions this issue: cmd/compile: add "deadlocals" pass to remove unused locals

Comment From: gopherbot

Change https://go.dev/cl/588455 mentions this issue: cmd/link, ir, cmd/compile, test: combo CL for testing and benchmarking

Comment From: gopherbot

Change https://go.dev/cl/600498 mentions this issue: cmd/compile: add "deadlocals" pass to remove unused locals

Comment From: gopherbot

Change https://go.dev/cl/601975 mentions this issue: cmd/compile: fix reproducible build with deadlocals pass

Comment From: gopherbot

Change https://go.dev/cl/602075 mentions this issue: cmd/compile: fix order of map iteration in deadlocals