mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
go/doc: avoid panic on references to functions with no body
This change guards a call to ast.Inspect with a nil check on the first
argument. This avoids a panic when inspecting a reference to a function
with a nil body. This can only happen when a function body is defined outside Go.
Fixes #42706
Change-Id: I91bc607b24b6224920c24cfd07e76ce7737a98d4
GitHub-Last-Rev: 08072b9ce5
GitHub-Pull-Request: golang/go#43011
Reviewed-on: https://go-review.googlesource.com/c/go/+/275516
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Trust: Daniel Martí <mvdan@mvdan.cc>
Trust: Emmanuel Odeke <emmanuel@orijtech.com>
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Go Bot <gobot@golang.org>
This commit is contained in:
parent
6cadfe2fee
commit
c40dc677be
2 changed files with 29 additions and 1 deletions
|
|
@ -237,7 +237,10 @@ func playExample(file *ast.File, f *ast.FuncDecl) *ast.File {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ast.Inspect(d.Body, inspectFunc)
|
// Functions might not have a body. See #42706.
|
||||||
|
if d.Body != nil {
|
||||||
|
ast.Inspect(d.Body, inspectFunc)
|
||||||
|
}
|
||||||
case *ast.GenDecl:
|
case *ast.GenDecl:
|
||||||
for _, spec := range d.Specs {
|
for _, spec := range d.Specs {
|
||||||
switch s := spec.(type) {
|
switch s := spec.(type) {
|
||||||
|
|
|
||||||
|
|
@ -352,6 +352,25 @@ func main() {
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
|
const exampleWholeFileExternalFunction = `package foo_test
|
||||||
|
|
||||||
|
func foo(int)
|
||||||
|
|
||||||
|
func Example() {
|
||||||
|
foo(42)
|
||||||
|
// Output:
|
||||||
|
}
|
||||||
|
`
|
||||||
|
|
||||||
|
const exampleWholeFileExternalFunctionOutput = `package main
|
||||||
|
|
||||||
|
func foo(int)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
foo(42)
|
||||||
|
}
|
||||||
|
`
|
||||||
|
|
||||||
var exampleWholeFileTestCases = []struct {
|
var exampleWholeFileTestCases = []struct {
|
||||||
Title, Source, Play, Output string
|
Title, Source, Play, Output string
|
||||||
}{
|
}{
|
||||||
|
|
@ -367,6 +386,12 @@ var exampleWholeFileTestCases = []struct {
|
||||||
exampleWholeFileFunctionOutput,
|
exampleWholeFileFunctionOutput,
|
||||||
"Hello, world!\n",
|
"Hello, world!\n",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"ExternalFunction",
|
||||||
|
exampleWholeFileExternalFunction,
|
||||||
|
exampleWholeFileExternalFunctionOutput,
|
||||||
|
"",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestExamplesWholeFile(t *testing.T) {
|
func TestExamplesWholeFile(t *testing.T) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue