Skip to content

try! error message should report the right location #19565

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/swift/AST/KnownDecls.def
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ FUNC_DECL(BridgeAnyObjectToAny,

FUNC_DECL(ConvertToAnyHashable, "_convertToAnyHashable")

FUNC_DECL(DiagnoseUnexpectedError, "_unexpectedError")
FUNC_DECL(DiagnoseUnexpectedNilOptional, "_diagnoseUnexpectedNilOptional")
FUNC_DECL(DiagnoseUnexpectedEnumCase, "_diagnoseUnexpectedEnumCase")
FUNC_DECL(DiagnoseUnexpectedEnumCaseValue, "_diagnoseUnexpectedEnumCaseValue")
Expand Down
25 changes: 19 additions & 6 deletions lib/SILGen/SILGenExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1269,7 +1269,7 @@ manageBufferForExprResult(SILValue buffer, const TypeLowering &bufferTL,
}

SILGenFunction::ForceTryEmission::ForceTryEmission(SILGenFunction &SGF,
Expr *loc)
ForceTryExpr *loc)
: SGF(SGF), Loc(loc), OldThrowDest(SGF.ThrowDest) {
assert(loc && "cannot pass a null location");

Expand All @@ -1292,11 +1292,24 @@ void SILGenFunction::ForceTryEmission::finish() {
// Otherwise, we need to emit it.
SILGenSavedInsertionPoint scope(SGF, catchBB, FunctionSection::Postmatter);

ASTContext &ctx = SGF.getASTContext();
auto error = catchBB->createPhiArgument(SILType::getExceptionType(ctx),
ValueOwnershipKind::Owned);
SGF.B.createBuiltin(Loc, ctx.getIdentifier("unexpectedError"),
SGF.SGM.Types.getEmptyTupleType(), {}, {error});
if (auto diagnoseError = SGF.getASTContext().getDiagnoseUnexpectedError(nullptr)) {
ASTContext &ctx = SGF.getASTContext();
auto error = SGF.B.createOwnedPhiArgument(SILType::getExceptionType(ctx));
auto args = SGF.emitSourceLocationArgs(Loc->getExclaimLoc(), Loc);

SGF.emitApplyOfLibraryIntrinsic(
Loc,
diagnoseError,
SubstitutionMap(),
{
error,
args.filenameStartPointer,
args.filenameLength,
args.filenameIsAscii,
args.line
},
SGFContext());
}
SGF.B.createUnreachable(Loc);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/SILGen/SILGenFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -1903,11 +1903,11 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction

class ForceTryEmission {
SILGenFunction &SGF;
Expr *Loc;
ForceTryExpr *Loc;
JumpDest OldThrowDest;

public:
ForceTryEmission(SILGenFunction &SGF, Expr *loc);
ForceTryEmission(SILGenFunction &SGF, ForceTryExpr *loc);

ForceTryEmission(const ForceTryEmission &) = delete;
ForceTryEmission &operator=(const ForceTryEmission &) = delete;
Expand Down
16 changes: 14 additions & 2 deletions stdlib/public/core/ErrorType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,20 @@ public func _bridgeErrorToNSError(_ error: __owned Error) -> AnyObject
/// Invoked by the compiler when the subexpression of a `try!` expression
/// throws an error.
@_silgen_name("swift_unexpectedError")
public func _unexpectedError(_ error: Error) {
preconditionFailure("'try!' expression unexpectedly raised an error: \(String(reflecting: error))")
public func _unexpectedError(
_ error: __owned Error,
filenameStart: Builtin.RawPointer,
filenameLength: Builtin.Word,
filenameIsASCII: Builtin.Int1,
line: Builtin.Word
) {
preconditionFailure(
"'try!' expression unexpectedly raised an error: \(String(reflecting: error))",
file: StaticString(
_start: filenameStart,
utf8CodeUnitCount: filenameLength,
isASCII: filenameIsASCII),
line: UInt(line))
}

/// Invoked by the compiler when code at top level throws an uncaught error.
Expand Down
6 changes: 5 additions & 1 deletion stdlib/public/runtime/ErrorObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,11 @@ void swift_willThrow(SWIFT_CONTEXT void *unused,
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_API LLVM_ATTRIBUTE_NORETURN
void swift_errorInMain(SwiftError *object);
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_API LLVM_ATTRIBUTE_NORETURN
void swift_unexpectedError(SwiftError *object);
void swift_unexpectedError(SwiftError *object,
OpaqueValue *filenameStart,
long filenameLength,
bool isAscii,
unsigned long line);

#if SWIFT_OBJC_INTEROP

Expand Down
5 changes: 3 additions & 2 deletions test/SILGen/errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ func testForceTry(_ fn: () -> Int) {
// CHECK: [[FUNC:%.*]] = function_ref @$s6errors9createIntyySiyXEKF : $@convention(thin) (@noescape @callee_guaranteed () -> Int) -> @error Error
// CHECK: try_apply [[FUNC]]([[ARG]])
// CHECK: return
// CHECK: builtin "unexpectedError"
// CHECK: function_ref @swift_unexpectedError
// CHECK: unreachable

func testForceTryMultiple() {
Expand All @@ -346,7 +346,8 @@ func testForceTryMultiple() {
// CHECK-NEXT: [[VOID:%.+]] = tuple ()
// CHECK-NEXT: return [[VOID]] : $()
// CHECK: [[FAILURE:.+]]([[ERROR:%.+]] : @owned $Error):
// CHECK-NEXT: = builtin "unexpectedError"([[ERROR]] : $Error)
// CHECK: [[UNEXPECTED_ERROR:%.+]] = function_ref @swift_unexpectedError
// CHECK-NEXT: apply [[UNEXPECTED_ERROR]]([[ERROR]]
// CHECK-NEXT: unreachable
// CHECK: [[CLEANUPS_1]]([[ERROR:%.+]] : @owned $Error):
// CHECK-NEXT: br [[FAILURE]]([[ERROR]] : $Error)
Expand Down
4 changes: 2 additions & 2 deletions test/SILOptimizer/stack-nesting-wrong-scope.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
// RUN: -sil-print-only-functions=$s3red19ThrowAddrOnlyStructV016throwsOptionalToG0ACyxGSgSi_tcfC \
// RUN: -Xllvm -sil-print-debuginfo -o /dev/null 2>&1 | %FileCheck %s

// CHECK: bb5(%27 : @owned $Error):
// CHECK: bb5(%33 : @owned $Error):
// CHECK: dealloc_stack %6 : $*ThrowAddrOnlyStruct<T>, loc {{.*}}:27:68, scope 2
// CHECK: dealloc_stack %3 : $*ThrowAddrOnlyStruct<T>, loc {{.*}}:27:68, scope 2
// CHECK: br bb4(%27 : $Error), loc {{.*}}:27:15, scope 2
// CHECK: br bb4(%33 : $Error), loc {{.*}}:27:15, scope 2

protocol Patatino {
init()
Expand Down
13 changes: 12 additions & 1 deletion test/stdlib/Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,18 @@ ErrorTests.test("try!")
.code {
expectCrashLater()
let _: () = try! { throw SillyError.JazzHands }()
}
}

ErrorTests.test("try!/location")
.skip(.custom({ _isFastAssertConfiguration() },
reason: "trap is not guaranteed to happen in -Ounchecked"))
.crashOutputMatches(_isDebugAssertConfiguration()
? "test/stdlib/Error.swift, line 128"
: "")
.code {
expectCrashLater()
let _: () = try! { throw SillyError.JazzHands }()
}

ErrorTests.test("try?") {
var value = try? { () throws -> Int in return 1 }()
Expand Down