mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
cmd/compile: track reflect.Type.Method in deadcode
In addition to reflect.Value.Call, exported methods can be invoked by the Func value in the reflect.Method struct. This CL has the compiler track what functions get access to a legitimate reflect.Method struct by looking for interface calls to either of: Method(int) reflect.Method MethodByName(string) (reflect.Method, bool) This is a little overly conservative. If a user implements a type with one of these methods without using the underlying calls on reflect.Type, the linker will assume the worst and include all exported methods. But it's cheap. No change to any of the binary sizes reported in cl/20483. For #14740 Change-Id: Ie17786395d0453ce0384d8b240ecb043b7726137 Reviewed-on: https://go-review.googlesource.com/20489 Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
parent
4112f0f7e6
commit
cc158403d6
13 changed files with 195 additions and 12 deletions
|
|
@ -632,6 +632,7 @@ opswitch:
|
|||
}
|
||||
|
||||
case OCALLINTER:
|
||||
usemethod(n)
|
||||
t := n.Left.Type
|
||||
if n.List.Len() != 0 && n.List.First().Op == OAS {
|
||||
break
|
||||
|
|
@ -3765,6 +3766,48 @@ func bounded(n *Node, max int64) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
// usemethod check interface method calls for uses of reflect.Type.Method.
|
||||
func usemethod(n *Node) {
|
||||
t := n.Left.Type
|
||||
|
||||
// Looking for either of:
|
||||
// Method(int) reflect.Method
|
||||
// MethodByName(string) (reflect.Method, bool)
|
||||
//
|
||||
// TODO(crawshaw): improve precision of match by working out
|
||||
// how to check the method name.
|
||||
if n := countfield(t.Params()); n != 1 {
|
||||
return
|
||||
}
|
||||
if n := countfield(t.Results()); n != 1 && n != 2 {
|
||||
return
|
||||
}
|
||||
p0 := t.Params().Field(0)
|
||||
res0 := t.Results().Field(0)
|
||||
var res1 *Type
|
||||
if countfield(t.Results()) == 2 {
|
||||
res1 = t.Results().Field(1)
|
||||
}
|
||||
|
||||
if res1 == nil {
|
||||
if p0.Type.Etype != TINT {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
if p0.Type.Etype != TSTRING {
|
||||
return
|
||||
}
|
||||
if res1.Type.Etype != TBOOL {
|
||||
return
|
||||
}
|
||||
}
|
||||
if Tconv(res0, 0) != "reflect.Method" {
|
||||
return
|
||||
}
|
||||
|
||||
Curfn.Func.ReflectMethod = true
|
||||
}
|
||||
|
||||
func usefield(n *Node) {
|
||||
if obj.Fieldtrack_enabled == 0 {
|
||||
return
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue