Commit Graph

44152 Commits

Author SHA1 Message Date
Robert Griesemer 704beb3881 go/parser, go/types: parse/type-check interface (literal) type constraints
Accept (parse and type-check) parameterized interfaces with literal
type constraints, as in contracts. For instance, instead of

	contract C(T) {
		T add(T) T
		T int, string
	}

one can write

	type C(type T) interface {
		add(T) T
		type int, string
	}

Their use as type bounds still needs some work, though.

Change-Id: I9d59681dc0ba38054ee5627be141fc6e8a78381b
2020-06-13 12:02:28 -07:00
Robert Griesemer 6db0b6d33a go/parser, go/types: various minor fixes around printing and tracing
- set correct token position for ast.ContractType
- print "best effort" interface position in "complete interface" traces
- don't print trace output for types that are already set up
- print contract "signtature" when printing Contract type

Also: rebased on top of today's master branch

Change-Id: I6a528ce51a4152c25ad5d83ad52e01df8e89d283
2020-06-13 12:02:28 -07:00
Robert Griesemer 234ccd01f3 go/types: print contract type strings
Change-Id: Id698420124d526eb481659856705798bc5648b91
2020-06-13 12:02:28 -07:00
Robert Griesemer 4fa7fd828d go/types: fix type inference and method calls with parameterized receivers
This change expands type inference to parameterized named types
and clarifies handling of type parameters during type inference
(specifically, an inferred type may be a type parameter itself).

The change also implements method selectors with parameterized
receiver types by using type inference to deduce the receiver
type parameters.

Add more tests and enable many disabled tests that now work
correctly.

Change-Id: I8e10f9166fec9a8454f14b8fb089230c70422a1b
2020-06-13 12:02:28 -07:00
Robert Griesemer 1a0bc57392 go/types: removed instantiate call, renamed instantiate2 => instantiate
Also: added a currently failing test case to testdata/tmp.go2

Change-Id: I605bffb457e3b3979b42a642010215630c0544c6
2020-06-13 12:02:28 -07:00
Robert Griesemer 45eb8a74f0 go/types: added another example to examples/types.go2
Also: disabled aggressive rewriting of methods upon type instantiation

(Need to first figure out what we really need to do.)

Change-Id: I6fba78d60963534ebfe130164058ac1fbed7e5a3
2020-06-13 12:02:28 -07:00
Robert Griesemer 47ddbd09af go/types: added example to examples/types.go2
Change-Id: Ib2e5d28c48bc818e1c1a54ae4888bbab916b758a
2020-06-13 12:02:28 -07:00
Robert Griesemer 3e2c9743cb go/types: write TypeParam id's using subscript digits (fun)
Change-Id: I0df6e534774b2616f67c29a1525ddb40dbed9b05
2020-06-13 12:02:28 -07:00
Robert Griesemer 8c7ab48154 go/types: remove Parameterized type and related code
The need for the Parameterized type was eliminated by
the prior commit.

Change-Id: I8a2a2ae19ec5fef9292885fc7d23f8950d917e2f
2020-06-13 12:02:27 -07:00
Robert Griesemer 703974d608 go/types: rewrote instantiation of named types
A parameterized named type is in one of two states: not yet
instantiated, or instantiated. It may be instantiated with
any type, including (outer) type parameters. An instantiated
parameterized type may needs its type parameters substituted
if it is part of an enclosing type (or function) that is
instantiated.

A Named type has an extra field targs which is set to the
type arguments if the type is instantiated.

As a result, we don't need a representation for (partially)
instantiated parameterized types anymore: the Parameterized type
is now unused (but still present in the code).

Added a unique id field to TypeParam types so they can be
printed with a unique name. This permits the creation of a
unique type name for instantiated types which then can be used
for hashing that type name.

Also, made interfaces as type bounds work in more cases.

A handful of places (in testdata/map.go2, map2.go2) are broken
(commented out) and need investigation.

Change-Id: I137d352b419b2520eadde1b07f823375e0273dab
2020-06-13 12:02:27 -07:00
Robert Griesemer 46442bd4a9 go/parser: complain if type parameters are not permitted
Always accept them but complain if they are not permitted.
The analogous parser check was lost in a recent change.

Also: Fix index computation in collectTypeParams.

Change-Id: I7cb89944984df6dadd7f603b56f0c3941d60f7c5
2020-06-13 12:02:27 -07:00
Robert Griesemer c47bad9a28 go/types: interfaces as type bounds starting to work
With various loose ends.

Change-Id: Idbcb4affc585f5bb14caa2045943a64a78d05d09
2020-06-13 12:02:27 -07:00
Robert Griesemer 51d599efa2 go/parser, go/types: steps towards accepting interfaces as type bounds
Also, rebased branch on top of master.

Change-Id: I758b19ae577a4554687b2206c016277c1ca21a45
2020-06-13 12:02:27 -07:00
Robert Griesemer 34f753b9d1 go/types: add testdata/linalg.go2 (not yet type-checked)
Also, added missing copyright notices in a few places.

