golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.72k stars 17.62k forks source link

x/tools/gopls: Diagnostics: incorrect end column on errors containing "-" (wrong cutset in TypeErrorEndPos) #69505

Open oversoul opened 1 month ago

oversoul commented 1 month ago

Go version

go1.23.1

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/ae/.cache/go-build'
GOENV='/home/ae/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/ae/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/ae/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/opt/go/'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/opt/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.23.1'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/ae/.config/go/telemetry'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/mnt/ddrive/Codes/golang/memo/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build4126319014=/tmp/go-build -gno-record-gcc-switches'

What did you do?

Take this example

import (
    "go.mongodb.org/mongo-driver/bson"
    "go.mongodb.org/mongo-driver/bson/primitive"
)

type (
    Token struct {
        Id     primitive.ObjectID `bson:"_id,omitempty"`
        Name   string             `bson:"name"`
        Token  string             `bson:"token"`
        UserId primitive.ObjectID `bson:"user_id"`
    }
        // ...
)

What did you see happen?

On editor (sublime, neovim), because of the unused import "go.mongodb.org/mongo-driver/bson" it highlights only until the -. Using gopls command gopls check api/auth/models.go the output is:

api/auth/models.go:4:2-23: "go.mongodb.org/mongo-driver/bson" imported and not used

23 here is wrong. should be 36.

What did you expect to see?

api/auth/models.go:4:2-36: "go.mongodb.org/mongo-driver/bson" imported and not used

After some research, The issue is in TypeErrorEndPos

tools/gopls/internal/analysisinternal/analysis.go Line 29

if width := bytes.IndexAny(src[offset:], " \n,():;[]+-*"); width > 0 {

after removing the hyphen, it works as expected. I did run the tests after the change but no tests seems to break.

adonovan commented 1 month ago

Thanks for the thorough investigation.

The inclusion of -+* in TypeErrorEndPos logic was added in https://go.dev/cl/222237; neither the CL description nor any of the test cases reveal why. It would be interesting to instrument go/types to record whenever it produces an error containing one of these symbols, and run its tests (and run it over the standard library) to see what comes up.

gabyhelp commented 1 month ago

Related Issues and Documentation

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)