@@ -364,6 +364,179 @@ func TestDeclSelection_RemoveUnusedTypeConstraint(t *testing.T) {
364
364
sel .IsDead (`var:command-line-arguments.ghost` )
365
365
}
366
366
367
+ func TestDeclSelection_RemoveUnusedNestedTypesInFunction (t * testing.T ) {
368
+ src := `
369
+ package main
370
+ func Foo[T any](u T) any {
371
+ type Bar struct { v T }
372
+ return Bar{v: u}
373
+ }
374
+ func deadCode() {
375
+ println(Foo[int](42))
376
+ }
377
+ func main() {
378
+ println(Foo[string]("cat"))
379
+ }`
380
+
381
+ srcFiles := []srctesting.Source {{Name : `main.go` , Contents : []byte (src )}}
382
+ sel := declSelection (t , srcFiles , nil )
383
+ sel .IsAlive (`func:command-line-arguments.main` )
384
+
385
+ sel .IsAlive (`funcVar:command-line-arguments.Foo` )
386
+ sel .IsAlive (`func:command-line-arguments.Foo<string>` )
387
+ sel .IsDead (`func:command-line-arguments.Foo<int>` )
388
+
389
+ sel .IsAlive (`typeVar:command-line-arguments.Bar` )
390
+ sel .IsAlive (`type:command-line-arguments.Bar<string;>` )
391
+ sel .IsDead (`type:command-line-arguments.Bar<int;>` )
392
+
393
+ sel .IsDead (`funcVar:command-line-arguments.deadCode` )
394
+ sel .IsDead (`func:command-line-arguments.deadCode` )
395
+ }
396
+
397
+ func TestDeclSelection_RemoveUnusedNestedTypesInMethod (t * testing.T ) {
398
+ src := `
399
+ package main
400
+ type Baz[T any] struct{}
401
+ func (b *Baz[T]) Foo(u T) any {
402
+ type Bar struct { v T }
403
+ return Bar{v: u}
404
+ }
405
+ func deadCode() {
406
+ b := Baz[int]{}
407
+ println(b.Foo(42))
408
+ }
409
+ func main() {
410
+ b := Baz[string]{}
411
+ println(b.Foo("cat"))
412
+ }`
413
+
414
+ srcFiles := []srctesting.Source {{Name : `main.go` , Contents : []byte (src )}}
415
+ sel := declSelection (t , srcFiles , nil )
416
+ sel .IsAlive (`func:command-line-arguments.main` )
417
+
418
+ sel .IsAlive (`typeVar:command-line-arguments.Baz` )
419
+ sel .IsDead (`type:command-line-arguments.Baz<int>` )
420
+ sel .IsAlive (`type:command-line-arguments.Baz<string>` )
421
+
422
+ sel .IsDead (`func:command-line-arguments.(*Baz).Foo<int>` )
423
+ sel .IsAlive (`func:command-line-arguments.(*Baz).Foo<string>` )
424
+
425
+ sel .IsAlive (`typeVar:command-line-arguments.Bar` )
426
+ sel .IsDead (`type:command-line-arguments.Bar<int;>` )
427
+ sel .IsAlive (`type:command-line-arguments.Bar<string;>` )
428
+
429
+ sel .IsDead (`funcVar:command-line-arguments.deadCode` )
430
+ sel .IsDead (`func:command-line-arguments.deadCode` )
431
+ }
432
+
433
+ func TestDeclSelection_RemoveAllUnusedNestedTypes (t * testing.T ) {
434
+ src := `
435
+ package main
436
+ func Foo[T any](u T) any {
437
+ type Bar struct { v T }
438
+ return Bar{v: u}
439
+ }
440
+ func deadCode() {
441
+ println(Foo[int](42))
442
+ println(Foo[string]("cat"))
443
+ }
444
+ func main() {}`
445
+
446
+ srcFiles := []srctesting.Source {{Name : `main.go` , Contents : []byte (src )}}
447
+ sel := declSelection (t , srcFiles , nil )
448
+ sel .IsAlive (`func:command-line-arguments.main` )
449
+
450
+ sel .IsDead (`funcVar:command-line-arguments.Foo` )
451
+ sel .IsDead (`func:command-line-arguments.Foo<string>` )
452
+ sel .IsDead (`func:command-line-arguments.Foo<int>` )
453
+
454
+ sel .IsDead (`typeVar:command-line-arguments.Bar` )
455
+ sel .IsDead (`type:command-line-arguments.Bar<string;>` )
456
+ sel .IsDead (`type:command-line-arguments.Bar<int;>` )
457
+
458
+ sel .IsDead (`funcVar:command-line-arguments.deadCode` )
459
+ sel .IsDead (`func:command-line-arguments.deadCode` )
460
+ }
461
+
462
+ func TestDeclSelection_CompletelyRemoveNestedType (t * testing.T ) {
463
+ src := `
464
+ package main
465
+ func Foo[T any](u T) any {
466
+ type Bar struct { v T }
467
+ return Bar{v: u}
468
+ }
469
+ func deadCode() {
470
+ println(Foo[int](42))
471
+ }
472
+ func main() {}`
473
+
474
+ srcFiles := []srctesting.Source {{Name : `main.go` , Contents : []byte (src )}}
475
+ sel := declSelection (t , srcFiles , nil )
476
+
477
+ sel .IsAlive (`func:command-line-arguments.main` )
478
+
479
+ sel .IsDead (`funcVar:command-line-arguments.Foo` )
480
+ sel .IsDead (`func:command-line-arguments.Foo<int>` )
481
+
482
+ sel .IsDead (`typeVar:command-line-arguments.Bar` )
483
+ sel .IsDead (`type:command-line-arguments.Bar<int;>` )
484
+
485
+ sel .IsDead (`funcVar:command-line-arguments.deadCode` )
486
+ sel .IsDead (`func:command-line-arguments.deadCode` )
487
+ }
488
+
489
+ func TestDeclSelection_RemoveAnonNestedTypes (t * testing.T ) {
490
+ // Based on test/fixedbugs/issue53635.go
491
+ // This checks that if an anon type (e.g. []T) is used in a function
492
+ // that is not used, the type is removed, otherwise it is kept.
493
+
494
+ src := `
495
+ package main
496
+ func Foo[T any](u T) any {
497
+ return []T(nil)
498
+ }
499
+ func deadCode() {
500
+ println(Foo[string]("cat"))
501
+ }
502
+ func main() {
503
+ println(Foo[int](42))
504
+ }`
505
+
506
+ srcFiles := []srctesting.Source {{Name : `main.go` , Contents : []byte (src )}}
507
+ sel := declSelection (t , srcFiles , nil )
508
+ sel .IsDead (`anonType:command-line-arguments.sliceType` ) // []string
509
+ sel .IsAlive (`anonType:command-line-arguments.sliceType$1` ) // []int
510
+ }
511
+
512
+ func TestDeclSelection_NoNestAppliedToFuncCallInMethod (t * testing.T ) {
513
+ // Checks that a function call to a non-local function isn't
514
+ // being labeled as a nested function call.
515
+ src := `
516
+ package main
517
+ func foo(a any) {
518
+ println(a)
519
+ }
520
+ type Bar[T any] struct { u T }
521
+ func (b *Bar[T]) Baz() {
522
+ foo(b.u)
523
+ }
524
+ func main() {
525
+ b := &Bar[int]{u: 42}
526
+ b.Baz()
527
+ }`
528
+
529
+ srcFiles := []srctesting.Source {{Name : `main.go` , Contents : []byte (src )}}
530
+ sel := declSelection (t , srcFiles , nil )
531
+ sel .IsAlive (`init:main` )
532
+
533
+ sel .IsAlive (`typeVar:command-line-arguments.Bar` )
534
+ sel .IsAlive (`type:command-line-arguments.Bar<int>` )
535
+ sel .IsAlive (`func:command-line-arguments.(*Bar).Baz<int>` )
536
+
537
+ sel .IsAlive (`func:command-line-arguments.foo` )
538
+ }
539
+
367
540
func TestLengthParenthesizingIssue841 (t * testing.T ) {
368
541
// See issue https://github.com/gopherjs/gopherjs/issues/841
369
542
//
0 commit comments