Commit graph

302 commits

Author SHA1 Message Date
Robert Griesemer dab16c1c90 cmd/compile/internal/types2: accept constraint literals with elided interfaces
When collecting type parameters, wrap constraint literals of the
form ~T or A|B into interfaces so the type checker doesn't have
to deal with these type set expressions syntactically anywhere
else but in interfaces (i.e., union types continue to appear
only as embedded elements in interfaces).

Since a type constraint doesn't need to be an interface anymore,
we can remove the respective restriction. Instead, when accessing
the constraint interface via TypeParam.iface, wrap non-interface
constraints at that point and update the constraint so it happens
only once. By computing the types sets of all type parameters at
before the end of type-checking, we ensure that type constraints
are in their final form when accessed through the API.

For #48424.

Change-Id: I3a47a644ad4ab20f91d93ee39fcf3214bb5a81f9
Reviewed-on: https://go-review.googlesource.com/c/go/+/353139
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
2021-10-01 17:18:34 +00:00
Dan Scales c2de759581 cmd/compile: make sure shapes have proper indexes for sub-instantiation
The computation for determining the shapes to use at the top of
getInstantation was not always creating shapes with the proper indexes.
If an instantiation is being called from another instantiated function,
we cannot just copy the shape types unchanged, because their indexes may
have changed. So, for type args that already shapes, we still call
Shapify() with the correct index.

Fixes #48645

Change-Id: Ibb61c6f9a3c317220fb85135ca87eb5ad4dcff9e
Reviewed-on: https://go-review.googlesource.com/c/go/+/353030
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
2021-09-29 16:09:04 +00:00
Keith Randall 301f6c8019 cmd/compile: keep methods on generic types from being deadcode eliminated
We currently make dictionaries contain a relocation pointing to
methods that generic code might use, so that those methods are not
deadcode eliminated. However, with inlining we can end up not using
the dictionary, making the reference from the dictionary to the method
no longer keep the method alive.

Fix this by keeping the dictionary alive at generic interface call sites.
It's a bit of overkill, as we only need to keep the dictionary statically
alive. We don't actually need it dynamically alive, which is what KeepAlive
does. But it works. It ends up generating a LEAQ + stack spill that aren't
necessary, but that's pretty low overhead.

To make this work, I needed to stop generating methods on shape types.
We should do this anyway, as we shouldn't ever need them. But currently
we do use them! issue44688.go has a test that only works because it calls
a method on a shape type. I've disabled that test for now, will work on it
in a subsequent CL.

Fixes #48047

Change-Id: I78968868d6486c1745f51b8b43be0898931432a2
Reviewed-on: https://go-review.googlesource.com/c/go/+/349169
Trust: Keith Randall <khr@golang.org>
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Dan Scales <danscales@google.com>
Reviewed-by: Than McIntosh <thanm@google.com>
2021-09-27 20:42:34 +00:00
korzhao 54079dfd7f cmd/compile: fix stencil call expression
In CL 349613,we have supported types.IdentityStrict() that does strict
type comparison.
Therefore, OCONVNOP becomes a possible case in call.X.Op().

Fixes #48604

Change-Id: Ibab27ffcf09656e3380314662f05f38294c1c6ed
Reviewed-on: https://go-review.googlesource.com/c/go/+/351857
Trust: Dan Scales <danscales@google.com>
Trust: David Chase <drchase@google.com>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Dan Scales <danscales@google.com>
2021-09-27 05:10:56 +00:00
korzhao aeea5bacbf test/typeparam: add a test case for issue48617
For #48617

Change-Id: I6c00b7912c441ac323a0adede63b7d4a9ae6f92d
Reviewed-on: https://go-review.googlesource.com/c/go/+/351858
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Dan Scales <danscales@google.com>
Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
2021-09-25 17:12:41 +00:00
korzhao ba7673069d cmd/compile: add required CONVIFACE nodes when translating OFUNCINST node
In CL 349614. we removed the early transformation code that
was needed to create the implicit CONVIFACE nodes.

Because the transformCall function is not called when translating OFUNCINST.
So we add in needed CONVIFACE nodes via typecheckaste().

Fixes #48598

Change-Id: If9dc7040cdc38ef2e52fdbb08c840095651426f2
Reviewed-on: https://go-review.googlesource.com/c/go/+/351856
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Dan Scales <danscales@google.com>
Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
2021-09-25 17:06:17 +00:00
Dan Scales 8854368cb0 cmd/compile: deal with blank nodes with typeparam type during stenciling
Deal correctly with a blank local variable with type param type. This is
a special case, because a blank local variable is not in the fn.Dcl
list. In this case, we must explicitly create a new blank node with the
correct substituted type, so we have correct types if the blank local
variable has an initializing assignment.

Fixes #48602

Change-Id: I903ea44b29934e180404e32800773b7309bf297b
Reviewed-on: https://go-review.googlesource.com/c/go/+/352117
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Trust: Dan Scales <danscales@google.com>
2021-09-25 01:24:46 +00:00
Cuong Manh Le cfd0868fc3 cmd/compile: fix delayTransform condition
The delayTransform only checks whether ir.CurFunc is generic function or
not. but when compiling a non-generic closure inside a generic function,
we also want to delay the transformation, which delayTransform fails to
detect, since when ir.CurFunc is the closure, not the top level function.

Instead, we must rely on irgen.topFuncIsGeneric field to decide whether
to delay the transformation, the same logic with what is being done for
not adding closure inside a generic function to g.target.Decls list.

Fixes #48609

Change-Id: I5bf5592027d112fe8b19c92eb906add424c46507
Reviewed-on: https://go-review.googlesource.com/c/go/+/351855
Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Dan Scales <danscales@google.com>
2021-09-24 19:40:03 +00:00
Dan Scales f6b5ffb5e1 cmd/compile: fix crawler for unexported fields with instantiated types
In markType() in crawler.go, mark the type of a unexported field if it
is a fully-instantiated type, since we create and instantiate the
methods of any fully-instantiated type that we see during import. As
before, we still do not mark the type of an unexported field if that
type is not generic. Fixes #48454 and most recent issue described in
48337. The included test is similar to the case in 48454.

Fixes #48454
Fixes #48337

Change-Id: I77a2a62b9e2647876facfa6f004201e8f699c905
Reviewed-on: https://go-review.googlesource.com/c/go/+/351315
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Keith Randall <khr@golang.org>
2021-09-24 18:21:14 +00:00
Robert Griesemer 1e57748892 cmd/compile, go/types: allow any anywhere (as a type)
Adjust types2 and go/types and some test cases.

Because `any` is not treated specially anymore in constraint
position we get additional errors in constraints if `any` is
used before Go1.18 (in addition to the error that type parameter
lists are not permitted before Go1.18).

Fixes #33232.

Change-Id: I85590c6094b07c3e494fef319e3a38d0217cf6f0
Reviewed-on: https://go-review.googlesource.com/c/go/+/351456
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
2021-09-22 19:19:49 +00:00
Keith Randall fa5c5043bc encoding/xml: truncate generic type names
xml names can't have any of '[],' in them, which might appear in
generic type names. Truncate at the first '[' so the names are still valid.

Fixes #48318

Change-Id: I110ff4269f763089467e7cf84b0f0c5075fb44b7
Reviewed-on: https://go-review.googlesource.com/c/go/+/349349
Trust: Keith Randall <khr@golang.org>
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
2021-09-21 17:25:35 +00:00
Dan Scales b6dddaccd7 cmd/compile: fix transform.AssignOp to deal with tricky case
When going to dictionary formats derived from the function
instantiations, I had broken out noder.Assignop() to deal specially with
shape types, but didn't quite get the tricky case right. We still need
to allow conversion between shape types, but if the destination is an
interface, we need to use CONVIFACE rather than CONVNOP.

Fixes #48453.

Change-Id: I8c4b39c2e628172ac34f493f1dd682cbac1e55ae
Reviewed-on: https://go-review.googlesource.com/c/go/+/350949
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Dan Scales <danscales@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
2021-09-20 17:44:56 +00:00
Dan Scales 9ebe7c8ec6 go/test: add a test for issue 48344
This was fixed by https://go-review.googlesource.com/c/go/+/349613
and https://go-review.googlesource.com/c/go/+/349614

Fixes #48344

Change-Id: I4c62109fd34b20566b07fcca87fb3946a5702fef
Reviewed-on: https://go-review.googlesource.com/c/go/+/350309
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
Trust: Dan Scales <danscales@google.com>
2021-09-20 16:26:11 +00:00
Dan Scales a83a558733 cmd/compile: fix export/import of range loop.
As with other recent issues, the Init field of a range loop was not
being handled properly. Generally, it is much better to explicitly
import/export the Init statements, else they are incorrectly added
before the associated node, rather than as the Init value of the node.
This was causing labels to not be correctly added to the range loop that
it is immediately preceding.

Made the ORANGE handling completely similar to the OFOR handling.

Fixes #48462

Change-Id: I999530e84f9357f81deaa3dda50660061f710e7c
Reviewed-on: https://go-review.googlesource.com/c/go/+/350911
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Trust: Dan Scales <danscales@google.com>
2021-09-20 00:13:47 +00:00
Dan Scales 07b30a4f77 cmd/compile: delay transformAssign if lhs/rhs have typeparam
This also requires that we sometimes delay transformSelect(), if the
assignments in the Comm part of the select have not been transformed.

Fixes #48137

Change-Id: I163aa1f999d1e63616280dca807561b12b2aa779
Reviewed-on: https://go-review.googlesource.com/c/go/+/347915
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
2021-09-17 23:04:15 +00:00
Dan Scales cea7a71d40 cmd/compile: fix generic type handling in crawler
There are a bunch of nodes beside ONAME and OTYPE, (such as OSTRUCTLIT
and OCOMPLIT) which can introduce a generic type that we need to mark.
So, just mark any generic type on any node in markInlBody. In this
particular issue, the type is introduced by an OSTRUCTLIT node.

Updates #48337

Change-Id: I271932518f0c1fb54d91a603e01a855c69df631d
Reviewed-on: https://go-review.googlesource.com/c/go/+/349909
Trust: Dan Scales <danscales@google.com>
Trust: Carlos Amedee <carlos@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
2021-09-17 19:50:04 +00:00
Dan Scales 137543bb93 cmd/compile: set IsShape based on type being in the Shapes pkg
Move ShapePkg to types, and change types.NewNamed to automatically set
IsShape/HasShape if a type is in the shapes pkg. This means that
imported shape types will automatically have the correct
IsShape/HasShape flags, even though we are not explicitly
exporting/importing those flags.

Updates #48337

Change-Id: I8b6131a663205f73f395943c9d0c8bdb2a213401
Reviewed-on: https://go-review.googlesource.com/c/go/+/349869
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
Trust: Dan Scales <danscales@google.com>
2021-09-14 23:07:15 +00:00
Cuong Manh Le ee91bb8319 cmd/compile: prevent typecheck importer reading type parameter twice
This is a port of CL 349009 to typecheck importer.

Fixes #48306

Change-Id: Iec3f078089346bd85f0ab739896e079940325011
Reviewed-on: https://go-review.googlesource.com/c/go/+/349011
Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Dan Scales <danscales@google.com>
2021-09-14 02:53:17 +00:00
Cuong Manh Le b8c802b116 cmd/compile: prevent importReader reading type parameter twice
The importReader always reads type parameter before declaring type stub
declaration. Thus, for recursive type, the type parameter is going to be
read twice, cause the bound more than once error.

To fix this, only read the type parameter after declaring stub obj, thus
r.doDecl can see the type was already inserted and terminate the
recursive call earlier.

Fixes #48280

Change-Id: I272e2f214f739fb8ec71a8628ba297477e1b7755
Reviewed-on: https://go-review.googlesource.com/c/go/+/349009
Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Dan Scales <danscales@google.com>
2021-09-14 02:52:21 +00:00
korzhao 42057e9848 cmd/compile: save the note of fields when translating struct
Fixes #48317

Change-Id: I756ae6253022870071004332dd8f49169307f7e6
Reviewed-on: https://go-review.googlesource.com/c/go/+/349013
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Dan Scales <danscales@google.com>
Trust: Dan Scales <danscales@google.com>
2021-09-13 21:48:41 +00:00
korzhao c981874a5a cmd/compile: fix implement for closure in a global assignment
If closure in a global assignment and has a method receiver.
We should assign receiver as a global variable, not a local variable.

Fixes #48225

Change-Id: I8f65dd6e8baf66a5eff24028d28ad0a594091add
Reviewed-on: https://go-review.googlesource.com/c/go/+/348512
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Dan Scales <danscales@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
Trust: Dan Scales <danscales@google.com>
2021-09-09 21:28:56 +00:00
Matthew Dempsky fb84e99eb7 test: add compiler regress tests for #46461
gri@ reports that types2 now correctly handles when type parameters
recursively refer back to the parameterized type, so we might as well
add tests to exercise that. Unified IR also correctly handles
importing and exporting these types, but -G=3 currently does not.

Updates #46461.

Change-Id: I272102aa08c40c980b9aeeca9f834291dfbbcc3e
Reviewed-on: https://go-review.googlesource.com/c/go/+/348738
Trust: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-09-09 19:37:25 +00:00
Keith Randall 19457a58e5 cmd/compile: stenciled conversions might be NOPs
A generic conversion might be required for when converting T->interface{}.
When stenciled with T=interface{}, then that conversion doesn't need
to do anything.

Fixes #48276

