Skip to content

Commit

Permalink
[gopls-release-branch.0.16] internal/typeparams: fix crash in interfa…
Browse files Browse the repository at this point in the history
…ce hover with empty type set

The logic of typeparams.Free.Has was not handling ErrEmptyTypeSet from
InterfaceTermSet, leading to a crash.

Also fix a hover marker in hover/generics.txt that was apparently
missing a '@'.

For the cherry pick: add back the replace directive.

Fixes golang/go#68213

Change-Id: I0e264b5c5f5ecc0d09a0b8ab381eb7d7c492c18f
Reviewed-on: https://go-review.googlesource.com/c/tools/+/595555
Auto-Submit: Robert Findley <rfindley@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
(cherry picked from commit 72edac2)
Reviewed-on: https://go-review.googlesource.com/c/tools/+/595556
  • Loading branch information
findleyr authored and gopherbot committed Jun 28, 2024
1 parent 7ad3a0d commit 911cda3
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 34 deletions.
2 changes: 2 additions & 0 deletions gopls/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ require (
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect

)

replace golang.org/x/tools => ../
21 changes: 18 additions & 3 deletions gopls/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,37 @@ github.com/jba/templatecheck v0.7.0/go.mod h1:n1Etw+Rrw1mDDD8dDRsEKTwMZsJ98Ekktg
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM=
golang.org/x/exp/typeparams v0.0.0-20221212164502-fae10dda9338 h1:2O2DON6y3XMJiQRAS1UWU+54aec2uopH3x7MAiqGW6Y=
golang.org/x/exp/typeparams v0.0.0-20221212164502-fae10dda9338/go.mod h1:AbB0pIl9nAr9wVwH+Z2ZpaocVmF5I4GyWCDIsVjR0bk=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.18.0 h1:5+9lSbEzPSdWkH32vYPBwEpX8KwDbM52Ud9xBUvNlb0=
golang.org/x/mod v0.18.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/telemetry v0.0.0-20240521205824-bda55230c457/go.mod h1:pRgIJT+bRLFKnoM1ldnzKoxTIn14Yxz928LQRYYgIN0=
golang.org/x/telemetry v0.0.0-20240607193123-221703e18637 h1:3Wt8mZlbFwG8llny+t18kh7AXxyWePFycXMuVdHxnyM=
golang.org/x/telemetry v0.0.0-20240607193123-221703e18637/go.mod h1:n38mvGdgc4dA684EC4NwQwoPKSw4jyKw8/DgZHDA1Dk=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
golang.org/x/term v0.21.0/go.mod h1:ooXLefLobQVslOqselCNF4SxFAaoS6KujMbsGzSDmX0=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.22.1-0.20240620150659-cb3016b76f3e h1:Cht5EwNRmW9EgQ7ihPmgIswaQW4jR1cPbmzGCNl2++8=
golang.org/x/tools v0.22.1-0.20240620150659-cb3016b76f3e/go.mod h1:aCwcsjqvq7Yqt6TNyX7QMU2enbQ/Gt0bo6krSeEri+c=
golang.org/x/vuln v1.0.4 h1:SP0mPeg2PmGCu03V+61EcQiOjmpri2XijexKdzv8Z1I=
golang.org/x/vuln v1.0.4/go.mod h1:NbJdUQhX8jY++FtuhrXs2Eyx0yePo9pF7nPlIjo9aaQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
90 changes: 61 additions & 29 deletions gopls/internal/test/marker/testdata/hover/generics.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ This file contains tests for hovering over generic Go code.
Requires go1.20+ for the new go/doc/comment package, and a change in Go 1.20
that affected the formatting of constraint interfaces.

Its size expectations assume a 64-bit machine.

-- flags --
-min_go=go1.20
-skip_goarch=386,arm

-- go.mod --
// A go.mod is require for correct pkgsite links.
Expand All @@ -13,10 +16,28 @@ module mod.com

go 1.18

-- issue68213.go --
package generics

// Hovering over an interface with empty type set must not panic.
type empty interface { //@hover("empty", "empty", empty)
int
string
}

-- @empty --
```go
type empty interface { // size=16 (0x10)
int
string
}
```

Hovering over an interface with empty type set must not panic.
-- generics.go --
package generics

type value[T any] struct { //hover("lue", "value", value),hover("T", "T", valueT)
type value[T any] struct { //@hover("lue", "value", value),hover("T", "T", valueT)
val T //@hover("T", "T", valuevalT)
Q int64 //@hover("Q", "Q", valueQ)
}
Expand All @@ -30,51 +51,44 @@ func F[P interface{ ~int | string }]() { //@hover("P", "P", Ptparam)
var _ P //@hover("P","P",Pvar)
}

-- inferred.go --
package generics

func app[S interface{ ~[]E }, E interface{}](s S, e E) S {
return append(s, e)
}

func _() {
_ = app[[]int] //@hover("app", "app", appint)
_ = app[[]int, int] //@hover("app", "app", appint)
_ = app[[]int]([]int{}, 0) //@hover("app", "app", appint), diag("[[]int]", re"unnecessary")
_ = app([]int{}, 0) //@hover("app", "app", appint)
}

-- @ValueQ --
-- @value --
```go
field Q int64 // size=8
type value[T any] struct {
val T //@hover("T", "T", valuevalT)
Q int64 //@hover("Q", "Q", valueQ)
}
```

@hover("Q", "Q", ValueQ)


[`(generics.Value).Q` on pkg.go.dev](https://pkg.go.dev/mod.com#Value.Q)
-- @ValueT --
-- @valueT --
```go
type parameter T any
```
-- @ValuevalT --
-- @valuevalT --
```go
type parameter T any
```
-- @appint --
```go
func app(s []int, e int) []int // func[S interface{~[]E}, E interface{}](s S, e E) S
```
-- @valueQ --
```go
field Q int64 // size=8
```

@hover("Q", "Q", valueQ)
-- @valuevalT --
-- @ValueT --
```go
type parameter T any
```
-- @ValuevalT --
```go
type parameter T any
```
-- @ValueQ --
```go
field Q int64 // size=8
```

@hover("Q", "Q", ValueQ)


[`(generics.Value).Q` on pkg.go.dev](https://pkg.go.dev/mod.com#Value.Q)
-- @Ptparam --
```go
type parameter P interface{~int | string}
Expand All @@ -83,3 +97,21 @@ type parameter P interface{~int | string}
```go
type parameter P interface{~int | string}
```
-- inferred.go --
package generics

func app[S interface{ ~[]E }, E interface{}](s S, e E) S {
return append(s, e)
}

func _() {
_ = app[[]int] //@hover("app", "app", appint)
_ = app[[]int, int] //@hover("app", "app", appint)
_ = app[[]int]([]int{}, 0) //@hover("app", "app", appint), diag("[[]int]", re"unnecessary")
_ = app([]int{}, 0) //@hover("app", "app", appint)
}

-- @appint --
```go
func app(s []int, e int) []int // func[S interface{~[]E}, E interface{}](s S, e E) S
```
3 changes: 1 addition & 2 deletions internal/typeparams/free.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ type Free struct {

// Has reports whether the specified type has a free type parameter.
func (w *Free) Has(typ types.Type) (res bool) {

// detect cycles
if x, ok := w.seen[typ]; ok {
return x
Expand Down Expand Up @@ -83,7 +82,7 @@ func (w *Free) Has(typ types.Type) (res bool) {
}
terms, err := InterfaceTermSet(t)
if err != nil {
panic(err)
return false // ill typed
}
for _, term := range terms {
if w.Has(term.Type()) {
Expand Down

0 comments on commit 911cda3

Please sign in to comment.