Commit Graph

44249 Commits

Author SHA1 Message Date
Robert Griesemer 73bb221120 go/types: add types.Info.Inferred map recording inferred function signatures
Change-Id: Idc57c1662fe63edbe8f494961cb4dcda6db61e79
2020-06-13 12:24:45 -07:00
Robert Griesemer 1211c65e41 go/types: provide accessor to type arguments for instantiated types
Plus a couple of minor unrelated comment updates.

Change-Id: I38554c9ae9143172ea65da6c270fba10772c194b
2020-06-13 12:24:45 -07:00
Ian Lance Taylor 74ba054b38 go/go2go: start to handle inferred types
Also support for statements and inc/dec statements.

Change-Id: I9af474065d3b433f582422b739fc49ef266d2751
2020-06-13 12:24:45 -07:00
Robert Griesemer e27cae74ec go/types: assertions/type switches on generic types are strict
Assertions/type switches that are guaranteed to fail on generic
types are not permitted (in contrast to regular type assertions
and type switches).

Change-Id: Iaa5b96f094585cb206fdadaa501445f96f26c166
2020-06-13 12:24:45 -07:00
Robert Griesemer 86b6990e9e go/types: implement type assertions and switches over generic types
Change-Id: I5e8debe3fee832e7829bcf2d451bde9bdc50a600
2020-06-13 12:24:45 -07:00
Ian Lance Taylor fe586a7418 go/go2go, cmd/go2go: support importing Go2 packages
This support works for the top level test directory, but needs more
testing of "go tool go2go".

Change-Id: I01c4ce4a428535777b0fff1e6f8f39eee9005e04
2020-06-13 12:24:45 -07:00
Robert Griesemer 8578f6bfe6 go/types: address TODO (follow-up on previous commit)
Instead of rewriting the underlying AST for a receiver type in place,
explicitly pass the rewritten AST to Checker.collectParams.
Also, better comments in various places.

Change-Id: If64e80b2c6b4b57477a4a0bf87caaa2f4ea1bf21
2020-06-13 12:24:45 -07:00
Robert Griesemer f7bc9d382f go/types: support blank identifiers for unused receiver type parameters
Change-Id: I5d7d359d7453f7ed949a7cab7b85419f8f4ee2a9
2020-06-13 12:24:45 -07:00
Robert Griesemer 08e2d11638 go/types: update NOTES
Missed with prior commit.

Change-Id: If111c8c6df6663199a23f8a1d2c147114f34ba6f
2020-06-13 12:24:45 -07:00
Robert Griesemer 86c2daf9e9 go/parser, go/types: permit parentheses around embedded contracts
For symmetry with embedding in structs and interfaces.
Fixed an incorrect error message print in the process.

Change-Id: I295685438a22971edc610e6c51cfefd286eaffba
2020-06-13 12:24:45 -07:00
Ian Lance Taylor 5cc90acd4b go/build: add go/go2go to pkgDeps
Change-Id: I43c131a910c372d849095160e6feb958da906e19
2020-06-13 12:24:45 -07:00
Robert Griesemer 6c083a7972 go/types: use correct package when creating a new instantiated type
Fix provided by iant@.

Change-Id: Iec89f007cfe9b9b50351122a03550e64238f9c44
2020-06-13 12:24:45 -07:00
Robert Griesemer 3d8c442870 go/parser, go/types: accept parameterized embedded struct fields
To be able to distinguish between a named field with a parenthesized
type `foo (T)` and an embedded parameterized type `foo(T)` in structs
we permit the use of parentheses in embedded types. For instance, the
latter example will need to be written as `(foo(T))`.

The parser now accepts any parenthesized embedded type liberally
and assumes that type-checking makes sure only permissible types
are accepted. As a result, parsing of field declarations is now
massively simpler. Also, removed an unused function.

The type-checker now complains about invalid embedded types (as
opposed to an invalid AST); it also now accepts embedded parameterized
types.

Change-Id: Ib8c6cbea9a0b739f66ba266fb5f8b71f91b7b64e
2020-06-13 12:24:45 -07:00
Robert Griesemer 38c2ef660d go/types: implement substMap and use it for substitutions
Instead of passing around a type parameter list and type arguments,
create a substitution map from them and use that map instead.

Change-Id: Ia4a041d95bfaa98888c9c06812d33b3d2a79227d
2020-06-13 12:24:44 -07:00
Robert Griesemer 72d320ec6e go/types: don't compute parameter list twice for regular generic functions (optimization)
Avoid a substitution operation for non-variadic generic functions.

Change-Id: Id40c0078a05be3aa53cd5b8551fa9fe6107bcda8
2020-06-13 12:24:44 -07:00
Robert Griesemer 99c8ae0d28 go/types: cleanup instantiation of function signatures
Make sure that a generic signature is not generic anymore
after instantiation. Update various related comments.

