Commit Graph

44472 Commits

Author SHA1 Message Date
Ian Lance Taylor 39dc66f9c2 go/types: improve contract error messages
If a type or type parameter does not satisfy a contract because the
type or type constraint is not found in the list of type constraints,
change
    T does not satisfy C(T) (T type constraint int not found in interface{type ...})
to
    T does not satisfy C(T) (T type constraint int not found in [type ...])
Also, before this change, if the contract had any required methods,
those were also listed; now we just list the type constraints.

If a defined type does not satisfy a contract because the underlying
type is not found in the list of type constraints, change
    MyUint does not satisfy C(T) (MyUint not found in interface{type ...})
to
    MyUint does not satisfy C(T) (uint not found in [type ...])
That is, when describing the type that is not found, use the underlying type,
not the defined type.

Change-Id: I345836c44921e2e13e7f02041839b5eb4df75c45
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go2-dev/+/718718
Reviewed-by: Robert Griesemer <gri@google.com>
2020-06-13 12:24:49 -07:00
Ian Lance Taylor 7d185f0ba6 go/types: improve contract error messages
If a type parameter that has no type constraints is trying to satisfy
a contract that does have type constraints, change
    T does not satisfy C(T) (missing type int)
to
    T does not satisfy C(T) (T has no type constraints)

If a type parameter that has type constraints is trying to satisfy a
contract that has type constraints, but the parameter has type
constraints that the contract does not permit, change
    T does not satisfy C(T) (missing type int)
