Don't treat missing imports as a fatal error in REPL mode.

We don't want typos in import statements to take down the whole REPL, but we
/do/ want the REPL to be honoring fatal errors that effectively take down the
ASTContext.

This doesn't (yet) apply to the real LLDB REPL, which does not use
SourceFileKind::REPL for its input. The right option to test there is
LangOpts.DebuggerSupport, but that's currently being set for Playgrounds as
well. I've filed <rdar://problem/18090611> for LLDB to adjust their input.

Part of <rdar://problem/17994094>

Swift SVN r21383
This commit is contained in:
Jordan Rose
2014-08-21 22:36:22 +00:00
parent f8face49ab
commit 9ddf57f7ba
7 changed files with 33 additions and 18 deletions

View File

@@ -88,15 +88,15 @@ NOTE(expression_too_complex,sema,none,
ERROR(sema_no_import,sema_nb,Fatal, ERROR(sema_no_import,sema_nb,Fatal,
"no such module '%0'", (StringRef)) "no such module '%0'", (StringRef))
ERROR(sema_no_import_submodule,sema_nb,Fatal, ERROR(sema_no_import_repl,sema_nb,none,
"no such module", ()) "no such module '%0'", (StringRef))
NOTE(sema_no_import_no_sdk,sema_nb,none, NOTE(sema_no_import_no_sdk,sema_nb,none,
"did you forget to set an SDK using -sdk or SDKROOT?", ()) "did you forget to set an SDK using -sdk or SDKROOT?", ())
NOTE(sema_no_import_no_sdk_xcrun,sema_nb,none, NOTE(sema_no_import_no_sdk_xcrun,sema_nb,none,
"use \"xcrun -sdk macosx swift\" to select the default OS X SDK installed " "use \"xcrun -sdk macosx swift\" to select the default OS X SDK installed "
"with Xcode", ()) "with Xcode", ())
ERROR(sema_opening_import,sema_nb,Fatal, ERROR(sema_opening_import,sema_nb,Fatal,
"opening import file '%0.swift': %1", (StringRef, StringRef)) "opening import file for module %0: %1", (Identifier, StringRef))
ERROR(serialization_malformed_module,sema,Fatal, ERROR(serialization_malformed_module,sema,Fatal,
"malformed module file: %0", (StringRef)) "malformed module file: %0", (StringRef))

View File

@@ -985,6 +985,9 @@ private:
} }
if (CI.getASTContext().hadError()) { if (CI.getASTContext().hadError()) {
if (CI.getDiags().hasFatalErrorOccurred())
return false;
CI.getASTContext().Diags.resetHadAnyError(); CI.getASTContext().Diags.resetHadAnyError();
while (REPLInputFile.Decls.size() > RC.CurElem) while (REPLInputFile.Decls.size() > RC.CurElem)
REPLInputFile.Decls.pop_back(); REPLInputFile.Decls.pop_back();

View File

@@ -133,14 +133,19 @@ void
NameBinder::addImport(SmallVectorImpl<std::pair<ImportedModule, bool>> &imports, NameBinder::addImport(SmallVectorImpl<std::pair<ImportedModule, bool>> &imports,
ImportDecl *ID) { ImportDecl *ID) {
Module *M = getModule(ID->getModulePath()); Module *M = getModule(ID->getModulePath());
if (M == 0) { if (!M) {
// FIXME: print entire path regardless. SmallString<64> modulePathStr;
if (ID->getModulePath().size() == 1) { interleave(ID->getModulePath(),
diagnose(ID->getLoc(), diag::sema_no_import, [&](ImportDecl::AccessPathElement elem) {
ID->getModulePath().front().first.str()); modulePathStr += elem.first.str();
} else { },
diagnose(ID->getLoc(), diag::sema_no_import_submodule); [&] { modulePathStr += "."; });
}
auto diagKind = diag::sema_no_import;
if (SF.Kind == SourceFileKind::REPL)
diagKind = diag::sema_no_import_repl;
diagnose(ID->getLoc(), diagKind, modulePathStr);
if (Context.SearchPathOpts.SDKPath.empty()) { if (Context.SearchPathOpts.SDKPath.empty()) {
diagnose(SourceLoc(), diag::sema_no_import_no_sdk); diagnose(SourceLoc(), diag::sema_no_import_no_sdk);
diagnose(SourceLoc(), diag::sema_no_import_no_sdk_xcrun); diagnose(SourceLoc(), diag::sema_no_import_no_sdk_xcrun);

View File

@@ -84,7 +84,7 @@ Module *SourceLoader::loadModule(SourceLoc importLoc,
auto err = inputFileOrError.getError(); auto err = inputFileOrError.getError();
if (err != std::errc::no_such_file_or_directory) { if (err != std::errc::no_such_file_or_directory) {
Ctx.Diags.diagnose(moduleID.second, diag::sema_opening_import, Ctx.Diags.diagnose(moduleID.second, diag::sema_opening_import,
moduleID.first.str(), err.message()); moduleID.first, err.message());
} }
return nullptr; return nullptr;

View File

@@ -299,7 +299,7 @@ Module *SerializedModuleLoader::loadModule(SourceLoc importLoc,
isFramework)) { isFramework)) {
if (err != std::errc::no_such_file_or_directory) { if (err != std::errc::no_such_file_or_directory) {
Ctx.Diags.diagnose(moduleID.second, diag::sema_opening_import, Ctx.Diags.diagnose(moduleID.second, diag::sema_opening_import,
moduleID.first.str(), err.message()); moduleID.first, err.message());
} }
return nullptr; return nullptr;

View File

@@ -1,5 +1,7 @@
// RUN: %swift -sdk %sdk -repl < %s 2>%t.txt | FileCheck %s // RUN: rm -rf %t && mkdir %t
// RUN: FileCheck -check-prefix CHECK-ERROR %s < %t.txt // RUN: touch %t/Corrupted_Module.swiftmodule
// RUN: not %swift -sdk %sdk -I %t -repl <%s 2>%t/stderr.txt | FileCheck %s
// RUN: FileCheck -check-prefix CHECK-ERROR %s < %t/stderr.txt
// REQUIRES: sdk // REQUIRES: sdk
// REQUIRES: swift_repl // REQUIRES: swift_repl
@@ -27,3 +29,10 @@ MKMapRectIsNull(x)
"end" "end"
// CHECK-NEXT: String = "end"{{$}} // CHECK-NEXT: String = "end"{{$}}
import Corrupted_Module
// CHECK-ERROR: error: malformed module file
"unreached"
// CHECK-NOT: unreached

View File

@@ -4,7 +4,6 @@
// RUN: echo "public func foo() -> Int { return false }" > %t/import_text.swift // RUN: echo "public func foo() -> Int { return false }" > %t/import_text.swift
// RUN: echo "public func pho$(printf '\xC3\xBB')x() -> Int { return false }" > %t/fran$(printf '\xC3\xA7')ais.swift // RUN: echo "public func pho$(printf '\xC3\xBB')x() -> Int { return false }" > %t/fran$(printf '\xC3\xA7')ais.swift
// RUN: %swift %s -I=%t -sdk "" -enable-source-import -verify -show-diagnostics-after-fatal // RUN: %swift %s -I=%t -sdk "" -enable-source-import -verify -show-diagnostics-after-fatal
// RUN: not %swift %s -I=%t -sdk "" -enable-source-import 2>&1 | FileCheck %s
import Builtin // expected-error {{no such module 'Builtin'}} import Builtin // expected-error {{no such module 'Builtin'}}
@@ -48,8 +47,7 @@ import Swift.import.abc // expected-error 2 {{expected identifier}}
import where Swift.Int // expected-error {{expected identifier}} import where Swift.Int // expected-error {{expected identifier}}
import 2 // expected-error {{expected identifier}} import 2 // expected-error {{expected identifier}}
// CHECK-NOT: no such module 'really' import really.nonexistent // expected-error {{no such module 'really.nonexistent'}}
import really.nonexistent // expected-error {{no such module}}
import import_text // no-warning despite function body problems import import_text // no-warning despite function body problems