Change-Id: Ife65d01c99fbd0895cb7eec79df9e93e752b1fa5
Reviewed-on: https://go-review.googlesource.com/c/go/+/348736
Trust: Keith Randall <khr@golang.org>
Run-TryBot: Keith Randall <khr@golang.org>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
2021-09-09 15:34:08 +00:00
Tobias Klauser b86e8dd0f3 test/typeparam: fix issue48094b test build
CL 347529 broke the longtest builders due to missing -G=3 flag when
compiling the added test.

Change-Id: I73007801dd6871a8cf3554e957d247f5f56fd641
Reviewed-on: https://go-review.googlesource.com/c/go/+/348330
Trust: Tobias Klauser <tobias.klauser@gmail.com>
Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
2021-09-09 09:06:46 +00:00
Cuong Manh Le 376a079762 cmd/compile: fix unified IR panic when expanding nested inline function
When reading body of inlining function, which has another inlined
function in the body, the reader still add this inlined function to
todoBodies, which it shouldn't because the inlined function was read
already.

To fix this, introduce new flag to signal that we are done construting
all functions in todoBodies, thus the addBody shouldn't add anything
to todoBodies then.

Updates #48094

Change-Id: I45105dd518f0a7b69c6dcbaf23b957623f271203
Reviewed-on: https://go-review.googlesource.com/c/go/+/347529
Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2021-09-09 04:37:47 +00:00
Cuong Manh Le 2481f6e367 cmd/compile: fix wrong instantiated type for embedded receiver
In case of embedded field, if the receiver was fully instantiated, we
must use its instantiated type, instead of passing the type params of
the base receiver.

Fixes #47797
Fixes #48253

Change-Id: I97613e7e669a72605137e82406f7bf5fbb629378
Reviewed-on: https://go-review.googlesource.com/c/go/+/348549
Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Dan Scales <danscales@google.com>
2021-09-09 03:31:51 +00:00
Dan Scales 3fff213ac2 cmd/compile: add CONVIFACE nodes needed in generic code due to assignments
Added new function earlyTransformAssign() to add needed CONVIFACE nodes
due to assignments in generic functions.

Fixes #48049

Change-Id: I7cd9cee6ecf34ed2ef0743d1b17645b9f520fa00
Reviewed-on: https://go-review.googlesource.com/c/go/+/347914
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
2021-09-07 23:27:08 +00:00
Dan Scales a9a01a3fbd cmd/compile: remove now-unneeded SetHasTParam() for cached ptr element
We had code in NewPtr() to set the HasTParam/HasShape flag as needed for
the cached ptr element if it wasn't set correctly based on its Elem.
This was causing the race mentioned in the issue.

But that setting code is no longer needed, as long as we call
SetRParams() soon after calling NewIncompleteNamedType(), before
creating/translating the underlying type (which we do). The
HasTParam/HasShape attribute can only come from setting of rparams or a
direct typeparam/shape somewhere in the underlying type, both of which
don't depend on recursion, etc. (as long as the rparams are set early).
Added a check that HasTParam/HasShape are set correctly for the cached
pointer/slice elems in NewPtr() and NewSlice().

Fixes #48191

Change-Id: Ide7d82efb77ae97901e75b2e6c65bd1bfc25e0ee
Reviewed-on: https://go-review.googlesource.com/c/go/+/348089
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
2021-09-07 22:23:36 +00:00
Dan Scales 80783558b0 cmd/compile: make sure imported instantiated types have their methods created
We should be putting a newly instantiated imported type in
Instantiate/doInst onto the instTypeList, so its methods/dictionaries
are instantiated. To do this, we needed a more general way to add a
type to instTypeList, so add NeedInstType(), analogous to
NeedRuntimeType(). This has the extra advantage that now all types
created by the type substituter are added to instTypeList without any
extra code, which was easy to forget. doInst() now correctly calls
NeedInstType().

This is a bit aggressive, since a fully instantiated type in a generic
function/method may never be used, if the generic method is never
instantiated in the local package. But it should be fairly uncommon for
a generic method to mention a fully instantiated type (but it does
happen in this bug).

Fixes both cases mentioned in the bug.

Fixed #48185

Change-Id: I19b5012dfac17e306c8005f8595a648b0ab280d0
Reviewed-on: https://go-review.googlesource.com/c/go/+/347909
Trust: Dan Scales <danscales@google.com>
Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
2021-09-07 20:37:05 +00:00
Cuong Manh Le 2a4845257f cmd/compile: fix deadlock in (*Named).load
For lazy import resolution, there's reentrancy issue with (*Named).load
method, when "t.resolve(t)" can lead us to the same named type, thus
(*Named).load is called recursively, causing the deadlock.

The main problem is that when instantinate a type, we calculate the type
hashing, including TParams. Calling t.TParams().Len() triggers the
reentrancy call to "(*Named).load".

To fix this, just not checking TParams().Len() if we are hashing.

Updates #48185

Change-Id: Ie34842d7b10fad5d11fbcf75bb1c64a89deac6b8
Reviewed-on: https://go-review.googlesource.com/c/go/+/347534
Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
2021-09-07 17:38:14 +00:00
korzhao 6226020c2f cmd/compile: make sure that the names created for instantiated type are the same
Now we have two functions that create names for instantiated types.
They are inconsistent when dealing with byte/rune type.

This CL makes instTypeName2 reuse the code of typecheck.InstTypeName

Fixes #48198

Change-Id: I4c216b532cba6618ef9b63fd0b76e8f1c0ed7a75
Reviewed-on: https://go-review.googlesource.com/c/go/+/347491
Reviewed-by: Dan Scales <danscales@google.com>
Trust: Dan Scales <danscales@google.com>
Trust: Keith Randall <khr@golang.org>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
2021-09-07 03:56:13 +00:00
korzhao ecfff58fb8 cmd/compile: fix delay transformation in *subster.node()
Add OCALL, OCALLFUNC, OCALLMETH, OCALLINTER, ODYNAMICDOTTYPE transformations to the CALL check switch statement.

Fixes #48042

Change-Id: Ied93efd979c5b2c56b72fad26fccfd9f887361d3
Reviewed-on: https://go-review.googlesource.com/c/go/+/345949
Trust: Keith Randall <khr@golang.org>
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Dan Scales <danscales@google.com>
2021-09-06 14:02:24 +00:00
Robert Griesemer df4c625d88 cmd/compile: disable type list syntax for the compiler
Add (temporary) syntax.AllowTypeLists mode to control the
acceptance of type lists; the compiler doesn't set it,
but existing syntax and types2 tests do so that the code
remains exercised while it exists.

Adjust various tests to use the type set notation.

Change-Id: I798e607912552db6bfe38a7cd4324b74c6bf4d95
Reviewed-on: https://go-review.googlesource.com/c/go/+/347249
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
2021-09-02 01:06:17 +00:00
korzhao 5670ff4ae5 cmd/compile: fix conversions from TypeParam to interface
If the TypeParam has all the methods of an interface, allow conversions from TypeParam to interface

Fixes #47708

Change-Id: I40a82a31f6ea9354130dbe3bcfc83537094bf12c
Reviewed-on: https://go-review.googlesource.com/c/go/+/342509
Reviewed-by: Dan Scales <danscales@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
Trust: Dan Scales <danscales@google.com>
Trust: Keith Randall <khr@golang.org>
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
2021-09-01 16:45:26 +00:00
Dan Scales 2dd7b770de cmd/compile: fix missing case for shape double-check function
Missing case types.TUNSAFEPTR in parameterizedBy().

Also realized there was the same missing case in the type substituter
(*Tsubster).Typ().

Fixes #48103

Change-Id: If71f1a6ef80932f0e2120d4c18b39a30189fd8fe
Reviewed-on: https://go-review.googlesource.com/c/go/+/346669
Reviewed-by: Keith Randall <khr@golang.org>
Trust: Dan Scales <danscales@google.com>
2021-09-01 02:51:18 +00:00
Dan Scales 891470fbf7 cmd/compile: fix handling of Defn field during stenciling
When the Defn field of a name node is not an ONAME (for a closure
variable), then it points to a body node of the same function/closure.
Therefore, we should not attempt to substitute it at the time we are
substituting the local variables. Instead, we remember a mapping from the
Defn node to the nodes that reference it, and update the Defn fields of
the copied name nodes at the time that we create the new copy of the
Defn node.

Added some comments to the Defn field of ir.Name.

Moved the Defn (and Outer code, for consistency) from namelist() to
localvar(), since Defn needs to updated for all local variables, not
just those in a closure. Fixed case where .Defn was not being set
properly in noder2 for type switches. Fixed another case where the Defn
field had to be updated during transformSelect() because the Defn node
was being completely changed to a new node.

Fixed some spacing in typeswitch2.go

Fixes #47676
Fixes #48016

Change-Id: Iae70dd76575f4a647c1db79e1eba9bbe44bfc226
Reviewed-on: https://go-review.googlesource.com/c/go/+/346290
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
2021-08-31 19:07:50 +00:00
Keith Randall f27d6a23b0 cmd/compile: builtins may be in the unsafe package
Now that unsafe.Sizeof and friends can operate on generic parameters,
and evaluate to non-constants, we need to export/import them correctly.

Fixes #48094

Change-Id: If3ebf77255385cd5462e13fb7ced8b157ba3cf5b
Reviewed-on: https://go-review.googlesource.com/c/go/+/346469
Trust: Keith Randall <khr@golang.org>
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
2021-08-31 17:21:38 +00:00
Robert Griesemer 68152359fd cmd/compile/internal/types2: disallow aliases for generic types
The existing approach (alias name stands for generic type name)
is an exception: it's the only place where a generic type could
be used without explicit instantiation. The correct solution is
currently under discussion (see proposal issue #46477).

This CL requires that the RHS of an alias type declaration be
an instantiated non-generic type. If #46477 is accepted, the
implementation will require proper representation of alias
types.

Change-Id: Ie85b923213a64f39837e56e38e14757458272b93
Reviewed-on: https://go-review.googlesource.com/c/go/+/346294
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
2021-08-31 16:43:46 +00:00
Dan Scales 8f4c020660 cmd/compile: fix bug with Inferred targs
We were using the type from the wrong Node (the partially filled-in
FUNCINST) rather than the original function node - which is pointed to
by the OFUNCINST)) to set the final fully-substituted type of the
OFUNCINST. So fixed the node reference. Also, added check so we don't do
any work at all if the OFUNCINST already has all type args filled in.

Added few extra cases to the test file issue48030.go, to cover
fully-specified type args, partially inferred type args, and fully
inferred type args.

Fixes #48030

Change-Id: If9e4f2e0514d68b9d241f30c423259133932b25b
Reviewed-on: https://go-review.googlesource.com/c/go/+/346229
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
2021-08-30 21:22:35 +00:00
korzhao b602daea1b cmd/compile: fix error when revcType is ptr in selectorExpr
Fixes #48056

Change-Id: I13ca4caadbabf02084f66ab28b4cf0c4a3705370
Reviewed-on: https://go-review.googlesource.com/c/go/+/346049
Reviewed-by: Dan Scales <danscales@google.com>
Trust: Dan Scales <danscales@google.com>
Trust: Keith Randall <khr@golang.org>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
2021-08-30 17:13:00 +00:00
citizen233 a29d9aad7a test/typeparam/sliceimp.dir: fix typo in a.go
Fixing a typo, comparision -> comparison

Change-Id: I369f95c251f155bc6a9a6b86077bcf1ab245fc3f
Reviewed-on: https://go-review.googlesource.com/c/go/+/345950
Reviewed-by: Alberto Donizetti <alb.donizetti@gmail.com>
Reviewed-by: Keith Randall <khr@golang.org>
Trust: Alberto Donizetti <alb.donizetti@gmail.com>
Trust: Keith Randall <khr@golang.org>
2021-08-30 01:55:58 +00:00
wdvxdr f29abccd8a test: add test cases for issue47892.
This issue has been fixed in https://golang.org/cl/345411

Fixes: #47892

Change-Id: I13dd3814650913da065e5f24a0c61d30adb0633a
Reviewed-on: https://go-review.googlesource.com/c/go/+/345229
Reviewed-by: Dan Scales <danscales@google.com>
Trust: Dan Scales <danscales@google.com>
Trust: Keith Randall <khr@golang.org>
Trust: Alberto Donizetti <alb.donizetti@gmail.com>
Run-TryBot: Alberto Donizetti <alb.donizetti@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
2021-08-29 14:10:21 +00:00
Dan Scales d7a43e8912 cmd/compile: support type C comparable
Support 'type C comparable' properly by using the same logic as for
'type T error', since ErrorType and ComparableType are entirely
analogous.

Added support for 'any' type as well, as requested by Robert. (For the
future - we can't currently have 'any' anywhere other than in a
constraint.)

Fixes #47966

Change-Id: I68bd284ced9a8bfca7d2339cd576f3cb909b1b83
Reviewed-on: https://go-review.googlesource.com/c/go/+/345174
Trust: Dan Scales <danscales@google.com>
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-08-28 16:00:51 +00:00
Keith Randall 6a35e07512 cmd/compile: fix stenciling of conversions between interfaces
Conversions need to allow nil through.
We do that using a CONVIFACE instead of a DOTTYPE.

Also reorganize how nonempty interfaces are handled.
For nonempty to empty, a simple CONVIFACE suffices.
For nonempty to nonempty, we need to call the runtime to get the
new itab. Use the entry from the dictionary to identify the
target interface type (if parameterized).

Fixes #47925

Change-Id: I4ffeed964318bb3e270b06f558e6ab9c5bfc7188
Reviewed-on: https://go-review.googlesource.com/c/go/+/344830
Trust: Keith Randall <khr@golang.org>
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Dan Scales <danscales@google.com>
2021-08-27 16:41:29 +00:00
Keith Randall 4f0dedca71 cmd/compile: fix parameterized interfaces
type I[T any] interface{}

