From ccb6077d11e9e70cb17d3eaadaee4e673ee650c2 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Wed, 21 Feb 2024 12:54:10 -0800 Subject: [PATCH] go/types, types2: handle Alias types in substitution Fixes #65854. For #65778. // for x/tools/cmd/gotype Change-Id: I67d4644b28e831926fc6c233098aa1755c57162f Reviewed-on: https://go-review.googlesource.com/c/go/+/565835 Auto-Submit: Robert Griesemer LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley Reviewed-by: Robert Griesemer --- src/cmd/compile/internal/types2/subst.go | 12 ++++++++++++ src/go/types/subst.go | 12 ++++++++++++ src/internal/types/testdata/fixedbugs/issue65854.go | 13 +++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 src/internal/types/testdata/fixedbugs/issue65854.go diff --git a/src/cmd/compile/internal/types2/subst.go b/src/cmd/compile/internal/types2/subst.go index 09dc58527a1..1ad73c41ce1 100644 --- a/src/cmd/compile/internal/types2/subst.go +++ b/src/cmd/compile/internal/types2/subst.go @@ -95,6 +95,18 @@ func (subst *subster) typ(typ Type) Type { case *Basic: // nothing to do + case *Alias: + rhs := subst.typ(t.fromRHS) + if rhs != t.fromRHS { + // This branch cannot be reached because the RHS of an alias + // may only contain type parameters of an enclosing function. + // Such function bodies are never "instantiated" and thus + // substitution is not called on locally declared alias types. + // TODO(gri) adjust once parameterized aliases are supported + panic("unreachable for unparameterized aliases") + // return subst.check.newAlias(t.obj, rhs) + } + case *Array: elem := subst.typOrNil(t.elem) if elem != t.elem { diff --git a/src/go/types/subst.go b/src/go/types/subst.go index 1934ebab2b1..178f7172835 100644 --- a/src/go/types/subst.go +++ b/src/go/types/subst.go @@ -97,6 +97,18 @@ func (subst *subster) typ(typ Type) Type { case *Basic: // nothing to do + case *Alias: + rhs := subst.typ(t.fromRHS) + if rhs != t.fromRHS { + // This branch cannot be reached because the RHS of an alias + // may only contain type parameters of an enclosing function. + // Such function bodies are never "instantiated" and thus + // substitution is not called on locally declared alias types. + // TODO(gri) adjust once parameterized aliases are supported + panic("unreachable for unparameterized aliases") + // return subst.check.newAlias(t.obj, rhs) + } + case *Array: elem := subst.typOrNil(t.elem) if elem != t.elem { diff --git a/src/internal/types/testdata/fixedbugs/issue65854.go b/src/internal/types/testdata/fixedbugs/issue65854.go new file mode 100644 index 00000000000..744777a94f8 --- /dev/null +++ b/src/internal/types/testdata/fixedbugs/issue65854.go @@ -0,0 +1,13 @@ +// -gotypesalias=1 + +// Copyright 2024 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 + +type A = int + +type T[P any] *A + +var _ T[int]