@@ -2257,7 +2257,25 @@ pub fn fnc_typetrees<'tcx>(tcx: TyCtxt<'tcx>, fn_ty: Ty<'tcx>) -> FncTree {
2257
2257
2258
2258
/// Generate TypeTree for a specific type.
2259
2259
/// This function analyzes a Rust type and creates appropriate TypeTree metadata.
2260
+
2261
+ /// Maximum recursion depth for TypeTree generation to prevent infinite loops
2262
+ /// Set to 32 levels which should be sufficient for most practical type hierarchies
2263
+ /// while preventing stack overflow from pathological recursive types.
2264
+ const MAX_TYPETREE_DEPTH : usize = 32 ;
2265
+
2266
+ fn typetree_from_ty_with_depth < ' tcx > ( tcx : TyCtxt < ' tcx > , ty : Ty < ' tcx > , depth : usize ) -> TypeTree {
2267
+ if depth > MAX_TYPETREE_DEPTH {
2268
+ return TypeTree :: new ( ) ;
2269
+ }
2270
+
2271
+ typetree_from_ty_impl ( tcx, ty, depth)
2272
+ }
2273
+
2260
2274
pub fn typetree_from_ty < ' tcx > ( tcx : TyCtxt < ' tcx > , ty : Ty < ' tcx > ) -> TypeTree {
2275
+ typetree_from_ty_with_depth ( tcx, ty, 0 )
2276
+ }
2277
+
2278
+ fn typetree_from_ty_impl < ' tcx > ( tcx : TyCtxt < ' tcx > , ty : Ty < ' tcx > , depth : usize ) -> TypeTree {
2261
2279
if ty. is_scalar ( ) {
2262
2280
let ( kind, size) = if ty. is_integral ( ) || ty. is_char ( ) || ty. is_bool ( ) {
2263
2281
( Kind :: Integer , ty. primitive_size ( tcx) . bytes_usize ( ) )
@@ -2299,7 +2317,7 @@ pub fn typetree_from_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> TypeTree {
2299
2317
return TypeTree :: new ( ) ;
2300
2318
}
2301
2319
2302
- let element_tree = typetree_from_ty ( tcx, * element_ty) ;
2320
+ let element_tree = typetree_from_ty_impl ( tcx, * element_ty, depth + 1 ) ;
2303
2321
2304
2322
let element_layout = tcx
2305
2323
. layout_of ( ty:: TypingEnv :: fully_monomorphized ( ) . as_query_input ( * element_ty) )
@@ -2335,7 +2353,7 @@ pub fn typetree_from_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> TypeTree {
2335
2353
2336
2354
if ty. is_slice ( ) {
2337
2355
if let ty:: Slice ( element_ty) = ty. kind ( ) {
2338
- let element_tree = typetree_from_ty ( tcx, * element_ty) ;
2356
+ let element_tree = typetree_from_ty_impl ( tcx, * element_ty, depth + 1 ) ;
2339
2357
return element_tree;
2340
2358
}
2341
2359
}
@@ -2349,7 +2367,7 @@ pub fn typetree_from_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> TypeTree {
2349
2367
let mut current_offset = 0 ;
2350
2368
2351
2369
for tuple_ty in tuple_types. iter ( ) {
2352
- let element_tree = typetree_from_ty ( tcx, tuple_ty) ;
2370
+ let element_tree = typetree_from_ty_impl ( tcx, tuple_ty, depth + 1 ) ;
2353
2371
2354
2372
let element_layout = tcx
2355
2373
. layout_of ( ty:: TypingEnv :: fully_monomorphized ( ) . as_query_input ( tuple_ty) )
@@ -2385,7 +2403,7 @@ pub fn typetree_from_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> TypeTree {
2385
2403
2386
2404
for ( field_idx, field_def) in adt_def. all_fields ( ) . enumerate ( ) {
2387
2405
let field_ty = field_def. ty ( tcx, args) ;
2388
- let field_tree = typetree_from_ty ( tcx, field_ty) ;
2406
+ let field_tree = typetree_from_ty_impl ( tcx, field_ty, depth + 1 ) ;
2389
2407
2390
2408
let field_offset = layout. fields . offset ( field_idx) . bytes_usize ( ) ;
2391
2409
0 commit comments