This is an interface, but it has a type parameter.
We need to distinguish that from an interface that is not parameterized.

That means when doing type substitution on an interface with
parameters, we need to make a new one.

Same for non-empty interfaces. Even if the type parameter is not
used in any method, we sill need to make a new type.

Similar case to tstruct, above.

Change-Id: I23ad9f21d2c4ef675bf3f7d84899d9e4919d05e7
Reviewed-on: https://go-review.googlesource.com/c/go/+/344578
Trust: Keith Randall <khr@golang.org>
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Dan Scales <danscales@google.com>
2021-08-27 15:03:09 +00:00
Dan Scales d7e2e2ec2b cmd/compile: delay fillinMethods to deal with mutually-recursive types
We need to delay fillinMethods until we get to a top-level type, so we
know all the TFORW types have been filled in, and we can do the
substitutions required by fillinMethods.

Fixes #47710

Change-Id: I298de7e7753ed31a2c2b1ff04f35177a8afc7a66
Reviewed-on: https://go-review.googlesource.com/c/go/+/345149
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
2021-08-27 01:42:38 +00:00
Dan Scales c9e05fdcf7 cmd/compile: fix reference to generic type needed by crawler
This problem happens when you create a new local type that uses an
imported generic type (maybe just by instantiating it), and then that
local type needed to be included as part of an export. In that case, the
imported generic type is does not have a declaration in the local
package, so it is not necessarily created in types1, so the
crawler/export doesn't work.

To fix this issue, we just need to add a call to g.obj() for the base
generic type, to make sure that it will exist if needed later in the
compilation or for the crawler during export.

Fixes #47514

Change-Id: Ie756578f07ad0007de8a88ae909cf7534a22936e
Reviewed-on: https://go-review.googlesource.com/c/go/+/345411
Reviewed-by: Keith Randall <khr@golang.org>
Trust: Dan Scales <danscales@google.com>
2021-08-26 20:18:58 +00:00
korzhao 3d667671ad cmd/compile: fix function contains no TParam in generic function
Fixes #47948

Change-Id: I446a9548265d195ae4d88aff6b1361474d1b6214
Reviewed-on: https://go-review.googlesource.com/c/go/+/344910
Trust: Alexander Rakoczy <alex@golang.org>
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Alexander Rakoczy <alex@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Dan Scales <danscales@google.com>
2021-08-25 19:06:16 +00:00
wdvxdr 08d4cc20ca cmd/compile: fix stencil call expression.
Fixes: #47878

Change-Id: I369350813726fd518b4eab2b98f43bf031a6dee6
Reviewed-on: https://go-review.googlesource.com/c/go/+/344210
Reviewed-by: Dan Scales <danscales@google.com>
Trust: Dan Scales <danscales@google.com>
Trust: Keith Randall <khr@golang.org>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
2021-08-25 16:37:31 +00:00
Dan Scales 099b819085 cmd/compile: fix CheckSize() calculation for -G=3 and stencils
Because the Align/Width of pointer types are always set when created,
CalcSize() never descends past a pointer. Therefore, we need to do
CheckSize() at every level when creating type. We need to do this for
types creates by types2-to-types1 conversion and also by type
substitution (mostly for stenciling). We also need to do
Defer/ResumeCheckSize() at the top level in each of these cases to deal
with potentially recursive types.

These changes fix issue #47929 and also allow us to remove the
special-case CheckSize() call that causes the problem for issue #47901.

Fixes #47901
Fixes #47929

Change-Id: Icd8192431c145009cd6df2f4ade6db7da0f4dd3e
Reviewed-on: https://go-review.googlesource.com/c/go/+/344829
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
2021-08-25 15:30:19 +00:00
Keith Randall c2f96e686f cmd/compile: mark ODYNAMICDOTTYPE as an expression that can panic
Fixes #47924

Change-Id: I4325b3c4ed9d369d9ea778478285436e1b2ab08a
Reviewed-on: https://go-review.googlesource.com/c/go/+/344571
Trust: Keith Randall <khr@golang.org>
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Dan Scales <danscales@google.com>
2021-08-24 18:31:34 +00:00
Dan Scales 5b64381155 cmd/compile: fix naming of types inside instantiations
Issues 47713 and 47877 were both due to problems with the names used for
instantiated functions/methods, which must be in sync with the names
used by types2.

 - Switched to using NameString() for writing out type arguments in
   instantiation names. This ensures that we are always adding the
   package to type names even for the local package. Previously, we were
   explicitly adding the package name for local packages, but that
   doesn't handle the case when the local type is embedded inside a
   pointer or slice type. By switching to NameString(), we fix #47713.

 - types1 and types2 write out 'interface {' differently (vs.
   'interface{') and we were already handling that. But we needed to add
   similar code to handle 'struct {' vs 'struct{'. This fixes issue
   #47877.

While fixing these bugs, I also moved some duplicated code (which
include some of the changes above) into a common function addTargs(). I
also moved InstType() name to subr.go, and renamed: MakeInstName ->
MakeFuncInstSym and MakeDictName -> MakeDictSym.

Also removed a couple of ".inst..inst." prefix checks which are
irrelvant now, since we don't add ".inst." anymore to function
instantiations.

Fixes #47713
Fixes #47877
Fixes #47922

Change-Id: I19e9a073451f3ababd8ec31b6608cd79ba8cba36
Reviewed-on: https://go-review.googlesource.com/c/go/+/344613
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
2021-08-24 18:30:13 +00:00
Dan Scales 8eeb1bff1d cmd/compile: reuse same node for global dictionaries
Change stencil.go:getDictionaryValue() and reflect.go:getDictionary() to
reuse any existing name node that has been created for the needed global
dictionary. Otherwise, these functions may set the Def on a specific
dictionary sym to two different name nodes, which means the first node
will not satisfy the invariant 'n.Sym().Def.(*ir.Name) == n' (which is
the assertion in this issue).

Fixes #47896

Change-Id: I1e7ae1efd077a83c7878b4342feb6d28d52476cc
Reviewed-on: https://go-review.googlesource.com/c/go/+/344609
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
Trust: Dan Scales <danscales@google.com>
2021-08-24 00:01:29 +00:00
Dan Scales be1a693477 cmd/compile: fixes for non-constant Sizeof/Alignof/Offsetof
Includes Robert's suggested fix in validate.go to not fail on
non-constant alignof/offsetof/sizeof calls. Further changes to wait on
transforming these calls until stenciling time, when we can call
EvalConst() to evaluate them once all the relevant types are known.

Added a bunch of new tests for non-constant Sizeof/Alignof/Offsetof.

Fixes #47716

Change-Id: I469af888eb9ce3a853124d919eda753971009b3e
Reviewed-on: https://go-review.googlesource.com/c/go/+/344250
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
Trust: Dan Scales <danscales@google.com>
2021-08-23 22:55:34 +00:00
Keith Randall 3081f817da cmd/compile: always remove receiver type from instantiated method values
If a type T has a method foo, then

var t T
var i interface{} = t.foo

The type of foo is a method type, but the type of t.foo should be a
standard function type. Make sure we always do that conversion.

Fixes #47775

Change-Id: I464ec792196b050aba1914e070a4ede34bfd0bfa
Reviewed-on: https://go-review.googlesource.com/c/go/+/343881
Trust: Keith Randall <khr@golang.org>
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Dan Scales <danscales@google.com>
2021-08-23 19:27:46 +00:00
Keith Randall 8486ced8b0 cmd/compile: copy captured dictionary var to local var
When starting a closure that needs a dictionary, copy the closure
variable to a local variable. This lets child closures capture that
dictionary variable correctly.

This is a better fix for #47684, which does not cause problems
like #47723.

Fixes #47723
Update #47684

Change-Id: Ib5d9ffc68a5142e28daa7d0d75683e7a35508540
Reviewed-on: https://go-review.googlesource.com/c/go/+/343871
Trust: Keith Randall <khr@golang.org>
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Dan Scales <danscales@google.com>
2021-08-23 17:51:41 +00:00
Matthew Dempsky c9912780ab cmd/compile: enable -G=3 by default
This CL changes cmd/compile's -G flag's default from 0 to 3, which
enables use of the new types2 type checker and support for type
parameters. The old type checker is still available with
-gcflags=all=-G=0.

The CL also updates the regress test harness to account for the change
in default behavior (e.g., to expect known types2 changes/failures).
However, the -G=0 mode is still being tested for now.

Copy of CL 340914 by danscales@, minus the cmd/internal/objabi.AbsFile
change (handled instead by CL 343731) and rebased to master branch.

Updates #43651.

Change-Id: I1f62d6c0a3ff245e15c5c0e8f3d922129fdd4f29
Reviewed-on: https://go-review.googlesource.com/c/go/+/343732
Trust: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
2021-08-21 00:24:02 +00:00
korzhao 97d17dc023 test/typeparam: add a test case for issue46591
Fixes #46591

Change-Id: I4875092ecd7760b0cd487e793576ef7a9a569a0e
Reviewed-on: https://go-review.googlesource.com/c/go/+/343970
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
Trust: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
2021-08-20 21:00:08 +00:00
Keith Randall 687f2acf6a cmd/compile: only use dictionaries for conversions to type parameters
Conversions to regular concrete types should not be rewritten during
stenciling.

Fixes #47740

Change-Id: I2b45e22f962dcd2e18bd6cc876ebc0f850860822
Reviewed-on: https://go-review.googlesource.com/c/go/+/342989
Trust: Keith Randall <khr@golang.org>
Trust: Dan Scales <danscales@google.com>
Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Dan Scales <danscales@google.com>
2021-08-18 22:34:31 +00:00
Yasuhiro Matsumoto 4012fea822 all: fix typos
Change-Id: I83180c472db8795803c1b9be3a33f35959e4dcc2
Reviewed-on: https://go-review.googlesource.com/c/go/+/336889
Reviewed-by: Rob Pike <r@golang.org>
Reviewed-by: Austin Clements <austin@google.com>
2021-08-17 13:54:10 +00:00
Dan Scales b7b790a71a cmd/compile: fix CONVIFACE case converting interface to empty interface
We need an extra case in convertToDictionary. In the case of an operand
which is an interface and converting to an empty interface, we don't
want to get the run-time type from the dictionary (which would be the
run-time type of the interface). We want to do a type-assert to the
empty interface.

Change-Id: I414247210168153151272fab198bfe82ad7b1567
Reviewed-on: https://go-review.googlesource.com/c/go/+/342009
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
Trust: Dan Scales <danscales@google.com>
2021-08-17 13:23:13 +00:00
Keith Randall 5a40100141 cmd/compile: fix dictionaries for nested closures
Capturing dictionary closure variables is ok.

Fixes #47684

Change-Id: I049c87117915e0c5a172b9665bfac2f91064b2d4
Reviewed-on: https://go-review.googlesource.com/c/go/+/342050
Trust: Keith Randall <khr@golang.org>
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Dan Scales <danscales@google.com>
2021-08-16 16:55:27 +00:00
Keith Randall 2fbf6aafe7 [dev.typeparams] cmd/compile: handle interface type parameters in type switches
Change-Id: I9bba21a64d7e9f42395b6fcdf8aa3ca01cf131dc
Reviewed-on: https://go-review.googlesource.com/c/go/+/340912
Trust: Keith Randall <khr@golang.org>
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Dan Scales <danscales@google.com>
2021-08-10 01:56:50 +00:00
Robert Griesemer e4cfa2f6da [dev.typeparams] cmd/compile/internal/types2: parameterized functions must have a body
Add the respective check and add missing bodies to tests.
Use {} as body for functions that don't return a result.
Use { panic(0) } as body for functions that return a result.

For #47069.

Change-Id: Ia5d7525c9c036baf8a955d13bff448401e08235e
Reviewed-on: https://go-review.googlesource.com/c/go/+/340911
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
2021-08-10 01:20:34 +00:00
Dan Scales 9f4d6a8359 [dev.typeparams] cmd/compile: call transformArgs before early typecheckaste in noder
In the cases where we do an early call to typecheckaste() in noder to
expose CONVIFACE nodes, we need a preceding call to transformArgs().
This is needed to allow typecheckaste() to run correctly, in the case of
f(g()), where g has multiple return values.

I also cleaned up the code a bit and commented the code in Call(), and
we do the call to typecheckaste() in several more cases.

In stencil.go:stencil(), I moved the transformCall earlier for the
OCALLMETH/ODOTMETH case, just as I did in my previous CL for
OCALL/OFUNCINST. By doing this, transformArgs no longer needs to deal
with the extra dictionary args. Therefore, I was able to simply
transformArgs() to look like typecheckargs() again, and make use of
RewriteMultiValue directly.

Updates #47514

Change-Id: I49eb82ac05707e50c2e2fb03e39458a70491d406
Reviewed-on: https://go-review.googlesource.com/c/go/+/340531
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
2021-08-09 19:43:09 +00:00
Keith Randall ca3c6985cd [dev.typeparams] cmd/compile: implement generic type switches
Add a new dynamicType node, which is used as a case entry when
the type being switched to is generic.

Change-Id: Ice77c6f224b8fdd3ff574fdf4a8ea5f6c7ddbe75
Reviewed-on: https://go-review.googlesource.com/c/go/+/339429
Trust: Keith Randall <khr@golang.org>
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Keith Randall <khr@golang.org>
Reviewed-by: Dan Scales <danscales@google.com>
2021-08-09 18:41:45 +00:00
Robert Griesemer 0811108670 [dev.typeparams] cmd/compile/internal/types2: fix make with type parameter argument
For make with a type parameter argument, the structural type of
the type parameter's constraint determines what make is making.

Change-Id: I3b48f8ce3236b7624e0638b5f5be208c5915c987
Reviewed-on: https://go-review.googlesource.com/c/go/+/339899
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
2021-08-06 20:34:46 +00:00
Dan Scales ac78501b9c [dev.typeparams] cmd/compile: make sure closures inside generic funcs are not compiled
Closures inside generic functions were being added to the g.target.Decls
list during noding, just like other closures. We remove generic
functions/methods from g.target.Decls, so they don't get compiled
(they're only available for export and stenciling). Most closures inside
generic functions/methods were similarly being removed from
g.target.Decls, because they have a generic parameter. But we need to
ensure no closures in generic function/methods are left remaining in
g.target.Decls, since we don't want them transformed and compiled.

So, we set a flag in (*irgen) that records when we are noding a
top-level generic function/method, and don't add any closures to
g.target.Decls when the flag is true.

Updates #47514

Change-Id: Id66b4c41d307ffa8f54cab6ce3646ade81606862
Reviewed-on: https://go-review.googlesource.com/c/go/+/340258
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
2021-08-06 18:50:46 +00:00
Robert Griesemer bb5608dd5d [dev.typeparams] cmd/compile/internal/types2: implement type sets with term lists
This CL resolves several known issues and TODOs.

- Represent type sets with term lists and using term list abstractions.

- Represent Unions internally as a list of (syntactical) terms.
  Use term operations to print terms and detect overlapping union
  entries.

- Compute type sets corresponding to unions lazily, on demand.

- Adjust code throughout.

- Adjusted error check in test/typeparam/mincheck.dir/main.go
  to make test pass.

Change-Id: Ib36fb7e1d343c2b6aec51d304f0f7d1ad415f999
Reviewed-on: https://go-review.googlesource.com/c/go/+/338310
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
2021-08-05 19:36:47 +00:00
Dan Scales 3cdf8b429e [dev.typeparams] cmd/compile: fixing case where type arg is an interface
In this case, we can't use an itab for doing a bound call, since we're
converting from an interface to an interface. We do a static or dynamic
type assert in new function assertToBound().

The dynamic type assert in assertToBound() is only needed if a bound is
parameterized. In that case, we must do a dynamic type assert, and
therefore need a dictionary entry for the type bound (see change in
getGfInfo). I'm not sure if we can somehow limit this case, since using
an interface as a type arg AND having the type bound of the type
arg be parameterized is a very unlikely case.

Had to add the TUNION case to parameterizedBy1() (which is only used for
extra checking).

Added a bunch of these test cases to 13.go, which now passes.

Change-Id: Ic22eed637fa879b5bbb46d36b40aaad6f90b9d01
Reviewed-on: https://go-review.googlesource.com/c/go/+/339898
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
2021-08-05 17:33:41 +00:00
Keith Randall b730a26729 [dev.typeparams] cmd/compile: put shape types in their own package
Put shape types in the top level package called ".shape".
Name them using the serialization of the shape name, instead of
the .shapeN names.

This allows the linker to deduplicate instantiations across packages.

Not sure that this is entirely correct, as shapes in this package
may reference other packages (e.g. a field of a struct). But it seems
to work for now.

For the added test, when you look at the resulting binary (use the -k
option with run.go) it has only one instantiation of F, and 4 call sites:

$ objdump -d a.exe | grep _a\.F
 1053cb0:	e8 8b 00 00 00 	callq	139 <_a.F[.shape.*uint8]>
 1053ce9:	e8 52 00 00 00 	callq	82 <_a.F[.shape.*uint8]>
_a.F[.shape.*uint8]:
 1053d90:	e8 ab ff ff ff 	callq	-85 <_a.F[.shape.*uint8]>
 1053dc9:	e8 72 ff ff ff 	callq	-142 <_a.F[.shape.*uint8]>

Change-Id: I627f7e50210aabe4a10d0e2717d87b75ac82e99b
Reviewed-on: https://go-review.googlesource.com/c/go/+/339595
Trust: Keith Randall <khr@golang.org>
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Dan Scales <danscales@google.com>
2021-08-04 17:56:00 +00:00
Keith Randall 0b8a9ccb25 [dev.typeparams] cmd/compile: make all pointer types have the same shape
Except unsafe.Pointer. It has a different Kind, which makes it trickier.

Change-Id: I12582afb6e591bea35da9e43ac8d141ed19532a3
Reviewed-on: https://go-review.googlesource.com/c/go/+/338749
Trust: Keith Randall <khr@golang.org>
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Dan Scales <danscales@google.com>
2021-07-31 17:03:07 +00:00
Keith Randall 40e561d933 [dev.typeparams] cmd/compile: allow types with the same underlying type to have the same shape
First baby step to sharing the underlying implementation among several types.

Change-Id: I6a156176d2b7f0131a87285a03b881ce380c26ed
Reviewed-on: https://go-review.googlesource.com/c/go/+/338610
Trust: Keith Randall <khr@golang.org>
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Dan Scales <danscales@google.com>
2021-07-30 21:01:32 +00:00
Dan Scales 600b7b431b [dev.typeparams] cmd/compile: handle meth expressions on typeparams
Rewrite a method expression such as 'T.String' (where T is type param
and String is part of its type bound Stringer) as:

func(rcvr T, other params...) {
    return Stringer(rcvr).String(other params...)
}

New function buildClosure2 to create the needed closure. The conversion
Stringer(rcvr) uses the dictionary in the outer function.

For a method expression like 'Test[T].finish' (where finish is a method
of Test[T]), we can already deal with this in buildClosure(). We just
need fix transformDot() to allow the method lookup to fail, since shapes
have no methods on them. That's fine, since for any instantiated
receiver type, we always use the methods on the generic base type.

Also removed the OMETHEXPR case in the main switch of node(), which
isn't needed any (and removes one more potential unshapify).

Also, fixed two small bugs with handling closures that have generic
params or generic captured variables. Need to set the instInfo for the
closure in the subst struct when descending into a closure during
genericSubst() and was missing initializing the startItabConv and gfInfo
fields in the closure info.

Change-Id: I6dadedd1378477936a27c9c544c014cd2083cfb7
Reviewed-on: https://go-review.googlesource.com/c/go/+/338129
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
2021-07-29 21:58:06 +00:00
Keith Randall f4f503e0a3 [dev.typeparams] cmd/compile: implement generic .(T) operations
Introduce new dynamic dottype operations which take a dynamic
instead of static type to convert to.

Change-Id: I5824a1fea056fe811b1226ce059e1e8da1baa335
Reviewed-on: https://go-review.googlesource.com/c/go/+/337609
Trust: Keith Randall <khr@golang.org>
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Dan Scales <danscales@google.com>
2021-07-29 17:15:21 +00:00
Matthew Dempsky adedf54288 [dev.typeparams] test: rename blank functions
This CL renames blank functions in the test/ directory so that they
don't rely on the compiler doing anything more than typechecking them.

In particular, I ran this search to find files that used blank
functions and methods:

$ git grep -l '^func.*\b_(' | xargs grep -n '^' | grep '\.go:1:' | grep -v '// errorcheck$'

I then skipped updating a few files:

* blank.go
* fixedbugs/issue11699.go
* fixedbugs/issue29870.go

  These tests specifically check that blank functions/methods work.

* interface/fail.go

  Not sure the motivation for the blank method here, but it's empty
  anyway.

* typeparam/tparam1.go

  Type-checking test, but uses "-G" (to use types2 instead of typecheck).

Updates #47446.

Change-Id: I9ec1714f499808768bd0dcd7ae6016fb2b078e5e
Reviewed-on: https://go-review.googlesource.com/c/go/+/338094
Trust: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-07-28 21:41:07 +00:00
Matthew Dempsky 5355753009 [dev.typeparams] test/typeparam: gofmt -w
We don't usually reformat the test directory, but all of the files in
test/typeparam are syntactically valid. I suspect the misformattings
here are because developers aren't re-installing gofmt with
-tags=typeparams, not intentionally exercising non-standard
formatting.

Change-Id: I3767d480434c19225568f3c7d656dc8589197183
Reviewed-on: https://go-review.googlesource.com/c/go/+/338093
Trust: Matthew Dempsky <mdempsky@google.com>
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-07-28 21:40:40 +00:00
Robert Griesemer 37d2219960 [dev.typeparams] cmd/compile/internal/types2: embedded type cannot be a (pointer to) a type parameter
Change-Id: I5eb03ae349925f0799dd866e207221429bc9fb3c
Reviewed-on: https://go-review.googlesource.com/c/go/+/337353
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
2021-07-26 20:53:17 +00:00
Matthew Dempsky bfcb7c4c8a [dev.typeparams] cmd/compile: fix unified IR support for //go:nointerface
This CL changes fixedbugs/issue30862.go into a "runindir" test so that
it can use '-goexperiment fieldtrack' and test that //go:nointerface
works with cmd/compile. In particular, this revealed that -G=3 and
unified IR did not handle it correctly.

This CL also fixes unified IR's support for //go:nointerface and adds
a test that checks that //go:nointerface, promoted methods, and
generics all interact as expected.

Updates #47045.

Change-Id: Ib8acff8ae18bf124520d00c98e8915699cba2abd
Reviewed-on: https://go-review.googlesource.com/c/go/+/332611
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-07-26 18:43:12 +00:00
Keith Randall b27c7e30dc [dev.typeparams] cmd/compile: fix HasShape, add dottype test
HasShape needs a TINTER case.

Add a test for x.(T) in various situations. Needs the fix above.

Also remove ONEW unshapify case. It is ok for ONEW to have a shape
type, as it will just be passed to mallocgc, or possibly used as a
stack object type, both of which are ok.

Change-Id: Ibddf8f5c8c254d32cb5ebcaca7dc94b4c00ab893
Reviewed-on: https://go-review.googlesource.com/c/go/+/337231
Trust: Keith Randall <khr@golang.org>
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Dan Scales <danscales@google.com>
2021-07-24 22:49:24 +00:00
Dan Scales 8e9109e95a [dev.typeparams] Fix problem with 14.go
Removed a case in transformCall() where we were setting a type on n,
which isn't needed, since noder2 already set the type of n. More
importantly, we are losing information, since the type of the results
may be a shape type, but the actual type of call is the known type
from types2, which may be a concrete type (in this case Zero[MyInt]).
That concrete type will then be used correctly if the concrete result is
converted to an interface.

If we are inlining the call to Zero[MyInt], we need to add an implicit
CONVNOP operation, since we are going to use the result variable
directly, which has a shape type. So, add an implicit CONVNOP to
remember that the known type is the concrete type.

Also cleaned up 14.go a bit, so it is more understandable. Renamed type
T to AnyInt, since T is used elsewhere as a type parameter. Reformatted
Zero function and added a comment.

Change-Id: Id917a2e054e0bbae9bd302232853fa8741d49b64
Reviewed-on: https://go-review.googlesource.com/c/go/+/336430
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
2021-07-22 04:45:49 +00:00
Dan Scales dcc8350ad3 [dev.typeparams] cmd/compile: handle ++/-- in noder2 for operands with generic type
types2 will have already proved the expression's type is compatible, so
just assign the one const to have the same type as the operand.

Fixes #47258.

Change-Id: If0844e6bf6d0a5e6b11453b87df71353863ccc5d
Reviewed-on: https://go-review.googlesource.com/c/go/+/336009
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
Trust: Dan Scales <danscales@google.com>
2021-07-21 21:30:48 +00:00
Dan Scales f19e49e7b1 [dev.typeparams] cmd/compile: added a builtins.go test, fixed one bug
The builtins.go test is derived from
cmd/compile/internal/types2/testdata/check/builtins.go2, after removing
the error cases.  Added a few extra tests for len/cap/append.

Fixed one bug, which is that DELETE operations can't be transformed if
their argument is a typeparam. Also, the tranform of LEN/CAP calls does
not need to be delayed. Removed out-date references to the old
typechecker in the comments.

Change-Id: If7a21506a7ff63ff7c8e87ccd614ef4ff3a0d3c8
Reviewed-on: https://go-review.googlesource.com/c/go/+/336010
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
Trust: Dan Scales <danscales@google.com>
2021-07-21 21:25:20 +00:00
Keith Randall 10c8b7c1d7 [dev.typeparams] cmd/compile: use dictionary to convert arguments of ==, != to interfaces
When comparing a value whose type is a type parameter to an interface,
we need to convert that type parameter to an interface using the dictionary
entries.

Change-Id: I409c9e36e376fe4ef8163407d0fd4e84496d5b65
Reviewed-on: https://go-review.googlesource.com/c/go/+/334150
Trust: Keith Randall <khr@golang.org>
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Dan Scales <danscales@google.com>
2021-07-16 18:31:15 +00:00
Dan Scales ed9e109dc9 [dev.typeparams] cmd/compile: fix small -G=3 issues for tests disabled in run.go
- set correct position for closure capture variable in (*irgen).use()
   (issue20250.go) Also, evaluate rhs, lhs in that order in assignment
   statements to match noder1 (affects ordering of closure variables).

 - make sure to set Assign flag properly in (*irgen).forStmt() for range
   variables which are map accesses (issue9691.go)

 - make sure CheckSize() is call on the base type for top-level types
   converted by (*irgen).typ() that are pointer types (issue20174.go and
   issue37837.go)

 - deal with parentheses properly in validation function
   (*irgen).validate() (issue17270.go)

 - avoid HasNil call on type TTYPEPARAM - types2 typechecker will have
   already checked validity of the typeparam having nil value (new test
   issue39755.go)

Change-Id: Ie68004d964698aea047e19e7dcd79b297e9d47ca
Reviewed-on: https://go-review.googlesource.com/c/go/+/334733
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
2021-07-16 18:30:16 +00:00
Robert Griesemer 3d8453e00e [dev.typeparams] cmd/compile/internal/types2: more consistent handling of predeclared "any"
Rather than removing "any" from the universe scope, keep it predeclared
but provide a better error message.

While at it, remove some unnecessary type assertions.

Change-Id: I10603274282ea6afc107f703ab194f32bd334dd1
Reviewed-on: https://go-review.googlesource.com/c/go/+/334911
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
2021-07-16 04:41:47 +00:00
Robert Griesemer 4ff0e04c2e [dev.typeparams] cmd/compile/internal/types2: embedding stand-alone type parameters is not permitted
For #47127.

Change-Id: Ie979ff56ae7c2dd0e5ce0ff39588f98ae68b5ee9
Reviewed-on: https://go-review.googlesource.com/c/go/+/334151
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
2021-07-14 23:33:52 +00:00
Dan Scales 1c783dc148 [dev.typeparams] Add optional sub-dict entry for typeparam bound calls
In the case that a generic function/method f does a method call on a
type param allowed by its bound, an instantiation of f may do a direct
method call of a concrete type or a method call defined on a generic
type, depending on whether the passed type in a concrete type or an
instantiated type with the appropriate method defined. See the test case
boundmethod.go added to this change.

In order to keep the dictionary format the same for all instantiations
of a generic function/method, I decided to have an optional
sub-dictionary entry for "bounds" calls. At the point that we are
creating the actual dictionary, we can then fill in the needed
sub-dictionary, if the type arg is an instantiated type, or a zeroed
dictionary entry, if type arg is not instantiated and the method will be
on a concrete type.

In order to implement this, I now fill in n.Selection for "bounds"
method calls in generic functions as well. Also, I need to calculate
n.Selection correctly during import for the case where it is now set -
method calls on generic types, and bounds calls on typeparams.

With this change, the dictionaries/sub-dictionaries are correct for
absdiff.go. The new test boundmethod.go illustrates the case where the
bound sub-dict entry is not used for a dictionary for stringify[myint],
but is used for a dictionary for stringify[StringInt[myint]].

Change-Id: Ie2bcb971b7019a9f1da68c97eb03da2333327457
Reviewed-on: https://go-review.googlesource.com/c/go/+/333456
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
Trust: Dan Scales <danscales@google.com>
2021-07-12 16:09:57 +00:00
Dan Scales 501725032c [dev.typeparams] cmd/compile: handle derived types that are converted to interfaces
Up to this point, we were only handling typeparams that were converted
to empty or non-empty interfaces. But we have a dictionary entry for
each derived type (i.e. type derived from typeparams) as well. So, when
doing a conversion, look for the source type in both the type params and
derived types of the generic info, and then use the appropriate
dictionary entry.

Added some cases to ifaceconv.go (e.g. converting []T to an empty
interface).

Change-Id: I7bbad0128bec20ccccd93ae1d65c1ffd44ca79a0
Reviewed-on: https://go-review.googlesource.com/c/go/+/333011
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
2021-07-07 17:40:11 +00:00
Dan Scales b4844c9f54 [dev.typeparams] cmd/compile: handle the (*T).M method expression with dictionaries
The (*T).M method expression is where M is a value method, but the type
(*T) is a pointer to the main type. In this case, after following any
embedded fields, we need to add an extra star operator when using the
receiver arg in the closure call.

Thanks to Cuong for finding/pointing out an example for this case
(typeparam/mdempsky/14.go) This example also shows that we now need the
ability to export/import OEFACE and OIDATA, which I added.

Change-Id: Ida0f81ce757fff78fec6276c60052ed71d207454
Reviewed-on: https://go-review.googlesource.com/c/go/+/333014
Run-TryBot: Dan Scales <danscales@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
2021-07-07 17:31:57 +00:00
Matthew Dempsky 5c42b6a953 [dev.typeparams] test: add regress tests that fail(ed) with -G=3
This CL includes multiple test cases that exercise unique failures
with -G=3 mode that did not affect unified IR mode. Most of these were
found over a period of about 3 hours of manual experimentation.

Thanks to Cuong Manh Le for test cases 11 and 12.

Updates #46704.

Change-Id: Ia2fa619536732b121b6c929329065c85b9384511
Reviewed-on: https://go-review.googlesource.com/c/go/+/326169
Trust: Matthew Dempsky <mdempsky@google.com>
Trust: Dan Scales <danscales@google.com>
Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Dan Scales <danscales@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-07-07 11:12:24 +00:00
Matthew Dempsky 49ade6b298 [dev.typeparams] test: add expected failure mechanism
This CL changes the existing excluded-test mechanism into a
known-failure mechanism instead. That is, it runs the test regardless,
but only reports if it failed (or succeeded) unexpectedly.

It also splits the known failures list into fine-grain failure lists
for types2, types2 w/ 32-bit target, -G=3, and unified.

Updates #46704.

Change-Id: I1213cbccf1bab6a92d9bfcf0d971a2554249bbff
Reviewed-on: https://go-review.googlesource.com/c/go/+/332551
Trust: Matthew Dempsky <mdempsky@google.com>
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Dan Scales <danscales@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-07-07 11:12:13 +00:00
Dan Scales 6dec18cc75 [dev.typeparams] cmd/compile: start using sub-dictionary entries where needed
Added new struct instInfo for information about an instantiation (of a
generic function/method with gcshapes or concrete types). We use this to
remember the dictionary param node, the nodes where sub-dictionaries
need to be used, etc. The instInfo map replaces the Stencil map in
Package.

Added code to access sub-dictionary entries at the appropriate call
sites. We are currently still calculating the corresponding main
dictionary, even when we really only need a sub-dictionary. I'll clean
that up in a follow-up CL.

Added code to deal with "generic" closures (closures that reference some
generic variables/types). We decided that closures will share the same
dictionary as the containing function (accessing the dictionary via a
closure variable). So, the getGfInfo function now traverses all the
nodes of each closure in a function that it is analyzing, so that a
function's dictionary has all the entries needed for all its closures as
well. Also, the instInfo of a closure is largely shared with its
containing function. A good test for generic closures already exists
with orderedmap.go.

Other improvements:
 - Only create sub-dictionary entries when the function/method
   call/value or closure actually has type params in it. Added new test
   file subdict.go with an example where a generic method has an
   instantiated method call that does not depend not have type params.

Change-Id: I691b9dc024a89d2305fcf1d8ba8540e53c9d103f
Reviewed-on: https://go-review.googlesource.com/c/go/+/331516
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
2021-07-02 19:07:06 +00:00
Dan Scales 8767b87ab5 [dev.typeparams] cmd/compile: functions to create GC shape types/names for a concrete type
Created functions to create GC shape type and names, based on a proposal
from Keith. Kept unsigned and signed integer types as different, since
they have different shift operations.

Included adding in alignment fields where padding is
required between fields, even though that seems like it will be fairly
uncommon to use.

Added some extra unusual struct typeparams (for testing the gcshape
names/types) in index.go test.

Change-Id: I8132bbd28098bd933435b8972ac5cc0b39f4c0df
Reviewed-on: https://go-review.googlesource.com/c/go/+/329921
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
2021-06-30 20:51:17 +00:00
Keith Randall 6a5f7e8498 [dev.typeparams] cmd/compile: use dictionary entries for more conversion cases
This CL handles I(x) where I is an interface type and x has
typeparam type.

Change-Id: Ib99de2b741d588947f5e0164255f6365e98acd8a
Reviewed-on: https://go-review.googlesource.com/c/go/+/326189
Trust: Keith Randall <khr@golang.org>
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Dan Scales <danscales@google.com>
2021-06-29 21:50:53 +00:00
Matthew Dempsky f4198f85d5 [dev.typeparams] cmd/compile: generate wrappers within unified IR
This CL extends unified IR to handle creating wrapper methods. There's
relatively little about this code that's actually specific to unified
IR, but rewriting this logic allows a few benefits:

1. It decouples unified IR from reflectdata.methodWrapper, so the
latter code can evolve freely for -G=3's needs. This will also allow
the new code to evolve to unified IR's wrapper needs, which I
anticipate will operate slightly differently.

2. It provided an opportunity to revisit a lot of the code and
simplify/update it to current style. E.g., in the process, I
discovered #46903, which unified IR now gets correctly. (I have not
yet attempted to fix reflectdata.methodWrapper.)

