Skip to content

Commit fd5e0d2

Browse files
seankhliaogopherbot
authored andcommitted
go/doc: resolve imports before predeclared identifiers in examples
Fixes #70611 Fixes #70630 Change-Id: I868e68dbdf50ce34259eeef7b3d7985ede1f4c0b Reviewed-on: https://go-review.googlesource.com/c/go/+/639175 Reviewed-by: Ian Lance Taylor <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]>
1 parent a785d11 commit fd5e0d2

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

src/go/doc/example.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,6 @@ func playExample(file *ast.File, f *ast.FuncDecl) *ast.File {
192192
// Find unresolved identifiers and uses of top-level declarations.
193193
depDecls, unresolved := findDeclsAndUnresolved(body, topDecls, typMethods)
194194

195-
// Remove predeclared identifiers from unresolved list.
196-
for n := range unresolved {
197-
if predeclaredTypes[n] || predeclaredConstants[n] || predeclaredFuncs[n] {
198-
delete(unresolved, n)
199-
}
200-
}
201-
202195
// Use unresolved identifiers to determine the imports used by this
203196
// example. The heuristic assumes package names match base import
204197
// paths for imports w/o renames (should be good enough most of the time).
@@ -251,6 +244,13 @@ func playExample(file *ast.File, f *ast.FuncDecl) *ast.File {
251244
}
252245
}
253246

247+
// Remove predeclared identifiers from unresolved list.
248+
for n := range unresolved {
249+
if predeclaredTypes[n] || predeclaredConstants[n] || predeclaredFuncs[n] {
250+
delete(unresolved, n)
251+
}
252+
}
253+
254254
// If there are other unresolved identifiers, give up because this
255255
// synthesized file is not going to build.
256256
if len(unresolved) > 0 {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2021 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package foo_test
6+
7+
import (
8+
"fmt"
9+
10+
"example.com/error"
11+
)
12+
13+
func Print(s string) {
14+
fmt.Println(s)
15+
}
16+
17+
func Example() {
18+
Print(error.Hello)
19+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
-- .Play --
2+
package main
3+
4+
import (
5+
"fmt"
6+
7+
"example.com/error"
8+
)
9+
10+
func Print(s string) {
11+
fmt.Println(s)
12+
}
13+
14+
func main() {
15+
Print(error.Hello)
16+
}

0 commit comments

Comments
 (0)