1
0
mirror of https://github.com/golang/go synced 2024-11-19 06:34:42 -07:00
go/pointer/testdata/finalizer.go
Alan Donovan 6643abb26c go.tools/pointer: inclusion-based pointer analysis for Go.
Suggested reading order:
- doc.go
- api.go, analysis.go, callgraph.go, labels.go
- print.go, util.go
- gen.go
- solve.go
- pointer_test.go, testdata/*
- intrinsics.go (none are implemented yet)

R=dannyb, gri, crawshaw, 0xjnml
CC=golang-dev
https://golang.org/cl/10618043
2013-08-22 12:27:55 -04:00

71 lines
1.5 KiB
Go

package main
import "runtime"
func final1a(x *int) int {
print(x) // @pointsto alloc@newint:10
return *x
}
func final1b(x *bool) {
print(x) // @pointsto
}
func setfinalizer1() {
x := new(int) // @line newint
runtime.SetFinalizer(x, final1a) // ok: final1a's result is ignored
runtime.SetFinalizer(x, final1b) // param type mismatch: no effect
}
// @calls runtime.SetFinalizer -> main.final1a
// @calls main.setfinalizer1 -> runtime.SetFinalizer
func final2a(x *bool) {
print(x) // @pointsto alloc@newbool1:10 | alloc@newbool2:10
}
func final2b(x *bool) {
print(x) // @pointsto alloc@newbool1:10 | alloc@newbool2:10
}
func setfinalizer2() {
x := new(bool) // @line newbool1
f := final2a
if unknown {
x = new(bool) // @line newbool2
f = final2b
}
runtime.SetFinalizer(x, f)
}
// @calls runtime.SetFinalizer -> main.final2a
// @calls runtime.SetFinalizer -> main.final2b
// @calls main.setfinalizer2 -> runtime.SetFinalizer
// type T int
// func (t *T) finalize() {
// print(t) // #@pointsto x
// }
// func setfinalizer3() {
// x := new(T)
// runtime.SetFinalizer(x, (*T).finalize) // go/types gives wrong type to f.
// }
// #@calls runtime.SetFinalizer -> (*T) finalize
func funcForPC() {
f := runtime.FuncForPC(0) // @line funcforpc
print(f) // @pointsto reflectAlloc@funcforpc:25
}
func main() {
setfinalizer1()
setfinalizer2()
// setfinalizer3()
funcForPC()
}
var unknown bool // defeat dead-code elimination