Skip to content

Commit 911cda3

Browse files
findleyrgopherbot
authored andcommitted
[gopls-release-branch.0.16] internal/typeparams: fix crash in interface 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 <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Alan Donovan <[email protected]> (cherry picked from commit 72edac2) Reviewed-on: https://go-review.googlesource.com/c/tools/+/595556
1 parent 7ad3a0d commit 911cda3

File tree

4 files changed

+82
-34
lines changed

4 files changed

+82
-34
lines changed

gopls/go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@ require (
2525
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
2626

2727
)
28+
29+
replace golang.org/x/tools => ../

gopls/go.sum

+18-3
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,37 @@ github.com/jba/templatecheck v0.7.0/go.mod h1:n1Etw+Rrw1mDDD8dDRsEKTwMZsJ98Ekktg
1010
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
1111
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
1212
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
13+
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
14+
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
15+
golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM=
1316
golang.org/x/exp/typeparams v0.0.0-20221212164502-fae10dda9338 h1:2O2DON6y3XMJiQRAS1UWU+54aec2uopH3x7MAiqGW6Y=
1417
golang.org/x/exp/typeparams v0.0.0-20221212164502-fae10dda9338/go.mod h1:AbB0pIl9nAr9wVwH+Z2ZpaocVmF5I4GyWCDIsVjR0bk=
18+
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
19+
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
1520
golang.org/x/mod v0.18.0 h1:5+9lSbEzPSdWkH32vYPBwEpX8KwDbM52Ud9xBUvNlb0=
1621
golang.org/x/mod v0.18.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
22+
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
23+
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
24+
golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE=
1725
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
1826
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
27+
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
28+
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
29+
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
30+
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
1931
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
2032
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
33+
golang.org/x/telemetry v0.0.0-20240521205824-bda55230c457/go.mod h1:pRgIJT+bRLFKnoM1ldnzKoxTIn14Yxz928LQRYYgIN0=
2134
golang.org/x/telemetry v0.0.0-20240607193123-221703e18637 h1:3Wt8mZlbFwG8llny+t18kh7AXxyWePFycXMuVdHxnyM=
2235
golang.org/x/telemetry v0.0.0-20240607193123-221703e18637/go.mod h1:n38mvGdgc4dA684EC4NwQwoPKSw4jyKw8/DgZHDA1Dk=
36+
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
37+
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
38+
golang.org/x/term v0.21.0/go.mod h1:ooXLefLobQVslOqselCNF4SxFAaoS6KujMbsGzSDmX0=
2339
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
40+
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
41+
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
2442
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
2543
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
26-
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
27-
golang.org/x/tools v0.22.1-0.20240620150659-cb3016b76f3e h1:Cht5EwNRmW9EgQ7ihPmgIswaQW4jR1cPbmzGCNl2++8=
28-
golang.org/x/tools v0.22.1-0.20240620150659-cb3016b76f3e/go.mod h1:aCwcsjqvq7Yqt6TNyX7QMU2enbQ/Gt0bo6krSeEri+c=
2944
golang.org/x/vuln v1.0.4 h1:SP0mPeg2PmGCu03V+61EcQiOjmpri2XijexKdzv8Z1I=
3045
golang.org/x/vuln v1.0.4/go.mod h1:NbJdUQhX8jY++FtuhrXs2Eyx0yePo9pF7nPlIjo9aaQ=
3146
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

gopls/internal/test/marker/testdata/hover/generics.txt

+61-29
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ This file contains tests for hovering over generic Go code.
33
Requires go1.20+ for the new go/doc/comment package, and a change in Go 1.20
44
that affected the formatting of constraint interfaces.
55

6+
Its size expectations assume a 64-bit machine.
7+
68
-- flags --
79
-min_go=go1.20
10+
-skip_goarch=386,arm
811

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

1417
go 1.18
1518

