From 31e2128c9be91445df05c3bbfb34e0dc3f1e4809 Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Mon, 24 Dec 2018 17:34:44 -0800 Subject: [PATCH] go-fuzz-build: use _go_fuzz_dep_.Bool instead of bool in generated code This avoids compilation errors when user code shadows the built-in bool, as happens (for example) in cmd/compile. --- go-fuzz-build/cover.go | 5 +++-- go-fuzz-dep/main.go | 5 +++++ test/test.go | 9 +++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/go-fuzz-build/cover.go b/go-fuzz-build/cover.go index b3e7d4c12..5537d8732 100644 --- a/go-fuzz-build/cover.go +++ b/go-fuzz-build/cover.go @@ -224,7 +224,8 @@ func (s *Sonar) Visit(n ast.Node) ast.Visitor { // Replace: // x != y // with: - // func() bool { v1 := x; v2 := y; go-fuzz-dep.Sonar(v1, v2, flags); return v1 != v2 }() == true + // func() _go_fuzz_dep_.Bool { v1 := x; v2 := y; go-fuzz-dep.Sonar(v1, v2, flags); return v1 != v2 }() == true + // Using "== true" lets us modify the AST Node in-place. v1 := nn.X v2 := nn.Y ast.Walk(s, v1) @@ -339,7 +340,7 @@ func (s *Sonar) Visit(n ast.Node) ast.Visitor { ) nn.X = &ast.CallExpr{ Fun: &ast.FuncLit{ - Type: &ast.FuncType{Results: &ast.FieldList{List: []*ast.Field{{Type: ast.NewIdent("bool")}}}}, + Type: &ast.FuncType{Results: &ast.FieldList{List: []*ast.Field{{Type: ast.NewIdent("_go_fuzz_dep_.Bool")}}}}, Body: block, }, } diff --git a/go-fuzz-dep/main.go b/go-fuzz-dep/main.go index 011e3f98e..0691a2dcd 100644 --- a/go-fuzz-dep/main.go +++ b/go-fuzz-dep/main.go @@ -15,6 +15,11 @@ import ( . "go-fuzz-defs" ) +// Bool is just a bool. +// It is used by code autogenerated by go-fuzz-build +// to avoid compilation errors when a user's code shadows the built-in bool. +type Bool bool + var ( inFD FD outFD FD diff --git a/test/test.go b/test/test.go index 937b6b6d3..a711e4e29 100644 --- a/test/test.go +++ b/test/test.go @@ -185,3 +185,12 @@ func test6() { func issue194(v testdep.I) testdep.I { return testdep.B(v == testdep.V1{} || v == testdep.V2{}) } + +func shadowedBool() { + var bool int + var x int + if x == 0 { + return + } + _ = bool +}