1
0
mirror of https://github.com/golang/go synced 2024-11-19 05:04:43 -07:00
go/pointer/testdata/arrayreflect.go
Alan Donovan 3b5de067a1 go.tools/pointer: reflection, part 1: maps, and some core features.
Core:
        reflect.TypeOf
        reflect.ValueOf
        reflect.Zero
        reflect.Value.Interface
Maps:
        (reflect.Value).MapIndex
        (reflect.Value).MapKeys
        (reflect.Value).SetMapIndex
        (*reflect.rtype).Elem
        (*reflect.rtype).Key

+ tests:
  pointer/testdata/mapreflect.go.
  oracle/testdata/src/main/reflection.go.

Interface objects (T, V...) have been renamed "tagged objects".

Abstraction: we model reflect.Value similar to
interface{}---as a pointer that points only to tagged
objects---but a reflect.Value may also point to an "indirect
tagged object", one in which the payload V is of type *T not T.
These are required because reflect.Values can hold lvalues,
e.g. when derived via Field() or Elem(), though we won't use
them till we get to structs and pointers.

Solving: each reflection intrinsic defines a new constraint
and resolution rule.  Because of the nature of reflection,
generalizing across types, the resolution rules dynamically
create additional complex constraints during solving, where
previously only simple (copy) constraints were created.
This requires some solver changes:

  The work done before the main solver loop (to attach new
  constraints to the graph) is now done before each iteration,
  in processNewConstraints.

  Its loop over constraints is broken into two passes:
  the first handles base (addr-of) constraints,
  the second handles simple and complex constraints.

  constraint.init() has been inlined.  The only behaviour that
  varies across constraints is ptr()

Sadly this will pessimize presolver optimisations, when we get
there; such is the price of reflection.

Objects: reflection intrinsics create objects (i.e. cause
memory allocations) with no SSA operation.  We will represent
them as the cgnode of the instrinsic (e.g. reflect.New), so we
extend Labels and node.data to represent objects as a product
(not sum) of ssa.Value and cgnode and pull this out into its
own type, struct object.  This simplifies a number of
invariants and saves space.  The ntObject flag is now
represented by obj!=nil; the other flags are moved into
object.

cgnodes are now always recorded in objects/Labels for which it
is appropriate (all but those for globals, constants and the
shared contours for functions).

Also:
- Prepopulate the flattenMemo cache to consider reflect.Value
  a fake pointer, not a struct.
- Improve accessors and documentation on type Label.
- @conctypes assertions renamed @types (since dyn. types needn't be concrete).
- add oracle 'describe' test on an interface (missing, an oversight).

R=crawshaw
CC=golang-dev
https://golang.org/cl/13418048
2013-09-16 09:49:10 -04:00

109 lines
3.0 KiB
Go

// +build ignore
package main
import "reflect"
// Test of arrays & slices with reflection.
var a int
func arrayreflect1() {
sl := make([]*int, 10) // @line ar1make
sl[0] = &a
srv := reflect.ValueOf(sl).Slice(0, 0)
print(srv.Interface()) // @types []*int
print(srv.Interface().([]*int)) // @pointsto makeslice@ar1make:12
print(srv.Interface().([]*int)[42]) // @pointsto main.a
}
func arrayreflect2() {
var arr [10]*int
sl := arr[:]
sl[0] = &a
srv := reflect.ValueOf(sl).Slice(0, 0)
print(srv.Interface()) // @types []*int
print(srv.Interface().([]*int)) // pointsto TODO
print(srv.Interface().([]*int)[42]) // @pointsto main.a
}
func arrayreflect3() {
srv := reflect.ValueOf("hi").Slice(0, 0)
print(srv.Interface()) // @types string
type S string
srv2 := reflect.ValueOf(S("hi")).Slice(0, 0)
print(srv2.Interface()) // @types main.S
}
func arrayreflect4() {
rv1 := reflect.ValueOf("hi")
rv2 := rv1 // backflow!
if unknown {
rv2 = reflect.ValueOf(123)
}
// We see backflow through the assignment above causing an
// imprecise result for rv1. This is because the SSA builder
// doesn't yet lift structs (like reflect.Value) into
// registers so these are all loads/stores to the stack.
// Under Das's algorithm, the extra indirection results in
// (undirected) unification not (directed) flow edges.
// TODO(adonovan): precision: lift aggregates.
print(rv1.Interface()) // @types string | int
print(rv2.Interface()) // @types string | int
}
func arrayreflect5() {
sl1 := make([]byte, 0)
sl2 := make([]byte, 0)
srv := reflect.ValueOf(sl1)
print(srv.Interface()) // @types []byte
print(srv.Interface().([]byte)) // @pointsto makeslice@testdata/arrayreflect.go:62:13
print(srv.Bytes()) // @pointsto makeslice@testdata/arrayreflect.go:62:13
srv2 := reflect.ValueOf(123)
srv2.SetBytes(sl2)
print(srv2.Interface()) // @types []byte | int
print(srv2.Interface().([]byte)) // @pointsto makeslice@testdata/arrayreflect.go:63:13
print(srv2.Bytes()) // @pointsto makeslice@testdata/arrayreflect.go:63:13
}
func arrayreflect6() {
sl1 := []*bool{new(bool)}
sl2 := []*int{&a}
srv1 := reflect.ValueOf(sl1)
print(srv1.Index(42).Interface()) // @types *bool
print(srv1.Index(42).Interface().(*bool)) // @pointsto alloc@testdata/arrayreflect.go:79:20
srv2 := reflect.ValueOf(sl2)
print(srv2.Index(42).Interface()) // @types *int
print(srv2.Index(42).Interface().(*int)) // @pointsto main.a
p1 := &sl1[0]
p2 := &sl2[0]
prv1 := reflect.ValueOf(p1)
print(prv1.Elem().Interface()) // @types *bool
print(prv1.Elem().Interface().(*bool)) // @pointsto alloc@testdata/arrayreflect.go:79:20
prv2 := reflect.ValueOf(p2)
print(prv2.Elem().Interface()) // @types *int
print(prv2.Elem().Interface().(*int)) // @pointsto main.a
}
func main() {
arrayreflect1()
arrayreflect2()
arrayreflect3()
arrayreflect4()
arrayreflect5()
arrayreflect6()
}
var unknown bool