@@ -24,7 +24,6 @@ import (
24
24
25
25
"golang.org/x/tools/go/gcexportdata"
26
26
"golang.org/x/tools/go/packages"
27
- "golang.org/x/tools/internal/aliases"
28
27
"golang.org/x/tools/internal/gcimporter"
29
28
"golang.org/x/tools/internal/testenv"
30
29
)
@@ -440,7 +439,7 @@ func TestIExportDataTypeParameterizedAliases(t *testing.T) {
440
439
// * import the data (via either x/tools or GOROOT's gcimporter), and
441
440
// * check the imported types.
442
441
443
- const src = `package a
442
+ const src = `package pkg
444
443
445
444
type A[T any] = *T
446
445
type B[R any, S *R] = []S
@@ -452,12 +451,12 @@ type Chained = C[Named] // B[Named, A[Named]] = B[Named, *Named] = []*Named
452
451
453
452
// parse and typecheck
454
453
fset1 := token .NewFileSet ()
455
- f , err := parser .ParseFile (fset1 , "a " , src , 0 )
454
+ f , err := parser .ParseFile (fset1 , "pkg " , src , 0 )
456
455
if err != nil {
457
456
t .Fatal (err )
458
457
}
459
458
var conf types.Config
460
- pkg1 , err := conf .Check ("a " , fset1 , []* ast.File {f }, nil )
459
+ pkg1 , err := conf .Check ("pkg " , fset1 , []* ast.File {f }, nil )
461
460
if err != nil {
462
461
t .Fatal (err )
463
462
}
@@ -499,7 +498,7 @@ type Chained = C[Named] // B[Named, A[Named]] = B[Named, *Named] = []*Named
499
498
500
499
// Write export data to temporary file
501
500
out := t .TempDir ()
502
- name := filepath .Join (out , "a .out" )
501
+ name := filepath .Join (out , "pkg .out" )
503
502
if err := os .WriteFile (name + ".a" , buf .Bytes (), 0644 ); err != nil {
504
503
t .Fatal (err )
505
504
}
@@ -514,58 +513,23 @@ type Chained = C[Named] // B[Named, A[Named]] = B[Named, *Named] = []*Named
514
513
for name , importer := range testcases {
515
514
t .Run (name , func (t * testing.T ) {
516
515
pkg := importer (t )
516
+ for name , want := range map [string ]string {
517
+ "A" : "type pkg.A[T any] = *T" ,
518
+ "B" : "type pkg.B[R any, S *R] = []S" ,
519
+ "C" : "type pkg.C[U any] = pkg.B[U, pkg.A[U]]" ,
520
+ "Named" : "type pkg.Named int" ,
521
+ "Chained" : "type pkg.Chained = pkg.C[pkg.Named]" ,
522
+ } {
523
+ obj := pkg .Scope ().Lookup (name )
524
+ if obj == nil {
525
+ t .Errorf ("failed to find %q in package %s" , name , pkg )
526
+ continue
527
+ }
517
528
518
- obj := pkg .Scope ().Lookup ("A" )
519
- if obj == nil {
520
- t .Fatalf ("failed to find %q in package %s" , "A" , pkg )
521
- }
522
-
523
- // Check that A is type A[T any] = *T.
524
- // TODO(taking): fix how go/types prints parameterized aliases to simplify tests.
525
- alias , ok := obj .Type ().(* types.Alias )
526
- if ! ok {
527
- t .Fatalf ("Obj %s is not an Alias" , obj )
528
- }
529
-
530
- targs := aliases .TypeArgs (alias )
531
- if targs .Len () != 0 {
532
- t .Errorf ("%s has %d type arguments. expected 0" , alias , targs .Len ())
533
- }
534
-
535
- tparams := aliases .TypeParams (alias )
536
- if tparams .Len () != 1 {
537
- t .Fatalf ("%s has %d type arguments. expected 1" , alias , targs .Len ())
538
- }
539
- tparam := tparams .At (0 )
540
- if got , want := tparam .String (), "T" ; got != want {
541
- t .Errorf ("(%q).TypeParams().At(0)=%q. want %q" , alias , got , want )
542
- }
543
-
544
- anyt := types .Universe .Lookup ("any" ).Type ()
545
- if c := tparam .Constraint (); ! types .Identical (anyt , c ) {
546
- t .Errorf ("(%q).Constraint()=%q. expected %q" , tparam , c , anyt )
547
- }
548
-
549
- ptparam := types .NewPointer (tparam )
550
- if rhs := aliases .Rhs (alias ); ! types .Identical (ptparam , rhs ) {
551
- t .Errorf ("(%q).Rhs()=%q. expected %q" , alias , rhs , ptparam )
552
- }
553
-
554
- // TODO(taking): add tests for B and C once it is simpler to write tests.
555
-
556
- chained := pkg .Scope ().Lookup ("Chained" )
557
- if chained == nil {
558
- t .Fatalf ("failed to find %q in package %s" , "Chained" , pkg )
559
- }
560
-
561
- named , _ := pkg .Scope ().Lookup ("Named" ).(* types.TypeName )
562
- if named == nil {
563
- t .Fatalf ("failed to find %q in package %s" , "Named" , pkg )
564
- }
565
-
566
- want := types .NewSlice (types .NewPointer (named .Type ()))
567
- if got := chained .Type (); ! types .Identical (got , want ) {
568
- t .Errorf ("(%q).Type()=%q which should be identical to %q" , chained , got , want )
529
+ got := strings .ReplaceAll (obj .String (), pkg .Path (), "pkg" )
530
+ if got != want {
531
+ t .Errorf ("(%q).String()=%q. wanted %q" , name , got , want )
532
+ }
569
533
}
570
534
})
571
535
}
0 commit comments