types2, go/types: add missing alias test to types2, simplify go/types test

Change-Id: I4d3377e704d718a1914135a444b9df59f84af179
Reviewed-on: https://go-review.googlesource.com/c/go/+/780120
Reviewed-by: Robert Griesemer <gri@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
Reviewed-by: David Chase <drchase@google.com>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Robert Griesemer 2026-05-19 14:34:18 -07:00 committed by Gopher Robot
parent 2760c3f5a3
commit 37bce6617f
2 changed files with 67 additions and 19 deletions

View file

@ -0,0 +1,61 @@
// Copyright 2026 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 types2_test
import (
"cmd/compile/internal/types2"
"testing"
)
func TestIssue74181(t *testing.T) {
src := `package p
type AB = A[B]
type _ struct {
_ AB
}
type B struct {
f *AB
}
type A[T any] struct{}
`
pkg := mustTypecheck(src, nil, nil)
b := pkg.Scope().Lookup("B").Type()
if n, ok := b.(*types2.Named); ok {
if s, ok := n.Underlying().(*types2.Struct); ok {
got := s.Field(0).Type()
want := types2.NewPointer(pkg.Scope().Lookup("AB").Type())
if !types2.Identical(got, want) {
t.Errorf("wrong type for f: got %v, want %v", got, want)
}
return
}
}
t.Errorf("unexpected type for B: %v", b)
}
func TestPartialTypeCheckUndeclaredAliasPanic(t *testing.T) {
src := `package p
type A = B // undeclared
`
pkg, _ := typecheck(src, nil, nil) // don't panic on error
a := pkg.Scope().Lookup("A").Type()
if alias, ok := a.(*types2.Alias); ok {
got := alias.Rhs()
want := types2.Typ[types2.Invalid]
if !types2.Identical(got, want) {
t.Errorf("wrong type for B: got %v, want %v", got, want)
}
return
}
t.Errorf("unexpected type for A: %v", a)
}

View file

@ -5,8 +5,6 @@
package types_test
import (
"go/ast"
"go/token"
"go/types"
"testing"
)
@ -24,17 +22,10 @@ type B struct {
f *AB
}
type A[T any] struct{}`
fset := token.NewFileSet()
file := mustParse(fset, src)
conf := types.Config{}
pkg, err := conf.Check(file.Name.Name, fset, []*ast.File{file}, &types.Info{})
if err != nil {
t.Fatalf("could not type check: %v", err)
}
type A[T any] struct{}
`
pkg := mustTypecheck(src, nil, nil)
b := pkg.Scope().Lookup("B").Type()
if n, ok := b.(*types.Named); ok {
if s, ok := n.Underlying().(*types.Struct); ok {
@ -52,15 +43,11 @@ type A[T any] struct{}`
func TestPartialTypeCheckUndeclaredAliasPanic(t *testing.T) {
src := `package p
type A = B // undeclared`
type A = B // undeclared
`
fset := token.NewFileSet()
file := mustParse(fset, src)
conf := types.Config{} // no error handler, panic
pkg, _ := conf.Check(file.Name.Name, fset, []*ast.File{file}, &types.Info{})
pkg, _ := typecheck(src, nil, nil) // don't panic on error
a := pkg.Scope().Lookup("A").Type()
if alias, ok := a.(*types.Alias); ok {
got := alias.Rhs()
want := types.Typ[types.Invalid]