3. It gives a convenient way for unified IR to ensure all of the
wrapper methods it needs are generated correctly.

For now, the wrapper generation is specific to non-quirks mode.

Change-Id: I5798de6b141f29e8eb6a5c563e7049627ff2868a
Reviewed-on: https://go-review.googlesource.com/c/go/+/330569
Trust: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
2021-06-25 05:00:20 +00:00
Dan Scales ddb09af1b8 [dev.typeparams] cmd/compile: add derived types and subdictionaries to dictionaries
This is code in progress to generate the two main other types of entries
in dictionaries:
 - all types in the instantiated function derived from the type
   arguments (which are currently concrete, but will eventually be
   gcshapes)
 - pointers (i.e. mainly the unique name) to all needed sub-dictionaries

In order to generate these entries, we now generate cached information
gfInfo about generic functions/methods that can be used for creating the
instantiated dictionaries. We use the type substituter to compute the
right type args for instantiated sub-dictionaries.

If infoPrintMode is changed to true, the code prints out all the
information gathered about generic functions, and also the entries in
all the dictionaries that are instantiated. The debug mode also prints
out the locations where we need main dictionaries in non-instantiated
functions.

Other changes:
 - Moved the dictionary generation back to stencil.go from reflect.go,
   since we need to do extra analysis for the new dictionary entries. In
   the process, made getInstantiation generate both the function
   instantiation and the associated dictionary.

 - Put in small change for now in reflect.go, so that we don't try
   generate separate dictionaries for Value[T].get and the
   auto-generated (*Value[T]).get.  The auto-generated wrapper shouldn't really
   need a dictionary.

 - Detected, but not handling yet, a new case which needs
   dictionaries - closures that have function params or captured
   variables whose types are derived from type arguments.

 - Added new tests in dictionaryCapture for use of method
   value/expressions in generic functions and for mutually recursive
   generic functions.

Change-Id: If0cbde8805a9f673a23f5ec798769c85c9c5359b
Reviewed-on: https://go-review.googlesource.com/c/go/+/327311
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
2021-06-24 20:11:51 +00:00
Dan Scales ee4fc0c1bc [dev.typeparams] Fix issues related to dictionaries and method calls with embedded fields
- Fix handling of method expressions with embedded fields. Fix an
   incorrect lookup for method expressions, which have only the
   top-level type (and don't have DOT operations for the embedded
   fields). Add the embedded field dot operations into the closure.

 - Don't need a dictionary and so don't build a closure if the last
   embedded field reached in a method expression is an interface value.

 - Fix methodWrapper() to use the computed 'dot' node in the
   generic-only part of the code.

 - For a method expression, don't create a generic wrapper if the last
   embedded field reached before the method lookup is an interface.

Copied cmd/compile/internal/types2/testdata/fixedbugs/issue44688.go2 to
test/typeparam/issue44688.go, made it fully runnable (rather than just
for compilation), and added a bunch more tests.

Change-Id: I90c1aa569e1c7272e986c9d2ae683e553c3a38a1
Reviewed-on: https://go-review.googlesource.com/c/go/+/329550
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
2021-06-23 23:38:05 +00:00
Matthew Dempsky 1ba2074440 [dev.typeparams] cmd/compile/internal/types2: support local defined types
This CL changes types2's instance hashing logic to include position
information for function-scope defined types as disambiguation. This
isn't ideal, but it worked for getting nested.go passing.

Updates #46592.

Change-Id: Id83ba0001f44af69b81260306cc8b05e44fc4f09
Reviewed-on: https://go-review.googlesource.com/c/go/+/327170
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
Trust: Matthew Dempsky <mdempsky@google.com>
Trust: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
2021-06-16 21:38:50 +00:00
Matthew Dempsky db7c868307 [dev.typeparams] test: add string quoting support to test/run.go
This CL copies go/build's splitQuoted function (used for parsing #cgo
directives within `import "C"` preambles) to parse test recipe
commands. In particular, this now allows writing "build" and "run"
tests that use -gcflags to pass multiple compiler flags.

Change-Id: I0d18a9c13a4ce24bbdfa1da8662c0498c93a6762
Reviewed-on: https://go-review.googlesource.com/c/go/+/327275
Trust: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
2021-06-12 14:36:25 +00:00
Keith Randall cf4b6dc48e [dev.typeparams] cmd/compile: allow conversions from type parameter to interface
When converting from a type param to an interface, allow it if
the type bound implements that interface.

Query: some conversions go through this path, some use another path?
The test does

   var i interface{foo()int} = x

but

   i := (interface{foo()int})(x)

works at tip.

Change-Id: I84d497e5228c0e1d1c9d76ffebaedce09dc45e8e
Reviewed-on: https://go-review.googlesource.com/c/go/+/325409
Trust: Keith Randall <khr@golang.org>
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Dan Scales <danscales@google.com>
2021-06-07 19:53:38 +00:00
Dan Scales 201d55e637 [dev.typeparams] cmd/compile: create .dict Param in the package of the instantiated function
The instantiated functions are created in the source package of the
generic function, so all lookups of symbols should be relative to that
package, so all symbols are consistently in the source package.

Fixes #46575

Change-Id: Iba67b2ba8014a630c5d4e032c0f2f2fbaaedce65
Reviewed-on: https://go-review.googlesource.com/c/go/+/325529
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2021-06-07 19:34:39 +00:00
Keith Randall bad388744b [dev.typeparams] cmd/compile: handle dictionaries for top-level instantiations
There's no outer function in these cases, so we won't be reading
the dictionary as a subdictionary from the outer scope's dictionary.
It will always be a compile-time constant.