to
    T does not satisfy C(T) (T type constraint int not found in interface{type ...}

Change-Id: I3bd4f59b0544b382399d9b9d891a509ce62f56c3
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go2-dev/+/718426
Reviewed-by: Robert Griesemer <gri@google.com>
2020-06-13 12:24:49 -07:00
Robert Griesemer 67b6f218e9 go/types: fix type bound check
A type argument with an empty type list bound never satisfies
a type parameter with a non-empty type list bound.

Change-Id: Ic4206f74fd4625b4207baa28bdb0de0fc669b37e
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go2-dev/+/717296
Reviewed-by: Robert Griesemer <gri@google.com>
2020-06-13 12:24:49 -07:00
Jonathan Amsterdam 7a57f4d76e slices: fix Equal contract
slices.Equal only requires comparable, not Ordered.

Change-Id: I0150f1370e478aaa33bbd8788a1f9c9d33b85e06
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go2-dev/+/716483
Reviewed-by: Robert Griesemer <gri@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
2020-06-13 12:24:49 -07:00
Robert Griesemer 63d4750592 go/types: avoid crashes when printing/tracing invalid interfaces
Rewrite Interface.Empty to not be dependent on an interface being
complete. Be careful when printing invalid interfaces.

This fixes a couple of crashes when running tests with -v (trace)
mode enabled.

Change-Id: I4be416eac7e3fae83c08608ed74bf3987b2a7457
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go2-dev/+/716089
Reviewed-by: Robert Griesemer <gri@google.com>
2020-06-13 12:24:49 -07:00
Robert Griesemer a89350ee85 go/types: interfaces are comparable (bug fix)
Also: Updated NOTES (unrelated).

Change-Id: I12ce0465e408920d4cd430ab529f346281f4ac64
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go2-dev/+/716118
Reviewed-by: Robert Griesemer <gri@google.com>
2020-06-13 12:24:49 -07:00
Ian Lance Taylor 2b5e8d70c0 go/go2go: parse Go1 packages using go/types
When a Go2 package imports a Go1 package, then if the Go1 package is
in the standard library use the default importer, but otherwise
parse the package ourselves by recursively calling go/types.
This supports a Go2 package importing a Go1 package that imports
a Go2 package.

Change-Id: I8906d8ea5b81e6239652c0e46de79b5c1b59cfcd
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go2-dev/+/716063
Reviewed-by: Ian Lance Taylor <iant@google.com>
2020-06-13 12:24:49 -07:00
Ian Lance Taylor c2d8b59433 go/go2go: record types of instantiated AST types
Otherwise we don't know the type of an instantiated type in an
instantiated function.

Change-Id: If7eb75dd920a3c9fc1b6992addcd3b91b6d425a9
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go2-dev/+/715718
Reviewed-by: Ian Lance Taylor <iant@google.com>
2020-06-13 12:24:49 -07:00
Ian Lance Taylor 760a3244a7 go/go2go: only copy .go2 files of import to tmpdir
We are only going to translate the .go2 files of an imported package,
so we don't need to copy anything else to the temporary directory.
This fixes the case of an imported package with a subdirectory,
which previously crashed trying to copy the directory.

Change-Id: I54cfb43ad2a3e23a4e0d7201d9e6e2ade02b7469
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go2-dev/+/715029
Reviewed-by: Ian Lance Taylor <iant@google.com>
2020-06-13 12:24:49 -07:00
Ian Lance Taylor 6df3320d36 test: drop a 0 from gen/err000n.go2
Matches the g cases.

Change-Id: If0c2558b9304cdd73e01d8718303e8fb0fccaf34
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go2-dev/+/714999
Reviewed-by: Ian Lance Taylor <iant@google.com>
2020-06-13 12:24:49 -07:00
Ian Lance Taylor 5dac261e8b cmd/go2go: pass GO2PATH as GOPATH to regular go tool
Also when looking for packages specified on the command line,
search GO2PATH before trying to invoke "go list".

Change-Id: I9704ef86fa46b877ee195a40843a034d014c2a3d
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go2-dev/+/714998
Reviewed-by: Ian Lance Taylor <iant@google.com>
2020-06-13 12:24:49 -07:00
Ian Lance Taylor dbe1a544f9 go/go2go: run "go install" for Go 1 packages
The default importer looks for installed packages, not sources.

Change-Id: I0c85460fa5792953e78288347fc637e0ee540b15
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go2-dev/+/714780
Reviewed-by: Ian Lance Taylor <iant@google.com>
2020-06-13 12:24:49 -07:00
Ian Lance Taylor 39ba9cff56 cmd/go2go: add basic test for go2go build
Change-Id: I44109a4c0fe81d4c88e13200db1d2e6da576d262
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go2-dev/+/714759
Reviewed-by: Ian Lance Taylor <iant@google.com>
2020-06-13 12:24:49 -07:00
Robert Griesemer fd583fcbae go/types: update README
Change-Id: Ifc694498d0661c02907a1f39f2ea9f1535d8dbdf
2020-06-13 12:24:48 -07:00
Ian Lance Taylor 061f93c871 test: add test for package name in error messages
Also add support for errorcheckdir in Go 2 tests.

Change-Id: Id69577050917d02ca98c91d6184318286d75ac2f
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go2-dev/+/712204
Reviewed-by: Ian Lance Taylor <iant@google.com>
2020-06-13 12:24:48 -07:00
Ian Lance Taylor 1d379da30b go/go2go: don't expect type arguments after instantiated name
Change-Id: I639adb020890e29b96458db5dffdea31bd1f8ffe
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go2-dev/+/712186
Reviewed-by: Ian Lance Taylor <iant@google.com>
2020-06-13 12:24:48 -07:00
Robert Griesemer faf2cc0cb8 go/types: improve printing of type parameter lists
- leave type bounds away if empty (interface); don't write "any"
- boundle type parameters with the same type type bound
- simpler code for error message cleanup

Change-Id: I8c1b662479de185b2ebae47ed1d16482253dfe03
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go2-dev/+/710076
Reviewed-by: Robert Griesemer <gri@google.com>
2020-06-13 12:24:48 -07:00
Robert Griesemer f936401d53 go/types: use local and global type cache consistently (cleanup)
- store a *Named type in both caches
- update local cache also when we have a global cache hit

Change-Id: If4ab24a0c24d21f425a76dc51e4ec2ed495b26e4
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go2-dev/+/710128
Reviewed-by: Robert Griesemer <gri@google.com>
2020-06-13 12:24:48 -07:00
Robert Griesemer 81022d54ae go/types: remove instantiated type name hacks
Instead of creating a new instantiated type name "name<T1, T2...>"
keep the regular name for the corresponding type object since
these newly created types are never declared. This eliminates
the need to clean up the names (remove the <>'s) before they can
be used.

When printing a named type, simply decide what to print depending
on whether it is instantiated or not.

When looking up an instantiated type, simply create the instantiated
type name string for use in the (local and global) type caches.

Change-Id: I4b20d09ec64d8deefccdd338ffb2f78fdfd2101e
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go2-dev/+/710062
Reviewed-by: Ian Lance Taylor <iant@google.com>
2020-06-13 12:24:48 -07:00
Robert Griesemer f25fd9bf57 go/types: remove an empty line (test for code review)
Change-Id: I5595aa5ea2ae808c60dde1566d35bad8ad7761c5
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go2-dev/+/709260
Reviewed-by: Ian Lance Taylor <iant@google.com>
2020-06-13 12:24:48 -07:00
Ian Lance Taylor be37919cd7 go/types: better error message for incomparable type
If a type does not satisfy comparable, don't report "missing method ==".

Change-Id: Idae2477e134f73db9ba5fb94302eec84fefe98ef
2020-06-13 12:24:48 -07:00
Ian Lance Taylor d3ecc7c57c cmd/go2go: mention some tool restrictions in the doc
Change-Id: I8568fffa6570610d909b0f23fa9bdd3ab6097a78
2020-06-13 12:24:48 -07:00
Robert Griesemer ab835222b0 go/types: updated README (after rebasing on top of master)
Change-Id: I3d63346aa00b6de8a7ba725081affb155cc828f1
2020-06-13 12:24:48 -07:00
Robert Griesemer ac135a4bcf go/types: better error messages for type inference
Change-Id: I8a5b07eca76b084901cce6f1bae4f8d107d10701
2020-06-13 12:24:48 -07:00
Robert Griesemer 1ad26bf1a7 go/types: fix some internal comments
Change-Id: I50f9127219321c3f173b28bf42b03e01384d920a
2020-06-13 12:24:48 -07:00
Ian Lance Taylor 0c3eb6800e test: support errorcheck for .go2 files
Change-Id: I90f840c89d4437a8b9fbd4f08582b5cb0d2eec6d
2020-06-13 12:24:48 -07:00
Ian Lance Taylor fad64b3083 cmd/go2go: add pointer variant to graph package
Change-Id: Ib5cf28be42943ea816c64740f5698a5716d3dec1
2020-06-13 12:24:48 -07:00
Robert Griesemer 07382406ea go/types: fix satisfaction check of argument type against type bound
Per the draft design:

In a generic function body all method calls will be pointer method calls.
If necessary, the function body will insert temporary variables, not seen
by the user, in order to get an addressable variable to use to call the method.

When checking if a concrete type argument satisfies its type bound, assume
that concrete type is the type of an addressable value.

Change-Id: I9d40138755c38330448c010994b95a82f38e60e0
2020-06-13 12:24:48 -07:00
Robert Griesemer 6768c4f513 go/types: first cut at better error messages
- added Error.Full and Error.FullError which provide the full error
  message, possibly containing internal details
- Error.Msg and Error.Error provide a user-friendly error message

Change-Id: Id3044165331af71be31ef423cd2c9b8fe28accbd
2020-06-13 12:24:48 -07:00
Robert Griesemer 62683a7752 go/types: fix lookup of methods of pointers to type parameters
Since the type bound of a type parameter is an interface, no method
is found if the receiver is a pointer to a type parameter (pointers
to interfaces have no methods).

Ignore the indirection in this case.

Change-Id: Ie2af78b4cfb2f70b5d4f1d2afc631716a10ff7d9
2020-06-13 12:24:48 -07:00
Robert Griesemer f81ad49939 go/types: report an error when seeing pointer designation in contracts
Pointer designations were not recognized nor supported.
Now we recognize them and report an error until type-checking
can handle them.

Change-Id: I0deed536aa03c06c3e9ec60a5c2e186e916b970f
2020-06-13 12:24:48 -07:00
Robert Griesemer e03920500c go/printer: print pointer designation of type parameters in contracts
Also: Align start or methods and type lists.
Change-Id: Icae3be0ebcbc633d2ed88ff6f620dc078a7c698e
2020-06-13 12:24:48 -07:00
Robert Griesemer 49c8636998 go/parser: accept pointer designation for type parameters in contracts
Change-Id: Ib119b7cd22de7117db82b987a3cd4573cbb6f6c8
2020-06-13 12:24:48 -07:00
Ian Lance Taylor 29365a1738 cmd/go2go: rename graphs_test.go2 to graph_test.go2
Change-Id: I25cfedbf6c115db5545c0c12b96588a581e41a53
2020-06-13 12:24:47 -07:00
Ian Lance Taylor 87059675da go/go2go: support instantiating with an inferred local type
Add a graph example.

Change-Id: Iec533487f2757b9fdc883e4a57715ad993e11254
2020-06-13 12:24:47 -07:00
Ian Lance Taylor 5f4c825cf8 go/go2go: support imports that only defines funcs
That case was breaking when looking for a symbol to import to avoid an
unused package error.

Change-Id: I51ccacf36a95a16eac89071d5fa141608b759014
2020-06-13 12:24:47 -07:00
Robert Griesemer a31f6ae875 go/*: change representation of type lists in interface types, adjusted dependent code
Until now, types of type lists in interface types were collected in
a single expression list "Types". This made it not possible to gofmt
such interfaces while preserving the original layout of type lists.

This change represents types of type lists as part of an ast.FieldList.
The ast.InterfaceType.Methods field list now represents embedded interfaces,
methods, or type list types through ast.Fields. This preserves all position
information and thus permits accurate gofmt-ing.

The new representation is as follows: For an ast.Field f, if
- len(f.Names) == 0        : f.Type is an embedded interface
- f.Names[0].Name == "type": f.Type is a type of a type list
- otherwise                : f represents a method

Since "type" is a keyword, a Go field name cannot be "type".
Fields of types of type lists that share the same "type" keyword
in the source share the same f.Names[0] identifier (named "type"),
and the position of that identifier is the position of the "type"
keyword in the source.

Related changes:
- Adjusted go/parser to build the new representation.
- Adjusted go/printer and implemented formatting of type lists.
  (This is still not quite correct if there are comments, but
  that is fine-tuning).
- Adjusted go/types to work with the new representation.
- Implemented SplitFieldList and MergeFieldList in go2go
  translator to easily switch between old and new representation.
- Updated documentation.

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