Commit Graph

39 Commits

Author SHA1 Message Date
Robert Griesemer 6a585a2bf9 x/tools/internal/typeparams: use regexp to match wanted result
CL 410955 changes printing of unions which breaks two typeparams tests.
Use regexp matching and adjust the wanted results accordingly.

For golang/go#53279.
For golang/go#54822.

Change-Id: I7060df47d36ce3069570237dafff024aaad637a5
Reviewed-on: https://go-review.googlesource.com/c/tools/+/428055
Auto-Submit: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
Run-TryBot: Robert Griesemer <gri@google.com>
2022-09-02 19:06:05 +00:00
Robert Griesemer 6bfd3a440e Revert "internal: temporarily disable tests so we can land CL 410955"
This reverts commit 5ca4cc8b9a.

Reason for revert: appears insufficient to land CL 410955.

Change-Id: If7029c9a43568e210e89d473b738400f5155715e
Reviewed-on: https://go-review.googlesource.com/c/tools/+/410995
Reviewed-by: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Griesemer <gri@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
2022-06-08 01:09:07 +00:00
Robert Griesemer 5ca4cc8b9a internal: temporarily disable tests so we can land CL 410955
For golang/go#53279.

Change-Id: I3e0a502f8f0789ff9920af504ea8c86812bf49a1
Reviewed-on: https://go-review.googlesource.com/c/tools/+/410956
Reviewed-by: Robert Griesemer <gri@google.com>
Run-TryBot: Robert Griesemer <gri@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
2022-06-08 00:38:18 +00:00
Tim King 960b4ce5ae internal/typeparams: adds core type implementation
Adds a function for computing the core type of a type for use within x/tools.

Updates golang/go#52940

Change-Id: I91645c0b27031506be51a5f9d53b3e125e6fd1c2
Reviewed-on: https://go-review.googlesource.com/c/tools/+/406838
Run-TryBot: Tim King <taking@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
2022-05-17 21:56:59 +00:00
Hana (Hyang-Ah) Kim 033cbfc76d internal/typeparams: run go generate with go1.18.2
And fix a minor issue in copytermlist.go --

copytermlist.go replaces type names from go/types with
qualified type names. Use of token.NoPos (filled with
ast.NewIdent) however confuses the go/format printer.
As a result, while transforming

  func (x *term) includes(t Type) bool

to

  func (x *term) includes(t types.Type) bool

go/format printer failed to compute the end position of
the parameter list, concluded RPAREN should be in a
different line from the parameter list, added a comma,
and printed

  func (x *term) includes(t types.Type,) bool

Reuse the replaced node's position instead. (not 100%
correct, but better than NoPos)

Change-Id: Ia34e11562cc80c68dcf4b921ffffd926971c2215
Reviewed-on: https://go-review.googlesource.com/c/tools/+/405536
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
Reviewed-by: Robert Findley <rfindley@google.com>
2022-05-11 13:38:50 +00:00
Robert Findley bc0e26ea12 internal/typeparams: remove examples in favor of x/exp/typeparams
The internal/typeparams directory contains an initial draft of the
documentation later published under the golang.org/x/exp/typeparams
module. Remove this draft to avoid confusion.

Change-Id: I1f40468f99c0d2885e8fc380c75669f654ba971e
Reviewed-on: https://go-review.googlesource.com/c/tools/+/405260
Reviewed-by: Alan Donovan <adonovan@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
2022-05-10 12:58:44 +00:00
Russ Cox d5f48fca53 all: gofmt
Gofmt to update doc comments to the new formatting.

