Skip to content

go/format: any input to format.Source panics #42300

Closed
@FiloSottile

Description

@FiloSottile

What version of Go are you using (go version)?

$ go version
go version devel +91b7619310 Thu Oct 22 15:30:02 2020 +0000 darwin/amd64

Does this issue reproduce with the latest release?

It does not reproduce with Go 1.15.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/valsorda/Library/Caches/go-build"
GOENV="/Users/valsorda/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/valsorda/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/valsorda"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/Users/valsorda/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/valsorda/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/df/mrk3bfz149n8zb5h5p1vp_1m00hbbm/T/go-build525898641=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Pass any input to format.Source.

https://play.golang.org/p/kZcA7s59sam

What did you expect to see?

Formatted output.

What did you see instead?

panic: unreachable

goroutine 1 [running]:
go/printer.(*printer).expr1(0xc000167ab0, 0x29712dc0, 0xc0004f6100, 0x0, 0x1)
	/Users/valsorda/go/src/go/printer/nodes.go:973 +0x274e
go/printer.(*printer).expr(...)
	/Users/valsorda/go/src/go/printer/nodes.go:1067
go/printer.(*printer).printNode(0xc000167ab0, 0x12bb140, 0xc0004f6100, 0xc00037a170, 0x100fe78)
	/Users/valsorda/go/src/go/printer/printer.go:1128 +0x385
go/printer.(*Config).fprint(0xc000167d20, 0x1363640, 0xc0001ba3c0, 0xc0002e6000, 0x12bb140, 0xc0004f6100, 0xc0001ba3f0, 0xc0002e6000, 0x0)
	/Users/valsorda/go/src/go/printer/printer.go:1309 +0x170
go/printer.(*Config).Fprint(...)
	/Users/valsorda/go/src/go/printer/printer.go:1367
go/format.format(0xc0002e6000, 0xc0004f6100, 0x0, 0x0, 0xc000828000, 0x49d49, 0x92e01, 0x40000006, 0x8, 0x0, ...)
	/Users/valsorda/go/src/go/format/internal.go:105 +0x671
go/format.Source(0xc000828000, 0x49d49, 0x92e01, 0x1, 0x0, 0x0, 0x0, 0x1)
	/Users/valsorda/go/src/go/format/format.go:116 +0x127

panic("unreachable")

This is reached because the type of expr is *ast.File, which is not handled by the type switch.

Activity

added
NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
on Oct 30, 2020
added this to the Go1.16 milestone on Oct 30, 2020
ianlancetaylor

ianlancetaylor commented on Oct 30, 2020

@ianlancetaylor
Contributor

I can't reproduce this.

ianlancetaylor

ianlancetaylor commented on Oct 30, 2020

@ianlancetaylor
Contributor

The type *ast.File does not implement the interface ast.Expr, so the code should never get to the point of the panic (and, for me, it doesn't).

FiloSottile

FiloSottile commented on Oct 30, 2020

@FiloSottile
ContributorAuthor

Indeed, neither does it for me after trying to bisect it. I suppose I was using a cmd/go out of sync with the Go tree and weird things were happening. I do wish there some level of safety against that. make.bash is fast but not so fast I run it on every git checkout, and I never know if I skipped over some change that will cause random compiler behaviors.

locked and limited conversation to collaborators on Oct 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.release-blocker

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @FiloSottile@ianlancetaylor@gopherbot

        Issue actions

          go/format: any input to format.Source panics · Issue #42300 · golang/go