@@ -139,8 +139,7 @@ LLDBNameLookup::LLDBNameLookup(
139139 SwiftExpressionParser::SILVariableMap &variable_map, SymbolContext &sc,
140140 ExecutionContextScope &exe_scope)
141141 : SILDebuggerClient(source_file.getASTContext()),
142- m_log(GetLog(LLDBLog::Expressions)), m_source_file(source_file),
143- m_variable_map(variable_map), m_sc(sc) {
142+ m_source_file(source_file), m_variable_map(variable_map), m_sc(sc) {
144143 source_file.getParentModule ()->setDebugClient (this );
145144
146145 if (!m_sc.target_sp )
@@ -178,11 +177,15 @@ void LLDBNameLookup::RegisterTypeAliases(
178177
179178// / A name lookup class for debugger expr mode.
180179class LLDBExprNameLookup : public LLDBNameLookup {
180+ const SwiftASTContextForExpressions *m_this_context;
181+
181182public:
182183 LLDBExprNameLookup (swift::SourceFile &source_file,
183184 SwiftExpressionParser::SILVariableMap &variable_map,
184- SymbolContext &sc, ExecutionContextScope &exe_scope)
185- : LLDBNameLookup(source_file, variable_map, sc, exe_scope) {}
185+ SymbolContext &sc, ExecutionContextScope &exe_scope,
186+ const SwiftASTContextForExpressions *this_context)
187+ : LLDBNameLookup(source_file, variable_map, sc, exe_scope),
188+ m_this_context (this_context) {}
186189
187190 bool shouldGlobalize (swift::Identifier Name, swift::DeclKind Kind) override {
188191 // Extensions have to be globalized, there's no way to mark them
@@ -196,7 +199,7 @@ class LLDBExprNameLookup : public LLDBNameLookup {
196199 return true ;
197200
198201 if (Name.str ().starts_with (" $" )) {
199- LLDB_LOG (m_log ,
202+ LLDB_LOG (GetLog (LLDBLog::Expressions) ,
200203 " [LLDBExprNameLookup::shouldGlobalize] Returning true to "
201204 " globalizing {0}" ,
202205 Name.str ());
@@ -224,7 +227,7 @@ class LLDBExprNameLookup : public LLDBNameLookup {
224227 static unsigned counter = 0 ;
225228 unsigned count = counter++;
226229
227- LLDB_LOG (m_log ,
230+ LLDB_LOG (GetLog (LLDBLog::Expressions) ,
228231 " [LLDBExprNameLookup::lookupOverrides({0})] Searching for \" {1}\" " ,
229232 count, Name.getIdentifier ().get ());
230233
@@ -235,6 +238,8 @@ class LLDBExprNameLookup : public LLDBNameLookup {
235238 swift::SourceLoc Loc, bool IsTypeLookup,
236239 ResultVector &RV) override {
237240 LLDB_SCOPED_TIMER ();
241+ assert (SwiftASTContext::GetSwiftASTContext (&DC->getASTContext ()) ==
242+ m_this_context);
238243 static unsigned counter = 0 ;
239244 unsigned count = counter++;
240245
@@ -243,7 +248,7 @@ class LLDBExprNameLookup : public LLDBNameLookup {
243248 return false ;
244249
245250 LLDB_LOG (
246- m_log ,
251+ GetLog (LLDBLog::Expressions) ,
247252 " [LLDBExprNameLookup::lookupAdditions ({0})] Searching for \" {1}\" " ,
248253 count, NameStr);
249254
@@ -291,9 +296,10 @@ class LLDBExprNameLookup : public LLDBNameLookup {
291296 persistent_results);
292297
293298 for (CompilerDecl & decl : persistent_results) {
294- if (decl.GetTypeSystem () !=
295- SwiftASTContext::GetSwiftASTContext (&DC->getASTContext ())) {
296- LLDB_LOG (m_log, " ignoring persistent result from other context" );
299+ if (decl.GetTypeSystem () != m_this_context) {
300+ LLDB_LOG (GetLog (LLDBLog::Expressions),
301+ " Ignoring persistent result from other context: {0}" ,
302+ NameStr);
297303 continue ;
298304 }
299305 auto *value_decl =
@@ -369,11 +375,15 @@ class LLDBExprNameLookup : public LLDBNameLookup {
369375
370376// / A name lookup class for REPL and Playground mode.
371377class LLDBREPLNameLookup : public LLDBNameLookup {
378+ const SwiftASTContextForExpressions *m_this_context;
379+
372380public:
373381 LLDBREPLNameLookup (swift::SourceFile &source_file,
374382 SwiftExpressionParser::SILVariableMap &variable_map,
375- SymbolContext &sc, ExecutionContextScope &exe_scope)
376- : LLDBNameLookup(source_file, variable_map, sc, exe_scope) {}
383+ SymbolContext &sc, ExecutionContextScope &exe_scope,
384+ const SwiftASTContextForExpressions *this_context)
385+ : LLDBNameLookup(source_file, variable_map, sc, exe_scope),
386+ m_this_context (this_context) {}
377387
378388 bool shouldGlobalize (swift::Identifier Name, swift::DeclKind kind) override {
379389 return false ;
@@ -391,6 +401,9 @@ class LLDBREPLNameLookup : public LLDBNameLookup {
391401 swift::SourceLoc Loc, bool IsTypeLookup,
392402 ResultVector &RV) override {
393403 LLDB_SCOPED_TIMER ();
404+
405+ assert (SwiftASTContext::GetSwiftASTContext (&DC->getASTContext ()) ==
406+ m_this_context);
394407 static unsigned counter = 0 ;
395408 unsigned count = counter++;
396409
@@ -399,7 +412,7 @@ class LLDBREPLNameLookup : public LLDBNameLookup {
399412 return false ;
400413
401414 LLDB_LOG (
402- m_log ,
415+ GetLog (LLDBLog::Expressions) ,
403416 " [LLDBREPLNameLookup::lookupAdditions ({0})] Searching for \" {1}\" " ,
404417 count, NameStr);
405418
@@ -421,6 +434,12 @@ class LLDBREPLNameLookup : public LLDBNameLookup {
421434
422435 // Append the persistent decls that we found to the result vector.
423436 for (auto result : persistent_decl_results) {
437+ if (result.GetTypeSystem () != m_this_context) {
438+ LLDB_LOG (GetLog (LLDBLog::Expressions),
439+ " Ignoring persistent result from other context: {0}" , NameStr);
440+ continue ;
441+ }
442+
424443 // No import required.
425444 auto *result_decl =
426445 static_cast <swift::ValueDecl *>(result.GetOpaqueDecl ());
@@ -1380,11 +1399,11 @@ SwiftExpressionParser::ParseAndImport(
13801399
13811400 LLDBNameLookup *external_lookup;
13821401 if (m_options.GetPlaygroundTransformEnabled () || m_options.GetREPLEnabled ()) {
1383- external_lookup =
1384- new LLDBREPLNameLookup (*source_file, variable_map, m_sc, *m_exe_scope);
1402+ external_lookup = new LLDBREPLNameLookup (*source_file, variable_map, m_sc,
1403+ *m_exe_scope, &m_swift_ast_ctx );
13851404 } else {
1386- external_lookup =
1387- new LLDBExprNameLookup (*source_file, variable_map, m_sc, *m_exe_scope);
1405+ external_lookup = new LLDBExprNameLookup (*source_file, variable_map, m_sc,
1406+ *m_exe_scope, &m_swift_ast_ctx );
13881407 }
13891408
13901409 // FIXME: This call is here just so that the we keep the
@@ -1769,8 +1788,15 @@ SwiftExpressionParser::Parse(DiagnosticManager &diagnostic_manager,
17691788
17701789 // Signal that we want to retry the expression exactly once with a
17711790 // fresh SwiftASTContext.
1772- if (retry)
1791+ if (retry) {
1792+ if (auto *persistent_state =
1793+ llvm::cast_or_null<SwiftPersistentExpressionState>(
1794+ m_sc.target_sp ->GetPersistentExpressionStateForLanguage (
1795+ lldb::eLanguageTypeSwift)))
1796+ persistent_state->Clear ();
1797+
17731798 return ParseResult::retry_fresh_context;
1799+ }
17741800
17751801 // Unrecoverable error.
17761802 return parse_result_failure;
@@ -2033,6 +2059,11 @@ SwiftExpressionParser::Parse(DiagnosticManager &diagnostic_manager,
20332059 return parse_result_failure;
20342060 }
20352061
2062+ if (m_swift_ast_ctx.GetASTContext ()->hadError ()) {
2063+ DiagnoseSwiftASTContextError ();
2064+ return ParseResult::unrecoverable_error;
2065+ }
2066+
20362067 {
20372068 std::lock_guard<std::recursive_mutex> global_context_locker (
20382069 IRExecutionUnit::GetLLVMGlobalContextMutex ());
0 commit comments