Commit Graph

44435 Commits

Author SHA1 Message Date
Robert Griesemer e5e519e893 go/printer: cleanups around contract printing
- added more tests
- documented invariants and status quo

Change-Id: I74d75eee3936e659476fc5b428e8d11d5c5e4098
2020-06-13 12:24:47 -07:00
Ian Lance Taylor 01ab391f82 go/go2go: support instantiating with an instantiated type
Add metrics test case to test GO2PATH.

Change-Id: I48c8a418d743ccd80cdcf36fc043ca0e2dd932f4
2020-06-13 12:24:47 -07:00
Ian Lance Taylor 42884d7524 go/types: support setting the TArgs of a types.Named
Change-Id: I468b77165079ab30844e54b4988717be452cb648
2020-06-13 12:24:47 -07:00
Ian Lance Taylor 10efd1cfc6 go/printer, go/ast: gofmt improvements for generic code
Change-Id: I62f7c0f52ac9d51eacd2392083e1aa7799022559
2020-06-13 12:24:47 -07:00
Robert Griesemer e39082c80f go/types: remove vestiges of type unification in Checker.identical0
Type unification now happens through the new unifier code.
We can revert the functionality of Checker.identical back
to its original purpose of type comparison. This change
should make identity comparison faster again.

Change-Id: I844515b5f20a890152a0a5436f04c553b3b722e5
2020-06-13 12:24:47 -07:00
Robert Griesemer 5da07bf767 go/types: implemented (bidirectional) unifier and use it for type inference
- Implemented bidirectional unifier as a stand-alone mechanism
  separate from Checker.identical0.
- Use it instead of Checker.identical0 where we need unification.
- Missing: Bidirection functionality not fully implemented because
  we don't use it yet, but the basic outline is present.

Change-Id: I1666c9e4c9094eda749084bb69c700f1b5e879bb
2020-06-13 12:24:47 -07:00
Ian Lance Taylor 42e310c4a6 cmd/go2go: add Append and Copy to slices test
Change-Id: Ie6826d8c66ae33a377dac9e66ff8363439a2be6c
2020-06-13 12:24:47 -07:00
Ian Lance Taylor 4a8a353a22 go/go2go, cmd/go2go: add orderedmap example
Add transitive imports so that imports used by instantiated functions
and types work correctly.

Change-Id: Ie8fe0aaece7ca1bdbf201242870113d741ad3f37
2020-06-13 12:24:47 -07:00
Robert Griesemer 46b40480b6 go/types: updated documentation
Change-Id: Id84fd18dc0e1928c96bb50a1759ced1e56605d51
2020-06-13 12:24:47 -07:00
Robert Griesemer 74bad35110 go/types: updated README
Change-Id: Ib68114593c5c26f05e06e7326c1567d3f253c5a5
2020-06-13 12:24:47 -07:00
Ian Lance Taylor 01b5212797 cmd/go2go: point to testdata/go2path in package docs
Change-Id: I94857bb943503d1107b814e5111ccd6512c4a477
2020-06-13 12:24:47 -07:00
Ian Lance Taylor 0b55a72ba3 cmd/go2go: remove explicit type arguments in chans_test
They are no longer needed as the type checker now ignores channel
direction for type inference.

Change-Id: I06f2ffaf60a140747f1fc5c47135045e9004d551
2020-06-13 12:24:47 -07:00
Robert Griesemer e490a1bfb5 go/types: ignore channel directions for type inference
Change-Id: Ic7c61dfea7b43bb102bff077bf17d86b26fffba9
2020-06-13 12:24:47 -07:00
Ian Lance Taylor db2cf1336d go/go2go: instantiate all kinds of types
Change-Id: Iaf17de0e5556171e18d4b0c4ae7e3dd1e9f7c777
2020-06-13 12:24:47 -07:00
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