@@ -33,16 +33,6 @@ fn lazy_default() {
3333 assert_eq ! ( CALLED . load( SeqCst ) , 1 ) ;
3434}
3535
36- #[ test]
37- #[ cfg_attr( not( panic = "unwind" ) , ignore = "test requires unwinding support" ) ]
38- fn lazy_poisoning ( ) {
39- let x: LazyCell < String > = LazyCell :: new ( || panic ! ( "kaboom" ) ) ;
40- for _ in 0 ..2 {
41- let res = panic:: catch_unwind ( panic:: AssertUnwindSafe ( || x. len ( ) ) ) ;
42- assert ! ( res. is_err( ) ) ;
43- }
44- }
45-
4636#[ test]
4737#[ cfg_attr( any( target_os = "emscripten" , target_os = "wasi" ) , ignore) ] // no threads
4838fn sync_lazy_new ( ) {
@@ -123,16 +113,6 @@ fn static_sync_lazy_via_fn() {
123113 assert_eq ! ( xs( ) , & vec![ 1 , 2 , 3 ] ) ;
124114}
125115
126- #[ test]
127- #[ cfg_attr( not( panic = "unwind" ) , ignore = "test requires unwinding support" ) ]
128- fn sync_lazy_poisoning ( ) {
129- let x: LazyLock < String > = LazyLock :: new ( || panic ! ( "kaboom" ) ) ;
130- for _ in 0 ..2 {
131- let res = panic:: catch_unwind ( || x. len ( ) ) ;
132- assert ! ( res. is_err( ) ) ;
133- }
134- }
135-
136116// Check that we can infer `T` from closure's type.
137117#[ test]
138118fn lazy_type_inference ( ) {
@@ -145,17 +125,6 @@ fn is_sync_send() {
145125 assert_traits :: < LazyLock < String > > ( ) ;
146126}
147127
148- #[ test]
149- #[ should_panic = "has previously been poisoned" ]
150- fn lazy_force_mut_panic ( ) {
151- let mut lazy = LazyLock :: < String > :: new ( || panic ! ( ) ) ;
152- panic:: catch_unwind ( panic:: AssertUnwindSafe ( || {
153- let _ = LazyLock :: force_mut ( & mut lazy) ;
154- } ) )
155- . unwrap_err ( ) ;
156- let _ = & * lazy;
157- }
158-
159128#[ test]
160129fn lazy_force_mut ( ) {
161130 let s = "abc" . to_owned ( ) ;
@@ -165,3 +134,56 @@ fn lazy_force_mut() {
165134 p. clear ( ) ;
166135 LazyLock :: force_mut ( & mut lazy) ;
167136}
137+
138+ #[ test]
139+ #[ cfg_attr( not( panic = "unwind" ) , ignore = "test requires unwinding support" ) ]
140+ fn lazy_poisoning ( ) {
141+ let x: LazyCell < String > = LazyCell :: new ( || panic ! ( "kaboom" ) ) ;
142+ for _ in 0 ..2 {
143+ let res = panic:: catch_unwind ( panic:: AssertUnwindSafe ( || x. len ( ) ) ) ;
144+ assert ! ( res. is_err( ) ) ;
145+ }
146+ }
147+
148+ /// Verifies that when a `LazyLock` is poisoned, it panics with the correct error message ("LazyLock
149+ /// instance has previously been poisoned") instead of the underlying `Once` error message.
150+ #[ test]
151+ #[ cfg_attr( not( panic = "unwind" ) , ignore = "test requires unwinding support" ) ]
152+ #[ should_panic( expected = "LazyLock instance has previously been poisoned" ) ]
153+ fn lazy_lock_deref_panic ( ) {
154+ let lazy: LazyLock < String > = LazyLock :: new ( || panic ! ( "initialization failed" ) ) ;
155+
156+ // First access will panic during initialization.
157+ let _ = panic:: catch_unwind ( panic:: AssertUnwindSafe ( || {
158+ let _ = & * lazy;
159+ } ) ) ;
160+
161+ // Second access should panic with the poisoned message.
162+ let _ = & * lazy;
163+ }
164+
165+ #[ test]
166+ #[ should_panic( expected = "LazyLock instance has previously been poisoned" ) ]
167+ fn lazy_lock_deref_mut_panic ( ) {
168+ let mut lazy: LazyLock < String > = LazyLock :: new ( || panic ! ( "initialization failed" ) ) ;
169+
170+ // First access will panic during initialization.
171+ let _ = panic:: catch_unwind ( panic:: AssertUnwindSafe ( || {
172+ let _ = LazyLock :: force_mut ( & mut lazy) ;
173+ } ) ) ;
174+
175+ // Second access should panic with the poisoned message.
176+ let _ = & * lazy;
177+ }
178+
179+ /// Verifies that when the initialization closure panics with a custom message, that message is
180+ /// preserved and not overridden by `LazyLock`.
181+ #[ test]
182+ #[ cfg_attr( not( panic = "unwind" ) , ignore = "test requires unwinding support" ) ]
183+ #[ should_panic( expected = "custom panic message from closure" ) ]
184+ fn lazy_lock_preserves_closure_panic_message ( ) {
185+ let lazy: LazyLock < String > = LazyLock :: new ( || panic ! ( "custom panic message from closure" ) ) ;
186+
187+ // This should panic with the original message from the closure.
188+ let _ = & * lazy;
189+ }
0 commit comments