Skip to content

Commit c9940fe

Browse files
cuonglmgopherbot
authored andcommitted
[release-branch.go1.23] types2, go/types: fix instantiation of named type with generic alias
The typechecker is assuming that alias instances cannot be reached from a named type. However, when type parameters on aliases are permited, it can happen. This CL changes the typechecker to propagate the correct named instance is being expanded. Updates #46477 Fixes #68580 Change-Id: Id0879021f4640c0fefe277701d5096c649413811 Reviewed-on: https://go-review.googlesource.com/c/go/+/601115 Auto-Submit: Robert Griesemer <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> Auto-Submit: Cuong Manh Le <[email protected]> Reviewed-on: https://go-review.googlesource.com/c/go/+/601116
1 parent 3509415 commit c9940fe

File tree

7 files changed

+31
-12
lines changed

7 files changed

+31
-12
lines changed

src/cmd/compile/internal/types2/alias.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,10 @@ func (check *Checker) newAlias(obj *TypeName, rhs Type) *Alias {
134134
// newAliasInstance creates a new alias instance for the given origin and type
135135
// arguments, recording pos as the position of its synthetic object (for error
136136
// reporting).
137-
func (check *Checker) newAliasInstance(pos syntax.Pos, orig *Alias, targs []Type, ctxt *Context) *Alias {
137+
func (check *Checker) newAliasInstance(pos syntax.Pos, orig *Alias, targs []Type, expanding *Named, ctxt *Context) *Alias {
138138
assert(len(targs) > 0)
139139
obj := NewTypeName(pos, orig.obj.pkg, orig.obj.name, nil)
140-
rhs := check.subst(pos, orig.fromRHS, makeSubstMap(orig.TypeParams().list(), targs), nil, ctxt)
140+
rhs := check.subst(pos, orig.fromRHS, makeSubstMap(orig.TypeParams().list(), targs), expanding, ctxt)
141141
res := check.newAlias(obj, rhs)
142142
res.orig = orig
143143
res.tparams = orig.tparams

src/cmd/compile/internal/types2/instantiate.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"cmd/compile/internal/syntax"
1212
"errors"
1313
"fmt"
14+
"internal/buildcfg"
1415
. "internal/types/errors"
1516
)
1617

@@ -126,8 +127,9 @@ func (check *Checker) instance(pos syntax.Pos, orig genericType, targs []Type, e
126127
res = check.newNamedInstance(pos, orig, targs, expanding) // substituted lazily
127128

128129
case *Alias:
129-
// TODO(gri) is this correct?
130-
assert(expanding == nil) // Alias instances cannot be reached from Named types
130+
if !buildcfg.Experiment.AliasTypeParams {
131+
assert(expanding == nil) // Alias instances cannot be reached from Named types
132+
}
131133

132134
tparams := orig.TypeParams()
133135
// TODO(gri) investigate if this is needed (type argument and parameter count seem to be correct here)
@@ -138,7 +140,7 @@ func (check *Checker) instance(pos syntax.Pos, orig genericType, targs []Type, e
138140
return orig // nothing to do (minor optimization)
139141
}
140142

141-
return check.newAliasInstance(pos, orig, targs, ctxt)
143+
return check.newAliasInstance(pos, orig, targs, expanding, ctxt)
142144

143145
case *Signature:
144146
assert(expanding == nil) // function instances cannot be reached from Named types

src/cmd/compile/internal/types2/subst.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func (subst *subster) typ(typ Type) Type {
115115
// that has a type argument for it.
116116
targs, updated := subst.typeList(t.TypeArgs().list())
117117
if updated {
118-
return subst.check.newAliasInstance(subst.pos, t.orig, targs, subst.ctxt)
118+
return subst.check.newAliasInstance(subst.pos, t.orig, targs, subst.expanding, subst.ctxt)
119119
}
120120

121121
case *Array:

src/go/types/alias.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/go/types/instantiate.go

Lines changed: 5 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/go/types/subst.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixedbugs/issue68580.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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 main
8+
9+
type A[P any] = struct{ _ P }
10+
11+
type N[P any] A[P]
12+
13+
func f[P any](N[P]) {}
14+
15+
var _ = f[int]

0 commit comments

Comments
 (0)