1
0
mirror of https://github.com/golang/go synced 2024-11-19 09:24:39 -07:00
go/pointer/testdata/structreflect.go
Alan Donovan 8bb20b8231 go.tools/pointer: more reflection.
Support for:
        (*reflect.rtype).Field
        (*reflect.rtype).FieldByName
        reflect.MakeSlice
        runtime.SetFinalizer

Details:
- analysis locates ssa.Functions for (reflect.Value).Call
  and runtime.SetFinalizer during startup to that it can
  special-case them during genCall.  ('Call' is forthcoming.)
- The callsite.targets mechanism is only used for dynamic
  calls now.  For static calls we call callEdge during constraint
  generation; this is a minor optimisation.
- Static calls to SetFinalizer are inlined so that the call
  appears to go direct to the finalizer.  (We'll use the same
  trick for (reflect.Value).Call.)
- runtime.FuncForPC: treat as a no-op.
- Fixed pointer_test to properly deal with expectations
  that are multi-sets.
- Inlined rtypeMethodByNameConstraint.addMethod.
- More tests.

R=crawshaw
CC=golang-dev
https://golang.org/cl/14682045
2013-10-17 09:26:44 -04:00

50 lines
1.2 KiB
Go

// +build ignore
package main
import "reflect"
type A struct {
f *int
g interface{}
h bool
}
var dyn string
func reflectTypeFieldByName() {
f, _ := reflect.TypeOf(A{}).FieldByName("f")
print(f.Type) // @pointsto *int
g, _ := reflect.TypeOf(A{}).FieldByName("g")
print(g.Type) // @pointsto interface{}
print(reflect.Zero(g.Type)) // @pointsto <alloc in reflect.Zero>
print(reflect.Zero(g.Type)) // @types interface{}
// TODO(adonovan): fix: the following should return a zero
// value of the empty interface (i.e. pts is empty), but that
// requires fixing the TODO comment in
// reflectZeroConstraint.solve, which in turn requires that we
// add a "settable" flag to tagged objects.
print(reflect.Zero(g.Type).Interface()) // @types interface{}
h, _ := reflect.TypeOf(A{}).FieldByName("h")
print(h.Type) // @pointsto bool
missing, _ := reflect.TypeOf(A{}).FieldByName("missing")
print(missing.Type) // @pointsto
dyn, _ := reflect.TypeOf(A{}).FieldByName(dyn)
print(dyn.Type) // @pointsto *int | bool | interface{}
}
func reflectTypeField() {
fld := reflect.TypeOf(A{}).Field(0)
print(fld.Type) // @pointsto *int | bool | interface{}
}
func main() {
reflectTypeFieldByName()
reflectTypeField()
}