modernc.org/gc@v1.0.1-0.20240304020402-f0dba7c97c2b/testdata/errchk/test/nilptr3.go (about)

     1  // errorcheck -0 -d=nil
     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  // Test that nil checks are removed.
     8  // Optimization is enabled.
     9  
    10  package p
    11  
    12  type Struct struct {
    13  	X int
    14  	Y float64
    15  }
    16  
    17  type BigStruct struct {
    18  	X int
    19  	Y float64
    20  	A [1 << 20]int
    21  	Z string
    22  }
    23  
    24  type Empty struct {
    25  }
    26  
    27  type Empty1 struct {
    28  	Empty
    29  }
    30  
    31  var (
    32  	intp       *int
    33  	arrayp     *[10]int
    34  	array0p    *[0]int
    35  	bigarrayp  *[1 << 26]int
    36  	structp    *Struct
    37  	bigstructp *BigStruct
    38  	emptyp     *Empty
    39  	empty1p    *Empty1
    40  )
    41  
    42  func f1() {
    43  	_ = *intp // ERROR "generated nil check"
    44  
    45  	// This one should be removed but the block copy needs
    46  	// to be turned into its own pseudo-op in order to see
    47  	// the indirect.
    48  	_ = *arrayp // ERROR "generated nil check"
    49  
    50  	// 0-byte indirect doesn't suffice.
    51  	// we don't registerize globals, so there are no removed.* nil checks.
    52  	_ = *array0p // ERROR "generated nil check"
    53  	_ = *array0p // ERROR "removed nil check"
    54  
    55  	_ = *intp    // ERROR "removed nil check"
    56  	_ = *arrayp  // ERROR "removed nil check"
    57  	_ = *structp // ERROR "generated nil check"
    58  	_ = *emptyp  // ERROR "generated nil check"
    59  	_ = *arrayp  // ERROR "removed nil check"
    60  }
    61  
    62  func f2() {
    63  	var (
    64  		intp       *int
    65  		arrayp     *[10]int
    66  		array0p    *[0]int
    67  		bigarrayp  *[1 << 20]int
    68  		structp    *Struct
    69  		bigstructp *BigStruct
    70  		emptyp     *Empty
    71  		empty1p    *Empty1
    72  	)
    73  
    74  	_ = *intp       // ERROR "generated nil check"
    75  	_ = *arrayp     // ERROR "generated nil check"
    76  	_ = *array0p    // ERROR "generated nil check"
    77  	_ = *array0p    // ERROR "removed.* nil check"
    78  	_ = *intp       // ERROR "removed.* nil check"
    79  	_ = *arrayp     // ERROR "removed.* nil check"
    80  	_ = *structp    // ERROR "generated nil check"
    81  	_ = *emptyp     // ERROR "generated nil check"
    82  	_ = *arrayp     // ERROR "removed.* nil check"
    83  	_ = *bigarrayp  // ERROR "generated nil check" ARM removed nil check before indirect!!
    84  	_ = *bigstructp // ERROR "generated nil check"
    85  	_ = *empty1p    // ERROR "generated nil check"
    86  }
    87  
    88  func fx10k() *[10000]int
    89  
    90  var b bool
    91  
    92  func f3(x *[10000]int) {
    93  	// Using a huge type and huge offsets so the compiler
    94  	// does not expect the memory hardware to fault.
    95  	_ = x[9999] // ERROR "generated nil check"
    96  
    97  	for {
    98  		if x[9999] != 0 { // ERROR "removed nil check"
    99  			break
   100  		}
   101  	}
   102  
   103  	x = fx10k()
   104  	_ = x[9999] // ERROR "generated nil check"
   105  	if b {
   106  		_ = x[9999] // ERROR "removed.* nil check"
   107  	} else {
   108  		_ = x[9999] // ERROR "removed.* nil check"
   109  	}
   110  	_ = x[9999] // ERROR "removed nil check"
   111  
   112  	x = fx10k()
   113  	if b {
   114  		_ = x[9999] // ERROR "generated nil check"
   115  	} else {
   116  		_ = x[9999] // ERROR "generated nil check"
   117  	}
   118  	_ = x[9999] // ERROR "generated nil check"
   119  
   120  	fx10k()
   121  	// This one is a bit redundant, if we figured out that
   122  	// x wasn't going to change across the function call.
   123  	// But it's a little complex to do and in practice doesn't
   124  	// matter enough.
   125  	_ = x[9999] // ERROR "removed nil check"
   126  }
   127  
   128  func f3a() {
   129  	x := fx10k()
   130  	y := fx10k()
   131  	z := fx10k()
   132  	_ = &x[9] // ERROR "generated nil check"
   133  	y = z
   134  	_ = &x[9] // ERROR "removed.* nil check"
   135  	x = y
   136  	_ = &x[9] // ERROR "generated nil check"
   137  }
   138  
   139  func f3b() {
   140  	x := fx10k()
   141  	y := fx10k()
   142  	_ = &x[9] // ERROR "generated nil check"
   143  	y = x
   144  	_ = &x[9] // ERROR "removed.* nil check"
   145  	x = y
   146  	_ = &x[9] // ERROR "removed.* nil check"
   147  }
   148  
   149  func fx10() *[10]int
   150  
   151  func f4(x *[10]int) {
   152  	// Most of these have no checks because a real memory reference follows,
   153  	// and the offset is small enough that if x is nil, the address will still be
   154  	// in the first unmapped page of memory.
   155  
   156  	_ = x[9] // ERROR "generated nil check" // bug: would like to remove this check (but nilcheck and load are in different blocks)
   157  
   158  	for {
   159  		if x[9] != 0 { // ERROR "removed nil check"
   160  			break
   161  		}
   162  	}
   163  
   164  	x = fx10()
   165  	_ = x[9] // ERROR "generated nil check" // bug would like to remove before indirect
   166  	if b {
   167  		_ = x[9] // ERROR "removed nil check"
   168  	} else {
   169  		_ = x[9] // ERROR "removed nil check"
   170  	}
   171  	_ = x[9] // ERROR "removed nil check"
   172  
   173  	x = fx10()
   174  	if b {
   175  		_ = x[9] // ERROR "generated nil check"  // bug would like to remove before indirect
   176  	} else {
   177  		_ = &x[9] // ERROR "generated nil check"
   178  	}
   179  	_ = x[9] // ERROR "generated nil check"  // bug would like to remove before indirect
   180  
   181  	fx10()
   182  	_ = x[9] // ERROR "removed nil check"
   183  
   184  	x = fx10()
   185  	y := fx10()
   186  	_ = &x[9] // ERROR "generated nil check"
   187  	y = x
   188  	_ = &x[9] // ERROR "removed[a-z ]* nil check"
   189  	x = y
   190  	_ = &x[9] // ERROR "removed[a-z ]* nil check"
   191  }
   192  
   193  func f5(p *float32, q *float64, r *float32, s *float64) float64 {
   194  	x := float64(*p) // ERROR "removed nil check"
   195  	y := *q          // ERROR "removed nil check"
   196  	*r = 7           // ERROR "removed nil check"
   197  	*s = 9           // ERROR "removed nil check"
   198  	return x + y
   199  }
   200  
   201  type T [29]byte
   202  
   203  func f6(p, q *T) {
   204  	x := *p // ERROR "removed nil check"
   205  	*q = x  // ERROR "removed nil check"
   206  }
   207  
   208  func m1(m map[int][80]byte) byte {
   209  	v := m[3] // ERROR "removed nil check"
   210  	return v[5]
   211  }
   212  func m2(m map[int][800]byte) byte {
   213  	v := m[3] // ERROR "removed nil check"
   214  	return v[5]
   215  }
   216  func m3(m map[int][80]byte) (byte, bool) {
   217  	v, ok := m[3] // ERROR "removed nil check"
   218  	return v[5], ok
   219  }
   220  func m4(m map[int][800]byte) (byte, bool) {
   221  	v, ok := m[3] // ERROR "removed nil check"
   222  	return v[5], ok
   223  }
   224  func p1() byte {
   225  	p := new([100]byte)
   226  	return p[5] // ERROR "removed nil check"
   227  }
   228  
   229  // make sure not to do nil check for access of PAUTOHEAP
   230  //go:noinline
   231  func (p *Struct) m() {}
   232  func c1() {
   233  	var x Struct
   234  	func() { x.m() }() // ERROR "removed nil check"
   235  }
   236  
   237  type SS struct {
   238  	x byte
   239  }
   240  
   241  type TT struct {
   242  	SS
   243  }
   244  
   245  func f(t *TT) *byte {
   246  	// See issue 17242.
   247  	s := &t.SS  // ERROR "removed nil check"
   248  	return &s.x // ERROR "generated nil check"
   249  }
   250  
   251  // make sure not to do nil check for newobject
   252  func f7() (*Struct, float64) {
   253  	t := new(Struct)
   254  	p := &t.Y    // ERROR "removed nil check"
   255  	return t, *p // ERROR "removed nil check"
   256  }
   257  
   258  // make sure to remove nil check for memory move (issue #18003)
   259  func f8(t *[8]int) [8]int {
   260  	return *t // ERROR "removed nil check"
   261  }
   262  
   263  func f9() []int {
   264  	x := new([1]int)
   265  	x[0] = 1  // ERROR "removed nil check"
   266  	y := x[:] // ERROR "removed nil check"
   267  	return y
   268  }