Skip to content

Commit

Permalink
[release-branch.go1.23] cmd/compile: more informative panic when impo…
Browse files Browse the repository at this point in the history
…rting generic type alias

When GOEXPERIMENT=aliastypeparams is set, type aliases may have type
parameters. The compiler export data doesn't export that type parameter
information yet, which leads to an index-out-of-bounds panic when a
client package imports a package with a general type alias and then
refers to one of the missing type parameters.

This CL detects this specific case and panics with a more informative
panic message explaining the shortcoming. The change is only in effect
if the respective GOEXPERIMENT is enabled.

Manually tested. No test addded since this is just a temporary fix
(Go 1.24 will have a complete implementation), and because the existing
testing framework doesn't easily support testing that a compilation
panics.

Together with @taking and input from @rfindley.

For golang#68526.

Change-Id: I24737b153a7e2f9b705cd29a5b70b2b9e808dffc
Reviewed-on: https://go-review.googlesource.com/c/go/+/601035
Reviewed-by: Tim King <taking@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Robert Griesemer <gri@google.com>
  • Loading branch information
griesemer authored and wangyifan committed Dec 16, 2024
1 parent 973f086 commit 8ea3c8b
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/cmd/compile/internal/importer/ureader.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"cmd/compile/internal/syntax"
"cmd/compile/internal/types2"
"cmd/internal/src"
"internal/buildcfg"
"internal/pkgbits"
)

Expand Down Expand Up @@ -417,6 +418,14 @@ func (pr *pkgReader) objIdx(idx pkgbits.Index) (*types2.Package, string) {
panic("weird")

case pkgbits.ObjAlias:
if buildcfg.Experiment.AliasTypeParams && len(r.dict.bounds) > 0 {
// Temporary work-around for issue #68526: rather than panicking
// with an non-descriptive index-out-of-bounds panic when trying
// to access a missing type parameter, instead panic with a more
// descriptive error. Only needed for Go 1.23; Go 1.24 will have
// the correct implementation.
panic("importing generic type aliases is not supported in Go 1.23 (see issue #68526)")
}
pos := r.pos()
var tparams []*types2.TypeParam
if r.Version().Has(pkgbits.AliasTypeParamNames) {
Expand Down

0 comments on commit 8ea3c8b

Please sign in to comment.