}
}
- // We process non-alias declarations first, in order to avoid situations where
- // the type of an alias declaration is needed before it is available. In general
- // this is still not enough, as it is possible to create sufficiently convoluted
- // recursive type definitions that will cause a type alias to be needed before it
- // is available (see issue #25838 for examples).
- // As an aside, the cmd/compiler suffers from the same problem (#25838).
+ // We process non-alias type declarations first, followed by alias declarations,
+ // and then everything else. This appears to avoid most situations where the type
+ // of an alias is needed before it is available.
+ // There may still be cases where this is not good enough (see also issue #25838).
+ // In those cases Checker.ident will report an error ("invalid use of type alias").
var aliasList []*TypeName
- // phase 1
+ var othersList []Object // everything that's not a type
+ // phase 1: non-alias type declarations
for _, obj := range objList {
- // If we have a type alias, collect it for the 2nd phase.
- if tname, _ := obj.(*TypeName); tname != nil && check.objMap[tname].tdecl.Alias {
- aliasList = append(aliasList, tname)
- continue
+ if tname, _ := obj.(*TypeName); tname != nil {
+ if check.objMap[tname].tdecl.Alias {
+ aliasList = append(aliasList, tname)
+ } else {
+ check.objDecl(obj, nil)
+ }
+ } else {
+ othersList = append(othersList, obj)
}
-
- check.objDecl(obj, nil)
}
- // phase 2
+ // phase 2: alias type declarations
for _, obj := range aliasList {
check.objDecl(obj, nil)
}
+ // phase 3: all other declarations
+ for _, obj := range othersList {
+ check.objDecl(obj, nil)
+ }
// At this point we may have a non-empty check.methods map; this means that not all
// entries were deleted at the end of typeDecl because the respective receiver base
--- /dev/null
+// Copyright 2022 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
+
+// examples from the issue
+
+type (
+ e = f
+ f = g
+ g = []h
+ h i
+ i = j
+ j = e
+)
+
+type (
+ e1 = []h1
+ h1 e1
+)
+
+type (
+ P = *T
+ T P
+)
var x T[B]
type T[_ any] struct{}
-type A T[B /* ERROR invalid use of type alias */ ]
+type A T[B]
type B = T[A]
// test case from issue
var v Box[Step]
type Box[T any] struct{}
type Step = Box[StepBox]
-type StepBox Box[Step /* ERROR invalid use of type alias */ ]
+type StepBox Box[Step]
var _ transform[step]
-type box transform[step /* ERROR invalid use of type alias */ ]
+type box transform[step]
type step = pair[box]
// test case from issue
// This line doesn't use the Step alias, and it compiles fine if you uncomment it.
var second Transform[Pair[Box, interface{}]]
-type Box *Transform[Step /* ERROR invalid use of type alias */ ]
+type Box *Transform[Step]
// This line is the same as the `first` line, but it comes after the Box declaration and
// does not break the compile.
}
}
- // We process non-alias declarations first, in order to avoid situations where
- // the type of an alias declaration is needed before it is available. In general
- // this is still not enough, as it is possible to create sufficiently convoluted
- // recursive type definitions that will cause a type alias to be needed before it
- // is available (see issue #25838 for examples).
- // As an aside, the cmd/compiler suffers from the same problem (#25838).
+ // We process non-alias type declarations first, followed by alias declarations,
+ // and then everything else. This appears to avoid most situations where the type
+ // of an alias is needed before it is available.
+ // There may still be cases where this is not good enough (see also issue #25838).
+ // In those cases Checker.ident will report an error ("invalid use of type alias").
var aliasList []*TypeName
- // phase 1
+ var othersList []Object // everything that's not a type
+ // phase 1: non-alias type declarations
for _, obj := range objList {
- // If we have a type alias, collect it for the 2nd phase.
- if tname, _ := obj.(*TypeName); tname != nil && check.objMap[tname].tdecl.Assign.IsValid() {
- aliasList = append(aliasList, tname)
- continue
+ if tname, _ := obj.(*TypeName); tname != nil {
+ if check.objMap[tname].tdecl.Assign.IsValid() {
+ aliasList = append(aliasList, tname)
+ } else {
+ check.objDecl(obj, nil)
+ }
+ } else {
+ othersList = append(othersList, obj)
}
-
- check.objDecl(obj, nil)
}
- // phase 2
+ // phase 2: alias type declarations
for _, obj := range aliasList {
check.objDecl(obj, nil)
}
+ // phase 3: all other declarations
+ for _, obj := range othersList {
+ check.objDecl(obj, nil)
+ }
// At this point we may have a non-empty check.methods map; this means that not all
// entries were deleted at the end of typeDecl because the respective receiver base
--- /dev/null
+// Copyright 2022 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
+
+// examples from the issue
+
+type (
+ e = f
+ f = g
+ g = []h
+ h i
+ i = j
+ j = e
+)
+
+type (
+ e1 = []h1
+ h1 e1
+)
+
+type (
+ P = *T
+ T P
+)
var x T[B]
type T[_ any] struct{}
-type A T[B /* ERROR invalid use of type alias */ ]
+type A T[B]
type B = T[A]
// test case from issue
var v Box[Step]
type Box[T any] struct{}
type Step = Box[StepBox]
-type StepBox Box[Step /* ERROR invalid use of type alias */ ]
+type StepBox Box[Step]
var _ transform[step]
-type box transform[step /* ERROR invalid use of type alias */ ]
+type box transform[step]
type step = pair[box]
// test case from issue
// This line doesn't use the Step alias, and it compiles fine if you uncomment it.
var second Transform[Pair[Box, interface{}]]
-type Box *Transform[Step /* ERROR invalid use of type alias */ ]
+type Box *Transform[Step]
// This line is the same as the `first` line, but it comes after the Box declaration and
// does not break the compile.
-// errorcheck -G=3
+// compile -G=3
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
var x T[B]
type T[_ any] struct{}
-type A T[B] // ERROR "invalid use of type alias B in recursive type"
+type A T[B]
type B = T[A]