mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
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 <iant@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com>
This commit is contained in:
parent
a785d11ac4
commit
fd5e0d26d9
3 changed files with 42 additions and 7 deletions
|
|
@ -192,13 +192,6 @@ func playExample(file *ast.File, f *ast.FuncDecl) *ast.File {
|
|||
// Find unresolved identifiers and uses of top-level declarations.
|
||||
depDecls, unresolved := findDeclsAndUnresolved(body, topDecls, typMethods)
|
||||
|
||||
// Remove predeclared identifiers from unresolved list.
|
||||
for n := range unresolved {
|
||||
if predeclaredTypes[n] || predeclaredConstants[n] || predeclaredFuncs[n] {
|
||||
delete(unresolved, n)
|
||||
}
|
||||
}
|
||||
|
||||
// Use unresolved identifiers to determine the imports used by this
|
||||
// example. The heuristic assumes package names match base import
|
||||
// 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 {
|
|||
}
|
||||
}
|
||||
|
||||
// Remove predeclared identifiers from unresolved list.
|
||||
for n := range unresolved {
|
||||
if predeclaredTypes[n] || predeclaredConstants[n] || predeclaredFuncs[n] {
|
||||
delete(unresolved, n)
|
||||
}
|
||||
}
|
||||
|
||||
// If there are other unresolved identifiers, give up because this
|
||||
// synthesized file is not going to build.
|
||||
if len(unresolved) > 0 {
|
||||
|
|
|
|||
19
src/go/doc/testdata/examples/shadow_predeclared.go
vendored
Normal file
19
src/go/doc/testdata/examples/shadow_predeclared.go
vendored
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
// Copyright 2021 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package foo_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"example.com/error"
|
||||
)
|
||||
|
||||
func Print(s string) {
|
||||
fmt.Println(s)
|
||||
}
|
||||
|
||||
func Example() {
|
||||
Print(error.Hello)
|
||||
}
|
||||
16
src/go/doc/testdata/examples/shadow_predeclared.golden
vendored
Normal file
16
src/go/doc/testdata/examples/shadow_predeclared.golden
vendored
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
-- .Play --
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"example.com/error"
|
||||
)
|
||||
|
||||
func Print(s string) {
|
||||
fmt.Println(s)
|
||||
}
|
||||
|
||||
func main() {
|
||||
Print(error.Hello)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue