]> Cypherpunks repositories - gostls13.git/commit
cmd/compile: fix importers to deal with recursion through type constraints
authorDan Scales <danscales@google.com>
Thu, 17 Feb 2022 04:11:52 +0000 (20:11 -0800)
committerDan Scales <danscales@google.com>
Fri, 18 Feb 2022 16:56:42 +0000 (16:56 +0000)
commitd35ed094864617d82c5701f56811ad68b37eda6e
tree9d97aafae5ba0b264c0551a93fbadc7178acb8f3
parenteaf040502b763a6f00dced35e4173c2ce90eb52f
cmd/compile: fix importers to deal with recursion through type constraints

The code for issue #51219 reveals bugs in the types1 and types2
importers that can occur for recursive types that are recursive through
the type constraint.

The crash in the issue is caused by the types1 bug, which leads to the
production of a type1 type which is incomplete and improperly has the
HasTParam flag set. The bug in the types1 importer is that we were not
deferring type instantiations when reading the type parameters, but we
need to do that exactly to correctly handle recursion through the type
constraint. So, the fix is to move the start of the deferrals (in the
'U' section of doDecl in typecheck/iimport.go) above the code that reads
the type params.

Once that bug is fixed, the test still crashes due to a related types2
importer issues. The problem is that t.SetConstraint(c) requires c to be
fully constructed (have its underlying type set). Since that may not be
done yet in the 'U' case in (*importReader).obj() in
importer/iimport.go, we need to defer the call to SetConstraint() in
that case, until we are done importing all the types.

I added a test case with recursion through a type constraint that causes
a problem that is fixed by the types1 importer change, though the error
is not the same as in the issue. I added more types in the test case
(which try to imitate the issue types more closely) the types2 bug, but
wasn't able to cause it yet with the smaller test case.

Fixes #51219

Change-Id: I85d860c98c09dddc37f76ce87a78a6015ec6fd20
Reviewed-on: https://go-review.googlesource.com/c/go/+/386335
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
src/cmd/compile/internal/importer/iimport.go
src/cmd/compile/internal/typecheck/iimport.go
src/go/internal/gcimporter/iimport.go
test/typeparam/issue51219.dir/a.go [new file with mode: 0644]
test/typeparam/issue51219.dir/b.go [new file with mode: 0644]
test/typeparam/issue51219.dir/main.go [new file with mode: 0644]
test/typeparam/issue51219.go [new file with mode: 0644]
test/typeparam/issue51219.out [new file with mode: 0644]