@@ -2747,6 +2747,15 @@ llvm::Optional<SwiftScratchContextReader> Target::GetSwiftScratchContext(
2747
2747
log->Printf (" not allowed to create a new context\n " );
2748
2748
return nullptr ;
2749
2749
}
2750
+
2751
+ // Call for its side effects of establishing the Swift scratch type system.
2752
+ auto type_system_or_err =
2753
+ GetScratchTypeSystemForLanguage (eLanguageTypeSwift, false );
2754
+ if (!type_system_or_err) {
2755
+ llvm::consumeError (type_system_or_err.takeError ());
2756
+ return nullptr ;
2757
+ }
2758
+
2750
2759
if (!GetSwiftScratchContextLock ().try_lock ()) {
2751
2760
if (log)
2752
2761
log->Printf (" couldn't acquire scratch context lock\n " );
@@ -2756,8 +2765,12 @@ llvm::Optional<SwiftScratchContextReader> Target::GetSwiftScratchContext(
2756
2765
auto unlock = llvm::make_scope_exit (
2757
2766
[this ] { GetSwiftScratchContextLock ().unlock (); });
2758
2767
2759
- auto type_system_or_err =
2760
- GetScratchTypeSystemForLanguage (eLanguageTypeSwift, false );
2768
+ // With the lock held, get the current scratch type system. This ensures the
2769
+ // current instance is used even in the unlikely event it was changed during
2770
+ // the brief window between the call to `GetScratchTypeSystemForLanguage`
2771
+ // and taking the lock.
2772
+ type_system_or_err = m_scratch_type_system_map.GetTypeSystemForLanguage (
2773
+ eLanguageTypeSwift, this , false );
2761
2774
if (!type_system_or_err) {
2762
2775
llvm::consumeError (type_system_or_err.takeError ());
2763
2776
return nullptr ;
0 commit comments