@@ -357,30 +357,49 @@ void CompilerInstance::setupDependencyTrackerIfNeeded() {
357357 DepTracker = std::make_unique<DependencyTracker>(*collectionMode);
358358}
359359
360- bool CompilerInstance::setup (const CompilerInvocation &Invok) {
360+ bool CompilerInstance::setup (const CompilerInvocation &Invok,
361+ std::string &Error) {
361362 Invocation = Invok;
362363
363364 setupDependencyTrackerIfNeeded ();
364365
365366 // If initializing the overlay file system fails there's no sense in
366367 // continuing because the compiler will read the wrong files.
367- if (setUpVirtualFileSystemOverlays ())
368+ if (setUpVirtualFileSystemOverlays ()) {
369+ Error = " Setting up virtual file system overlays failed" ;
368370 return true ;
371+ }
369372 setUpLLVMArguments ();
370373 setUpDiagnosticOptions ();
371374
372375 assert (Lexer::isIdentifier (Invocation.getModuleName ()));
373376
374- if (setUpInputs ())
377+ if (setUpInputs ()) {
378+ Error = " Setting up inputs failed" ;
375379 return true ;
380+ }
376381
377- if (setUpASTContextIfNeeded ())
382+ if (setUpASTContextIfNeeded ()) {
383+ Error = " Setting up ASTContext failed" ;
378384 return true ;
385+ }
379386
380387 setupStatsReporter ();
381388
382- if (setupDiagnosticVerifierIfNeeded ())
389+ if (setupDiagnosticVerifierIfNeeded ()) {
390+ Error = " Setting up diagnostics verified failed" ;
383391 return true ;
392+ }
393+
394+ // If we expect an implicit stdlib import, load in the standard library. If we
395+ // either fail to find it or encounter an error while loading it, bail early. Continuing will at best
396+ // trigger a bunch of other errors due to the stdlib being missing, or at
397+ // worst crash downstream as many call sites don't currently handle a missing
398+ // stdlib.
399+ if (loadStdlibIfNeeded ()) {
400+ Error = " Loading the standard library failed" ;
401+ return true ;
402+ }
384403
385404 return false ;
386405}
@@ -1101,6 +1120,10 @@ void CompilerInstance::performSema() {
11011120}
11021121
11031122bool CompilerInstance::loadStdlibIfNeeded () {
1123+ if (!FrontendOptions::doesActionRequireSwiftStandardLibrary (
1124+ Invocation.getFrontendOptions ().RequestedAction )) {
1125+ return false ;
1126+ }
11041127 // If we aren't expecting an implicit stdlib import, there's nothing to do.
11051128 if (getImplicitImportInfo ().StdlibKind != ImplicitStdlibKind::Stdlib)
11061129 return false ;
0 commit comments