Coder Social home page Coder Social logo

declarations only used inside subsequently defined closures problematic: error "printf.go:485:6: argType declared but not used" about godebug HOT 12 CLOSED

mailgun avatar mailgun commented on July 3, 2024
declarations only used inside subsequently defined closures problematic: error "printf.go:485:6: argType declared but not used"

from godebug.

Comments (12)

jeremyschlatter avatar jeremyschlatter commented on July 3, 2024

Thanks for the report! Surprisingly, it looks like you found a bug in golang.org/x/tools/go/loader:

Minimal loader program loader.go:

package main

import (
    "fmt"

    "golang.org/x/tools/go/loader"
)

func main() {
    var conf loader.Config
    conf.CreateFromFilenames("main", "small.go")
    _, err := conf.Load()
    fmt.Println(err)
}

results on your minimal test case small.go

$ go run loader.go
small.go:7:6: touchedInClosure declared but not used
couldn't load packages due to errors: main

I'll see if I can find out what's going on and get this fixed upstream.

from godebug.

omeid avatar omeid commented on July 3, 2024

This isn't a bug. Using a variable isn't just writing to it but reading it and because the variable is never used inside it's scope, it complains about it.

from godebug.

jeremyschlatter avatar jeremyschlatter commented on July 3, 2024

@omeid is right. It's unfortunate that godebug does not accept a program that the go tool does, though.

I was curious what the spec had to say about this. I found this:

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

So the spec leaves this open, and both the go tool and the loader library are in compliance. The latter is just more strict.

from godebug.

jeremyschlatter avatar jeremyschlatter commented on July 3, 2024

Early versions of godebug used golang.org/x/tools/go/types for type checking. That library does not object to small.go:

package main

import (
    "fmt"
    "go/ast"
    "go/parser"
    "go/token"

    "golang.org/x/tools/go/types"
)

func main() {
    var fs token.FileSet
    f, err := parser.ParseFile(&fs, "small.go", nil, 0)
    if err != nil {
        fmt.Println("error in parsing: ", err)
        return
    }
    _, err = types.Check("main", &fs, []*ast.File{f})
    fmt.Println(err)
}

Running it:

$ go run parser.go
<nil>

The loader library provides a lot that the types library does not, so it might not be worth the effort to switch to it. But it would make godebug behave more like go for this case.

from godebug.

omeid avatar omeid commented on July 3, 2024

Please don't downgrade.

c2go has been written against an older version of Go and should be ported to the latest if they want to use godebug, not the other way around.

from godebug.

jeremyschlatter avatar jeremyschlatter commented on July 3, 2024

c2go isn't outdated. It runs on tip (or, at least, my copy of tip from last week):

~/src/github.com/glycerine/c2go$ go version
go version devel +180fbb1 Fri Mar 27 00:30:14 2015 +0000 linux/amd64
~/src/github.com/glycerine/c2go$ go run *.go
usage: c2go [options] *.c
  -I string
        include directory
  -c string
        config file
  -dst string
        GOPATH root of destination (default "/tmp/c2go")
exit status 2

The surprising behavior is that godebug's type checker behaves differently from go's type checker, even though both are compliant with the language spec.

from godebug.

jeremyschlatter avatar jeremyschlatter commented on July 3, 2024

@glycerine I tried just deleting that variable from the code and godebug run worked fine:

$ godebug run *.go
usage: c2go [options] *.c
  -I="": include directory
  -c="": config file
  -dst="/tmp/c2go": GOPATH root of destination

from godebug.

jeremyschlatter avatar jeremyschlatter commented on July 3, 2024

Oops, I messed up my test with golang.org/x/tools/go/types. I had edited small.go before I ran the test. Running again with the original file, it gives the same error as the loader library:

$ go run parser.go
small.go:7:6: touchedInClosure declared but not used

I'm going to call this one wontfix. Sorry, @glycerine! If you want to debug that library, just delete that variable.

from godebug.

glycerine avatar glycerine commented on July 3, 2024

Arg. Agree we can't fix this locally, but I am still inclined to think we
should upstream this as an issue so that "go run" and the types library are
consistent.

On Sun, Apr 5, 2015 at 9:58 PM, Jeremy Schlatter [email protected]
wrote:

Closed #14 #14.


Reply to this email directly or view it on GitHub
#14 (comment).

Best regards,
Jason

from godebug.

glycerine avatar glycerine commented on July 3, 2024

err, I misread -- It's the "golang.org/x/tools/go/loader" library that
differs in strictness, yes?

On Mon, Apr 6, 2015 at 7:55 AM, Jason E. Aten [email protected] wrote:

Arg. Agree we can't fix this locally, but I am still inclined to think we
should upstream this as an issue so that "go run" and the types library are
consistent.

On Sun, Apr 5, 2015 at 9:58 PM, Jeremy Schlatter <[email protected]

wrote:

Closed #14 #14.


Reply to this email directly or view it on GitHub
#14 (comment).

Best regards,
Jason

from godebug.

glycerine avatar glycerine commented on July 3, 2024

opened golang/go#10356 to track upstream

from godebug.

jeremyschlatter avatar jeremyschlatter commented on July 3, 2024

go/loader and go/types behave the same way, and both are different from gc. I thought differently earlier in the ticket, but I was mistaken.

from godebug.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.