Change-Id: I2ac81037e570dc3a96c138b85529f3d86030776a
2020-06-13 12:24:44 -07:00
Robert Griesemer 3ab56f5ff2 go/types: minor adjustment to an error message and comments
Change-Id: Ie9505d508507c9ed764ec725df1027c92bae0702
2020-06-13 12:24:44 -07:00
Robert Griesemer e70e49973e go/types: move method collection out of type declaration processing
Simplifies Checker.typeDecl. Also, methods can only be added to
top-level types; no need to try to collect them for local types.

Change-Id: I05b926df7ffa70c32c1dd5faed3c68617be8f270
2020-06-13 12:24:44 -07:00
Ian Lance Taylor 12a7da1eb0 go/go2go: add support for methods of parameterized types
Change-Id: I308eb692612cb8d6e7321c4972e90b102466b4c2
2020-06-13 12:24:44 -07:00
Robert Griesemer 1bcfb0add1 go/types: fix receiver type parameter inference
Make sure receiver type and actual receiver argument
match in "pointer-ness" before attempting to infer
any receiver type parameters.

Change-Id: Ie28004ce42ecef46e348424140b8d0c65e37cdaa
2020-06-13 12:24:44 -07:00
Robert Griesemer 5a2e660efa go/types: improved type strings for instantiated generic types
Better output for now, but the printing code is a hack and needs
a rethink and cleanup.

Change-Id: Ieb1f13a21645b48f9494ceda50f58017b31bbd82
2020-06-13 12:24:44 -07:00
Ian Lance Taylor b1322d38b6 go/go2go: support parameterized types
Added an accessor function to go/types:

// TParams returns the type parameters of the named type t, or nil.
func (t *Named) TParams() []*TypeName { return t.tparams }

Change-Id: Ife2322c73dd6eaecaed42655a57a37541661d1ed
2020-06-13 12:24:44 -07:00
Ian Lance Taylor f0d1b476a9 cmd/go2go: support "go2go translate a.go2 b.go2"
For the translate subcommand, support .go2 files as well as package names.
Creates a.go b.go.

Change-Id: Ica09ca447e7312dcec5fabbc68b365bc45d55ad5
2020-06-13 12:24:44 -07:00
Ian Lance Taylor 2687ed5586 go/go2go: simplify error handling in rewrite methods
Just store the error in the translator struct, rather than always
checking for an error and returning it up the stack.

Change-Id: I38ea2a282f61457eea123f852d9daf9337bcf9b8
2020-06-13 12:24:44 -07:00
Ian Lance Taylor 9ceb91ef5f go/go2go: cache type instantiations
Change-Id: Ie239e6ac7cfabf472970cd2aeddf337bcc242043
2020-06-13 12:24:44 -07:00
Ian Lance Taylor a480c1b795 go/go2go, go/types: support recursive instantiation
Permit a generic function to call a generic function.

Change-Id: Ic41b87eb4effd0d5eb9a6a209c98354b306d5449
2020-06-13 12:24:44 -07:00
Ian Lance Taylor 8238ad0ab5 test, go/go2go: support .go2 files in test directory
Add an initial test.

Change-Id: If1bd6922eebf6881d3ac5d93731078fb9ca7b032
2020-06-13 12:24:44 -07:00
Ian Lance Taylor a588cbcd8e go/go2go, go/types: preliminary support for rewriting and instantiation
Good enough to run this program:

package main

import "fmt"

func Print(type T)(s []T) {
	for _, v := range s {
		fmt.Println(v)
	}
}

func PrintInts(s []int) {
	Print(int)(s)
}

func main() {
	PrintInts([]int{1, 2})
}

Change-Id: I5ac205138085a63e7075b01ca2779b7eb71f9682
2020-06-13 12:24:44 -07:00
Ian Lance Taylor 10c3db1727 go/go2go: add line comments to rewritten files
Change-Id: I6fe3de668381d89131825ba00cc202f49c7971cf
2020-06-13 12:24:44 -07:00
Ian Lance Taylor f95754ecff cmd/go2go, go/go2go: new framework for translating generics to Go
Adds a new command go2go, invocable after installation as "go tool go2go".
The go2go command supports build, test, run, and translate subcommands.
These will translate .go2 files to .go files.
No actual translation is done yet; the files are simply rewritten.
So this is a framework that works if the .go2 files are pure Go 1 code.

Change-Id: I8183fd276c4c87577c428794fb87e8dde1fa398d
2020-06-13 12:24:44 -07:00
Robert Griesemer 7ad6dd2e38 go/types: add testdata/todos.go2 for documenting unimplemented features
Also: Updated NOTES file.

Change-Id: I9f760693a486433d5a496d155627cad330630e90
2020-06-13 12:24:43 -07:00
Robert Griesemer 352c86eca2 go/types: better error messages for range clauses over generic values
Change-Id: I7b4ad2734aea5ab508b35ab526f0953b4f99c3f3
2020-06-13 12:02:35 -07:00
Robert Griesemer 2b265905e9 go/types: better comments for range stmt code
Change-Id: Id7ccf3f47b5ba9d8edd023675122d5f975b89905
2020-06-13 12:02:35 -07:00
Robert Griesemer 172f10b1e2 go/types: first cut at supporting range over generic variables
TODO
- some channel errors are not reported for generic variables
- error messages should be better

