mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
[embedded] Add a temporary flag that turns throws into traps so that programs that use throwing can at least be compiled for now
This commit is contained in:
@@ -272,6 +272,9 @@ namespace swift {
|
||||
/// Allow throwing call expressions without annotation with 'try'.
|
||||
bool EnableThrowWithoutTry = false;
|
||||
|
||||
/// Turn all throw sites into immediate traps.
|
||||
bool ThrowsAsTraps = false;
|
||||
|
||||
/// If set, inserts instrumentation useful for testing the debugger.
|
||||
bool DebuggerTestingTransform = false;
|
||||
|
||||
|
||||
@@ -802,6 +802,9 @@ def enable_source_import : Flag<["-"], "enable-source-import">,
|
||||
def enable_throw_without_try : Flag<["-"], "enable-throw-without-try">,
|
||||
HelpText<"Allow throwing function calls without 'try'">;
|
||||
|
||||
def throws_as_traps : Flag<["-"], "throws-as-traps">,
|
||||
HelpText<"Turn all throw sites into immediate traps">;
|
||||
|
||||
def import_module : Separate<["-"], "import-module">,
|
||||
HelpText<"Implicitly import the specified module">;
|
||||
|
||||
|
||||
@@ -720,6 +720,8 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
|
||||
|
||||
Opts.EnableThrowWithoutTry |= Args.hasArg(OPT_enable_throw_without_try);
|
||||
|
||||
Opts.ThrowsAsTraps |= Args.hasArg(OPT_throws_as_traps);
|
||||
|
||||
if (auto A = Args.getLastArg(OPT_enable_objc_attr_requires_foundation_module,
|
||||
OPT_disable_objc_attr_requires_foundation_module)) {
|
||||
Opts.EnableObjCAttrRequiresFoundation
|
||||
|
||||
@@ -761,6 +761,13 @@ void StmtEmitter::visitReturnStmt(ReturnStmt *S) {
|
||||
}
|
||||
|
||||
void StmtEmitter::visitThrowStmt(ThrowStmt *S) {
|
||||
if (SGF.getASTContext().LangOpts.ThrowsAsTraps) {
|
||||
SGF.B.createBuiltin(S, SGF.SGM.getASTContext().getIdentifier("int_trap"),
|
||||
SGF.SGM.Types.getEmptyTupleType(), {}, {});
|
||||
SGF.B.createUnreachable(S);
|
||||
return;
|
||||
}
|
||||
|
||||
ManagedValue exn = SGF.emitRValueAsSingleValue(S->getSubExpr());
|
||||
SGF.emitThrow(S, exn, /* emit a call to willThrow */ true);
|
||||
}
|
||||
@@ -1475,6 +1482,13 @@ void SILGenFunction::emitThrow(SILLocation loc, ManagedValue exnMV,
|
||||
assert(ThrowDest.isValid() &&
|
||||
"calling emitThrow with invalid throw destination!");
|
||||
|
||||
if (getASTContext().LangOpts.ThrowsAsTraps) {
|
||||
B.createBuiltin(loc, SGM.getASTContext().getIdentifier("int_trap"),
|
||||
SGM.Types.getEmptyTupleType(), {}, {});
|
||||
B.createUnreachable(loc);
|
||||
return;
|
||||
}
|
||||
|
||||
// Claim the exception value. If we need to handle throwing
|
||||
// cleanups, the correct thing to do here is to recreate the
|
||||
// exception's cleanup when emitting each cleanup we branch through.
|
||||
|
||||
40
test/embedded/throw-trap.swift
Normal file
40
test/embedded/throw-trap.swift
Normal file
@@ -0,0 +1,40 @@
|
||||
// RUN: not %target-swift-frontend -emit-ir %s -enable-experimental-feature Embedded 2>&1 | %FileCheck %s --check-prefix CHECK-EXISTENTIALS
|
||||
// RUN: %target-swift-frontend -emit-sil %s -enable-experimental-feature Embedded -throws-as-traps | %FileCheck %s --check-prefix CHECK-TRAPS-SIL
|
||||
// RUN: %target-swift-frontend -emit-ir %s -enable-experimental-feature Embedded -throws-as-traps | %FileCheck %s --check-prefix CHECK-TRAPS-IR
|
||||
|
||||
// REQUIRES: VENDOR=apple
|
||||
// REQUIRES: OS=macosx
|
||||
|
||||
enum MyError : Error {
|
||||
case a
|
||||
}
|
||||
|
||||
public func throwing1() throws -> Int {
|
||||
throw MyError.a
|
||||
}
|
||||
|
||||
public func catching1() {
|
||||
do {
|
||||
try throwing1()
|
||||
} catch let e as MyError {
|
||||
_ = e
|
||||
} catch {
|
||||
_ = error
|
||||
}
|
||||
}
|
||||
|
||||
// CHECK-EXISTENTIALS: error: existential can cause metadata allocation or locks
|
||||
|
||||
// CHECK-TRAPS-SIL: sil @$s4main9throwing1SiyKF : $@convention(thin) () -> (Int, @error any Error) {
|
||||
// CHECK-TRAPS-SIL-NEXT: bb0:
|
||||
// CHECK-TRAPS-SIL-NEXT: debug_value
|
||||
// CHECK-TRAPS-SIL-NEXT: %1 = builtin "int_trap"
|
||||
// CHECK-TRAPS-SIL-NEXT: unreachable
|
||||
// CHECK-TRAPS-SIL-NEXT: }
|
||||
|
||||
|
||||
// CHECK-TRAPS-IR: define {{.*}}@"$s4main9throwing1SiyKF"{{.*}}{
|
||||
// CHECK-TRAPS-IR-NEXT: entry:
|
||||
// CHECK-TRAPS-IR-NEXT: call void @llvm.trap()
|
||||
// CHECK-TRAPS-IR-NEXT: unreachable
|
||||
// CHECK-TRAPS-IR-NEXT: }
|
||||
Reference in New Issue
Block a user