Skip to content

cmd/gc: failure to detect set but not used variable captured by anon func #10286

Closed
@davecheney

Description

@davecheney

For this function

package main

func f(fn func() error) error {
        return fn()
}

func main() {
        called := false
        f(func() error {
                called = true
                return nil
        })

}

gc considers called to be both set and used although nowhere in the scope is the value of called ever consulted.

gccgo-5.0 on the other hand considers this code to be faulty

% env LD_LIBRARY_PATH=/opt/gccgo/lib64 PATH=$PATH:/opt/gccgo/bin go build -compiler=gccgo /tmp/x.go
# command-line-arguments
/tmp/x.go:8:2: error: ‘called’ declared and not used
  called := false
  ^

gccgo version

% env LD_LIBRARY_PATH=/opt/gccgo/lib64 PATH=$PATH:/opt/gccgo/bin gccgo -v
Using built-in specs.
COLLECT_GCC=gccgo
COLLECT_LTO_WRAPPER=/opt/gccgo/libexec/gcc/x86_64-unknown-linux-gnu/5.0.0/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ../gcc/trunk/configure --prefix=/opt/gccgo --enable-languages=c,c++,go --disable-bootstrap
Thread model: posix
gcc version 5.0.0 20150324 (experimental) (GCC) 

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions