github.com/roboticscm/goman@v0.0.0-20210203095141-87c07b4a0a55/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 repeated nil checks.
    52  	_ = *array0p // ERROR "generated nil check"
    53  	_ = *array0p // ERROR "generated nil check"
    54  
    55  	_ = *intp    // ERROR "generated nil check"
    56  	_ = *arrayp  // ERROR "generated nil check"
    57  	_ = *structp // ERROR "generated nil check"
    58  	_ = *emptyp  // ERROR "generated nil check"
    59  	_ = *arrayp  // ERROR "generated 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 repeated nil check"
    78  	_ = *intp       // ERROR "removed repeated nil check"
    79  	_ = *arrayp     // ERROR "removed repeated nil check"
    80  	_ = *structp    // ERROR "generated nil check"
    81  	_ = *emptyp     // ERROR "generated nil check"
    82  	_ = *arrayp     // ERROR "removed repeated 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 "generated 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 repeated nil check"
   107  	} else {
   108  		_ = x[9999] // ERROR "removed repeated nil check"
   109  	}
   110  	_ = x[9999] // ERROR "generated 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 "generated 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 repeated 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 repeated nil check"
   145  	x = y
   146  	_ = &x[9] // ERROR "removed repeated 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 "removed nil check before indirect"
   157  
   158  	for {
   159  		if x[9] != 0 { // ERROR "removed nil check before indirect"
   160  			break
   161  		}
   162  	}
   163  
   164  	x = fx10()
   165  	_ = x[9] // ERROR "removed nil check before indirect"
   166  	if b {
   167  		_ = x[9] // ERROR "removed nil check before indirect"
   168  	} else {
   169  		_ = x[9] // ERROR "removed nil check before indirect"
   170  	}
   171  	_ = x[9] // ERROR "removed nil check before indirect"
   172  
   173  	x = fx10()
   174  	if b {
   175  		_ = x[9] // ERROR "removed nil check before indirect"
   176  	} else {
   177  		_ = &x[9] // ERROR "generated nil check"
   178  	}
   179  	_ = x[9] // ERROR "removed nil check before indirect"
   180  
   181  	fx10()
   182  	_ = x[9] // ERROR "removed nil check before indirect"
   183  
   184  	x = fx10()
   185  	y := fx10()
   186  	_ = &x[9] // ERROR "generated nil check"
   187  	y = x
   188  	_ = &x[9] // ERROR "removed repeated nil check"
   189  	x = y
   190  	_ = &x[9] // ERROR "removed repeated nil check"
   191  }