Skip to content

Commit 0fcd405

Browse files
committed
go/types: avoid certain problems with recursive alias type declarations
It is possible to create certain recursive type declarations involving alias types which cause the type-checker to produce an (invalid) type for the alias because it is not yet available. By type-checking alias declarations in a 2nd phase, the problem is mitigated a bit since it requires more convoluted alias declarations for the problem to appear. Also re-enable testing of fixedbugs/issue27232.go again (which was the original cause for this change). Updates #28576. Change-Id: If6f9656a95262e6575b01c4a003094d41551564b Reviewed-on: https://go-review.googlesource.com/c/147597 Reviewed-by: Alan Donovan <[email protected]>
1 parent 644ddaa commit 0fcd405

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/go/types/resolver.go

+18
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,25 @@ func (check *Checker) packageObjects() {
570570
}
571571
}
572572

573+
// We process non-alias declarations first, in order to avoid situations where
574+
// the type of an alias declaration is needed before it is available. In general
575+
// this is still not enough, as it is possible to create sufficiently convoluted
576+
// recursive type definitions that will cause a type alias to be needed before it
577+
// is available (see issue #25838 for examples).
578+
// As an aside, the cmd/compiler suffers from the same problem (#25838).
579+
var aliasList []*TypeName
580+
// phase 1
573581
for _, obj := range objList {
582+
// If we have a type alias, collect it for the 2nd phase.
583+
if tname, _ := obj.(*TypeName); tname != nil && check.objMap[tname].alias {
584+
aliasList = append(aliasList, tname)
585+
continue
586+
}
587+
588+
check.objDecl(obj, nil)
589+
}
590+
// phase 2
591+
for _, obj := range aliasList {
574592
check.objDecl(obj, nil)
575593
}
576594

src/go/types/stdlib_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,6 @@ func TestStdFixed(t *testing.T) {
180180
"issue22200b.go", // go/types does not have constraints on stack size
181181
"issue25507.go", // go/types does not have constraints on stack size
182182
"issue20780.go", // go/types does not have constraints on stack size
183-
"issue27232.go", // go/types has a bug with alias type (issue #28576)
184183
)
185184
}
186185

0 commit comments

Comments
 (0)