Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
/goPublic

Commita228e73

Browse files
jharmsenadg
authored andcommitted
go/doc/example: Fix bug causing false negatives for Example playability.
Allows Examples with KeyValue expressions to be playable in godoc.During the traversal of the abstract syntax tree any KeyValueExpr Key.Name was incorrectly being added as an unresolved identifier.Since this identifier could not be provided the Example was marked as unplayable.This updates the AST traversal to skip Keys (but continue traversing the Values).Example of problematic AST now fixed (see L99 where "UpperBound" was being added as a missing identifier): 81 . . . . . . . . . Values: []ast.Expr (len = 1) { 82 . . . . . . . . . . 0: *ast.UnaryExpr { 83 . . . . . . . . . . . OpPos: 12:19 84 . . . . . . . . . . . Op: & 85 . . . . . . . . . . . X: *ast.CompositeLit { 86 . . . . . . . . . . . . Type: *ast.SelectorExpr { 87 . . . . . . . . . . . . . X: *ast.Ident { 88 . . . . . . . . . . . . . . NamePos: 12:20 89 . . . . . . . . . . . . . . Name: "t_proto" 90 . . . . . . . . . . . . . } 91 . . . . . . . . . . . . . Sel: *ast.Ident { 92 . . . . . . . . . . . . . . NamePos: 12:41 93 . . . . . . . . . . . . . . Name: "BConfig" 94 . . . . . . . . . . . . . } 95 . . . . . . . . . . . . } 96 . . . . . . . . . . . . Lbrace: 12:79 97 . . . . . . . . . . . . Elts: []ast.Expr (len = 2) { 98 . . . . . . . . . . . . . 0: *ast.KeyValueExpr { 99 . . . . . . . . . . . . . . Key: *ast.Ident {100 . . . . . . . . . . . . . . . NamePos: 13:3101 . . . . . . . . . . . . . . . Name: "UpperBound"102 . . . . . . . . . . . . . . }103 . . . . . . . . . . . . . . Colon: 13:13104 . . . . . . . . . . . . . . Value: *ast.CallExpr {105 . . . . . . . . . . . . . . . Fun: *ast.SelectorExpr {106 . . . . . . . . . . . . . . . . X: *ast.Ident {107 . . . . . . . . . . . . . . . . . NamePos: 13:15108 . . . . . . . . . . . . . . . . . Name: "proto"109 . . . . . . . . . . . . . . . . }110 . . . . . . . . . . . . . . . . Sel: *ast.Ident {111 . . . . . . . . . . . . . . . . . NamePos: 13:21112 . . . . . . . . . . . . . . . . . Name: "Float32"113 . . . . . . . . . . . . . . . . }R=adgCC=gobot, golang-dev, grihttps://golang.org/cl/8569045
1 parentb13eac8 commita228e73

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

‎src/pkg/go/doc/example.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,13 @@ func playExample(file *ast.File, body *ast.BlockStmt) *ast.File {
166166
ast.Inspect(e.X,inspectFunc)
167167
returnfalse
168168
}
169+
// For key value expressions, only inspect the value
170+
// as the key should be resolved by the type of the
171+
// composite literal.
172+
ife,ok:=n.(*ast.KeyValueExpr);ok {
173+
ast.Inspect(e.Value,inspectFunc)
174+
returnfalse
175+
}
169176
ifid,ok:=n.(*ast.Ident);ok {
170177
ifid.Obj==nil {
171178
unresolved[id.Name]=true

‎src/pkg/go/doc/example_test.go‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const exampleTestFile = `
1818
package foo_test
1919
2020
import (
21+
"flag"
2122
"fmt"
2223
"log"
2324
"os/exec"
@@ -35,6 +36,38 @@ func ExampleImport() {
3536
}
3637
fmt.Printf("The date is %s\n", out)
3738
}
39+
40+
func ExampleKeyValue() {
41+
v := struct {
42+
a string
43+
b int
44+
}{
45+
a: "A",
46+
b: 1,
47+
}
48+
fmt.Print(v)
49+
// Output: a: "A", b: 1
50+
}
51+
52+
func ExampleKeyValueImport() {
53+
f := flag.Flag{
54+
Name: "play",
55+
}
56+
fmt.Print(f)
57+
// Output: Name: "play"
58+
}
59+
60+
var keyValueTopDecl = struct {
61+
a string
62+
b int
63+
}{
64+
a: "B",
65+
b: 2,
66+
}
67+
68+
func ExampleKeyValueTopDecl() {
69+
fmt.Print(keyValueTopDecl)
70+
}
3871
`
3972

4073
varexampleTestCases= []struct {
@@ -49,6 +82,20 @@ var exampleTestCases = []struct {
4982
Name:"Import",
5083
Play:exampleImportPlay,
5184
},
85+
{
86+
Name:"KeyValue",
87+
Play:exampleKeyValuePlay,
88+
Output:"a:\"A\", b: 1\n",
89+
},
90+
{
91+
Name:"KeyValueImport",
92+
Play:exampleKeyValueImportPlay,
93+
Output:"Name:\"play\"\n",
94+
},
95+
{
96+
Name:"KeyValueTopDecl",
97+
Play:"<nil>",
98+
},
5299
}
53100

54101
constexampleHelloPlay=`package main
@@ -78,6 +125,39 @@ func main() {
78125
}
79126
`
80127

128+
constexampleKeyValuePlay=`package main
129+
130+
import (
131+
"fmt"
132+
)
133+
134+
func main() {
135+
v := struct {
136+
a string
137+
b int
138+
}{
139+
a: "A",
140+
b: 1,
141+
}
142+
fmt.Print(v)
143+
}
144+
`
145+
146+
constexampleKeyValueImportPlay=`package main
147+
148+
import (
149+
"flag"
150+
"fmt"
151+
)
152+
153+
func main() {
154+
f := flag.Flag{
155+
Name: "play",
156+
}
157+
fmt.Print(f)
158+
}
159+
`
160+
81161
funcTestExamples(t*testing.T) {
82162
fs:=token.NewFileSet()
83163
file,err:=parser.ParseFile(fs,"test.go",strings.NewReader(exampleTestFile),parser.ParseComments)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp