From feb2a5d6103dad76b6374c5f346e33d55612cb2a Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Tue, 16 Feb 2016 12:09:05 -0500 Subject: [PATCH] cmd/compile: print struct tags in var decl in inlined func body This bug was introduced in golang.org/cl/18217, while trying to fix #13777. Originally I wanted to just disable inlining for the case being handled incorrectly, but it's fairly difficult to detect and much easier just to fix. Since the case being handled incorrectly was inlined correctly in Go 1.5, not inlining it would also be somewhat of a regression. So just fix it. Test case copied from Ian's CL 19520. The mistake to worry about in this CL would be relaxing the condition too much (we now print the note more often than we did yesterday). To confirm that we'd catch this mistake, I checked that changing (!fmtbody || !t.Funarg) to (true) does cause fixedbugs/issue13777.go to fail. And putting it back to what is written in this CL makes that test pass again as well as the new fixedbugs/issue14331.go. So I believe that the new condition is correct for both constraints. Fixes #14331. Change-Id: I91f75a4d5d07c53af5caea1855c780d9874b8df6 Reviewed-on: https://go-review.googlesource.com/19514 Run-TryBot: Russ Cox TryBot-Result: Gobot Gobot Reviewed-by: Ian Lance Taylor --- src/cmd/compile/internal/gc/fmt.go | 8 +++++++- test/fixedbugs/issue14331.dir/a.go | 14 ++++++++++++++ test/fixedbugs/issue14331.dir/b.go | 11 +++++++++++ test/fixedbugs/issue14331.go | 9 +++++++++ 4 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 test/fixedbugs/issue14331.dir/a.go create mode 100644 test/fixedbugs/issue14331.dir/b.go create mode 100644 test/fixedbugs/issue14331.go diff --git a/src/cmd/compile/internal/gc/fmt.go b/src/cmd/compile/internal/gc/fmt.go index 64b6e36758d..d00e5a6c462 100644 --- a/src/cmd/compile/internal/gc/fmt.go +++ b/src/cmd/compile/internal/gc/fmt.go @@ -748,7 +748,13 @@ func typefmt(t *Type, flag int) string { if name != "" { str = name + " " + typ } - if flag&obj.FmtShort == 0 && !fmtbody && t.Note != nil { + + // The fmtbody flag is intended to suppress escape analysis annotations + // when printing a function type used in a function body. + // (The escape analysis tags do not apply to func vars.) + // But it must not suppress struct field tags. + // See golang.org/issue/13777 and golang.org/issue/14331. + if flag&obj.FmtShort == 0 && (!fmtbody || !t.Funarg) && t.Note != nil { str += " " + strconv.Quote(*t.Note) } return str diff --git a/test/fixedbugs/issue14331.dir/a.go b/test/fixedbugs/issue14331.dir/a.go new file mode 100644 index 00000000000..1b7f853bc94 --- /dev/null +++ b/test/fixedbugs/issue14331.dir/a.go @@ -0,0 +1,14 @@ +// Copyright 2016 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 a + +var S struct { + Str string `tag` +} + +func F() string { + v := S + return v.Str +} diff --git a/test/fixedbugs/issue14331.dir/b.go b/test/fixedbugs/issue14331.dir/b.go new file mode 100644 index 00000000000..7a0abb25067 --- /dev/null +++ b/test/fixedbugs/issue14331.dir/b.go @@ -0,0 +1,11 @@ +// Copyright 2016 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 b + +import "./a" + +func G() string { + return a.F() +} diff --git a/test/fixedbugs/issue14331.go b/test/fixedbugs/issue14331.go new file mode 100644 index 00000000000..32f3e5156c2 --- /dev/null +++ b/test/fixedbugs/issue14331.go @@ -0,0 +1,9 @@ +// compiledir + +// Copyright 2016 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. + +// Inline function misses struct tags. + +package ignored