Skip to content

Commit dd7d9b7

Browse files
committed
go/doc: remove brackets from receiver in example identifier
When constructing a string for a method that will match an example function's name, remove brackets from the receiver. This makes it possible to write an example associated with a method of a generic type. Also, modify the test for classifying examples to check that all the expected examples actually appear. Fixes #52496. Change-Id: Iebc5768f6cb91df9671dd701b97958fb8081f986 Reviewed-on: https://go-review.googlesource.com/c/go/+/401761 TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Jonathan Amsterdam <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 59ef3a9 commit dd7d9b7

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/go/doc/example.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,6 @@ func classifyExamples(p *Package, examples []*Example) {
512512
if len(examples) == 0 {
513513
return
514514
}
515-
516515
// Mapping of names for funcs, types, and methods to the example listing.
517516
ids := make(map[string]*[]*Example)
518517
ids[""] = &p.Examples // package-level examples have an empty name
@@ -537,7 +536,7 @@ func classifyExamples(p *Package, examples []*Example) {
537536
if !token.IsExported(m.Name) {
538537
continue
539538
}
540-
ids[strings.TrimPrefix(m.Recv, "*")+"_"+m.Name] = &m.Examples
539+
ids[strings.TrimPrefix(nameWithoutInst(m.Recv), "*")+"_"+m.Name] = &m.Examples
541540
}
542541
}
543542

@@ -572,6 +571,24 @@ func classifyExamples(p *Package, examples []*Example) {
572571
}
573572
}
574573

574+
// nameWithoutInst returns name if name has no brackets. If name contains
575+
// brackets, then it returns name with all the contents between (and including)
576+
// the outermost left and right bracket removed.
577+
//
578+
// Adapted from debug/gosym/symtab.go:Sym.nameWithoutInst.
579+
func nameWithoutInst(name string) string {
580+
start := strings.Index(name, "[")
581+
if start < 0 {
582+
return name
583+
}
584+
end := strings.LastIndex(name, "]")
585+
if end < 0 {
586+
// Malformed name, should contain closing bracket too.
587+
return name
588+
}
589+
return name[0:start] + name[end+1:]
590+
}
591+
575592
// splitExampleName attempts to split example name s at index i,
576593
// and reports if that produces a valid split. The suffix may be
577594
// absent. Otherwise, it must start with a lower-case letter and

src/go/doc/example_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,12 @@ type (
617617
)
618618
619619
func (Conflict) Conflict() {}
620+
621+
func GFunc[T any]() {}
622+
623+
type GType[T any] int
624+
625+
func (GType[T]) M() {}
620626
`
621627
const test = `
622628
package p_test
@@ -676,6 +682,12 @@ func ExampleConflict_Conflict() {} // ambiguous with either Conflict or C
676682
func ExampleConflict_conflict() {} // ambiguous with either Conflict or Conflict_conflict type
677683
func ExampleConflict_Conflict_suffix() {} // ambiguous with either Conflict or Conflict_Conflict type
678684
func ExampleConflict_conflict_suffix() {} // ambiguous with either Conflict or Conflict_conflict type
685+
686+
func ExampleGFunc() {}
687+
func ExampleGFunc_suffix() {}
688+
689+
func ExampleGType_M() {}
690+
func ExampleGType_M_suffix() {}
679691
`
680692

681693
// Parse literal source code as a *doc.Package.
@@ -725,12 +737,19 @@ func ExampleConflict_conflict_suffix() {} // ambiguous with either Conflict or C
725737
// These are implementation dependent due to the ambiguous parsing.
726738
"Conflict_Conflict": {"", "suffix"},
727739
"Conflict_conflict": {"", "suffix"},
740+
741+
"GFunc": {"", "suffix"},
742+
"GType.M": {"", "suffix"},
728743
}
729744

730745
for id := range got {
731746
if !reflect.DeepEqual(got[id], want[id]) {
732747
t.Errorf("classification mismatch for %q:\ngot %q\nwant %q", id, got[id], want[id])
733748
}
749+
delete(want, id)
750+
}
751+
if len(want) > 0 {
752+
t.Errorf("did not find:\n%q", want)
734753
}
735754
}
736755

0 commit comments

Comments
 (0)