Change-Id: I754b126652a6ffb62255734d53fcec29d77cfa9e
Reviewed-on: https://go-review.googlesource.com/c/go/+/324949
Trust: Keith Randall <khr@golang.org>
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Dan Scales <danscales@google.com>
2021-06-04 18:11:39 +00:00
Dan Scales de61465156 [dev.typeparams] cmd/compile: allow inlining in instantiated functions
Change markType to scan generic types and methods, so that inlineable
functions inside generic functions/methods will be properly marked for
export, which means inlining inside instantiated functions will work
correctly.

Also, fix handling of closures for instantiated functions. Some code
needs to be adjusted, since instantiated functions/methods are compiled
as if in the package of the source generic function/type, rather than in
the local package. When we create the closure struct, we want to make
sure that the .F field has the same package as the other fields for the
closure variables. Also, we need to disable a check in tcCompLit() when
being done for an instantiated function, since fields of the closure
struct will be from the source package, not the local package.

Re-enabled part of the orderedmapsimp test that was disabled because of
these issues.

Change-Id: Ic4dba8917da0a36b17c0bdb69d6d6edfdf14104a
Reviewed-on: https://go-review.googlesource.com/c/go/+/324331
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Dan Scales <danscales@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
2021-06-04 17:48:10 +00:00
Dan Scales 8e6dfe1b31 [dev.typeparams] cmd/compile: export/import of recursive generic types.
Deal with export/import of recursive generic types. This includes
typeparams which have bounds that reference the typeparam.

