Skip to content

Commit 5d72618

Browse files
committed
go/doc: support examples on methods from embedded unexported types
In type T1 struct { t2 } type t2 int func (t2) M() T1 has method M because it embeds t2, which has M. Classify the example func ExampleT1_M with T1 instead of ignoring it, as is done currently. There is no other way to provide an example for such a method, since its original type is unexported. Continue to ignore examples on methods from embedded types that are exported, unless in AllMethods mode. Examples for those methods could be written on the original type. The change involves removing a check in classifyExamples. The check isn't necessary to get the above behavior because reader.collectEmbeddedMethods and sortedFuncs already generate the appropriate list of methods. For #40172. Change-Id: Ibe7d965ecba6426466184e6e6655fc05989e9caf Reviewed-on: https://go-review.googlesource.com/c/go/+/249557 Reviewed-by: Dmitri Shuralyov <[email protected]>
1 parent 9679b30 commit 5d72618

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/go/doc/example.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ func classifyExamples(p *Package, examples []*Example) {
486486
ids[f.Name] = &f.Examples
487487
}
488488
for _, m := range t.Methods {
489-
if !token.IsExported(m.Name) || m.Level != 0 { // avoid forwarded methods from embedding
489+
if !token.IsExported(m.Name) {
490490
continue
491491
}
492492
ids[strings.TrimPrefix(m.Recv, "*")+"_"+m.Name] = &m.Examples

src/go/doc/example_test.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,7 @@ type (
563563
type2 int
564564
565565
Embed struct { Type1 }
566+
Uembed struct { type2 }
566567
)
567568
568569
func Func1() {}
@@ -575,6 +576,8 @@ func (Type1) Func1_Foo() {}
575576
func (Type1) Func1_foo() {}
576577
func (Type1) func2() {}
577578
579+
func (type2) Func1() {}
580+
578581
type (
579582
Conflict int
580583
Conflict_Conflict int
@@ -633,7 +636,9 @@ func ExampleType1_Func1_foo_suffix() {}
633636
func ExampleType1_Func1_foo_Suffix() {} // matches Type1.Func1, instead of Type1.Func1_foo
634637
func ExampleType1_func2() {} // matches Type1, instead of Type1.func2
635638
636-
func ExampleEmbed_Func1() {} // invalid - no support for forwarded methods from embedding
639+
func ExampleEmbed_Func1() {} // invalid - no support for forwarded methods from embedding exported type
640+
func ExampleUembed_Func1() {} // methods from embedding unexported types are OK
641+
func ExampleUembed_Func1_suffix() {}
637642
638643
func ExampleConflict_Conflict() {} // ambiguous with either Conflict or Conflict_Conflict type
639644
func ExampleConflict_conflict() {} // ambiguous with either Conflict or Conflict_conflict type
@@ -683,6 +688,8 @@ func ExampleConflict_conflict_suffix() {} // ambiguous with either Conflict or C
683688
"Type1.Func1_Foo": {"", "suffix"},
684689
"Type1.Func1_foo": {"", "suffix"},
685690

691+
"Uembed.Func1": {"", "suffix"},
692+
686693
// These are implementation dependent due to the ambiguous parsing.
687694
"Conflict_Conflict": {"", "suffix"},
688695
"Conflict_conflict": {"", "suffix"},

0 commit comments

Comments
 (0)