Commit Graph

44261 Commits

Author SHA1 Message Date
Robert Griesemer a30aa6c43e go/types: fix contract embedding
Contract embedding is reduced to interface embedding. When embedding
interfaces:
1) use the correct interface to embed; and
2) don't leave type bounds residue on the incoming type arguments
   of the embedded interface/contract as those are also the type
   parameters of the embedding (outer) interface/contract.

Also:
- For now, print "any" instead of "interface{}" (empty interface)
  when printing type parameter lists to reduce clutter. Eventually
  we should not print anything for empty interface bounds, but we
  first must also group type parameters with the same type bounds.
- Print the type parameter subscript in type parameter lists to
  make it clearer which type parameter we are referring to.

Change-Id: Ic83516096387d0f512c4c76a8a8fe849d51e033a
2020-06-13 12:24:46 -07:00
Ian Lance Taylor 2044d8b94b cmd/go2go: use predeclared comparable in maps test
(But embedding comparable doesn't work yet.)

Change-Id: I977ca97597de47c79edb56b385c970392afb87f0
2020-06-13 12:24:46 -07:00
Robert Griesemer 10b59aa539 go/types: implement predeclared contract "comparable"
The comparable contract defines a magic method "==" which
enables comparisons with == and != . Comparable types
automagically implement this method.

TODO: If a type is not comparable but a comparable type is
expected, the error message can be confusing (missing ==
method).

Change-Id: Ie0d89b87c36d83549f7d869c18dd9786151adbae
2020-06-13 12:24:46 -07:00
Ian Lance Taylor e6f000d2f9 go/go2go: add maps test case
Change-Id: Iad570b218a7a3c2075f846cc86c58ec7e6b0e50c
2020-06-13 12:24:46 -07:00
Robert Griesemer 2bc31ee9f4 go/types: de-parameterize instantiated contracts when embedding them
More precisely, de-parameterize the corresponding interfaces when
embedding them in the corresponding type bounds (interfaces) for
the embedding contract. This prevents the embedded (and already
instantiated) interfaces from being instantiated again when the
outer interface is instantiated and avoids breaking an assertion.

See also the test case in testdata/issues.go2 for more details.

Change-Id: I70c9354849eda0c8a36905d0b80f4d3031542f30
2020-06-13 12:24:46 -07:00
Robert Griesemer 3935b33b31 go/types: avoid follow-on panic after assertion failure in subst
Also: Updated NOTES.

Change-Id: Iabc45e46aa76bacf5c6984dab6f4bcab74392562
2020-06-13 12:24:46 -07:00
Ian Lance Taylor 5a5b14a4b3 go/go2go: support type inference
We can now translate code that use type inference when calling generic
functions. A couple of test cases were adjusted to use it.

Change-Id: I53c2f3dd8f9fcdb44b4a8f592acee1a19ff46f22
2020-06-13 12:24:46 -07:00
Ian Lance Taylor 4172f069ae go/go2go: aggregate types.Info together for all packages
Change-Id: I4c73c417a1e7946422fd2e1035a2ce4406ca1cf3
2020-06-13 12:24:45 -07:00
Robert Griesemer 07a7684507 go/types: record inferred type arguments for parameterized calls
In api.go, introduce a new type `Inferred` which collects type arguments
and signature in one place and which can be extended if need be.

Change-Id: I014dd52641843e85ad408fdaa07eda121cf1c41c
2020-06-13 12:24:45 -07:00
Ian Lance Taylor 2eed31c177 go/go2go, cmd/go2go: build a GOPATH of translated packages
Also start a testsuite for cmd/go2go.

Change-Id: Ib693c79b3a7427ac2f6be3264469274157a39851
2020-06-13 12:24:45 -07:00
Ian Lance Taylor 1bf660ee7c go/go2go: initial support for contracts
Includes a small patch to go/types to correctly check whether type
parameters are comparable.

Change-Id: Icd3443e0cd968eabdbd55f90520b0c6ad485edf8
2020-06-13 12:24:45 -07:00
Robert Griesemer 3370013383 go/types: add support for imported contracts (denoted by qualified identifiers)
Original code by iant@. Slightly streamlined and fixed a bug in the
process (if there is a type error in the contract, we only must "use"
the type arguments if they exist in the first place).

Change-Id: Id404d2cd4fd081b9a8053e194bf643593093e397
2020-06-13 12:24:45 -07:00
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