@@ -2484,42 +2484,44 @@ mod tls_cache {
24842484 db_nonce : Nonce ,
24852485 }
24862486
2487+ impl Cache {
2488+ const fn default ( ) -> Cache {
2489+ Cache {
2490+ cache : GlobalCache :: new ( ) ,
2491+ revision : Revision :: max ( ) ,
2492+ db_nonce : Nonce :: invalid ( ) ,
2493+ }
2494+ }
2495+ }
2496+
24872497 thread_local ! {
2488- static GLOBAL_CACHE : RefCell <Option < Cache >> = const { RefCell :: new( None ) } ;
2498+ static GLOBAL_CACHE : RefCell <Cache > = const { RefCell :: new( Cache :: default ( ) ) } ;
24892499 }
24902500
24912501 pub ( super ) fn reinit_cache ( db : & dyn HirDatabase ) {
24922502 GLOBAL_CACHE . with_borrow_mut ( |handle| {
24932503 let ( db_nonce, revision) = db. nonce_and_revision ( ) ;
2494- match handle {
2495- Some ( handle) => {
2496- if handle. revision != revision || db_nonce != handle. db_nonce {
2497- * handle = Cache { cache : GlobalCache :: default ( ) , revision, db_nonce } ;
2498- }
2499- }
2500- None => * handle = Some ( Cache { cache : GlobalCache :: default ( ) , revision, db_nonce } ) ,
2504+ if handle. revision != revision || db_nonce != handle. db_nonce {
2505+ * handle = Cache { cache : GlobalCache :: default ( ) , revision, db_nonce } ;
25012506 }
25022507 } )
25032508 }
25042509
2510+ #[ inline]
25052511 pub ( super ) fn borrow_assume_valid < ' db , T > (
25062512 db : & ' db dyn HirDatabase ,
25072513 f : impl FnOnce ( & mut GlobalCache < DbInterner < ' db > > ) -> T ,
25082514 ) -> T {
25092515 if cfg ! ( debug_assertions) {
2510- let get_state = || {
2511- GLOBAL_CACHE . with_borrow ( |handle| {
2512- handle. as_ref ( ) . map ( |handle| ( handle. db_nonce , handle. revision ) )
2513- } )
2514- } ;
2516+ let get_state =
2517+ || GLOBAL_CACHE . with_borrow ( |handle| ( handle. db_nonce , handle. revision ) ) ;
25152518 let old_state = get_state ( ) ;
25162519 reinit_cache ( db) ;
25172520 let new_state = get_state ( ) ;
25182521 assert_eq ! ( old_state, new_state, "you assumed the cache is valid!" ) ;
25192522 }
25202523
25212524 GLOBAL_CACHE . with_borrow_mut ( |handle| {
2522- let handle = handle. as_mut ( ) . expect ( "you assumed the cache is valid!" ) ;
25232525 // SAFETY: No idea
25242526 f ( unsafe {
25252527 std:: mem:: transmute :: <
@@ -2535,7 +2537,7 @@ mod tls_cache {
25352537 /// Should be called before getting memory usage estimations, as the solver cache
25362538 /// is per-revision and usually should be excluded from estimations.
25372539 pub fn clear_tls_solver_cache ( ) {
2538- GLOBAL_CACHE . with_borrow_mut ( |handle| * handle = None ) ;
2540+ GLOBAL_CACHE . with_borrow_mut ( |handle| * handle = Cache :: default ( ) ) ;
25392541 }
25402542}
25412543
0 commit comments