]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: support generic alias type
authorCuong Manh Le <cuong.manhle.vn@gmail.com>
Thu, 20 Jun 2024 15:58:24 +0000 (22:58 +0700)
committerGopher Robot <gobot@golang.org>
Thu, 20 Jun 2024 16:46:54 +0000 (16:46 +0000)
Type parameters on aliases are now allowed after #46477 accepted.

Updates #46477
Fixes #68054

Change-Id: Ic2e3b6f960a898163f47666e3a6bfe43b8cc22e2
Reviewed-on: https://go-review.googlesource.com/c/go/+/593715
Reviewed-by: Robert Griesemer <gri@google.com>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Robert Griesemer <gri@google.com>

src/cmd/compile/internal/noder/writer.go
test/fixedbugs/issue68054.go [new file with mode: 0644]

index 9b33fb7c6dcaa506b61c2a3d1357d41089afa33e..fe8f8f2a351394b93f3085fb8f8198066f3bb901 100644 (file)
@@ -543,7 +543,7 @@ func (pw *pkgWriter) typIdx(typ types2.Type, dict *writerDict) typeInfo {
 
        case *types2.Alias:
                w.Code(pkgbits.TypeNamed)
-               w.namedType(typ.Obj(), nil)
+               w.namedType(splitAlias(typ))
 
        case *types2.TypeParam:
                w.derived = true
@@ -2958,6 +2958,9 @@ func objTypeParams(obj types2.Object) *types2.TypeParamList {
                if !obj.IsAlias() {
                        return obj.Type().(*types2.Named).TypeParams()
                }
+               if alias, ok := obj.Type().(*types2.Alias); ok {
+                       return alias.TypeParams()
+               }
        }
        return nil
 }
@@ -2974,6 +2977,14 @@ func splitNamed(typ *types2.Named) (*types2.TypeName, *types2.TypeList) {
        return typ.Obj(), typ.TypeArgs()
 }
 
+// splitAlias is like splitNamed, but for an alias type.
+func splitAlias(typ *types2.Alias) (*types2.TypeName, *types2.TypeList) {
+       orig := typ.Origin()
+       base.Assertf(typ.Obj() == orig.Obj(), "alias type %v has object %v, but %v has object %v", typ, typ.Obj(), orig, orig.Obj())
+
+       return typ.Obj(), typ.TypeArgs()
+}
+
 func asPragmaFlag(p syntax.Pragma) ir.PragmaFlag {
        if p == nil {
                return 0
diff --git a/test/fixedbugs/issue68054.go b/test/fixedbugs/issue68054.go
new file mode 100644 (file)
index 0000000..5409fc9
--- /dev/null
@@ -0,0 +1,23 @@
+// compile -goexperiment aliastypeparams
+
+// Copyright 2024 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package p
+
+type Seq[V any] = func(yield func(V) bool)
+
+func f[E any](seq Seq[E]) {
+       return
+}
+
+func g() {
+       f(Seq[int](nil))
+}
+
+type T[P any] struct{}
+
+type A[P any] = T[P]
+
+var _ A[int]