@@ -497,14 +497,14 @@ func TestIssue43088(t *testing.T) {
497
497
// _ T2
498
498
// }
499
499
// }
500
- n1 := NewTypeName (syntax. Pos {} , nil , "T1" , nil )
500
+ n1 := NewTypeName (nopos , nil , "T1" , nil )
501
501
T1 := NewNamed (n1 , nil , nil )
502
- n2 := NewTypeName (syntax. Pos {} , nil , "T2" , nil )
502
+ n2 := NewTypeName (nopos , nil , "T2" , nil )
503
503
T2 := NewNamed (n2 , nil , nil )
504
- s1 := NewStruct ([]* Var {NewField (syntax. Pos {} , nil , "_" , T2 , false )}, nil )
504
+ s1 := NewStruct ([]* Var {NewField (nopos , nil , "_" , T2 , false )}, nil )
505
505
T1 .SetUnderlying (s1 )
506
- s2 := NewStruct ([]* Var {NewField (syntax. Pos {} , nil , "_" , T2 , false )}, nil )
507
- s3 := NewStruct ([]* Var {NewField (syntax. Pos {} , nil , "_" , s2 , false )}, nil )
506
+ s2 := NewStruct ([]* Var {NewField (nopos , nil , "_" , T2 , false )}, nil )
507
+ s3 := NewStruct ([]* Var {NewField (nopos , nil , "_" , s2 , false )}, nil )
508
508
T2 .SetUnderlying (s3 )
509
509
510
510
// These calls must terminate (no endless recursion).
@@ -535,38 +535,69 @@ func TestIssue44515(t *testing.T) {
535
535
}
536
536
537
537
func TestIssue43124 (t * testing.T ) {
538
- testenv .MustHaveGoBuild (t )
538
+ // TODO(rFindley) move this to testdata by enhancing support for importing.
539
+
540
+ testenv .MustHaveGoBuild (t ) // The go command is needed for the importer to determine the locations of stdlib .a files.
539
541
540
542
// All involved packages have the same name (template). Error messages should
541
543
// disambiguate between text/template and html/template by printing the full
542
544
// path.
543
545
const (
544
546
asrc = `package a; import "text/template"; func F(template.Template) {}; func G(int) {}`
545
- bsrc = `package b; import ("a"; "html/template"); func _() { a.F(template.Template{}) }`
546
- csrc = `package c; import ("a"; "html/template"); func _() { a.G(template.Template{}) }`
547
- )
547
+ bsrc = `
548
+ package b
548
549
549
- a := mustTypecheck (asrc , nil , nil )
550
- conf := Config {Importer : importHelper {pkg : a , fallback : defaultImporter ()}}
550
+ import (
551
+ "a"
552
+ "html/template"
553
+ )
551
554
555
+ func _() {
552
556
// Packages should be fully qualified when there is ambiguity within the
553
557
// error string itself.
554
- _ , err := typecheck (bsrc , & conf , nil )
555
- if err == nil {
556
- t .Fatal ("package b had no errors" )
557
- }
558
- if ! strings .Contains (err .Error (), "text/template" ) || ! strings .Contains (err .Error (), "html/template" ) {
559
- t .Errorf ("type checking error for b does not disambiguate package template: %q" , err )
560
- }
558
+ a.F(template /* ERRORx "cannot use.*html/template.* as .*text/template" */ .Template{})
559
+ }
560
+ `
561
+ csrc = `
562
+ package c
561
563
562
- // ...and also when there is any ambiguity in reachable packages.
563
- _ , err = typecheck (csrc , & conf , nil )
564
- if err == nil {
565
- t .Fatal ("package c had no errors" )
566
- }
567
- if ! strings .Contains (err .Error (), "html/template" ) {
568
- t .Errorf ("type checking error for c does not disambiguate package template: %q" , err )
564
+ import (
565
+ "a"
566
+ "fmt"
567
+ "html/template"
568
+ )
569
+
570
+ // go.dev/issue/46905: make sure template is not the first package qualified.
571
+ var _ fmt.Stringer = 1 // ERRORx "cannot use 1.*as fmt\\.Stringer"
572
+
573
+ // Packages should be fully qualified when there is ambiguity in reachable
574
+ // packages. In this case both a (and for that matter html/template) import
575
+ // text/template.
576
+ func _() { a.G(template /* ERRORx "cannot use .*html/template.*Template" */ .Template{}) }
577
+ `
578
+
579
+ tsrc = `
580
+ package template
581
+
582
+ import "text/template"
583
+
584
+ type T int
585
+
586
+ // Verify that the current package name also causes disambiguation.
587
+ var _ T = template /* ERRORx "cannot use.*text/template.* as T value" */.Template{}
588
+ `
589
+ )
590
+
591
+ a := mustTypecheck (asrc , nil , nil )
592
+ imp := importHelper {pkg : a , fallback : defaultImporter ()}
593
+
594
+ withImporter := func (cfg * Config ) {
595
+ cfg .Importer = imp
569
596
}
597
+
598
+ testFiles (t , []string {"b.go" }, [][]byte {[]byte (bsrc )}, 0 , false , withImporter )
599
+ testFiles (t , []string {"c.go" }, [][]byte {[]byte (csrc )}, 0 , false , withImporter )
600
+ testFiles (t , []string {"t.go" }, [][]byte {[]byte (tsrc )}, 0 , false , withImporter )
570
601
}
571
602
572
603
func TestIssue50646 (t * testing.T ) {
0 commit comments