Change-Id: Ie388d6811b605645ea092481eee3a850c7ceb77b
2020-06-13 12:02:34 -07:00
Robert Griesemer c8947e760e go/types: update README
Change-Id: I73eec3fa1ce247ae91e6acacfdd238a8314a3969
2020-06-13 12:02:34 -07:00
Robert Griesemer 4150c6a490 go/types: fix various issues with contract instantiation
Contract instantiation (= contracts with explicit type
arguments) should work now but has some implementation
restrictions:

- The type arguments provided to a contract must be
  type parameters from the type parameter list using
  the contract or the type parameter list of the en-
  closing contract (in case of contract embedding).

- Each type argument may be used at most once per
  contract expression.

Change-Id: Ia9f9d81e95d84f11ff3821b9f17b74eadab201f8
2020-06-13 12:02:34 -07:00
Robert Griesemer 203449cf09 go/internal/gccgoimporter: fix test
The typestring for embedded fields in structs was changed
by 8d30bd752d4 (type checking of function type parameters)
but this test was not updated.

Change-Id: I0d9438e3000939de09aa298c33ad1e9604a59fe8
2020-06-13 12:02:34 -07:00
Robert Griesemer 2ebcbd9efd go/printer: add missing test files
Change-Id: If5522bcf0f75a86b7aa36ed853a1349ad4a2a1f4
2020-06-13 12:02:34 -07:00
Robert Griesemer ee164bc407 go/test: steps toward instantiated contracts (partially working)
See testdata/tmp.go for a test that shouldn't succeed.

Change-Id: Iefa738e0b6df6c3abe74670f8bc1d62a3e8c4e46
2020-06-13 12:02:34 -07:00
Robert Griesemer 5d80216f87 go/types: ensure a TypeParam always has a non-nil type bound
Plus minor cleanups.

Change-Id: I2508a576ad56d55fb188aa78c861afd0d032cf80
2020-06-13 12:02:34 -07:00
Robert Griesemer 3272b2b60b go/types: factor out contract expression handling
Change-Id: I499cce2abd6cf0647ea6c0aa5f93bbd513020518
2020-06-13 12:02:33 -07:00
Robert Griesemer 312a8ea57c go/types: initial code to handle instantiated contract expressions
New code not yet used. Snapshot.

Change-Id: Id3f9172d5ebd198d34b66707eaf429b791374ea1
2020-06-13 12:02:33 -07:00
Robert Griesemer 3c2505cae9 go/types: use Interface.is for generic variable indexing/len/cap (cleanup)
Interface.is already takes care of iterating through all types of
a generic type bound; use it instead of writing custom loops each
time.

Change-Id: Ie0f91b27e5a7b65ea85a8c3847a954db1e9f24fe
2020-06-13 12:02:33 -07:00
Robert Griesemer 1a2a175a8e go/types: implement indexing of generic types
Change-Id: I88a488d9d279b1029dd8086ea1befaa7b5733df6
2020-06-13 12:02:33 -07:00
Robert Griesemer d2fce6bc64 go/types: implement len/cap for arguments of type paramater type
Change-Id: Ieeaa41573e5ec10df46ab25ec0a3563e3ae5b32b
2020-06-13 12:02:33 -07:00
Robert Griesemer c49fde5b11 go/types: various comment improvments (cleanup)
Change-Id: I0763f7ef202f9dec11abd4f204ab950b86e0272c
2020-06-13 12:02:33 -07:00
Robert Griesemer 45dd45561e go/parser: better error recovery for (invalid) local contracts
Change-Id: I76b0d2af3767978ca138d899a083a6e399fea0fe
2020-06-13 12:02:33 -07:00
Robert Griesemer b5ecc31064 go/parser, go/types: minor cleanups around type parameters
Change-Id: Ic2faf89a29193d1d50bb5bdccce0fda2301785ed
2020-06-13 12:02:33 -07:00
Robert Griesemer 2c0e53f518 go/parser, go/types: allow parenthesized embedded interfaces
This enables the distinction between a method M(int) and an
embedded instantiated (parameterized) interface (M(int)).

Change-Id: I150d1b1cd53a2b14ddc0ad6336d84078fcb41ad6
2020-06-13 12:02:33 -07:00
Robert Griesemer 9c75653a79 go/types: generalize/cleanup missingMethod to handle generic methods
This simplifies the use of missingMethod again but moving the lazy
instantiation of methods into missingMethod. Method comparison now
also takes method type paramaters into account.

This change enables the first examples of parameterized methods
to type-check.

Change-Id: I4c629fb7b1f8959184c6ce5196365893d11b197d
2020-06-13 12:02:33 -07:00