Commit Graph

44236 Commits

Author SHA1 Message Date
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
Robert Griesemer 3fd481043b go/types: must have at least one type when checking type list properties
Fixes bug with generic min being accepted even though the contract
or interface bound doesn't enumerate any types (or is missing).

Change-Id: Icdfc62fbd2b73ece397d5b5f1ebe27e52ed9b32f
2020-06-13 12:02:33 -07:00
Robert Griesemer 32c4c5a5f8 go/*: move TParams field into ast.FuncType and adjust dependencies
This step further consolidates all parameter types (except for the
receiver) in an ast.FuncType which now matches more closely the
representation of a types.Signature. As a result, fewer parameters
need to passed around because we can just use an *ast.FuncType or
a *types.Signature instead.

As an immediate (and implicit) consequence, parameterized interface
methods now type-check. (But we cannot yet "implement" them with a
matching concrete type.)

Change-Id: I2ea24694ade9838488625ffec48d5e98070d1006
2020-06-13 12:02:32 -07:00
Robert Griesemer e3a0a7429a go/types: first steps towards parameterized methods
This is a generalization of type parameters to methods.
Being able to type-check them (even if we don't have an
idea how to actually implement such methods) is likely
going to inform the structure the type-checker: If the
code is organized correctly, the generalization should
mostly just work (and consist of the elimination of
extra checks).

So far, this first step only exposed some minor scoping
issues (where does the scope of type parameters start).

Change-Id: I8658ea8d1876a0ce9c62c0e9a7e943301e9cc19d
2020-06-13 12:02:32 -07:00
Robert Griesemer 464fb5c1e7 go/parser: allow method type parameters
Controlled via methodTypeParamsOk (internal flag).

Change-Id: I821a303f897f5a99ac42f667a3f592cbd1e98d0c
2020-06-13 12:02:32 -07:00
Robert Griesemer a9c105c1d7 go/parser: use named consts rather than bools to control paramater parsing (cleanup)
Change-Id: I5d0638f4a9ad396c0203fc88ea93fcc38945f1b4
2020-06-13 12:02:32 -07:00
Robert Griesemer bc236133bf go/types: implement interface embedding of interfaces with types
This follows the approach used for methods, but there is no error
reporting yet if a type is (explicitly) declared multiple times in
an interface.

Change-Id: I52428174ae278577a7c538b0817c6fb7af1c369e
2020-06-13 12:02:32 -07:00
Robert Griesemer d4338964b3 go/types: implement contract embedding
Also:
- updated README
- rebased on master

Change-Id: Ieafcdd94460e02964350abc98651d0348d6521f9
2020-06-13 12:02:32 -07:00
Robert Griesemer b6a8a0a86f go/types: first steps towards contract embedding
Incomplete but all the major steps are outlined now.

Change-Id: I05c0357072d8b2c9f85154d3dd14984d2134de62
2020-06-13 12:02:32 -07:00
Robert Griesemer af12458327 go/types: always create a bound for each contract parameter
Also:
- simplify Contract struct
- rename some variables for more consistency

Change-Id: Icd10b47ac97d11870c6d4590c154b7efe158c134
2020-06-13 12:02:32 -07:00
Robert Griesemer 24cbbe5229 go/types: use function body scope for type and ordinary parameters
Change-Id: Iab3cf89125d9e13e1dff86710b313770a520a54a
2020-06-13 12:02:32 -07:00
Robert Griesemer 97251af9c4 go/types: move tparams field out of Func, clean up Signature
Type parameters are now collected as part of signature type
checking, as they are part of the type not the object. This
opens the door to more uniform handling of type parameters,
also for concrete methods and interface methods (future use).

Cleaned up confusion between Signature.mtparams and tparams:
tparams are explicitly declared function type parameters.
mtparams is gone and replaced with rparams which are the
receiver type parameters used in the function.

Various related cleanups.

Change-Id: Id8422b57cc8fbc18ffdca12a69d890aef83c3f80
2020-06-13 12:02:32 -07:00
Robert Griesemer 6e52291881 go/types: factor out type parameter list declaration
Including minor related cleanups.

Change-Id: I9d6f6ce6d2460fed10aa0818baa3f53b6f723b39
2020-06-13 12:02:32 -07:00
Robert Griesemer 2741fd2ea1 go/types: rewrite Checker.collectTypeParams (fix TODO)
Change-Id: Ie504606340d6ce52c1d5241ba13ec02043ba5400
2020-06-13 12:02:32 -07:00