1
0
mirror of https://github.com/golang/go synced 2024-11-15 03:00:36 -07:00
go/test/nilptr2.go
Cherry Zhang ce5ae2f4bc test: use a real use function in nilptr2.go
Adjust the dummy use function to a real use. As suggested by the
println calls in the test, nilptr2.go supposes to check that a
used nil pointer dereference panics. This use function is not
real enough so an optimized compiler such as gccgo could
eliminate the call.

The spec requires that even a dummy use would cause a panic.
Unfortunately, due to #31151 this is not true for gccgo at -O1 or
above.

Change-Id: Ie07c8a5969ab94dad82d4f7cfec30597c25b7c46
Reviewed-on: https://go-review.googlesource.com/c/go/+/176579
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2019-05-11 03:02:33 +00:00

132 lines
3.6 KiB
Go

// run
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
func main() {
ok := true
for _, tt := range tests {
func() {
defer func() {
if err := recover(); err == nil {
println(tt.name, "did not panic")
ok = false
}
}()
tt.fn()
}()
}
if !ok {
println("BUG")
}
}
var intp *int
var slicep *[]byte
var a10p *[10]int
var a10Mp *[1<<20]int
var structp *Struct
var bigstructp *BigStruct
var i int
var m *M
var m1 *M1
var m2 *M2
var V interface{}
func use(x interface{}) {
V = x
}
var tests = []struct{
name string
fn func()
}{
// Edit .+1,/^}/s/^[^ ].+/ {"&", func() { println(&) }},\n {"\&&", func() { println(\&&) }},/g
{"*intp", func() { println(*intp) }},
{"&*intp", func() { println(&*intp) }},
{"*slicep", func() { println(*slicep) }},
{"&*slicep", func() { println(&*slicep) }},
{"(*slicep)[0]", func() { println((*slicep)[0]) }},
{"&(*slicep)[0]", func() { println(&(*slicep)[0]) }},
{"(*slicep)[i]", func() { println((*slicep)[i]) }},
{"&(*slicep)[i]", func() { println(&(*slicep)[i]) }},
{"*a10p", func() { use(*a10p) }},
{"&*a10p", func() { println(&*a10p) }},
{"a10p[0]", func() { println(a10p[0]) }},
{"&a10p[0]", func() { println(&a10p[0]) }},
{"a10p[i]", func() { println(a10p[i]) }},
{"&a10p[i]", func() { println(&a10p[i]) }},
{"*structp", func() { use(*structp) }},
{"&*structp", func() { println(&*structp) }},
{"structp.i", func() { println(structp.i) }},
{"&structp.i", func() { println(&structp.i) }},
{"structp.j", func() { println(structp.j) }},
{"&structp.j", func() { println(&structp.j) }},
{"structp.k", func() { println(structp.k) }},
{"&structp.k", func() { println(&structp.k) }},
{"structp.x[0]", func() { println(structp.x[0]) }},
{"&structp.x[0]", func() { println(&structp.x[0]) }},
{"structp.x[i]", func() { println(structp.x[i]) }},
{"&structp.x[i]", func() { println(&structp.x[i]) }},
{"structp.x[9]", func() { println(structp.x[9]) }},
{"&structp.x[9]", func() { println(&structp.x[9]) }},
{"structp.l", func() { println(structp.l) }},
{"&structp.l", func() { println(&structp.l) }},
{"*bigstructp", func() { use(*bigstructp) }},
{"&*bigstructp", func() { println(&*bigstructp) }},
{"bigstructp.i", func() { println(bigstructp.i) }},
{"&bigstructp.i", func() { println(&bigstructp.i) }},
{"bigstructp.j", func() { println(bigstructp.j) }},
{"&bigstructp.j", func() { println(&bigstructp.j) }},
{"bigstructp.k", func() { println(bigstructp.k) }},
{"&bigstructp.k", func() { println(&bigstructp.k) }},
{"bigstructp.x[0]", func() { println(bigstructp.x[0]) }},
{"&bigstructp.x[0]", func() { println(&bigstructp.x[0]) }},
{"bigstructp.x[i]", func() { println(bigstructp.x[i]) }},
{"&bigstructp.x[i]", func() { println(&bigstructp.x[i]) }},
{"bigstructp.x[9]", func() { println(bigstructp.x[9]) }},
{"&bigstructp.x[9]", func() { println(&bigstructp.x[9]) }},
{"bigstructp.x[100<<20]", func() { println(bigstructp.x[100<<20]) }},
{"&bigstructp.x[100<<20]", func() { println(&bigstructp.x[100<<20]) }},
{"bigstructp.l", func() { println(bigstructp.l) }},
{"&bigstructp.l", func() { println(&bigstructp.l) }},
{"m1.F()", func() { println(m1.F()) }},
{"m1.M.F()", func() { println(m1.M.F()) }},
{"m2.F()", func() { println(m2.F()) }},
{"m2.M.F()", func() { println(m2.M.F()) }},
}
type Struct struct {
i int
j float64
k string
x [10]int
l []byte
}
type BigStruct struct {
i int
j float64
k string
x [128<<20]byte
l []byte
}
type M struct {
}
func (m *M) F() int {return 0}
type M1 struct {
M
}
type M2 struct {
x int
M
}