There are three main changes:

  - Change export/import of typeparams to have an implicit "declaration"
    (doDecl). We need to do a declaration of typeparams (via the
    typeparam's package and unique name), because it may be referenced
    within its bound during its own definition.

  - We delay most of the processing of the Instantiate call until we
    finish the creation of the top-most type (similar to the way we
    delay CheckSize). This is because we can't do the full instantiation
    properly until the base type is fully defined (with methods). The
    functions delayDoInst() and resumeDoInst() delay and resume the
    processing of the instantiations.

  - To do the full needed type substitutions for type instantiations
    during import, I had to separate out the type subster in stencil.go
    and move it to subr.go in the typecheck package. The subster in
    stencil.go now does node substitution and makes use of the type
    subster to do type substitutions.

Notable other changes:
 - In types/builtins.go, put the newly defined typeparam for a union type
   (related to use of real/imag, etc.) in the current package, rather
   than the builtin package, so exports/imports work properly.

 - In types2, allowed NewTypeParam() to be called with a nil bound, and
   allow setting the bound later. (Needed to import a typeparam whose
   bound refers to the typeparam itself.)

 - During import of typeparams in types2 (importer/import.go), we need
   to keep an index of the typeparams by their package and unique name
   (with id). Use a new map typParamIndex[] for that. Again, this is
   needed to deal with typeparams whose bounds refer to the typeparam
   itself.

 - Added several new tests absdiffimp.go and orderedmapsimp.go. Some of
   the orderemapsimp tests are commented out for now, because there are
   some issues with closures inside instantiations (relating to unexported
   names of closure structs).

 - Renamed some typeparams in test value.go to make them all T (to make
   typeparam uniqueness is working fine).

Change-Id: Ib47ed9471c19ee8e9fbb34e8506907dad3021e5a
Reviewed-on: https://go-review.googlesource.com/c/go/+/323029
Trust: Dan Scales <danscales@google.com>
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-06-04 16:43:27 +00:00
Dan Scales 370ff5ff96 [dev.typeparams] test: update all the typeparam tests to use the new union/tilde syntax
Did a mix of tilde and non-tilde usage. Tilde notation is not quite
fully functional, so no tests are currently trying to distinguish
(fail/not fail) based on tilde usage.

Change-Id: Ib50cec2fc0684f9d9f3561c889fd44c7a7af458c
Reviewed-on: https://go-review.googlesource.com/c/go/+/324572
Trust: Dan Scales <danscales@google.com>
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-06-03 16:05:22 +00:00
Dan Scales 97cb0113a3 [dev.typeparams] cmd/compile: fix export/import of constants with typeparam type
A constant will have a TYPEPARAM type if it appears in a place where it
must match that typeparam type (e.g. in a binary operation with a
variable of that typeparam type). If so, then we must write out its
actual constant kind as well, so its constant val can be read in
properly during import.

Fixed some export/import tests which were casting some untyped constants
to avoid this problem.

Change-Id: I285ad8f1c8febbe526769c96e6b27acbd23050f0
Reviewed-on: https://go-review.googlesource.com/c/go/+/324189
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
2021-06-02 23:10:42 +00:00
Keith Randall 7b876def6c [dev.typeparams] cmd/compile: add dictionary argument to generic functions
When converting from a generic function to a concrete implementation,
add a dictionary argument to the generic function (both an actual
argument at each callsite, and a formal argument of each
implementation).

The dictionary argument comes before all other arguments (including
any receiver).

The dictionary argument is checked for validity, but is otherwise unused.
Subsequent CLs will start using the dictionary for, e.g., converting a
value of generic type to interface{}.

Import/export required adding support for LINKSYMOFFSET, which is used
by the dictionary checking code.

Change-Id: I16a7a8d23c7bd6a897e0da87c69f273be9103bd7
Reviewed-on: https://go-review.googlesource.com/c/go/+/323272
Trust: Keith Randall <khr@golang.org>
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Dan Scales <danscales@google.com>
2021-06-02 20:23:12 +00:00
Cuong Manh Le 4b10e4c547 [dev.typeparams] cmd/compile: handle ONONAME in subster.node
Fixes #46472

Change-Id: I27802978fa0c3bb32a29e452165a6fcac93473bb
Reviewed-on: https://go-review.googlesource.com/c/go/+/323731
Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Dan Scales <danscales@google.com>
2021-06-01 17:10:20 +00:00
Dan Scales 4ed6317e73 [dev.typeparams] cmd/compile: always generate (*T).M wrappers for instantiated methods
Always generate (*T).M wrappers for instantiated methods, even when the
instantiated method is being generated for another package (its source
package)

Added new function t.IsInstantiated() to check for fully-instantiated
types (generic type instantiated with concrete types, hence concrete
themselves). This function helps hide the representation of instantiated
types outside of the types package.

Added new export/import test setsimp.go that needs this change.

Change-Id: Ifb700db8c9494e1684c93735edb20f4709be5f7f
Reviewed-on: https://go-review.googlesource.com/c/go/+/322193
Trust: Dan Scales <danscales@google.com>
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-05-26 21:39:54 +00:00
Dan Scales b7f7d1cd7b [dev.typeparams] cmd/compile: get type aliases working with generic types
Generic types can the source type of a type alias, so modify g.typ0() to
be able to deal with base generic types.

Added test aliasimp.go that tests aliasing of local generic types and
imported generic types.

Change-Id: I1c398193819d47a36b014cc1f9bb55107e9a565b
Reviewed-on: https://go-review.googlesource.com/c/go/+/322194
Trust: Dan Scales <danscales@google.com>
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-05-26 21:38:54 +00:00
Dan Scales fd54ae8b0c [dev.typeparams] cmd/compile: adding union support in types1
Add union support in types1, and allow exporting of unions, and
importing unions back into types1 and types2.

Added new test mincheck.go/mincheck.dir that tests that type lists (type
sets) are correctly exported/imported, so that types2 gives correct
errors that an instantiation doesn't fit the type list in the type param
constraint.

Change-Id: I8041c6c79289c870a95ed5a1b10e4c1c16985b12
Reviewed-on: https://go-review.googlesource.com/c/go/+/322609
Trust: Dan Scales <danscales@google.com>
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-05-26 15:33:02 +00:00
Dan Scales d48f6d9f6f [dev.typeparams] Don't check typecheck(3) on transform, so no need to export/import it
We have a value typecheck(3) that indicates that a node in a generic
function still needs transformation (via the functions in transform.go).
But it is not very desirable to export/import the value of typecheck(3).
So, I changed the stenciling code to just try to transform all relevant
node types during node copy. Almost all tranform functions were already
idempotent. I only had to add an extra if check before calling
transformAssign() in the OAS case. We still use the typecheck(3) in
noder to determine when higher-nodes have to delay transformation
because one or more of their args are delaying transformation.

Added new test mapsimp.go that required these tranformations after import.

As an additional change, export/import of OINDEX requires exporting the
type using w.exoticType() rather than w.typ(), in order to handle
generic functions. Since generic functions can have pre-transform
operations, the index operation can have a tuple type (multiple return
from a map lookup).

Added printing of imported function bodies in -W=3 debug mode.

Change-Id: I220e2428dc5f2741e91db146f075eb5b6045f451
Reviewed-on: https://go-review.googlesource.com/c/go/+/322191
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Dan Scales <danscales@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
2021-05-24 22:17:33 +00:00
Dan Scales 4c50721cda [dev.typeparams] cmd/compile: Fix handling of Name nodes during stenciling
The name substitution for stenciling was incorrectly handling non-local
names. Made changes to explicitly built the vars[] name substitution map
based on the local variables (similar to what inlining substitution
does). Then, we we are stenciling a name node, we do NOT make a copy of
the name node if it is not in vars[], since it is then a reference to an
external name. Added new function localvar() to create the new nodes for
the local variables and put them in the vars[] map.

New test listimp2.go, added missing test calls in list2.go

Change-Id: I8946478250c7bf2bd31c3247089bd50cfeeda0fd
Reviewed-on: https://go-review.googlesource.com/c/go/+/322190
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
2021-05-24 22:16:09 +00:00
Dan Scales 5b1120fac7 [dev.typeparams] cmd/compile: fix handling of Nname field in (*subster).tstruct.
We want to keep the Nname references for external function references in
tstruct (not remove them, as is currently happening). We only change the
Nname reference (translate it) when it appears in subst.vars[].

New export/import test sliceimp.go which includes some of these external
function references.

Change-Id: Ie3d73bd989a16082f0cebfb566e0a7faeda55e60
Reviewed-on: https://go-review.googlesource.com/c/go/+/321735
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
Trust: Dan Scales <danscales@google.com>
2021-05-21 23:25:07 +00:00
Dan Scales 8d2b4cb6cc [dev.typeparams] cmd/compile: fixing import of comm clauses/closures in generic functions
Improvements:
 - Fix export/import of the default case of a select statement (was not
   dealing with nil Comm case)
 - Set properly the name of closure functions in imported generic
   functions

Added new test exporting/importing a reasonably large channel package,
chansimp.go.

Change-Id: If2ee12bd749e5df415f48ec4b629a2fa68a79dcb
Reviewed-on: https://go-review.googlesource.com/c/go/+/321734
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
Trust: Dan Scales <danscales@google.com>
2021-05-21 23:13:46 +00:00
Dan Scales b1a398cf0f [dev.typeparams] cmd/compile: add import/export of calls to builtin functions
For generic functions, we have to leave the builtins in OCALL form,
rather than transform to specific ops, since we don't know the exact
types involved. Allow export/import of builtins in OCALL form.

Added new export/import test mapimp.go.

Change-Id: I571f8eeaa13b4f69389dbdb9afb6cc61924b9bf2
Reviewed-on: https://go-review.googlesource.com/c/go/+/321750
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
2021-05-21 17:14:19 +00:00
Dan Scales ccbfbb1c33 [dev.typeparams] cmd/compile: export OFUNCINST and OSELRECV2 nodes (for generic functions)
Added new test typeparam/factimp.go and changed a bunch of other tests
to test exporting more generic functions and types.

Change-Id: I573d75431cc92482f8f908695cfbc8e84dbb36d2
Reviewed-on: https://go-review.googlesource.com/c/go/+/321749
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
2021-05-21 17:03:30 +00:00
Dan Scales 7b3ee6102d [dev.typeparams] cmd/compile: move to new export version, keep reading previous version
I added constants for the previous export versions, and for the final
generics export version. I also added a const for the current export
version. We can increment the current export version for unstable
changes in dev.typeparams, and eventally set it back to the generics
version (2) before release. Added the same constants in
typecheck/iexport.go, importer/iimport.go, and gcimporter/iimport.go,
must be kept in sync.

Put in the needed conditionals to be able to read old versions.

Added new export/import test listimp.dir.

Change-Id: I166d17d943e07951aa752562e952b067704aeeca
Reviewed-on: https://go-review.googlesource.com/c/go/+/319931
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
2021-05-21 04:03:26 +00:00
Dan Scales 15ad61aff5 [dev.typeparams] cmd/compile: get export/import of generic types & functions working
The general idea is that we now export/import typeparams, typeparam
lists for generic types and functions, and instantiated types
(instantiations of generic types with either new typeparams or concrete
types).

This changes the export format -- the next CL in the stack adds the
export versions and checks for it in the appropriate places.

We always export/import generic function bodies, using the same code
that we use for exporting/importing the bodies of inlineable functions.

To avoid complicated scoping, we consider all type params as unique and
give them unique names for types1. We therefore include the types2 ids
(subscripts) in the export format and re-create on import. We always
access the same unique types1 typeParam type for the same typeparam
name.

We create fully-instantiated generic types and functions in the original
source package. We do an extra NeedRuntimeType() call to make sure that
the correct DWARF information is written out. We call SetDupOK(true) for
the functions/methods to have the linker automatically drop duplicate
instantiations.

Other miscellaneous details:
 - Export/import of typeparam bounds works for methods (but not
   typelists) for now, but will change with the typeset changes.

 - Added a new types.Instantiate function roughly analogous to the
   types2.Instantiate function recently added.

 - Always access methods info from the original/base generic type, since
   the methods of an instantiated type are not filled in (in types2 or
   types1).

 - New field OrigSym in types.Type to keep track of base generic type
   that instantiated type was based on. We use the generic type's symbol
   (OrigSym) as the link, rather than a Type pointer, since we haven't
   always created the base type yet when we want to set the link (during
   types2 to types1 conversion).

 - Added types2.AsTypeParam(), (*types2.TypeParam).SetId()

 - New test minimp.dir, which tests use of generic function Min across
   packages. Another test stringimp.dir, which also exports a generic
   function Stringify across packages, where the type param has a bound
   (Stringer) as well. New test pairimp.dir, which tests use of generic
   type Pair (with no methods) across packages.

 - New test valimp.dir, which tests use of generic type (with methods
   and related functions) across packages.

 - Modified several other tests (adder.go, settable.go, smallest.go,
   stringable.go, struct.go, sum.go) to export their generic
   functions/types to show that generic functions/types can be exported
   successfully (but this doesn't test import).

Change-Id: Ie61ce9d54a46d368ddc7a76c41399378963bb57f
Reviewed-on: https://go-review.googlesource.com/c/go/+/319930
Trust: Dan Scales <danscales@google.com>
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-05-21 03:41:18 +00:00
Dan Scales 04f65d394c [dev.typeparams] cmd/compile: fix use of method values with stenciled methods
We were handling the case where an OFUNCINST node was used as a function
value, but not the case when an OFUNCINST node was used as a method
value. In the case of a method value, we need to create a new selector
expression that references the newly stenciled method.

To make this work, also needed small fix to noder2 code to properly set the
Sel of a method SelectorExpr (should be just the base method name, not
the full method name including the type string). This has to be correct,
so that the function created by MethodValueWrapper() can be typechecked
successfully.

Fixes #45817

Change-Id: I7343e8a0d35fc46b44dfe4d45b77997ba6c8733e
Reviewed-on: https://go-review.googlesource.com/c/go/+/319589
Reviewed-by: Keith Randall <khr@golang.org>
Trust: Dan Scales <danscales@google.com>
2021-05-12 22:25:00 +00:00
Keith Randall f432d3fc41 cmd/compile: fix nongeneric closures in generic functions
Ensure that formal parameter Names are correctly copied and marked
with the correct Curfn. We need to ensure this even when the underlying
closure has no type parameters.

(Aside: it is strange that the types of things contain formal
parameter names that need to be copied. Maybe that's an underlying
larger problem that needs to be fixed.)

Fixes #45738

Change-Id: Ia13d69eea992ff7080bd44065115bc52eb624e73
Reviewed-on: https://go-review.googlesource.com/c/go/+/313652
Trust: Keith Randall <khr@golang.org>
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Dan Scales <danscales@google.com>
2021-04-27 19:30:11 +00:00
Dan Scales 55c517a8b3 cmd/compile: fix handling of ir.CurFunc during stenciling
The transform functions (specifically transformArgs, which is used from
transformCall/transformReturn) require that ir.CurFunc is set correctly.
Since transformCall() is used on the call of an instantiated generic
function, we need to set ir.CurFunc correctly in stencil(). Also,
correctly save/restore ir.CurFunc in genericSubst().

Without this fix, ir.CurFunc can be nil when we call TransformCall()
from stencil(), which leads to some temp variables being added
incorrectly to ir.TodoFunc (which leads to the fatal panic in the
issue).

Fixes #45722

Change-Id: Iddf4a67d28f2100dde8cde5dbc9ca1e00dad6089
Reviewed-on: https://go-review.googlesource.com/c/go/+/313869
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
Trust: Dan Scales <danscales@google.com>
2021-04-27 16:55:20 +00:00
Keith Randall 083a26c7d2 cmd/compile: propagate pragmas from generic function to stenciled implementation
Change-Id: I28a1910890659aaa449ffd2a847cd4ced5a8600d
Reviewed-on: https://go-review.googlesource.com/c/go/+/310211
Trust: Keith Randall <khr@golang.org>
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Dan Scales <danscales@google.com>
2021-04-15 00:29:05 +00:00
Dan Scales bf634c76b2 cmd/compile: look for function in instantiations in all global assignments
Add in some missing global assignment ops to the list of globals ops
that should be traversed to look for generic function instantiations.
The most common other one for global assigments (and the relevant one
for this bug) is OAS2FUNC, but also look at global assigments with
OAS2DOTTYPE, OAS2MAPR, OAS2RECV, and OASOP.

Bonus small fix: get rid of -G=3 case in ir.IsAddressable. Now that we
don't call the old typechecker from noder2, we don't need this -G-3
check anymore.

Fixes #45547.

Change-Id: I75fecec55ea0d6f62e1c2294d4d77447ed9be6ae
Reviewed-on: https://go-review.googlesource.com/c/go/+/310210
Trust: Dan Scales <danscales@google.com>
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-04-15 00:14:55 +00:00
Dan Scales f2717b31b5 cmd/compile: deal correctly with unnamed function params during stenciling
During substitution of the function type during stenciling, we must set
the Name nodes of the param/result fields of the func type. We get those
name nodes from the substituted Dcl nodes of the PPARAMS and PPARAMOUTs.
But we must check that the names match with the Dcl nodes, so that we
skip any param fields that correspond to unnamed (in) parameters.

Added a few tests to typelist.go by removing a variety of unneeded
function parameter names.

Change-Id: If786961b64549da6f18eeeb5060ea58fab874eb9
Reviewed-on: https://go-review.googlesource.com/c/go/+/305912
Trust: Dan Scales <danscales@google.com>
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-31 00:52:26 +00:00
Dan Scales 1318fb4a32 cmd/compile: handle partial type inference that doesn't require function args
Handle the case where types can be partially inferred for an
instantiated function that is not immediately called. The key for the
Inferred map is the CallExpr (if inferring types required the function
arguments) or the IndexExpr (if types could be inferred without the
function arguments).

Added new tests for the case where the function isn't immediately called
to typelist.go.

Change-Id: I60f503ad67cd192da2f2002060229efd4930dc39
Reviewed-on: https://go-review.googlesource.com/c/go/+/305909
Trust: Dan Scales <danscales@google.com>
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Dan Scales <danscales@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
2021-03-30 19:44:38 +00:00
Dan Scales 032ef4bbfc cmd/compile: fix creation of named generic types (setting of t.nod)
The correct setting of t.nod is needed when exporting types. Make sure
we create instantiated named types correctly so t.nod is set.

New test file interfacearg.go that tests this (by instantiating a type
with an interface). Also has tests for various kinds of method
expressions.

Change-Id: Ia7fd9debd495336b73788af9e35d72331bb7d2b5
Reviewed-on: https://go-review.googlesource.com/c/go/+/305730
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-30 04:48:27 +00:00
Dan Scales eeadfa2d38 cmd/compile: fix various small bugs related to type lists
Fix various small bugs related to delaying transformations due to type
params. Most of these relate to the need to delay a transformation when
an argument of an expression or statement has a type parameter that has
a structural constraint. The structural constraint implies the operation
should work, but the transformation can't happen until the actual value
of the type parameter is known.

 - delay transformations for send statements and return statements if
   any args/values have type params.

 - similarly, delay transformation of a call where the function arg has
   type parameters. This is mainly important for the case where the
   function arg is a pure type parameter, but has a structural
   constraint that requires it to be a function. Move the setting of
   n.Use to transformCall(), since we may not know how many return
   values there are until then, if the function arg is a type parameter.

 - set the type of unary expressions from the type2 type (as we do with
   most other expressions), since that works better with expressions
   with type params.

 - deal with these delayed transformations in subster.node() and convert
   the CALL checks to a switch statement.

 - make sure ir.CurFunc is set properly during stenciling, including
   closures (needed for transforming return statements during
   stenciling).

New test file typelist.go with tests for these cases.

Change-Id: I1b82f949d8cec47d906429209e846f4ebc8ec85e
Reviewed-on: https://go-review.googlesource.com/c/go/+/305729
Trust: Dan Scales <danscales@google.com>
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-30 03:05:45 +00:00
Dan Scales 0265b6475f cmd/compile: replace calls to typecheck with transform functions
For additions, compares, and slices, create transform functions that do
just the transformations for those nodes by the typecheck package (given
that the code has been fully typechecked by types2). For nodes that have
no args with typeparams, we call these transform functions directly in
noder2. But for nodes that have args with typeparams, we have to delay
and call the tranform functions during stenciling, since we don't know
the specific types involved.

We indicate that a node still needs transformation by setting Typecheck
to a new value 3. This value means the current type of the node has been
set (via types2), but the node may still need transformation.

Had to export typcheck.IsCmp and typecheck.Assignop from the typecheck
package.

Added new tests list2.go (required delaying compare typecheck/transform
because of != compare in checkList) and adder.go (requires delaying add
typecheck/transform, since it can do addition for numbers or strings).

There are several more transformation functions needed for expressions
(indexing, calls, etc.) and several more complicated ones needed for
statements (mainly various kinds of assignments).

Change-Id: I7d89d13a4108308ea0304a4b815ab60b40c59b0a
Reviewed-on: https://go-review.googlesource.com/c/go/+/303091
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-23 04:23:52 +00:00
Dan Scales 90b1ed1602 cmd/compile: get untyped constants working in generic functions
types2 will give us a constant with a type T, if an untyped constant is
used with another operand of type T (in a provably correct way). When we
substitute in the type args during stenciling, we now know the real type
of the constant. We may then need to change the BasicLit.val to be the
correct type (e.g. convert an int64Val constant to a floatVal constant).
Otherwise, later parts of the compiler will be confused.

Updated tests list.go and double.go with uses of untyped constants.

Change-Id: I9966bbb0dea3a7de1c5a6420f8ad8af9ca84a33e
Reviewed-on: https://go-review.googlesource.com/c/go/+/303089
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-18 22:18:32 +00:00
Dan Scales 70d54df4f6 cmd/compile: getting more built-ins to work with generics
For Builtin ops, we currently stay with using the old
typechecker to transform the call to a more specific expression
and possibly use more specific ops. However, for a bunch of the
ops, we delay calling the old typechecker if any of the args have
type params, for a variety of reasons.

In the near future, we will start creating separate functions that do
the same transformations as the old typechecker for calls, builtins,
indexing, comparisons, etc. These functions can then be called at noder
time for nodes with no type params, and at stenciling time for nodes
with type params.

Remove unnecessary calls to types1 typechecker for most kinds of
statements (still need it for SendStmt, AssignStmt, ReturnStmt, and
SelectStmt). In particular, we don't need it for RangeStmt, and this
avoids some complaints by the types1 typechecker on generic code.

Other small changes:
 - Fix check on whether to delay calling types1-typechecker on type
   conversions. Should check if HasTParam is true, rather than if the
   type is directly a TYPEPARAM.

 - Don't call types1-typechecker on an indexing operation if the left
   operand has a typeparam in its type and is not obviously a TMAP,
   TSLICE, or TARRAY. As above, we will eventually have to create a new
   function that can do the required transformations (for complicated
   cases) at noder time or stenciling time.

 - Copy n.BuiltinOp in subster.node()

 - The complex arithmetic example in absdiff.go now works.

 - Added new tests double.go and append.go

 - Added new example with a new() call in settable.go

Change-Id: I8f377afb6126cab1826bd3c2732aa8cdf1f7e0b4
Reviewed-on: https://go-review.googlesource.com/c/go/+/301951
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-17 16:53:00 +00:00
Dan Scales 832a01aad4 cmd/compile: deal with comparable embedded in a constraint
Ignore an embedded type in an interface which is the predeclared
interface "comparable" (which currently can only be in a type
constraint), since the name doesn't resolve and the "comparable" type
doesn't have any relevant methods (for the purposes of the compiler).

Added new test case graph.go that needs this fix.

Change-Id: I2443d2c3dfeb9d0a78aaaaf91a2808ae2759d247
Reviewed-on: https://go-review.googlesource.com/c/go/+/301831
Trust: Dan Scales <danscales@google.com>
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-16 18:42:06 +00:00
Dan Scales 96aecdcb36 cmd/compile: fix case where func-valued field of a generic type is called
Added test example orderedmap.go (binary search tree) that requires this
fix (calling function compare in _Map).

Also added new tests slices.go and metrics.go that just work.

Change-Id: Ifa5f42ab6eee9aa54c40f0eca19e00a87f8f608a
Reviewed-on: https://go-review.googlesource.com/c/go/+/301829
Trust: Dan Scales <danscales@google.com>
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-15 20:29:11 +00:00
Dan Scales c236095638 cmd/compile: add support for generic maps
Add support for maps in subster.typ(). Add new test cases maps.go and set.go.

Change substitution of a TFUNC in subster.typ() to always create new
param and result structs if any of the receiver, param, or result
structs get substituted. All these func structs must be copied, because
they have offset fields that are dependent, and so must have an
independent copy for each new signature (else there will be an error
later when frame offsets are calculated).

Change-Id: I576942a62f06b46b6f005abc98f65533008de8dc
Reviewed-on: https://go-review.googlesource.com/c/go/+/301670
Trust: Dan Scales <danscales@google.com>
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-15 20:28:34 +00:00
Dan Scales dca9c11845 cmd/compile: add support for generic channels and type conversion during calls
Add support for channels in subster.typ(). Add new test file chans.go.

To support assignability of bidirectional channel args to directional
channel params, I needed to type check generic calls after they are
instantiated. (Eventually, we will create separate functions to just do
the assignability logic, so we don't need to call the old typechecker in
this case.) So, for generic calls, we now leave the call as OCALL (as a
signal that the call still needs typechecking), and do typecheck.Call()
during stenciling.

Smaller changes:
 - Set the type of an instantiated OCLOSURE node (and not just the associated
   OFUNC node)

 - In instTypeName2, filter out the space that types2.TypeString inserts
   after a common in a typelist. Our standard naming requires no space
   after the comma.

 - With the assignability fix above, I no longer need the explicit
   conversions in cons.go.

Change-Id: I148858bfc6708c0aa3f50bad7debce2b8c8c091f
Reviewed-on: https://go-review.googlesource.com/c/go/+/301669
Trust: Dan Scales <danscales@google.com>
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-15 20:28:10 +00:00
Dan Scales e87c4bb3ef cmd/compile: fix noder.Addr() to not call typechecker
Simple change to avoid calling the old typechecker in noder.Addr(). This
fixes cases where generic code calls a pointer method with a non-pointer
receiver.

Added test typeparam/lockable.go that now works with this change.

For lockable.go to work, also fix incorrect check to decide whether to
translate an OXDOT now or later. We should delay translating an OXDOT
until instantiation (because we don't know how embedding, etc. will
work) if the receiver has any typeparam, not just if the receiver type
is a simple typeparam. We also have to handle OXDOT for now in
IsAddressable(), until we can remove calls to the old typechecker in
(*irgen).funcBody().

Change-Id: I77ee5efcef9a8f6c7133564106a32437e36ba4bb
Reviewed-on: https://go-review.googlesource.com/c/go/+/300990
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-12 02:30:33 +00:00
Dan Scales fdded79e6e cmd/compile: fix handling of partially inferred type arguments
In the case of partially inferred type arguments, we need to use the
IndexExpr as the key in g.info.Inferred[] rather than the CallExpr.

Added an extra fromStrings1 call in the settable.go test that tests
partially inferred type arguments. This new call uses a new concrete
type SettableString as well.

I also added another implementation fromStrings3 (derived from a go2go
tests) that typechecks but intentionally causes a panic.

Change-Id: I74d35c5a741f72f37160a96fbec939451157f392
Reviewed-on: https://go-review.googlesource.com/c/go/+/300309
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-11 00:31:14 +00:00
Dan Scales 1811aeae66 cmd/compile: deal with helper generic types that add methods to T
Deal with cases like: 'type P[T any] T' (used to add methods to an
arbitrary type T), In this case, P[T] has kind types.TTYPEPARAM (as does
T itself), but requires more code to substitute than a simple TTYPEPARAM
T. See the comment near the beginning of subster.typ() in stencil.go.

Add new test absdiff.go. This test has a case for complex types (which
I've commented out) that will only work when we deal better with Go
builtins in generic functions (like real and imag).

Remove change in fmt.go for TTYPEPARAMS that is no longer needed (since
all TTYPEPARAMS have a sym) and was sometimes causing an extra prefix
when formatting method names.

Separate out the setting of a TTYPEPARAM bound, since it can reference
the TTYPEPARAM being defined, so must be done separately. Also, we don't
currently (and may not ever) need bounds after types2 typechecking.

Change-Id: Id173057e0c4563b309b95e665e9c1151ead4ba77
Reviewed-on: https://go-review.googlesource.com/c/go/+/300049
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-10 17:36:55 +00:00
Dan Scales a70eb2c9f2 cmd/compile: get instantiated generic types working with interfaces
Get instantiatiated generic types working with interfaces, including
typechecking assignments to interfaces and instantiating all the methods
properly. To get it all working, this change includes:

 - Add support for substituting in interfaces in subster.typ()

 - Fill in the info for the methods for all instantiated generic types,
   so those methods will be available for later typechecking (by the old
   typechecker) when assigning an instantiated generic type to an
   interface. We also want those methods available so we have the list
   when we want to instantiate all methods of an instantiated type. We
   have both for instantiated types encountered during the initial noder
   phase, and for instantiated types created during stenciling of a
   function/method.

 - When we first create a fully-instantiated generic type (whether
   during initial noder2 pass or while instantiating a method/function),
   add it to a list so that all of its methods will also be
   instantiated. This is needed so that an instantiated type can be
   assigned to an interface.

 - Properly substitute type names in the names of instantiated methods.

 - New accessor methods for types.Type.RParam.

 - To deal with generic types which are empty structs (or just don't use
   their type params anywhere), we want to set HasTParam if a named type
   has any type params that are not fully instantiated, even if the
   type param is not used in the type.

 - In subst.typ() and elsewhere, always set sym.Def for a new forwarding
   type we are creating, so we always create a single unique type for
   each generic type instantiation. This handles recursion within a
   type, and also recursive relationships across many types or methods.
   We remove the seen[] hashtable, which was serving the same purpose,
   but for subst.typ() only. We now handle all kinds of recursive types.

 - We don't seem to need to force types.CheckSize() on
   created/substituted generic types anymore, so commented out for now.

 - Add an RParams accessor to types2.Signature, and also a new
   exported types2.AsSignature() function.

Change-Id: If6c5dd98427b20bfe9de3379cc16f83df9c9b632
Reviewed-on: https://go-review.googlesource.com/c/go/+/298449
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-09 16:37:52 +00:00
Dan Scales d8e33d558e cmd/compile: deal with closures in generic functions and instantiated function values
- Deal with closures in generic functions by fixing the stenciling code

 - Deal with instantiated function values (instantiated generic
   functions that are not immediately called) during stenciling. This
   requires changing the OFUNCINST node to an ONAME node for the
   appropriately instantiated function. We do this in a second pass,
   since this is uncommon, but requires editing the tree at multiple
   levels.

 - Check global assignments (as well as functions) for generic function
   instantiations.

 - Fix a bug in (*subst).typ where a generic type in a generic function
   may definitely not use all the type args of the function, so we need
   to translate the rparams of the type based on the tparams/targs of
   the function.

 - Added new test combine.go that tests out closures in generic
   functions and instantiated function values.

 - Added one new variant to the settable test.

 - Enabling inlining functions with closures for -G=3. (For now, set
   Ntype on closures in -G=3 mode to keep compatibility with later parts
   of compiler, and allow inlining of functions with closures.)

Change-Id: Iea63d5704c322e42e2f750a83adc8b44f911d4ec
Reviewed-on: https://go-review.googlesource.com/c/go/+/296269
Reviewed-by: Robert Griesemer <gri@golang.org>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
2021-02-26 18:57:20 +00:00
Dan Scales 20050a15fe [dev.typeparams] cmd/compile: support generic types (with stenciling of method calls)
A type may now have a type param in it, either because it has been
composed from a function type param, or it has been declared as or
derived from a reference to a generic type. No objects or types with
type params can be exported yet. No generic type has a runtime
descriptor (but will likely eventually be associated with a dictionary).

types.Type now has an RParam field, which for a Named type can specify
the type params (in order) that must be supplied to fully instantiate
the type. Also, there is a new flag HasTParam to indicate if there is
a type param (TTYPEPARAM) anywhere in the type.

An instantiated generic type (whether fully instantiated or
re-instantiated to new type params) is a defined type, even though there
was no explicit declaration. This allows us to handle recursive
instantiated types (and improves printing of types).

To avoid the need to transform later in the compiler, an instantiation
of a method of a generic type is immediately represented as a function
with the method as the first argument.

Added 5 tests on generic types to test/typeparams, including list.go,
which tests recursive generic types.

Change-Id: Ib7ff27abd369a06d1c8ea84edc6ca1fd74bbb7c2
Reviewed-on: https://go-review.googlesource.com/c/go/+/292652
Trust: Dan Scales <danscales@google.com>
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Dan Scales <danscales@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-02-18 22:37:06 +00:00
Dan Scales c0aa7bd760 [dev.typeparams] cmd/compile: small fixes for stenciling
- Create the stencil name using targ.Type.String(), which handles cases
   where, for example, a type argument is a pointer to a named type,
   etc. *obj.

 - Set name.Def properly for a new stenciled func (have the symbol point
   back to the associated function node).  Will be required when exporting.

 - Add missing copying of Func field when making copies of Name nodes.
   (On purpose (it seems), Name nodes don't have a copy() function, so
   we have to copy all the needed fields explicitly.)

 - Deal with nil type in subster.node(), which is the type of the return
   value for a function that doesn't return anything.

 - Fix min to match standard want/go form, and add in float tests.  Changed
   Got -> got in bunch of other typeparam tests.

 - Add new tests index.go, settable.go, and smallest.go (similar to
   examples in the type param proposal), some of which need the above
   changes.

Change-Id: I09a72302bc1fd3635a326da92405222afa222e85
Reviewed-on: https://go-review.googlesource.com/c/go/+/291109
Trust: Dan Scales <danscales@google.com>
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-02-11 21:46:39 +00:00
Dan Scales fdf3496fcc [dev.typeparams] cmd/compile: make type conversions by type parameters work
When doing a type conversion using a type param, delay the
transformation to OCONV/OCONVNOP until stenciling, since the nodes
created depend on the actual type.

Re-enable the fact.go test.

Change-Id: I3d5861aab3dd0e781d767f67435afaf951dfe451
Reviewed-on: https://go-review.googlesource.com/c/go/+/290752
Trust: Dan Scales <danscales@google.com>
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-02-10 03:33:05 +00:00
Dan Scales 12e15d430d [dev.typeparams] cmd/compile: handle calling a method on a type param in stenciling
- Have to delay the extra transformation on methods invoked on a type
   param, since the actual transformation (including path through
   embedded fields) will depend on the instantiated type. I am currently
   doing the transformation during the stencil substitution phase. We
   probably should have a separate pass after noder2 and stenciling,
   which drives the extra transformations that were in the old
   typechecker.

 - We handle method values (that are not called) and method calls. We
   don't currently handle method expressions.

 - Handle type substitution in function types, which is needed for
   function args in generic functions.

 - Added stringer.go and map.go tests, testing the above changes
   (including constraints with embedded interfaces).

Change-Id: I3831a937d2b8814150f75bebf9f23ab10b93fa00
Reviewed-on: https://go-review.googlesource.com/c/go/+/290550
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Dan Scales <danscales@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-02-10 01:44:48 +00:00
Robert Griesemer a360eeb528 [dev.typeparams] cmd/compile/internal/types2: conversions to type parameters are not constant
Disabled test/typeparam/fact.go for now as there's an issue
with stenciling.

Change-Id: Ie328a217de6d7b6695737f08ef5c944bcdaabd39
Reviewed-on: https://go-review.googlesource.com/c/go/+/290471
Trust: Robert Griesemer <gri@golang.org>
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Dan Scales <danscales@google.com>
2021-02-08 22:24:09 +00:00
Dan Scales 0fbde54ea6 [dev.typeparams] cmd/compile: allow generic funcs to call other generic funcs for stenciling
- Handle generic function calling itself or another generic function in
   stenciling. This is easy - after it is created, just scan an
   instantiated generic function for function instantiations (that may
   needed to be stenciled), just like non-generic functions. The types
   in the function instantiation will already have been set by the
   stenciling.

 - Handle OTYPE nodes in subster.node() (allows for generic type
   conversions).

 - Eliminated some duplicated work in subster.typ().

 - Added new test case fact.go that tests a generic function calling
   itself, and simple generic type conversions.

 - Cause an error if a generic function is to be exported (which we
   don't handle yet).

 - Fixed some suggested changes in the add.go test case that I missed in
   the last review.

Change-Id: I5d61704254c27962f358d5a3d2e0c62a5099f148
Reviewed-on: https://go-review.googlesource.com/c/go/+/290469
Trust: Dan Scales <danscales@google.com>
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-02-08 19:32:55 +00:00
Dan Scales dcb5e0392e [dev.typeparams] cmd/compile: add stenciling of simple generic functions
Allow full compilation and running of simple programs with generic
functions by stenciling on the fly the needed generic functions. Deal
with some simple derived types based on type params.

Include a few new typeparam tests min.go and add.go which involve
fully compiling and running simple generic code.

Change-Id: Ifc2a64ecacdbd860faaeee800e2ef49ffef9df5e
Reviewed-on: https://go-review.googlesource.com/c/go/+/289630
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-02-05 16:40:12 +00:00
Robert Griesemer 036245862a [dev.typeparams] cmd/compile/internal/types2: set compiler error message for undeclared variable
Change-Id: Ie2950cdc5406915935f114bfd97ef03d965f9069
Reviewed-on: https://go-review.googlesource.com/c/go/+/274616
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
2020-12-02 04:48:13 +00:00
Robert Griesemer 6877ee1e07 [dev.typeparams] cmd/compile: use existing findpkg algorithm when importing through types2
Change-Id: I9044de7829d22addb5bc570401508082e3f007eb
Reviewed-on: https://go-review.googlesource.com/c/go/+/269057
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2020-11-11 01:11:43 +00:00
Robert Griesemer 87eab74628 [dev.typeparams] cmd/compile: enable type-checking of generic code
This change makes a first connection between the compiler and types2.
When the -G flag is provided, the compiler accepts code using type
parameters; with this change generic code is also type-checked (but
then compilation ends).

Change-Id: I0fa6f6213267a458a6b33afe8ff26869fd838a63
Reviewed-on: https://go-review.googlesource.com/c/go/+/264303
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2020-10-27 03:37:05 +00:00
Robert Griesemer 48755e06aa [dev.typeparams] cmd/compile: enable parsing of generic code with new -G flag
Providing the -G flag instructs the compiler to accept type parameters.
For now, the compiler only parses such files and then exits.

Added a new test directory (test/typeparam) and initial test case.

Port from dev.go2go branch.

Change-Id: Ic11e33a9d5f012f8def0bdae205043659562ac73
Reviewed-on: https://go-review.googlesource.com/c/go/+/261660
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2020-10-13 04:48:43 +00:00