golang / go

The Go programming language

Home Page:https://go.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

gccgo: does not error on unused var

dvyukov opened this issue · comments

gccgo successfully compiles the following incorrect program:

package a
func g() {
    var f = func() {}
}

Both gc and gotype say:

test.go:3:6: f declared but not used

gofrontend is on commit 81810917af7ba19e1f9f8efc8b1989f7d6419d30

That program is not incorrect. From the spec:

Implementation restriction: A compiler may make it illegal to declare a variable inside a function body if the variable is never used.

So gccgo is free to allow the unused var.

I'm not sure whether gccgo disallows other unused vars, though (don't have a gccgo installation available right now).

This is a gccgo bug; it doesn't check if variables initialized by a function literal are ever used. I don't think this is on purpose, despite the disclaimer in the specification.

CL https://golang.org/cl/13908 mentions this issue.