From 9ff91b90988945c7bbe85fdef4a16d5f1af6b2c2 Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Thu, 21 Oct 2021 14:31:40 +0700 Subject: [PATCH] cmd/compile: only look for struct type when crawling inline body CL 356254 fixed crawling of embeddable types during inline. However, we are too agressive, since when we call markEmbed for every type seen during inlining function body. That leads to false positive that for a non-embedded type, its unexported methods are also marked inline. Instead, we should only look at struct type that we seen during inlining function body, and calling markEmbed for all of its embedded fields. Fixes #49094 Change-Id: I6ef9a8bf1fc649ec6bf75e4883f6031ec8560ba1 Reviewed-on: https://go-review.googlesource.com/c/go/+/357232 Trust: Cuong Manh Le Run-TryBot: Cuong Manh Le TryBot-Result: Go Bot Reviewed-by: Matthew Dempsky --- src/cmd/compile/internal/typecheck/crawler.go | 8 +++++++- test/fixedbugs/issue49094.dir/a.go | 11 +++++++++++ test/fixedbugs/issue49094.dir/b.go | 11 +++++++++++ test/fixedbugs/issue49094.dir/p.go | 15 +++++++++++++++ test/fixedbugs/issue49094.go | 7 +++++++ 5 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 test/fixedbugs/issue49094.dir/a.go create mode 100644 test/fixedbugs/issue49094.dir/b.go create mode 100644 test/fixedbugs/issue49094.dir/p.go create mode 100644 test/fixedbugs/issue49094.go diff --git a/src/cmd/compile/internal/typecheck/crawler.go b/src/cmd/compile/internal/typecheck/crawler.go index b214ef2279..e1489ceedd 100644 --- a/src/cmd/compile/internal/typecheck/crawler.go +++ b/src/cmd/compile/internal/typecheck/crawler.go @@ -217,7 +217,13 @@ func (p *crawler) markInlBody(n *ir.Name) { // // We generate the wrapper for "struct{ t }".M, and inline call // to "struct{ t }".M, which makes "t.M" reachable. - p.markEmbed(t) + if t.IsStruct() { + for _, f := range t.FieldSlice() { + if f.Embedded != 0 { + p.markEmbed(f.Type) + } + } + } } } diff --git a/test/fixedbugs/issue49094.dir/a.go b/test/fixedbugs/issue49094.dir/a.go new file mode 100644 index 0000000000..9ec0fd9f93 --- /dev/null +++ b/test/fixedbugs/issue49094.dir/a.go @@ -0,0 +1,11 @@ +// 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 a + +type A struct{} + +func (a *A) f() bool { + return true +} diff --git a/test/fixedbugs/issue49094.dir/b.go b/test/fixedbugs/issue49094.dir/b.go new file mode 100644 index 0000000000..f2361958ac --- /dev/null +++ b/test/fixedbugs/issue49094.dir/b.go @@ -0,0 +1,11 @@ +// 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 b + +import "./a" + +func M(r *a.A) string { + return "" +} diff --git a/test/fixedbugs/issue49094.dir/p.go b/test/fixedbugs/issue49094.dir/p.go new file mode 100644 index 0000000000..581faf19ac --- /dev/null +++ b/test/fixedbugs/issue49094.dir/p.go @@ -0,0 +1,15 @@ +// 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 p + +import ( + "./b" +) + +type S struct{} + +func (S) M() { + b.M(nil) +} diff --git a/test/fixedbugs/issue49094.go b/test/fixedbugs/issue49094.go new file mode 100644 index 0000000000..b83fbd7af1 --- /dev/null +++ b/test/fixedbugs/issue49094.go @@ -0,0 +1,7 @@ +// compiledir + +// 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 ignored -- 2.50.0