There are still a couple of issues with pointer type parameters
that have constraints containing type lists, but at least as far
as method constraints are concerned the code is now matching the
design.
Passes all.bash.
Change-Id: I7b7ad9e03bc6ed159dca2343d3fa6a04fc188659
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go2-dev/+/765805
Reviewed-by: Robert Griesemer <gri@google.com>
Implement intersection of (sum) type lists so we can correctly
compute a type bound's type list if a type bound embeds other
type bounds (interfaces).
For type bounds A, B we want to preserve the property
"A embeds B" implies "A satisfies B"
Because type bounds are interfaces this is already true when
considering method sets only. But type bounds also may contain
type lists, and for this property to be true, type lists must
be intersected.
The intersection of two non-empty type lists with different types
may be the empty list (nil), which means that no type satisfies
the type bound. Because we already use nil to indicate the absence
of a type list (i.e., ignoring methods, any type satisfies the type
bound) we need a separate representation. This CL introduces two
new internal types, bottom and top, which stand for "no type" and
"any type" (they represent the bottom and top of the type lattice).
Since nil is already used for what should be the top type, and we
rely on the fact that the zero value for an Interface type is a
valid empty interface, we keep using nil in type lists to indicate
the top type. Eventually we may want to clean this up.
This CL also refines the understanding of type parameters: The
underlying type of a type parameter must be itself (it cannot be
defined by the associated type bound as assumed in a prior CL).
Consider the declaration
type T(type P) P
which declares a new defined, generic type. The question is, what
is the underlying type of T, i.e., the type which T represents
with a (new) name? (This type is stored in the underlying field
of a Named type.) The only possible answer is P, because otherwise
instantiation of T with any other type Q wouldn't produce that type
Q.
Thus, when checking if an operation is applicable on a value of type
parameter type, we need to explicitly get the type parameter's
"operational type" which is defined by the type parameter's type
bound; specifically the type bounds's type list (which is represented
as a sum type).
Passes all.bash.
Change-Id: Iad7e99884fd8f40a70f75c6470f1817d74994f48
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go2-dev/+/764158
Reviewed-by: Robert Griesemer <gri@google.com>
The type-checker type-checks method type parameters like
function type parameters without any extra work. But the
current design draft does not permit them and the go2go
translator does not expect them.
Add a flag to enable them for type checker tests, but do
not accept them by default. Instead, type-check but report
an error.
Passes all.bash.
Change-Id: Iec02f4a951a3faccd2409d97d48bc4dd4536a750
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go2-dev/+/764012
Reviewed-by: Robert Griesemer <gri@google.com>
This matches the latest design draft. It's unclear what the
correct approach is here, and there is an easy work-around:
just assign to an interface variable first.
Passes all.bash.
Change-Id: Ic1d6ed3de6333505e2044a318ea05fb984b408cf
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go2-dev/+/764001
Reviewed-by: Robert Griesemer <gri@google.com>
These are from the latest version of the design draft.
There are still a couple of FromStrings test cases that don't yet work.
Change-Id: I057875820d8250012c06faeabda4637f0585b6f8
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go2-dev/+/763941
Reviewed-by: Ian Lance Taylor <iant@google.com>
If a type parameter's type bound contains a type list, we can view that as
the underlying type(s) of the type parameter. If there is no type list the
underlying type of a type parameter is itself.
This approach resolves several open issues and clarifies the relationship
between type bound type lists and the type parameters. It also provides a
sensible definition for the underlying type of a type parameter (it will
take some more experimentation to see if this view is holding up within
the axiomatic framework of generic Go).
To implement this new view, in this CL we introduce the (implementation-
internal) notion of a sum type. A sum type is a set of two or more
distinct types. With this, we can view an interface with a type list as
an interface that provides a _single_ underlying type, which may be a
sum type.
Defining the underlying type of a type parameter this way enables a lot
of so far failing tests to work. In other instances, we have to implement
type-checking for sum types and can ignore the connection with generic code.
This simplifies the implementation and leads to a better separation of
concerns.
As part of this change:
- Several test cases in testdata/todos.go2 now work as expected
and move to testdata/issues.go2.
- Fix, clarify, and add various comments.
Passes all.bash.
Change-Id: I25a435b2ccd0c9f9fda5369bc998e52fe120ee0b
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go2-dev/+/762619
Reviewed-by: Robert Griesemer <gri@google.com>
The implementation of conversions T(x) between integers and floating-point
numbers checks that both T and x have either integer or floating-point
type. When the type of T or x is a type parameter, the respective simple
predicate disjunction in the implementation was wrong because if a type list
contains both an integer and a floating-point type, the type parameter is
neither an integer or a floating-point number.
Change-Id: I007aa956007ab1a0228e0ee2fca05804686b404c
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go2-dev/+/759562
Reviewed-by: Robert Griesemer <gri@google.com>
The existing code collected empty fields. Collect the actual
field and added type string test.
(This was not found before because the generic tests are still
disabled in this port because of different position information.)
Change-Id: I959711892aac3c8f7587a4025c4b32f1598f5df3
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go2-dev/+/756088
Reviewed-by: Robert Griesemer <gri@google.com>
If a type bound expects one type parameter, permit omitting explicit
instantiation of the type bound in a type parameter list. In that case,
the type bound is instantiated with each respective type parameter
and becomes that type bound for that type parameter.
For example, given:
type Bound(type T) interface { ... }
the type parameter list:
(type P, Q Bound)
is syntactic sugar for:
(type P Bound(P), Q Bound(Q))
This is a generalization of the former change:
https://team-review.git.corp.google.com/c/golang/go2-dev/+/728411
Passes all.bash.
Change-Id: I0191dcf3fbccf6e777f01dfa8ccef5aba0310213
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go2-dev/+/751267
Reviewed-by: Robert Griesemer <gri@google.com>
1) Recognize pointer designation in type parameter lists.
2) Recognize "underlying" type designation in interface type lists.
Adjust a few tests.
Passes all.bash.
Change-Id: Ic33195f2802698f957cbbda1770c78dc4d8a5e2a
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go2-dev/+/751264
Reviewed-by: Robert Griesemer <gri@google.com>
A type list may now be preceeded with "underlying" "type":
interface {
// type list
underlying type int, int32, int64
// method
underlying(int) int32
}
Change-Id: I37ea2ce3ca0e56f1fc35542ae04481dc7665b17c
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go2-dev/+/750491
Reviewed-by: Robert Griesemer <gri@google.com>
A type list may now be preceeded with "untyped" "type":
interface {
// type list
untyped type int, int32, int64
// method
untyped(int) int32
}
Passes all.bash.
Change-Id: I745c5f05ab5d6013d585da278b27e1eb781c0762
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go2-dev/+/750483
Reviewed-by: Robert Griesemer <gri@google.com>
Accept type parameters marked with a '*' as in:
(type A, *B bound)
indicating that the Bound requires pointer methods for B.
The notation is accepted but not yet recorded in the AST.
While at it, clean up some residue in parameter parsing
and disable acceptance of contract notation.
Passes all.bash.
Change-Id: I8d0c4614322bfed885ab26ef2da65a57208a9552
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go2-dev/+/750582
Reviewed-by: Robert Griesemer <gri@google.com>
After rebasing on top of master, the go/types version of
this branch includes the latest changes of go/types in
the std library. Apply the same changes to types2.
(Created a CL containing the differences between the starting
point for types2 (commit 89463ec, which was a copy of a
prior go/types) and the current go/types, and then cherry-
picked those differences on top of types2.)
Passes all.bash.
Change-Id: I6137a476dc90f5dbd0b90e9fbe144dc7bc13a9e3
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go2-dev/+/748099
Reviewed-by: Robert Griesemer <gri@google.com>
Make an exact copy of go/internal/gcimporter in
cmd/compile/internal/importer. Serves as starting
point for a types2-based importer.
Change-Id: Ib676794622bc1cb1614e396df6b391f374757cda
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go2-dev/+/744662
Reviewed-by: Robert Griesemer <gri@google.com>
Snapshot of work in progress.
- Entire package converted to use syntax package.
(We still need to convert syntax.Operators to token.Tokens
in some cases so we can use the go/constant package.)
- Some code is commented out and/or unimplemented.
- The functionality provided by eval.go, eval_test.go, gotype.go
is not needed and was removed.
- Compiles cleanly and many tests run at this point.
- Skips tests that require import to work.
Passes all.bash.
Change-Id: I2f215d97c858c22058779a6a5b1a2065b51bd4af
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go2-dev/+/741794
Reviewed-by: Robert Griesemer <gri@google.com>
Make an exact copy of go/types in cmd/compile/internal/types2.
Serves as starting point for a cmd/compile/internal/syntax-
based type checker, initially using the go/types logic.
Change-Id: Ide782fdaefd591b7757e54be5c16421bb8e935b2
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go2-dev/+/741793
Reviewed-by: Robert Griesemer <gri@google.com>
Encode an interface list element as a Field (like methods) but with
the special name "type" (no method can have that name as it is a
keyword). Types belonging to the same type list share the same name
pointer. This preserves full position information and removes the
need for another list in InterfaceType.
Change-Id: Ib7f02f7272078445546e47b394ed9e112dd5821f
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go2-dev/+/741894
Reviewed-by: Robert Griesemer <gri@google.com>
The previous CL changed the parser.list implementation to not consume
the opening token anymore; this was changed so it could be used for
parsing type parameter lists which start with a "(" and a "type".
Consequently, parser.appendGroup was changed to consume the opening
"(" early, before parser.clearPragma was invoked. As a result, a
correctly placed pragma for the next declaration was consumed too
early, leading to an error with test/directive.go. Fixed.
Passes all.bash.
Change-Id: Ief2f71228697e5516ebdecb773fdc30438f1fa9f
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go2-dev/+/738661
Reviewed-by: Robert Griesemer <gri@google.com>
Specifically, this change accepts now:
1) Type parameters in type and function declarations, such as:
type B(type T) interface {
m(T) T
}
func f(type T B) (list []T) T
2) Type instantiations:
type T B(int)
3) Embedded instantiated types, with necessary extra parentheses:
type T struct {
(B(int)) // ()'s to distinguish from field B of type (int)
}
type T interface {
(B(int)) // ()'s to distinguish from method B with int argument
}
The compiler simply ignores the new constructs.
Change-Id: Iecb8354d3846d7a5786cbe7d92870d8a2d578133
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go2-dev/+/736539
Reviewed-by: Robert Griesemer <gri@google.com>
AcceptContracts is a global flag (api.go) to enable/disable contracts.
All go/types tests pass with the flag in either setting, i.e., all
go/types tests involving contracts have been rewritten or disabled.
To ensure the existing go2go code runs, AcceptContracts is set to true
for now.
If contracts are disabled, "comparable" is a predeclared interface
(without type parameters) rather than a predeclared contract (with
one type parameter).
Change-Id: I2ddade9ae38d61d9edca6020f3334d2215b84f1d
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go2-dev/+/734984
Reviewed-by: Robert Griesemer <gri@google.com>