Change-Id: Iaa39c10a1821a189cef604d2442e6f7ed7ac68a8
2020-06-13 12:02:27 -07:00
Robert Griesemer 660f7c38d1 go/types: first cut at supporting real/imag/complex with type parameters
Change-Id: Ic0dba1a5e758942a03d114df10cb5536cf432985
2020-06-13 12:02:27 -07:00
Robert Griesemer e4a39e916f go/types: update README
Change-Id: I81e8304da885f36a2f6a41f6809063e74e87b0ea
2020-06-13 12:02:27 -07:00
Robert Griesemer 1bddb66b7c go/types: more operators supported by contracts
Change-Id: I03493508813d13dd1a16cd675de68170da14b29f
2020-06-13 12:02:27 -07:00
Robert Griesemer cfbeecaabb go/types: first cut at type-checking type parameters with operators
- implemented for comparisons for now
- can type-check generic min
- also: rebased on top of Go tip

Change-Id: Id35582a59c4cddcb2b4ae9c7d7154ef8e41580ab
2020-06-13 12:02:27 -07:00
Robert Griesemer dc4f9fb373 go/types: first steps towards collecting/recognizing type constraints
Change-Id: Ic0d70db24ad9fb76a5bb128df654c131f173247e
2020-06-13 12:02:27 -07:00
Robert Griesemer 7b6471a756 go/types: use new Checker.completeInterface when constructing Contracts
Change-Id: I1b54ddb7ac69541fcab1f17e0fe1574288a5b9b9
2020-06-13 12:02:27 -07:00
Robert Griesemer 0ac0b8518c go/types: add testdata/map2.go2, update README
Change-Id: I5a859fe3298998810ba56f3b445fae8e1d177995
2020-06-13 12:02:27 -07:00
Robert Griesemer 5891f8c720 go/types: add cycle detection to IsParameterized
Also: Recognize method expressions with parameterized receivers
and report an error for now (not implemented yet).

Change-Id: I96405b2b739d8e9fff6e9840347d3e78bfe8b6ec
2020-06-13 12:02:27 -07:00
Robert Griesemer 1f06612063 go/types: fix a bug with type instantiation
Change-Id: I977bb5525a40d0e17c27bbc01d7c77426e1bad00
2020-06-13 12:02:26 -07:00
Robert Griesemer e73de77f33 go/types: some prep work for making `import "chans"` (testdata/map.go2) work
To make the import work, the import needs to be changed into
`import "./chan"`, the file testdata/chans.go2 needs to be
copies into a new directory testdata/chan and the file must
be renamed to chans.go.

But this exposes some problem with instantiating parameterized
types with other type parameters. Delaying this for now.

Change-Id: I1cd784fb89c2374131f3b1105493492eb0189abc
2020-06-13 12:02:26 -07:00
Robert Griesemer fbff5cc915 go/types: fix collection of receiver type parameters
... and various related smaller fixes

We can now type-check the slices, chans, and maps
examples from the design doc (with the maps example
prodcing an error because importing chans doesn't
work yet). Progress!

Change-Id: Ifc00359a9a1cdad3bde1659a7de2028ac2544469
2020-06-13 12:02:26 -07:00
Robert Griesemer 3c6943daac go/types: more work on checking contract satisfaction
Change-Id: Ibe54e448603c685d714310ff83d4193150d47ebd
2020-06-13 12:02:26 -07:00
Robert Griesemer b87cfd9558 go/types: more steps towards contract type checking
- moved type parameter collection out of resolver and into decl phase
- collect (meta-) type information (contracts) for type parameters
- first cut at checking contract satisfaction (methods only for now)

Change-Id: I46707969a172423738171aaea9d5282fb4b25a44
2020-06-13 12:02:26 -07:00
Robert Griesemer 590e4ea3f0 go/types: re-enable initial contract type checking
- collect contract methods in respective interfaces
- basic checking on contract type constraints

Contracts are not yet tested against or used to type check
function bodies.

Change-Id: I13b00c44524e599f92f1ba5b4b5d6734e2bf22e1
2020-06-13 12:02:26 -07:00
Robert Griesemer 628e3a2c6f go/printer: first steps towards printing type parameters and contracts
- type parameters can be printed
- empty contracts can be printed
- contract constraints are missing (only started)

Change-Id: I787898203aeb064b0b2ac49a7b858313cee5f45b
2020-06-13 12:02:26 -07:00
Robert Griesemer a65d9b58a9 go/types: fix type inference bugs for untyped arguments
Change-Id: Ic98be8b102521090108d288514ee2cf016a7bb46
2020-06-13 12:02:26 -07:00
Robert Griesemer d03ba9f74c go/types: fix a type instantiation issue, added slices.go2 example
Change-Id: If020572762039adf10973bccd072005084e17e4e
2020-06-13 12:02:26 -07:00
Robert Griesemer c4a20af9f1 go/types: incorporate last-minute changes added for GopherCon
Change-Id: I8e1ff18f073ca47ef1a5d8ea43c8b6a3bc4d27b8
2020-06-13 12:02:26 -07:00
Robert Griesemer d4272d0350 go/types: add examples/contracts.go2, extended examples/functions.go2
Change-Id: Icb1c0dfc6e752598a51d8039b5df77cde9a115ca
2020-06-13 12:02:26 -07:00
Robert Griesemer 0f7748af30 go/types: add examples directory
Also: fix substitution for cyclic types

