@@ -41,8 +41,9 @@ const MAX_RECURSION: u32 = 3000;
41
41
// starlark function which calls to_str. We could change all evaluation stack
42
42
// signatures to accept some "context" parameters, but passing it as
43
43
// thread-local is easier.
44
- #[ thread_local]
45
- static STACK_DEPTH : Cell < u32 > = Cell :: new ( 0 ) ;
44
+ thread_local ! {
45
+ static STACK_DEPTH : Cell <u32 > = const { Cell :: new( 0 ) } ;
46
+ }
46
47
47
48
/// Stored previous stack depth before calling `try_inc`.
48
49
///
@@ -56,20 +57,24 @@ pub struct StackGuard {
56
57
57
58
impl Drop for StackGuard {
58
59
fn drop ( & mut self ) {
59
- STACK_DEPTH . set ( self . prev_depth ) ;
60
+ STACK_DEPTH . with ( |stack_depth| {
61
+ stack_depth. set ( self . prev_depth ) ;
62
+ } ) ;
60
63
}
61
64
}
62
65
63
66
/// Increment stack depth.
64
67
fn inc ( ) -> StackGuard {
65
- let prev_depth = STACK_DEPTH . get ( ) ;
66
- STACK_DEPTH . set ( prev_depth + 1 ) ;
67
- StackGuard { prev_depth }
68
+ STACK_DEPTH . with ( |stack_depth| {
69
+ let prev_depth = stack_depth. get ( ) ;
70
+ stack_depth. set ( prev_depth + 1 ) ;
71
+ StackGuard { prev_depth }
72
+ } )
68
73
}
69
74
70
75
/// Check stack depth does not exceed configured max stack depth.
71
76
fn check ( ) -> anyhow:: Result < ( ) > {
72
- if unlikely ( STACK_DEPTH . get ( ) >= MAX_RECURSION ) {
77
+ if unlikely ( STACK_DEPTH . with ( |stack_depth| stack_depth . get ( ) ) >= MAX_RECURSION ) {
73
78
return Err ( ControlError :: TooManyRecursionLevel . into ( ) ) ;
74
79
}
75
80
Ok ( ( ) )
0 commit comments