@@ -2,6 +2,7 @@ package check_test
2
2
3
3
import (
4
4
"errors"
5
+ "fmt"
5
6
"reflect"
6
7
"runtime"
7
8
@@ -222,7 +223,7 @@ func (s *CheckersS) TestPanics(c *check.C) {
222
223
c .Assert (names [0 ], check .Equals , "panic" )
223
224
224
225
// Verify a nil panic
225
- testCheck (c , check .Panics , true , "" , func () { panic (nil ) }, nil )
226
+ testCheck (c , check .Panics , true , "" , func () { panic (nil ) }, propagatedNilPanicValue () )
226
227
testCheck (c , check .Panics , false , "" , func () { panic (nil ) }, "NOPE" )
227
228
}
228
229
@@ -247,8 +248,21 @@ func (s *CheckersS) TestPanicMatches(c *check.C) {
247
248
c .Assert (params [0 ], check .Equals , "KABOOM" )
248
249
c .Assert (names [0 ], check .Equals , "panic" )
249
250
250
- // Verify a nil panic
251
- testCheck (c , check .PanicMatches , false , "Panic value is not a string or an error" , func () { panic (nil ) }, "" )
251
+ if v := propagatedNilPanicValue (); v == nil {
252
+ // Verify a propagated nil panic
253
+ testCheck (c , check .PanicMatches , false , "Panic value is not a string or an error" , func () { panic (nil ) }, "" )
254
+ } else {
255
+ // Verify a non-nil propagation from a nil panic
256
+ testCheck (c , check .PanicMatches , true , "" , func () { panic (nil ) }, fmt .Sprintf ("%s" , v ))
257
+ }
258
+ }
259
+
260
+ // propagatedNilPanicValue returns the value propagated when a nil panic occurs.
261
+ // Prior to go1.21, this is always nil.
262
+ // In go1.21+, this is unless GODEBUG=panicnil=1 is set
263
+ func propagatedNilPanicValue () (v interface {}) {
264
+ defer func () { v = recover () }()
265
+ panic (nil )
252
266
}
253
267
254
268
func (s * CheckersS ) TestFitsTypeOf (c * check.C ) {
0 commit comments