Skip to content

Commit 477ad7d

Browse files
cuonglmgopherbot
authored andcommitted
cmd/compile: support generic alias type
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 <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Robert Griesemer <[email protected]>
1 parent 4f77a83 commit 477ad7d

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/cmd/compile/internal/noder/writer.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ func (pw *pkgWriter) typIdx(typ types2.Type, dict *writerDict) typeInfo {
543543

544544
case *types2.Alias:
545545
w.Code(pkgbits.TypeNamed)
546-
w.namedType(typ.Obj(), nil)
546+
w.namedType(splitAlias(typ))
547547

548548
case *types2.TypeParam:
549549
w.derived = true
@@ -2958,6 +2958,9 @@ func objTypeParams(obj types2.Object) *types2.TypeParamList {
29582958
if !obj.IsAlias() {
29592959
return obj.Type().(*types2.Named).TypeParams()
29602960
}
2961+
if alias, ok := obj.Type().(*types2.Alias); ok {
2962+
return alias.TypeParams()
2963+
}
29612964
}
29622965
return nil
29632966
}
@@ -2974,6 +2977,14 @@ func splitNamed(typ *types2.Named) (*types2.TypeName, *types2.TypeList) {
29742977
return typ.Obj(), typ.TypeArgs()
29752978
}
29762979

2980+
// splitAlias is like splitNamed, but for an alias type.
2981+
func splitAlias(typ *types2.Alias) (*types2.TypeName, *types2.TypeList) {
2982+
orig := typ.Origin()
2983+
base.Assertf(typ.Obj() == orig.Obj(), "alias type %v has object %v, but %v has object %v", typ, typ.Obj(), orig, orig.Obj())
2984+
2985+
return typ.Obj(), typ.TypeArgs()
2986+
}
2987+
29772988
func asPragmaFlag(p syntax.Pragma) ir.PragmaFlag {
29782989
if p == nil {
29792990
return 0

test/fixedbugs/issue68054.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// compile -goexperiment aliastypeparams
2+
3+
// Copyright 2024 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package p
8+
9+
type Seq[V any] = func(yield func(V) bool)
10+
11+
func f[E any](seq Seq[E]) {
12+
return
13+
}
14+
15+
func g() {
16+
f(Seq[int](nil))
17+
}
18+
19+
type T[P any] struct{}
20+
21+
type A[P any] = T[P]
22+
23+
var _ A[int]

0 commit comments

Comments
 (0)