// A type parameter stands for the type (argument) it was instantiated with.
// Check the corresponding type argument for validity if we are in an
// instantiated type.
- if len(nest) > 0 {
- inst := nest[len(nest)-1] // the type instance
+ if d := len(nest) - 1; d >= 0 {
+ inst := nest[d] // the type instance
// Find the corresponding type argument for the type parameter
// and proceed with checking that type argument.
for i, tparam := range inst.TypeParams().list() {
// the current (instantiated) type (see the example
// at the end of this file).
// For error reporting we keep the full path.
- return check.validType0(pos, targ, nest[:len(nest)-1], path)
+ res := check.validType0(pos, targ, nest[:d], path)
+ // The check.validType0 call with nest[:d] may have
+ // overwritten the entry at the current depth d.
+ // Restore the entry (was issue go.dev/issue/66323).
+ nest[d] = inst
+ return res
}
}
}
// A type parameter stands for the type (argument) it was instantiated with.
// Check the corresponding type argument for validity if we are in an
// instantiated type.
- if len(nest) > 0 {
- inst := nest[len(nest)-1] // the type instance
+ if d := len(nest) - 1; d >= 0 {
+ inst := nest[d] // the type instance
// Find the corresponding type argument for the type parameter
// and proceed with checking that type argument.
for i, tparam := range inst.TypeParams().list() {
// the current (instantiated) type (see the example
// at the end of this file).
// For error reporting we keep the full path.
- return check.validType0(pos, targ, nest[:len(nest)-1], path)
+ res := check.validType0(pos, targ, nest[:d], path)
+ // The check.validType0 call with nest[:d] may have
+ // overwritten the entry at the current depth d.
+ // Restore the entry (was issue go.dev/issue/66323).
+ nest[d] = inst
+ return res
}
}
}
--- /dev/null
+// 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
+
+import "time"
+
+// This type declaration must not cause problems with
+// the type validity checker.
+
+type S[T any] struct {
+ a T
+ b time.Time
+}
+
+var _ S[time.Time]