]> Cypherpunks repositories - gostls13.git/commit
go/types, types2: report an error when using a broken alias
authorRobert Griesemer <gri@golang.org>
Fri, 21 Jan 2022 02:56:57 +0000 (18:56 -0800)
committerRobert Griesemer <gri@golang.org>
Mon, 24 Jan 2022 21:27:32 +0000 (21:27 +0000)
commitfe85c244315f82b1a6a21cd6ddc0255eed92a357
tree78604a55f0f4c383f87cd36c77d4726f1ab31833
parentfef14fdd1dc9106f872e75aae4fcd814abc47080
go/types, types2: report an error when using a broken alias

The type checker doesn't have a general mechanism to "use" the type
of a type alias whose type depends on a recursive type declaration
which is not yet completely type-checked. In some cases, the type of
a type alias is needed before it is determined; the type is incorrect
(invalid) in that case but no error is reported. The type-checker is
happy with this (incorrect type), but the compiler may crash under
some circumstances.

A correct fix will likely require some form of forwarding type which
is a fairly pervasive change and may also affect the type checker API.

This CL introduces a simple side table, a map of broken type aliases,
which is consulted before the type associated with a type alias is
used. If the type alias is broken, an error is reported.

This is a stop-gap solution that prevents the compiler from crashing.
The reported error refers to the corresponding issue which suggests
a work-around that may be applicable in some cases.

Also fix a minor error related to type cycles: If we have a cycle
that doesn't start with a type, don't use a compiler error message
that explicitly mentions "type".

Fixes #50259.
Fixes #50276.
Fixes #50779.

For #50729.

Change-Id: Ie8e38f49ef724e742e8e78625e6d4f3d4014a52c
Reviewed-on: https://go-review.googlesource.com/c/go/+/379916
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
15 files changed:
src/cmd/compile/internal/types2/check.go
src/cmd/compile/internal/types2/decl.go
src/cmd/compile/internal/types2/testdata/check/cycles5.src
src/cmd/compile/internal/types2/testdata/fixedbugs/issue50259.go2 [new file with mode: 0644]
src/cmd/compile/internal/types2/testdata/fixedbugs/issue50276.go2 [new file with mode: 0644]
src/cmd/compile/internal/types2/testdata/fixedbugs/issue50779.go2 [new file with mode: 0644]
src/cmd/compile/internal/types2/typexpr.go
src/go/types/check.go
src/go/types/decl.go
src/go/types/testdata/check/cycles5.src
src/go/types/testdata/fixedbugs/issue50259.go2 [new file with mode: 0644]
src/go/types/testdata/fixedbugs/issue50276.go2 [new file with mode: 0644]
src/go/types/testdata/fixedbugs/issue50779.go2 [new file with mode: 0644]
src/go/types/typexpr.go
test/typeparam/issue50259.go [new file with mode: 0644]