Source file src/cmd/compile/internal/types2/alias_test.go

     1  // Copyright 2026 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package types2_test
     6  
     7  import (
     8  	"cmd/compile/internal/types2"
     9  	"testing"
    10  )
    11  
    12  func TestIssue74181(t *testing.T) {
    13  	src := `package p
    14  
    15  type AB = A[B]
    16  
    17  type _ struct {
    18  	_ AB
    19  }
    20  
    21  type B struct {
    22  	f *AB
    23  }
    24  
    25  type A[T any] struct{}
    26  `
    27  
    28  	pkg := mustTypecheck(src, nil, nil)
    29  	b := pkg.Scope().Lookup("B").Type()
    30  	if n, ok := b.(*types2.Named); ok {
    31  		if s, ok := n.Underlying().(*types2.Struct); ok {
    32  			got := s.Field(0).Type()
    33  			want := types2.NewPointer(pkg.Scope().Lookup("AB").Type())
    34  			if !types2.Identical(got, want) {
    35  				t.Errorf("wrong type for f: got %v, want %v", got, want)
    36  			}
    37  			return
    38  		}
    39  	}
    40  	t.Errorf("unexpected type for B: %v", b)
    41  }
    42  
    43  func TestPartialTypeCheckUndeclaredAliasPanic(t *testing.T) {
    44  	src := `package p
    45  
    46  type A = B // undeclared
    47  `
    48  
    49  	pkg, _ := typecheck(src, nil, nil) // don't panic on error
    50  	a := pkg.Scope().Lookup("A").Type()
    51  	if alias, ok := a.(*types2.Alias); ok {
    52  		got := alias.Rhs()
    53  		want := types2.Typ[types2.Invalid]
    54  
    55  		if !types2.Identical(got, want) {
    56  			t.Errorf("wrong type for B: got %v, want %v", got, want)
    57  		}
    58  		return
    59  	}
    60  	t.Errorf("unexpected type for A: %v", a)
    61  }
    62  

View as plain text