Source file test/fixedbugs/issue72844.go

     1  // run
     2  
     3  // Copyright 2025 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  //go:noinline
    10  func nilPtrFunc() *[4]int {
    11  	return nil
    12  }
    13  
    14  var nilPtrVar *[4]int
    15  
    16  func testLen1() {
    17  	_ = len(*nilPtrFunc())
    18  }
    19  
    20  func testLen2() {
    21  	_ = len(nilPtrFunc())
    22  }
    23  
    24  func testLen3() {
    25  	_ = len(*nilPtrVar)
    26  }
    27  
    28  func testLen4() {
    29  	_ = len(nilPtrVar)
    30  }
    31  
    32  func testRange1() {
    33  	for range *nilPtrFunc() {
    34  	}
    35  }
    36  func testRange2() {
    37  	for range nilPtrFunc() {
    38  	}
    39  }
    40  func testRange3() {
    41  	for range *nilPtrVar {
    42  	}
    43  }
    44  func testRange4() {
    45  	for range nilPtrVar {
    46  	}
    47  }
    48  
    49  func main() {
    50  	shouldPanic(testLen1)
    51  	shouldNotPanic(testLen2)
    52  	shouldNotPanic(testLen3)
    53  	shouldNotPanic(testLen4)
    54  	shouldPanic(testRange1)
    55  	shouldNotPanic(testRange2)
    56  	shouldNotPanic(testRange3)
    57  	shouldNotPanic(testRange4)
    58  }
    59  
    60  func shouldPanic(f func()) {
    61  	defer func() {
    62  		if e := recover(); e == nil {
    63  			panic("should have panicked")
    64  		}
    65  	}()
    66  	f()
    67  }
    68  func shouldNotPanic(f func()) {
    69  	f()
    70  }
    71  

View as plain text