@@ -4,6 +4,7 @@ use crate::formatter::{FmtCtx, IntoFormatter};
44use crate :: llbc_ast;
55use crate :: options:: TranslateOptions ;
66use crate :: pretty:: FmtWithCtx ;
7+ use crate :: register_error;
78use crate :: transform:: CowBox ;
89use crate :: ullbc_ast;
910use std:: cell:: RefCell ;
@@ -121,24 +122,29 @@ impl TransformCtx {
121122 self . options . opacity_for_name ( & self . translated , name)
122123 }
123124
124- pub ( crate ) fn with_def_id < F , T > (
125+ /// Run an item transformation with its error context, reporting any panic on that item.
126+ pub ( crate ) fn with_def_id (
125127 & mut self ,
126128 def_id : impl Into < ItemId > ,
127129 def_id_is_local : bool ,
128- f : F ,
129- ) -> T
130- where
131- F : FnOnce ( & mut Self ) -> T ,
132- {
130+ span : Span ,
131+ f : impl FnOnce ( & mut Self ) ,
132+ ) {
133+ let def_id = def_id. into ( ) ;
133134 let mut errors = self . errors . borrow_mut ( ) ;
134- let current_def_id = errors. def_id . replace ( def_id. into ( ) ) ;
135+ let current_def_id = errors. def_id . replace ( def_id) ;
135136 let current_def_id_is_local = mem:: replace ( & mut errors. def_id_is_local , def_id_is_local) ;
136137 drop ( errors) ; // important: release the refcell "lock"
137- let ret = f ( self ) ;
138+ if std:: panic:: catch_unwind ( std:: panic:: AssertUnwindSafe ( || f ( self ) ) ) . is_err ( ) {
139+ register_error ! (
140+ self ,
141+ span,
142+ "Thread panicked while transforming item `{def_id:?}`."
143+ ) ;
144+ }
138145 let mut errors = self . errors . borrow_mut ( ) ;
139146 errors. def_id = current_def_id;
140147 errors. def_id_is_local = current_def_id_is_local;
141- ret
142148 }
143149
144150 /// Mutably iterate over the bodies.
@@ -153,8 +159,11 @@ impl TransformCtx {
153159 let mut body = mem:: replace ( & mut decl. body , Body :: Opaque ) ;
154160 let fun_decl_id = decl. def_id ;
155161 let is_local = decl. item_meta . is_local ;
156- self . with_def_id ( fun_decl_id, is_local, |ctx| f ( ctx, & mut body) ) ;
157- self . translated . fun_decls [ id] . body = body;
162+ let span = decl. item_meta . span ;
163+ self . with_def_id ( fun_decl_id, is_local, span, |ctx| f ( ctx, & mut body) ) ;
164+ let decl = & mut self . translated . fun_decls [ id] ;
165+ decl. body = body;
166+ decl. item_meta . has_errors |= self . errors . borrow ( ) . item_has_errors ( fun_decl_id) ;
158167 }
159168 }
160169 }
@@ -168,7 +177,9 @@ impl TransformCtx {
168177 if let Some ( mut decl) = self . translated . fun_decls . remove ( id) {
169178 let fun_decl_id = decl. def_id ;
170179 let is_local = decl. item_meta . is_local ;
171- self . with_def_id ( fun_decl_id, is_local, |ctx| f ( ctx, & mut decl) ) ;
180+ let span = decl. item_meta . span ;
181+ self . with_def_id ( fun_decl_id, is_local, span, |ctx| f ( ctx, & mut decl) ) ;
182+ decl. item_meta . has_errors |= self . errors . borrow ( ) . item_has_errors ( fun_decl_id) ;
172183 self . translated . fun_decls . set_slot ( id, decl) ;
173184 }
174185 }
@@ -183,7 +194,9 @@ impl TransformCtx {
183194 if let Some ( mut decl) = self . translated . type_decls . remove ( id) {
184195 let type_decl_id = decl. def_id ;
185196 let is_local = decl. item_meta . is_local ;
186- self . with_def_id ( type_decl_id, is_local, |ctx| f ( ctx, & mut decl) ) ;
197+ let span = decl. item_meta . span ;
198+ self . with_def_id ( type_decl_id, is_local, span, |ctx| f ( ctx, & mut decl) ) ;
199+ decl. item_meta . has_errors |= self . errors . borrow ( ) . item_has_errors ( type_decl_id) ;
187200 self . translated . type_decls . set_slot ( id, decl) ;
188201 }
189202 }
@@ -195,10 +208,9 @@ impl TransformCtx {
195208 for id in self . translated . all_ids ( ) {
196209 if let Some ( mut decl) = self . translated . remove_item_temporarily ( id) {
197210 let is_local = decl. as_ref ( ) . item_meta ( ) . is_local ;
198- self . with_def_id ( id, is_local, |ctx| f ( ctx, decl. as_mut ( ) ) ) ;
199- if self . errors . borrow ( ) . item_has_errors ( id) {
200- decl. as_mut ( ) . item_meta ( ) . has_errors = true ;
201- }
211+ let span = decl. as_ref ( ) . item_meta ( ) . span ;
212+ self . with_def_id ( id, is_local, span, |ctx| f ( ctx, decl. as_mut ( ) ) ) ;
213+ decl. as_mut ( ) . item_meta ( ) . has_errors |= self . errors . borrow ( ) . item_has_errors ( id) ;
202214 self . translated . put_item_back ( id, decl) ;
203215 }
204216 }
0 commit comments