Skip to content

go/parser: bad error message for a badly formed func literal #36750

Open

Description

@myitcv

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

$ go version
go version devel +8e0be05ec7 Sun Jan 19 14:04:09 2020 +0000 linux/amd64
$ go list -m golang.org/x/tools
golang.org/x/tools v0.0.0-20200121200311-d456b1cd8c86
$ go list -m golang.org/x/tools/gopls
golang.org/x/tools/gopls v0.1.8-0.20200121200311-d456b1cd8c86

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/myitcv/.cache/go-build"
GOENV="/home/myitcv/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/myitcv/gostuff"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/myitcv/gos"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/myitcv/gos/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/myitcv/.vim/plugged/govim/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 -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build878422207=/tmp/go-build -gno-record-gcc-switches"

What did you do?

The diagnostic for the following file is misleading:

package main

func main() {
	_ = func(format string, args ...interface) error {
		return nil
	}
}

We get:

PublishDiagnostics callback: &protocol.PublishDiagnosticsParams{
    URI:         "file:///home/myitcv/gostuff/src/github.com/myitcv/playground/main.go",
    Version:     36,
    Diagnostics: {
        {
            Range: protocol.Range{
                Start: protocol.Position{Line:3, Character:5},
                End:   protocol.Position{Line:3, Character:5},
            },
            Severity:           1,
            Code:               nil,
            Source:             "syntax",
            Message:            "expected expression",
            Tags:               nil,
            RelatedInformation: nil,
        },
    },
}

Contrast what we get from the compiler:

./main.go:4:43: syntax error: unexpected ), expecting {
./main.go:5:3: syntax error: unexpected return, expecting comma or )
./main.go:7:1: syntax error: non-declaration statement outside function body

What did you expect to see?

A better diagnostic.

What did you see instead?

Per above

Marking as gopls/v1.0.0 because this isn't critical for v0.3.0.


cc @stamblerre

FYI @leitzler

Activity

  1. added
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    goplsIssues related to the Go language server, gopls.
    on Jan 24, 2020
  2. added this to the gopls/v1.0.0 milestone on Jan 24, 2020
  3. added
    ToolsThis label describes issues relating to any tools in the x/tools repository.
    on Jan 24, 2020
  4. heschi commented on Jan 24, 2020

    @heschi
    Contributor

    This would be coming from go/parser or something, no? I don't think gopls can do anything to improve it.

  5. stamblerre commented on Mar 12, 2020

    @stamblerre
    Contributor

    Yes, this is is coming from go/parser. A minimal repro can be found here: https://play.golang.org/p/ca24wvyr5Wl. This seems like an instance where go/parser's errors should be updated to match the compiler's. I'll change the issue title to reflect this.

  6. changed the title [-]x/tools/gopls: misleading diagnostic for badly formed func literal[/-] [+]go/parser: bad error message for a badly formed func literal[/+] on Mar 12, 2020
  7. removed
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    ToolsThis label describes issues relating to any tools in the x/tools repository.
    goplsIssues related to the Go language server, gopls.
    on Mar 12, 2020
  8. added
    NeedsFixThe path to resolution is known, but the work has not been done.
    on Mar 12, 2020
  9. toothrot commented on Mar 12, 2020

    @toothrot
    Contributor
  10. gopherbot commented on Oct 6, 2020

    @gopherbot
    Contributor

    Change https://golang.org/cl/259817 mentions this issue: go/parser: Improve error message to user when interface parsing fails

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

    NeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions