Source file test/nilptr2.go

     1  // run
     2  
     3  // Copyright 2013 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  package main
     8  
     9  func main() {
    10  	ok := true
    11  	for _, tt := range tests {
    12  		func() {
    13  			defer func() {
    14  				if err := recover(); err == nil {
    15  					println(tt.name, "did not panic")
    16  					ok = false
    17  				}
    18  			}()
    19  			tt.fn()
    20  		}()
    21  	}
    22  	if !ok {
    23  		println("BUG")
    24  	}
    25  }
    26  
    27  var intp *int
    28  var slicep *[]byte
    29  var a10p *[10]int
    30  var a10Mp *[1<<20]int
    31  var structp *Struct
    32  var bigstructp *BigStruct
    33  var i int
    34  var m *M
    35  var m1 *M1
    36  var m2 *M2
    37  
    38  var V interface{}
    39  
    40  func use(x interface{}) {
    41  	V = x
    42  }
    43  
    44  var tests = []struct{
    45  	name string
    46  	fn func()
    47  }{
    48  	// Edit .+1,/^}/s/^[^	].+/	{"&", func() { println(&) }},\n	{"\&&", func() { println(\&&) }},/g
    49  	{"*intp", func() { println(*intp) }},
    50  	{"&*intp", func() { println(&*intp) }},
    51  	{"*slicep", func() { println(*slicep) }},
    52  	{"&*slicep", func() { println(&*slicep) }},
    53  	{"(*slicep)[0]", func() { println((*slicep)[0]) }},
    54  	{"&(*slicep)[0]", func() { println(&(*slicep)[0]) }},
    55  	{"(*slicep)[i]", func() { println((*slicep)[i]) }},
    56  	{"&(*slicep)[i]", func() { println(&(*slicep)[i]) }},
    57  	{"*a10p", func() { use(*a10p) }},
    58  	{"&*a10p", func() { println(&*a10p) }},
    59  	{"a10p[0]", func() { println(a10p[0]) }},
    60  	{"&a10p[0]", func() { println(&a10p[0]) }},
    61  	{"a10p[i]", func() { println(a10p[i]) }},
    62  	{"&a10p[i]", func() { println(&a10p[i]) }},
    63  	{"*structp", func() { use(*structp) }},
    64  	{"&*structp", func() { println(&*structp) }},
    65  	{"structp.i", func() { println(structp.i) }},
    66  	{"&structp.i", func() { println(&structp.i) }},
    67  	{"structp.j", func() { println(structp.j) }},
    68  	{"&structp.j", func() { println(&structp.j) }},
    69  	{"structp.k", func() { println(structp.k) }},
    70  	{"&structp.k", func() { println(&structp.k) }},
    71  	{"structp.x[0]", func() { println(structp.x[0]) }},
    72  	{"&structp.x[0]", func() { println(&structp.x[0]) }},
    73  	{"structp.x[i]", func() { println(structp.x[i]) }},
    74  	{"&structp.x[i]", func() { println(&structp.x[i]) }},
    75  	{"structp.x[9]", func() { println(structp.x[9]) }},
    76  	{"&structp.x[9]", func() { println(&structp.x[9]) }},
    77  	{"structp.l", func() { println(structp.l) }},
    78  	{"&structp.l", func() { println(&structp.l) }},
    79  	{"*bigstructp", func() { use(*bigstructp) }},
    80  	{"&*bigstructp", func() { println(&*bigstructp) }},
    81  	{"bigstructp.i", func() { println(bigstructp.i) }},
    82  	{"&bigstructp.i", func() { println(&bigstructp.i) }},
    83  	{"bigstructp.j", func() { println(bigstructp.j) }},
    84  	{"&bigstructp.j", func() { println(&bigstructp.j) }},
    85  	{"bigstructp.k", func() { println(bigstructp.k) }},
    86  	{"&bigstructp.k", func() { println(&bigstructp.k) }},
    87  	{"bigstructp.x[0]", func() { println(bigstructp.x[0]) }},
    88  	{"&bigstructp.x[0]", func() { println(&bigstructp.x[0]) }},
    89  	{"bigstructp.x[i]", func() { println(bigstructp.x[i]) }},
    90  	{"&bigstructp.x[i]", func() { println(&bigstructp.x[i]) }},
    91  	{"bigstructp.x[9]", func() { println(bigstructp.x[9]) }},
    92  	{"&bigstructp.x[9]", func() { println(&bigstructp.x[9]) }},
    93  	{"bigstructp.x[100<<20]", func() { println(bigstructp.x[100<<20]) }},
    94  	{"&bigstructp.x[100<<20]", func() { println(&bigstructp.x[100<<20]) }},
    95  	{"bigstructp.l", func() { println(bigstructp.l) }},
    96  	{"&bigstructp.l", func() { println(&bigstructp.l) }},
    97  	{"m1.F()", func() { println(m1.F()) }},
    98  	{"m1.M.F()", func() { println(m1.M.F()) }},
    99  	{"m2.F()", func() { println(m2.F()) }},
   100  	{"m2.M.F()", func() { println(m2.M.F()) }},
   101  }
   102  
   103  type Struct struct {
   104  	i int
   105  	j float64
   106  	k string
   107  	x [10]int
   108  	l []byte
   109  }
   110  
   111  type BigStruct struct {
   112  	i int
   113  	j float64
   114  	k string
   115  	x [128<<20]byte
   116  	l []byte
   117  }
   118  
   119  type M struct {
   120  }
   121  
   122  func (m *M) F() int {return 0}
   123  
   124  type M1 struct {
   125  	M
   126  }
   127  
   128  type M2 struct {
   129  	x int
   130  	M
   131  }
   132  

View as plain text