Change-Id: I2d4eca6846c1ac9a2b4d0278246228e1f61aea08
2020-06-13 12:02:25 -07:00
Robert Griesemer 025f8ad924 go/types: add README describing the state of this prototype
Change-Id: I9c994be7600ed969064cf49e8edcbe431f1b7242
2020-06-13 12:02:25 -07:00
Robert Griesemer e2ccf6356b go/types: initial steps towards customizing method signatures
Needs more work and tests.

Change-Id: Ic54e5798c374254d55b46cb582d7cf87efe125d1
2020-06-13 12:02:25 -07:00
Robert Griesemer 3663c91f3a go/types: unpack parameterized method receivers
Also: Print debug trace when running tests and -v is set.

Change-Id: I741377f82845d4713eebeb26706cb47b55afebd3
2020-06-13 12:02:25 -07:00
Robert Griesemer be88be6e7a go/types: steps towards type-checking of methods with parameterized receivers
Change-Id: I80b4d4c248cb5e29933366322cca1d76c3ed5e23
2020-06-13 12:02:25 -07:00
Robert Griesemer 0ffe62a473 go/types: more fixes around parameterized types
Change-Id: I81e6e70a2833e7dbb17f5155386ed3c7a75dc2ac
2020-06-13 12:02:25 -07:00
Robert Griesemer d5d25288d4 go/types: remove typemap again (incomplete and currently not used)
Change-Id: If7483716b2ea7f66ef9fe5311ce000a3b6212524
2020-06-13 12:02:25 -07:00
Robert Griesemer 38fa8c1e9b go/types: fix instantiated type name computation (for now)
This needs more work eventually.

Change-Id: I33fce18c454113620df9bb1db71a60b38b0a1306
2020-06-13 12:02:25 -07:00
Robert Griesemer de712f2df8 go/types: implemented comparison of parameterized types; minor bug fixes
Also: Added -h flag to test framework; setting -h causes a panic when
an error is reported (for debugging).

Change-Id: Ib45d4ef38769f2ecdd3ce53fa3aed9fd99ef6bc8
2020-06-13 12:02:25 -07:00
Robert Griesemer 06ce58509d go/types: cleaned up parameterized type code a bit; enabled more tests
Change-Id: I579239f30f26e8a483c6f5dc379124d8a9eb4576
2020-06-13 12:02:25 -07:00
Robert Griesemer 2a76603101 go/types: fixed various issues in check.subst
Next known issue:

type List(type E) []E
var _ List(List(List(int))) = [](List(List(int))){}

This won't work because the RHS's composite literal type
is not fully instantiated - it has non-instantiated elements
which are ignored by Checker.instantiatedType. We could just
call Checker.subst w/o any parameters and have it walk the
incoming type and instantiate any element Parameterized types
but that is expensive and also appears to lead to stack over-
flow. Need to investigate.

Change-Id: I80bd09ab5f06e3991f198965424ce3322c7d6402
2020-06-13 12:02:25 -07:00
Robert Griesemer b8b66e1708 go/types: make tests pass again
Disallow parametrized alias types again. They need a bit more work.

Change-Id: I90fd101a4c61f8693ee850c5dbd6849a60b85bdc
2020-06-13 12:02:24 -07:00
Robert Griesemer 52934f1b1e go/types: significant steps towards complete parametrized type instantiation
testdata/typeinst2.go has some complex examples that pass now.
But code elsewhere seems broken. go test has some issues at the
moment. Committing anyway to not lose the snapshot.

Change-Id: Id8f753a7b098405e2580a45ca1707ef6476c198e
2020-06-13 12:02:24 -07:00
Robert Griesemer a76283d6e4 go/types: add primitive instantiated type map for canonicalization
Change-Id: I265105d483b6fdcee95f3770b9d6d774c1633638
2020-06-13 12:02:24 -07:00
Robert Griesemer 8142528bfd go/types: correctly substitute types when instantiating parametrized types
- use the right type when instantiating a type
- substitute defined types when underlying types are changing
- improved printing of parametrized types
- improved printing of instantiated types
- more top-level trace output

Change-Id: Ie8a65c9cc51e80925d3f580f54dcc6c0b5abe4c6
2020-06-13 12:02:24 -07:00
Robert Griesemer fac6c330ac go/types: more steps towards type-checking instantiated types
Change-Id: Iab2dab932faad704fc7141f7afe04eb80597ec57
2020-06-13 12:02:24 -07:00
Robert Griesemer e6f3c8a0b4 go/types: add missing test file
Change-Id: I36af54691bf757f78612549283e3eea1358dca0e
2020-06-13 12:02:24 -07:00