Commit Graph

44271 Commits

Author SHA1 Message Date
Ian Lance Taylor 22a2e4f0c2 cmd/go2go, go/go2go: add chans test case
To make this work, add support for instantiating pointer types.

Change-Id: Ifcecf863ad4194014a242d0a0d01ca3dc0288811
2020-06-13 12:24:46 -07:00
Ian Lance Taylor 8d18d04312 go/go2go: report all type checking errors
Previously we reported just the first one.

Change-Id: Id8fbb50f8c6f8ab56ce76dacda4c388b76ce6c80
2020-06-13 12:24:46 -07:00
Ian Lance Taylor c165308eb6 go/go2go: fill in missing ast.Expr and Stmt nodes
Change-Id: I151a923c72fb61c8f31dc6d3688bcd338ab472f7
2020-06-13 12:24:46 -07:00
Ian Lance Taylor c65d33fa3d go/go2go: handle ast.Ellipsis in instantiate and rewrite
Change-Id: I03d316677c2a1cf31fe28d55df1ef21515a0a2ba
2020-06-13 12:24:46 -07:00
Ian Lance Taylor 87d1d10fbe go/go2go: register packages by full import path, not package name
Change-Id: Ie40021111c97c6b30a733912e3106e8a87a0634a
2020-06-13 12:24:46 -07:00
Robert Griesemer 399d59ee46 go/types: make type inference work for self-recursive function calls
Cases such as

	func f(type T)(x T) {
		f(x)
	}

can now be type-checked. For more examples see examples/functions.go2
in the go/types directory.

Change-Id: Id661c84f086cc8ee45ec372ac4af543e68bebe8a
2020-06-13 12:24:46 -07:00
Ian Lance Taylor 67e62d261e cmd/go2go: add sets package to testdata/go2path
Change-Id: Id47ae62192c1c8507689f8a5b1bb347baa5db4ea
2020-06-13 12:24:46 -07:00
Ian Lance Taylor 9482f72042 cmd/go2go: add Map/Reduce/Filter/Max/Min to test cases
Change-Id: I586d4c548d4a982fe15d75128ea4f82be9db2416
2020-06-13 12:24:46 -07:00
Robert Griesemer 54d6b25ee0 go/types: updated README
Change-Id: Ib4a86240ca82722d522f8b1ce67c96695c7c4b13
2020-06-13 12:24:46 -07:00
Ian Lance Taylor 2902418378 cmd/go2go: use embedded comparable in maps test
Change-Id: I9f3a8a377597330daa43d1757ddd8a18560cf5d4
2020-06-13 12:24:46 -07:00
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