Skip to content

Commit

Permalink
cmd/compile: print struct tags in var decl in inlined func body
Browse files Browse the repository at this point in the history
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 <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
  • Loading branch information
rsc committed Feb 16, 2016
1 parent e136cac commit feb2a5d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/cmd/compile/internal/gc/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions test/fixedbugs/issue14331.dir/a.go
Original file line number Diff line number Diff line change
@@ -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
}
11 changes: 11 additions & 0 deletions test/fixedbugs/issue14331.dir/b.go
Original file line number Diff line number Diff line change
@@ -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()
}
9 changes: 9 additions & 0 deletions test/fixedbugs/issue14331.go
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit feb2a5d

Please sign in to comment.