19+
-- issue68213.go --
20+
package generics
21+
22+
// Hovering over an interface with empty type set must not panic.
23+
type empty interface { //@hover("empty", "empty", empty)
24+
int
25+
string
26+
}
27+
28+
-- @empty --
29+
```go
30+
type empty interface { // size=16 (0x10)
31+
int
32+
string
33+
}
34+
```
35+
36+
Hovering over an interface with empty type set must not panic.
1637
-- generics.go --
1738
package generics
1839

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

33-
-- inferred.go --
34-
package generics
35-
36-
func app[S interface{ ~[]E }, E interface{}](s S, e E) S {
37-
return append(s, e)
38-
}
39-
40-
func _() {
41-
_ = app[[]int] //@hover("app", "app", appint)
42-
_ = app[[]int, int] //@hover("app", "app", appint)
43-
_ = app[[]int]([]int{}, 0) //@hover("app", "app", appint), diag("[[]int]", re"unnecessary")
44-
_ = app([]int{}, 0) //@hover("app", "app", appint)
45-
}
46-
47-
-- @ValueQ --
54+
-- @value --
4855
```go
49-
field Q int64 // size=8
56+
type value[T any] struct {
57+
val T //@hover("T", "T", valuevalT)
58+
Q int64 //@hover("Q", "Q", valueQ)
59+
}
5060
```
51-
52-
@hover("Q", "Q", ValueQ)
53-
54-
55-
[`(generics.Value).Q` on pkg.go.dev](https://pkg.go.dev/mod.com#Value.Q)
56-
-- @ValueT --
61+
-- @valueT --
5762
```go
5863
type parameter T any
5964
```
60-
-- @ValuevalT --
65+
-- @valuevalT --
6166
```go
6267
type parameter T any
6368
```
64-
-- @appint --
65-
```go
66-
func app(s []int, e int) []int // func[S interface{~[]E}, E interface{}](s S, e E) S
67-
```
6869
-- @valueQ --
6970
```go
7071
field Q int64 // size=8
7172
```
7273

7374
@hover("Q", "Q", valueQ)
74-
-- @valuevalT --
75+
-- @ValueT --
76+
```go
77+
type parameter T any
78+
```
79+
-- @ValuevalT --
7580
```go
7681
type parameter T any
7782
```
83+
-- @ValueQ --
84+
```go
85+
field Q int64 // size=8
86+
```
87+
88+
@hover("Q", "Q", ValueQ)
89+
90+
91+
[`(generics.Value).Q` on pkg.go.dev](https://pkg.go.dev/mod.com#Value.Q)
7892
-- @Ptparam --
7993
```go
8094
type parameter P interface{~int | string}
@@ -83,3 +97,21 @@ type parameter P interface{~int | string}
8397
```go
8498
type parameter P interface{~int | string}
8599
```
100+
-- inferred.go --
101+
package generics
102+
103+
func app[S interface{ ~[]E }, E interface{}](s S, e E) S {
104+
return append(s, e)
105+
}
106+
107+
func _() {
108+
_ = app[[]int] //@hover("app", "app", appint)
109+
_ = app[[]int, int] //@hover("app", "app", appint)
110+
_ = app[[]int]([]int{}, 0) //@hover("app", "app", appint), diag("[[]int]", re"unnecessary")
111+
_ = app([]int{}, 0) //@hover("app", "app", appint)
112+
}
113+
114+
-- @appint --
115+
```go
116+
func app(s []int, e int) []int // func[S interface{~[]E}, E interface{}](s S, e E) S
117+
```

internal/typeparams/free.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ type Free struct {
2121

2222
// Has reports whether the specified type has a free type parameter.
2323
func (w *Free) Has(typ types.Type) (res bool) {
24-
2524
// detect cycles
2625
if x, ok := w.seen[typ]; ok {
2726
return x
@@ -83,7 +82,7 @@ func (w *Free) Has(typ types.Type) (res bool) {
8382
}
8483
terms, err := InterfaceTermSet(t)
8584
if err != nil {
86-
panic(err)
85+
return false // ill typed
8786
}
8887
for _, term := range terms {
8988
if w.Has(term.Type()) {

0 commit comments

Comments
 (0)