@@ -138,8 +138,7 @@ LLDBNameLookup::LLDBNameLookup(
138138 SwiftExpressionParser::SILVariableMap &variable_map, SymbolContext &sc,
139139 ExecutionContextScope &exe_scope)
140140 : SILDebuggerClient(source_file.getASTContext()),
141- m_log(GetLog(LLDBLog::Expressions)), m_source_file(source_file),
142- m_variable_map(variable_map), m_sc(sc) {
141+ m_source_file(source_file), m_variable_map(variable_map), m_sc(sc) {
143142 source_file.getParentModule ()->setDebugClient (this );
144143
145144 if (!m_sc.target_sp )
@@ -177,11 +176,15 @@ void LLDBNameLookup::RegisterTypeAliases(
177176
178177// / A name lookup class for debugger expr mode.
179178class LLDBExprNameLookup : public LLDBNameLookup {
179+ const SwiftASTContextForExpressions *m_this_context;
180+
180181public:
181182 LLDBExprNameLookup (swift::SourceFile &source_file,
182183 SwiftExpressionParser::SILVariableMap &variable_map,
183- SymbolContext &sc, ExecutionContextScope &exe_scope)
184- : LLDBNameLookup(source_file, variable_map, sc, exe_scope) {}
184+ SymbolContext &sc, ExecutionContextScope &exe_scope,
185+ const SwiftASTContextForExpressions *this_context)
186+ : LLDBNameLookup(source_file, variable_map, sc, exe_scope),
187+ m_this_context (this_context) {}
185188
186189 bool shouldGlobalize (swift::Identifier Name, swift::DeclKind Kind) override {
187190 // Extensions have to be globalized, there's no way to mark them
@@ -195,7 +198,7 @@ class LLDBExprNameLookup : public LLDBNameLookup {
195198 return true ;
196199
197200 if (Name.str ().starts_with (" $" )) {
198- LLDB_LOG (m_log ,
201+ LLDB_LOG (GetLog (LLDBLog::Expressions) ,
199202 " [LLDBExprNameLookup::shouldGlobalize] Returning true to "
200203 " globalizing {0}" ,
201204 Name.str ());
@@ -223,7 +226,7 @@ class LLDBExprNameLookup : public LLDBNameLookup {
223226 static unsigned counter = 0 ;
224227 unsigned count = counter++;
225228
226- LLDB_LOG (m_log ,
229+ LLDB_LOG (GetLog (LLDBLog::Expressions) ,
227230 " [LLDBExprNameLookup::lookupOverrides({0})] Searching for \" {1}\" " ,
228231 count, Name.getIdentifier ().get ());
229232
@@ -234,6 +237,8 @@ class LLDBExprNameLookup : public LLDBNameLookup {
234237 swift::SourceLoc Loc, bool IsTypeLookup,
235238 ResultVector &RV) override {
236239 LLDB_SCOPED_TIMER ();
240+ assert (SwiftASTContext::GetSwiftASTContext (&DC->getASTContext ()) ==
241+ m_this_context);
237242 static unsigned counter = 0 ;
238243 unsigned count = counter++;
239244
@@ -242,7 +247,7 @@ class LLDBExprNameLookup : public LLDBNameLookup {
242247 return false ;
243248
244249 LLDB_LOG (
245- m_log ,
250+ GetLog (LLDBLog::Expressions) ,
246251 " [LLDBExprNameLookup::lookupAdditions ({0})] Searching for \" {1}\" " ,
247252 count, NameStr);
248253
@@ -290,9 +295,10 @@ class LLDBExprNameLookup : public LLDBNameLookup {
290295 persistent_results);
291296
292297 for (CompilerDecl & decl : persistent_results) {
293- if (decl.GetTypeSystem () !=
294- SwiftASTContext::GetSwiftASTContext (&DC->getASTContext ())) {
295- LLDB_LOG (m_log, " ignoring persistent result from other context" );
298+ if (decl.GetTypeSystem () != m_this_context) {
299+ LLDB_LOG (GetLog (LLDBLog::Expressions),
300+ " Ignoring persistent result from other context: {0}" ,
301+ NameStr);
296302 continue ;
297303 }
298304 auto *value_decl =
@@ -368,11 +374,15 @@ class LLDBExprNameLookup : public LLDBNameLookup {
368374
369375// / A name lookup class for REPL and Playground mode.
370376class LLDBREPLNameLookup : public LLDBNameLookup {
377+ const SwiftASTContextForExpressions *m_this_context;
378+
371379public:
372380 LLDBREPLNameLookup (swift::SourceFile &source_file,
373381 SwiftExpressionParser::SILVariableMap &variable_map,
374- SymbolContext &sc, ExecutionContextScope &exe_scope)
375- : LLDBNameLookup(source_file, variable_map, sc, exe_scope) {}
382+ SymbolContext &sc, ExecutionContextScope &exe_scope,
383+ const SwiftASTContextForExpressions *this_context)
384+ : LLDBNameLookup(source_file, variable_map, sc, exe_scope),
385+ m_this_context (this_context) {}
376386
377387 bool shouldGlobalize (swift::Identifier Name, swift::DeclKind kind) override {
378388 return false ;
@@ -390,6 +400,9 @@ class LLDBREPLNameLookup : public LLDBNameLookup {
390400 swift::SourceLoc Loc, bool IsTypeLookup,
391401 ResultVector &RV) override {
392402 LLDB_SCOPED_TIMER ();
403+
404+ assert (SwiftASTContext::GetSwiftASTContext (&DC->getASTContext ()) ==
405+ m_this_context);
393406 static unsigned counter = 0 ;
394407 unsigned count = counter++;
395408
@@ -398,7 +411,7 @@ class LLDBREPLNameLookup : public LLDBNameLookup {
398411 return false ;
399412
400413 LLDB_LOG (
401- m_log ,
414+ GetLog (LLDBLog::Expressions) ,
402415 " [LLDBREPLNameLookup::lookupAdditions ({0})] Searching for \" {1}\" " ,
403416 count, NameStr);
404417
@@ -420,6 +433,12 @@ class LLDBREPLNameLookup : public LLDBNameLookup {
420433
421434 // Append the persistent decls that we found to the result vector.
422435 for (auto result : persistent_decl_results) {
436+ if (result.GetTypeSystem () != m_this_context) {
437+ LLDB_LOG (GetLog (LLDBLog::Expressions),
438+ " Ignoring persistent result from other context: {0}" , NameStr);
439+ continue ;
440+ }
441+
423442 // No import required.
424443 auto *result_decl =
425444 static_cast <swift::ValueDecl *>(result.GetOpaqueDecl ());
@@ -1375,11 +1394,11 @@ SwiftExpressionParser::ParseAndImport(
13751394
13761395 LLDBNameLookup *external_lookup;
13771396 if (m_options.GetPlaygroundTransformEnabled () || m_options.GetREPLEnabled ()) {
1378- external_lookup =
1379- new LLDBREPLNameLookup (*source_file, variable_map, m_sc, *m_exe_scope);
1397+ external_lookup = new LLDBREPLNameLookup (*source_file, variable_map, m_sc,
1398+ *m_exe_scope, &m_swift_ast_ctx );
13801399 } else {
1381- external_lookup =
1382- new LLDBExprNameLookup (*source_file, variable_map, m_sc, *m_exe_scope);
1400+ external_lookup = new LLDBExprNameLookup (*source_file, variable_map, m_sc,
1401+ *m_exe_scope, &m_swift_ast_ctx );
13831402 }
13841403
13851404 // FIXME: This call is here just so that the we keep the
@@ -1758,8 +1777,15 @@ SwiftExpressionParser::Parse(DiagnosticManager &diagnostic_manager,
17581777
17591778 // Signal that we want to retry the expression exactly once with a
17601779 // fresh SwiftASTContext.
1761- if (retry)
1780+ if (retry) {
1781+ if (auto *persistent_state =
1782+ llvm::cast_or_null<SwiftPersistentExpressionState>(
1783+ m_sc.target_sp ->GetPersistentExpressionStateForLanguage (
1784+ lldb::eLanguageTypeSwift)))
1785+ persistent_state->Clear ();
1786+
17621787 return ParseResult::retry_fresh_context;
1788+ }
17631789
17641790 // Unrecoverable error.
17651791 return ParseResult::unrecoverable_error;
@@ -2023,6 +2049,11 @@ SwiftExpressionParser::Parse(DiagnosticManager &diagnostic_manager,
20232049 return ParseResult::unrecoverable_error;
20242050 }
20252051
2052+ if (m_swift_ast_ctx.GetASTContext ()->hadError ()) {
2053+ DiagnoseSwiftASTContextError ();
2054+ return ParseResult::unrecoverable_error;
2055+ }
2056+
20262057 {
20272058 std::lock_guard<std::recursive_mutex> global_context_locker (
20282059 IRExecutionUnit::GetLLVMGlobalContextMutex ());
0 commit comments