// newAliasInstance creates a new alias instance for the given origin and type
// arguments, recording pos as the position of its synthetic object (for error
// reporting).
-func (check *Checker) newAliasInstance(pos syntax.Pos, orig *Alias, targs []Type, ctxt *Context) *Alias {
+func (check *Checker) newAliasInstance(pos syntax.Pos, orig *Alias, targs []Type, expanding *Named, ctxt *Context) *Alias {
assert(len(targs) > 0)
obj := NewTypeName(pos, orig.obj.pkg, orig.obj.name, nil)
- rhs := check.subst(pos, orig.fromRHS, makeSubstMap(orig.TypeParams().list(), targs), nil, ctxt)
+ rhs := check.subst(pos, orig.fromRHS, makeSubstMap(orig.TypeParams().list(), targs), expanding, ctxt)
res := check.newAlias(obj, rhs)
res.orig = orig
res.tparams = orig.tparams
"cmd/compile/internal/syntax"
"errors"
"fmt"
+ "internal/buildcfg"
. "internal/types/errors"
)
res = check.newNamedInstance(pos, orig, targs, expanding) // substituted lazily
case *Alias:
- // TODO(gri) is this correct?
- assert(expanding == nil) // Alias instances cannot be reached from Named types
+ if !buildcfg.Experiment.AliasTypeParams {
+ assert(expanding == nil) // Alias instances cannot be reached from Named types
+ }
tparams := orig.TypeParams()
// TODO(gri) investigate if this is needed (type argument and parameter count seem to be correct here)
return orig // nothing to do (minor optimization)
}
- return check.newAliasInstance(pos, orig, targs, ctxt)
+ return check.newAliasInstance(pos, orig, targs, expanding, ctxt)
case *Signature:
assert(expanding == nil) // function instances cannot be reached from Named types
// that has a type argument for it.
targs, updated := subst.typeList(t.TypeArgs().list())
if updated {
- return subst.check.newAliasInstance(subst.pos, t.orig, targs, subst.ctxt)
+ return subst.check.newAliasInstance(subst.pos, t.orig, targs, subst.expanding, subst.ctxt)
}
case *Array:
// newAliasInstance creates a new alias instance for the given origin and type
// arguments, recording pos as the position of its synthetic object (for error
// reporting).
-func (check *Checker) newAliasInstance(pos token.Pos, orig *Alias, targs []Type, ctxt *Context) *Alias {
+func (check *Checker) newAliasInstance(pos token.Pos, orig *Alias, targs []Type, expanding *Named, ctxt *Context) *Alias {
assert(len(targs) > 0)
obj := NewTypeName(pos, orig.obj.pkg, orig.obj.name, nil)
- rhs := check.subst(pos, orig.fromRHS, makeSubstMap(orig.TypeParams().list(), targs), nil, ctxt)
+ rhs := check.subst(pos, orig.fromRHS, makeSubstMap(orig.TypeParams().list(), targs), expanding, ctxt)
res := check.newAlias(obj, rhs)
res.orig = orig
res.tparams = orig.tparams
"errors"
"fmt"
"go/token"
+ "internal/buildcfg"
. "internal/types/errors"
)
res = check.newNamedInstance(pos, orig, targs, expanding) // substituted lazily
case *Alias:
- // TODO(gri) is this correct?
- assert(expanding == nil) // Alias instances cannot be reached from Named types
+ if !buildcfg.Experiment.AliasTypeParams {
+ assert(expanding == nil) // Alias instances cannot be reached from Named types
+ }
tparams := orig.TypeParams()
// TODO(gri) investigate if this is needed (type argument and parameter count seem to be correct here)
return orig // nothing to do (minor optimization)
}
- return check.newAliasInstance(pos, orig, targs, ctxt)
+ return check.newAliasInstance(pos, orig, targs, expanding, ctxt)
case *Signature:
assert(expanding == nil) // function instances cannot be reached from Named types
// that has a type argument for it.
targs, updated := subst.typeList(t.TypeArgs().list())
if updated {
- return subst.check.newAliasInstance(subst.pos, t.orig, targs, subst.ctxt)
+ return subst.check.newAliasInstance(subst.pos, t.orig, targs, subst.expanding, subst.ctxt)
}
case *Array:
--- /dev/null
+// 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 main
+
+type A[P any] = struct{ _ P }
+
+type N[P any] A[P]
+
+func f[P any](N[P]) {}
+
+var _ = f[int]