From: Cuong Manh Le Date: Sun, 19 Apr 2020 15:18:15 +0000 (+0700) Subject: cmd/compile: refactor detecting package reflect logic X-Git-Tag: go1.15beta1~502 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=62ccee49d6d9bdb63841a259d835703ff85ab0b7;p=gostls13.git cmd/compile: refactor detecting package reflect logic Passes toolstash-check. Change-Id: Ie4b1f61528bb183dc66bb6955851a47b2641549c Reviewed-on: https://go-review.googlesource.com/c/go/+/228859 Run-TryBot: Cuong Manh Le TryBot-Result: Gobot Gobot Reviewed-by: Matthew Dempsky --- diff --git a/src/cmd/compile/internal/gc/go.go b/src/cmd/compile/internal/gc/go.go index a7fdfd5b7a..758c90931f 100644 --- a/src/cmd/compile/internal/gc/go.go +++ b/src/cmd/compile/internal/gc/go.go @@ -45,6 +45,18 @@ func isRuntimePkg(p *types.Pkg) bool { return p.Path == "runtime" } +// isReflectPkg reports whether p is package reflect. +func isReflectPkg(p *types.Pkg) bool { + // TODO(cuonglm): how to get rid this check. + if p == nil { + return false + } + if p == localpkg { + return myimportpath == "reflect" + } + return p.Path == "reflect" +} + // The Class of a variable/function describes the "storage class" // of a variable or function. During parsing, storage classes are // called declaration contexts. diff --git a/src/cmd/compile/internal/gc/inl.go b/src/cmd/compile/internal/gc/inl.go index 272d0bdab7..fa5b3ec698 100644 --- a/src/cmd/compile/internal/gc/inl.go +++ b/src/cmd/compile/internal/gc/inl.go @@ -585,7 +585,7 @@ func inlnode(n *Node, maxCost int32, inlMap map[*Node]bool) *Node { case OCALLMETH: // Prevent inlining some reflect.Value methods when using checkptr, // even when package reflect was compiled without it (#35073). - if s := n.Left.Sym; Debug_checkptr != 0 && s.Pkg.Path == "reflect" && (s.Name == "Value.UnsafeAddr" || s.Name == "Value.Pointer") { + if s := n.Left.Sym; Debug_checkptr != 0 && isReflectPkg(s.Pkg) && (s.Name == "Value.UnsafeAddr" || s.Name == "Value.Pointer") { return n } } diff --git a/src/cmd/compile/internal/gc/walk.go b/src/cmd/compile/internal/gc/walk.go index 8ad7f6ace8..3a2a97373d 100644 --- a/src/cmd/compile/internal/gc/walk.go +++ b/src/cmd/compile/internal/gc/walk.go @@ -3659,7 +3659,7 @@ func usemethod(n *Node) { // Note: Don't rely on res0.Type.String() since its formatting depends on multiple factors // (including global variables such as numImports - was issue #19028). // Also need to check for reflect package itself (see Issue #38515). - if s := res0.Type.Sym; s != nil && s.Name == "Method" && s.Pkg != nil && (s.Pkg.Path == "reflect" || s.Pkg == localpkg && myimportpath == "reflect") { + if s := res0.Type.Sym; s != nil && s.Name == "Method" && isReflectPkg(s.Pkg) { Curfn.Func.SetReflectMethod(true) } }