Skip to content

go/ast: unexpected associations for comments in empty function/loop bodies #39753

Open
@ec-m

Description

@ec-m

Comments that are placed inside empty function or loop bodies are associated with statements after the body, hence statements outside of the scope the comments are in. I would expect them to be associated with the function or loop declaration. This snipped demonstrates the issue:

package main

func main() {
}

func foo() {
	// inside empty function, associated with function bar below
}

func bar() {
	i := 1
	for i < 2 {
		// inside empty for, associated with if i == 3
	}
	// after empty for loop, this, however, is associated with for loop above

	if i == 3 {
		// inside empty if, associated with i = 4
	}
	i = 4
}

And here you can find code to reproduce the issue: https://play.golang.org/p/m4j-OTbdi-L

As @griesemer mentioned in #20744, the comment association heuristic is not straight forward to implement. Thus, I am wondering whether the above shown associations are intentional or whether this is a bug. Thanks for the clarification!

$ go version
go version go1.14.2 darwin/amd64
go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/.../Library/Caches/go-build"
GOENV="/Users/.../Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/.../go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/.../go.mod"
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/yb/hqncynqs0b5_3hyxcjpdsyr00000gr/T/go-build447564702=/tmp/go-build -gno-record-gcc-switches -fno-common"

Metadata

Metadata

Assignees

Labels

NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

Type

No type

Projects

Status

No status

Relationships

None yet

Development

No branches or pull requests

Issue actions