(There are so many files in x/tools I am breaking up the
gofmt'ing into multiple CLs. This is the leftovers.)

For golang/go#51082.

Change-Id: Id9d440cde9de7093d2ffe06cbaa7098993823d6b
Reviewed-on: https://go-review.googlesource.com/c/tools/+/399363
Run-TryBot: Russ Cox <rsc@golang.org>
Auto-Submit: Russ Cox <rsc@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
2022-04-12 17:53:17 +00:00
Robert Findley caecc2b0ac internal/typeparams: add a generic form of types.AssignableTo
For some use-cases, it is helpful to compute predicates between
uninstantiated generic types.

This CL implements one such predicate for types.AssignableTo, a helper
GenericAssignableTo which reports if, for generic types V and T with the
same number of type parameters, all instantiations V[A_1, ..., A_N] are
assignable to the corresponding instantiation T[A_1, ..., A_N].

For #50887
For #50447

Change-Id: I7a0550fba05666bb2375d478d5390a123e09f556
Reviewed-on: https://go-review.googlesource.com/c/tools/+/383094
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
2022-02-08 16:50:25 +00:00
Robert Findley ea5e1dc8bc internal/typeparams: add a helper to return the origin method
With instantiated types, method objects are no longer unique: they may
be instantiations of methods with generic receiver. However, some
use-cases require finding the canonical method representing the method
in the source. For these use-cases, provide an OriginMethod helper.

For golang/go#50447

Change-Id: I6f8af3fb5c5eeefb11f8f3bdba54cd6692ca389f
Reviewed-on: https://go-review.googlesource.com/c/tools/+/380554
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Tim King <taking@google.com>
2022-02-03 14:02:38 +00:00
Robert Findley 84f205d75e internal/typeparams/example: start adding a guide to generics for tools
This CL begins adding a guide for the new APIs introduced with Go 1.18
to support writing tools that understand generic Go code.

For now I've added a summary of the new APIs, an initial example, and
some discussion of the typeparams package. Subsequent CLs will add more
examples, and polish.

Updates golang/go#50447

Change-Id: I4ed8d7a2f43e748374d14f3f515673d69ab2d5a0
Reviewed-on: https://go-review.googlesource.com/c/tools/+/377834
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
Trust: Dominik Honnef <dominik@honnef.co>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Suzy Mueller <suzmue@golang.org>
2022-01-22 21:07:13 +00:00
Robert Findley 7c251d6b2f internal/typeparams: improve the package documentation
Update and expand on the package documentation for the
internal/typeparams package.

Change-Id: I2af16250580abf9e6b3a8692356faa498ba83f29
Reviewed-on: https://go-review.googlesource.com/c/tools/+/377654
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
Reviewed-by: Tim King <taking@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
2022-01-19 20:29:36 +00:00
Robert Findley 2762b18206 internal/typeparams: simplify the IndexExpr API
The IndexExprData type of internal/typeparams was redundant now
that the typeparams.IndexListExpr type has been introduced. Remove it,
and change GetIndexExprData to an UnpackIndexExpr method, symmetric with
PackIndexExpr. With the IndexListExpr type, both UnpackIndexExpr and
PackIndexExpr can be moved to common.go.

Change-Id: I6c31f52ab594175decfbd1a4cd94b60a23a1e629
Reviewed-on: https://go-review.googlesource.com/c/tools/+/377235
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
Reviewed-by: Tim King <taking@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
2022-01-19 20:29:28 +00:00
Robert Findley 2ac48c609c go/types/typeutil: add support for mapping generic types
Add support to the typeutil package for hashing the new types produced
when type-checking generic code.

Change-Id: I05a213baee80c53c673442f3c28fddb26ad0b03f
Reviewed-on: https://go-review.googlesource.com/c/tools/+/366614
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Tim King <taking@google.com>
2021-12-02 03:14:37 +00:00
Robert Findley 43b469a3a9 go/analysis/passes/printf: update logic now that type parameters are
interfaces

Now that the underlying of type parameters is their constraint
interface, a couple places in the printf analyzer need to be updated:
 - We need to explicitly exclude type parameters when looking at
   interfaces in isFormatter.
 - We need to consider at the element type, not its underlying, when
   looking at pointers to type parameters

Change-Id: I8c6843e001a98d45ff0f30df305e3536335f567e
Reviewed-on: https://go-review.googlesource.com/c/tools/+/364678
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-11-17 19:16:04 +00:00
Robert Findley 4adea5033c internal/typeparams: normalize the underlying constraint interface
A call to Underlying() was missing from the computation of a type
parameter's structural terms, resulting in incorrect results for type
parameters with defined constraints. This wasn't caught because all
analyzer tests use inlined interfaces out of convenience, and the
fallback behavior for vet analyzers using this API is to fail silently.

Fix this, and add tests that would have failed. Also update the
normalization tests to use the StructuralTerms API, and delete the
earlier NormalizeInterface API. StructuralTerms has proven to be more
useful.

For clarity, change the StructuralTerms function to accept a *TypeParam
rather than arbitrary Type, and significantly revamp its docstring.

Updates golang/go#49597

Change-Id: I8b863604655b44b943e6afbd5d22a66f44260d10
Reviewed-on: https://go-review.googlesource.com/c/tools/+/363982
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Tim King <taking@google.com>
2021-11-16 01:10:28 +00:00
Robert Findley 1050b5ce2e go/analysis/passes/composite: update for generics
Warn about unkeyed composite literals via literals of type parameter
type.

Updates golang/go#48704

Change-Id: Ia75139b56cb73288c133bd547d71664464015813
Reviewed-on: https://go-review.googlesource.com/c/tools/+/357756
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Tim King <taking@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
2021-10-25 21:11:43 +00:00
Robert Findley bcc6fa839d internal/typeparams: guard against generics in stdlib tests
Add a new package internal/typeparams/genericfeatures, factoring out the
check for usage of generics from the usesgenerics checker. For tests
operating on the standard library, this allows us to explicitly detect
usage of generics, rather than relying on a hard-coded list.

To make this work, I switched the go/ssa standard library test to use
go/packages rather than go/loader. This resulted in finding 457 packages
versus 676 (likely due to tests), but my assumption is that this is
still enough coverage.

Change-Id: I6ba32fb4068dd7f5eedb55c2081c642665ed124a
Reviewed-on: https://go-review.googlesource.com/c/tools/+/355972
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Tim King <taking@google.com>
2021-10-25 20:36:35 +00:00
Robert Findley 18096c5ad9 go/internal/gcimporter: add support for the Go 1.18 export data version
Add support for accepting the Go 1.18 export data version (2) to the
x/tools gcimporter, while preserving support for generic code at version
1.

For now, the exporter still outputs version 1.

For golang/go#49040

Change-Id: Ic4547e385ced72b88212d150bf16acaf200a010e
Reviewed-on: https://go-review.googlesource.com/c/tools/+/358034
Trust: Robert Findley <rfindley@google.com>
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Dan Scales <danscales@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-10-25 14:51:03 +00:00
Robert Findley ce04ca3bf6 go/internal/gcimporter: normalize implicit interfaces in export tests
The export data format does not yet support capturing whether interfaces
are implicit, so normalize them for the purposes of comparing type
parameter constraints.

Change-Id: I678fb5f5fe6481b9a1479176ca056a31d17bbd77
Reviewed-on: https://go-review.googlesource.com/c/tools/+/355971
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2021-10-15 00:16:20 +00:00
Robert Findley 3a269dc41f internal/typeparams: copy the term list algorithm from go/types
Add a go:generate script to sync the term list algorithm from go/types
into the internal/typeparams package. This allows us to experiment with
the API for type sets without committing to it in go/types.

Expose this algorithm via a new NormalizeInterface function, which
returns the normal form of an interface.

Change-Id: Idfe3b485cc29f67937f8d4a447e41d477be0b8e1
Reviewed-on: https://go-review.googlesource.com/c/tools/+/351830
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-10-14 22:21:32 +00:00
Robert Findley 074820e17b go/analysis/passes/usesgenerics: a new analysis to detect generic code
Add a new "usesgenerics" analyzer to report uses of features related to
generic programming.

This analyzer may be used to temporarily guard other analyzers until
they may be updated to support generic features.

Along the way, update the typeparams API to return the Info.Instances
map, rather than provide indirect access, so that it can be iterated.

Fixes golang/go#48790

Change-Id: Ia3555524beff6e19f0b9101582205e26e757c8da
Reviewed-on: https://go-review.googlesource.com/c/tools/+/355009
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Tim King <taking@google.com>
2021-10-12 22:26:24 +00:00
Robert Findley 942994fc75 internal/typeparams: use alias rather than indirection for IndexListExpr
Using aliases make it easier to translate code using the new APIs.

Change-Id: I9887569a3ba1d5b1434ac6cc185485433aca7090
Reviewed-on: https://go-review.googlesource.com/c/tools/+/352898
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-10-01 20:36:29 +00:00
Robert Findley 13c407cf48 go/ast/inspector: add support for the new IndexListExpr node
IndexListExpr nodes were being skipped by go/ast/inspector, due to not
having a type value. Add this value, along with a test that we can
inspect the new constructs in generic code.

Use a type alias in the typeparams package to achieve this, following
the pattern for new go/types nodes.

Change-Id: I894a9415a93806cc6dbb92cf190b2bdab368d5df
Reviewed-on: https://go-review.googlesource.com/c/tools/+/352896
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-10-01 19:30:53 +00:00
Robert Findley 2189684b10 internal/typeparams: use NewSignatureType, IsMethodSet, and Context
Update the internal/typeparams API proxy following CL 352615, CL
352616, and CL 353089.

Change-Id: Ib65a1876c7820945189e1dc953e1a82e98547a09
Reviewed-on: https://go-review.googlesource.com/c/tools/+/352852
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-10-01 15:55:45 +00:00
Robert Findley 1c35f2a5d7 internal/lsp/analysis: quick-fix to remove unnecessary type arguments
This CL adds a new infertypeargs analyzer, which finds call exprs where
type arguments could be inferred, and suggests a quick fix to simplify
them.

Along the way, may two changes to the supporting frameworks:
 - Initialized types.Info.Instances in go/packages
 - Fail analysis tests run with suggested fixes if formatting the
   resulting source fails.

Change-Id: Ib15e5bd7c26aa293c5fc18a4cff6bc047e9e31d2
Reviewed-on: https://go-review.googlesource.com/c/tools/+/351552
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2021-09-29 19:22:48 +00:00
Robert Findley 5dbd5e40d5 internal/typeparams: filter out API diffs related to types.Environment
This is a temporary work-around to allow the x/tools TryBots to pass on
the Go repo.

Change-Id: I122dfd0f2c034add913c859136ca6c08f916dde3
Reviewed-on: https://go-review.googlesource.com/c/tools/+/352897
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
2021-09-29 15:31:12 +00:00
Robert Findley df9a164fb3 Revert "internal/typeparams: hide go1.18 API usage behind a build constraint"
This reverts commit 0df0ca0f43.

Reason for revert: we've tagged x/tools@v0.1.7, and want to use the new APIs again.

Change-Id: I5c6f2075833948cc21896d1746e0c51cc0712bb1
Reviewed-on: https://go-review.googlesource.com/c/tools/+/352855
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
2021-09-28 23:30:19 +00:00
Robert Findley 0df0ca0f43 internal/typeparams: hide go1.18 API usage behind a build constraint
Temporarily hide usage of the new go/types API, so that we can safely
tag x/tools@v0.1.7 without risk of further breakage when these APIs
change.

Updates golang/go#48632

Change-Id: Idba02d09644622b3d973a684a76514c86eefa17f
Reviewed-on: https://go-review.googlesource.com/c/tools/+/352854
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
2021-09-28 22:34:40 +00:00
Robert Findley 284795867f internal/lsp/analysis: add a useany analyzer
Add an analyzer that checks for empty interfaces in constraint position,
that could instead use the new predeclared "any" type.

Change-Id: I6c11f74c479c2cba64b3b12e61d70d157f94393b
Reviewed-on: https://go-review.googlesource.com/c/tools/+/351549
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2021-09-22 22:01:06 +00:00
Robert Findley 0d12d39b86 internal/typeparams: update to the new Instances API
This CL updates the internal/typeparams package with the new API from CL
349629, switching Info.Inferred to Info.Instances.

Change-Id: I8f98dc39c844ef8891863c08b747f63924ab1c53
Reviewed-on: https://go-review.googlesource.com/c/tools/+/351250
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-09-21 18:37:00 +00:00
Robert Findley 91c880c34d go/internal/gcimporter: add support for exporting the 1.18 export format
This CL adds support for the Go 1.18 export format to iexport.go. This
support is based on cmd/compile/internal/typecheck/iexport.go, though
with a different types API and adjusted to build at Go versions below
1.18.

Specifically, this CL adds support for both column details in positions,
and generic types and functions. Along the way, some minor adjustments
are made to aid in debugging and testing.

Notably, after this CL the export data version produced by
go/gcexportdata will depend on the Go version used to build.

As part of this change, TestIExportData_stdlib is unskipped for Go 1.18.

Fixes golang/go#48392

Change-Id: I1b218e0acd4864de5c7b89706192273bc7850ffa
Reviewed-on: https://go-review.googlesource.com/c/tools/+/351029
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2021-09-21 02:24:06 +00:00
Robert Findley 0cffec9e30 go/internal/gcimporter: update iimport.go to support type parameters
This CL pulls in the latest changes from go/internal/gcimporter, while
avoiding breaking the build on older go versions. To help maintain
compatibility with older Go versions while minimizing the diff with the
standard library importer, the internal/typeparams package was
significantly expanded.

I decided to use type aliases in the internal/typeparams package on Go
version >= go1.18, and placeholder types on Go version < go1.18. This
reduces the amount of copying needed in the APIs, though it might not be
the best decision if we ever decide to export this package.
Documentation was also updated to be more concise and specific to the Go
version being used.

In order to actually fix the x/tools Trybot for packages using generics
in the standard library, we need to switch from the 'typeparams' build
constraint to the 'go1.18' build constraint. This means if we make any
additional API changes in go/types we'll have to submit them with a
broken x/tools Trybot and then immediately fix the x/tools build.

Change-Id: Ifa0b1c37b89dc549ee295fa3a959f03deda86e56
Reviewed-on: https://go-review.googlesource.com/c/tools/+/349949
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-09-15 00:13:36 +00:00
Robert Findley 076821bd2b internal/typeparams: update MultiIndexExpr to IndexListExpr
This type was renamed in CL 348609, based on discussion in the
go/ast proposal.

Change-Id: I3addcf1bb192ad03f59f599a7aec68a579178fd5
Reviewed-on: https://go-review.googlesource.com/c/tools/+/348629
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-09-08 23:54:26 +00:00
Koichi Shiraishi 2cae65cc5b internal/typeparams: follow changes to Type in the go/ast and go/types
As discussed on both proposals to CL 348375 (#47781) and
CL 348376 (#47916), Go core changed the 'T' word to 'Type'.
Follows those changes as well.

Change-Id: I52c354cdc7494aaf3c63bfb136efa86159175314
Reviewed-on: https://go-review.googlesource.com/c/tools/+/348509
Reviewed-by: Robert Findley <rfindley@google.com>
Trust: Robert Findley <rfindley@google.com>
Trust: Rebecca Stambler <rstambler@golang.org>
Run-TryBot: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
2021-09-08 19:08:28 +00:00
Koichi Shiraishi 3e0d083b85 internal/typeparams: update for recent API changes in go/types
This fixes the build when run with -tags=typeparams.

Change-Id: I94861e7f46e396804740a8e3c24f164380903a69
Reviewed-on: https://go-review.googlesource.com/c/tools/+/343129
Reviewed-by: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Hyang-Ah Hana Kim <hyangah@gmail.com>
2021-08-25 16:32:36 +00:00
Koichi Shiraishi 758a1a1db7 internal/typeparams: fix undefined *types.Named.TArgs method
Current tip Go runtime (7eaabae84d) haven't (actually removed)
implements *types.Named.TArgs() method.
Calculate and make targs using named.NumTArgs().

Change-Id: Ib6a61cf2f8da644f533891f9bbb30179f8a7df5c
Reviewed-on: https://go-review.googlesource.com/c/tools/+/342229
Reviewed-by: Robert Findley <rfindley@google.com>
Trust: Robert Findley <rfindley@google.com>
Trust: Rebecca Stambler <rstambler@golang.org>
Run-TryBot: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
2021-08-16 16:11:44 +00:00
Rob Findley 322816044c internal/typeparams: update x/tools for recent typeparams changes
Recent changes to the go/ast APIs for type parameters have broken the
internal/typeparams package when built with -tags=typeparams.

Fix this by adjusting the internal/typeparams API. Also update a few
tests accordingly.

Bump the build constraint used by the internal/typeparams package to
go1.18, as we are no longer compatible with the 1.17 typeparams API. It
is no long possible to opt in to type parameter specific functionality
1.17, which is fine as the dev.typeparams branch has moved to 1.18.

Even after these fixes, not all x/tools tests pass with go1.18. Some
completion tests are failing due to finding 'any' in types.Universe.

Change-Id: I5f92870aaf7853e531e3a154987f98520a52d70c
Reviewed-on: https://go-review.googlesource.com/c/tools/+/339349
Trust: Robert Findley <rfindley@google.com>
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-08-03 15:39:08 +00:00
Rob Findley 13cf844527 go/ast/astutil: fix panic when rewriting multi-argument type instances
Use the typeparams helper package to fix AST rewriting in go/ast/astutil
when encountering the new ListExpr type.

This is liable to break in the future when the go/ast API changes, but
at least it will be easy to find by virtue of using internal/typeparams.

Fixes golang/vscode-go#1551

Change-Id: Id34bbcdede9024ed9818bef6d73a19e993dd76a8
Reviewed-on: https://go-review.googlesource.com/c/tools/+/326131
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
2021-06-09 21:40:20 +00:00
Rob Findley 18795da840 internal/typeparams: a new helper library for working with generic code
Tools like gopls will want to add additional features to support working
with generic code, but need to continue to compile at older Go versions
that don't support type parameters.

This CL contains an initial draft of a helper library that may be used
to interrogate generic type information from go/ast and go/types without
having to guard the calling code with a build constraint.

I will use this library to implement some generics features for gopls.
Once we're confident in the API, it could potentially be exported as
x/tools/go/typeparams.

Change-Id: I0ad3050b57cf8d8e8dda7d350d18f5e50f4105ff
Reviewed-on: https://go-review.googlesource.com/c/tools/+/317451
Trust: Robert Findley <rfindley@google.com>
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-05-11 03:28:22 +00:00