Skip to content

Commit b9b568b

Browse files
authored
Merge pull request #19565 from moiseev/try-bang-location-info
try! error message should report the right location
2 parents cad921d + 7c7a976 commit b9b568b

File tree

8 files changed

+58
-16
lines changed

8 files changed

+58
-16
lines changed

include/swift/AST/KnownDecls.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ FUNC_DECL(BridgeAnyObjectToAny,
6262

6363
FUNC_DECL(ConvertToAnyHashable, "_convertToAnyHashable")
6464

65+
FUNC_DECL(DiagnoseUnexpectedError, "_unexpectedError")
6566
FUNC_DECL(DiagnoseUnexpectedNilOptional, "_diagnoseUnexpectedNilOptional")
6667
FUNC_DECL(DiagnoseUnexpectedEnumCase, "_diagnoseUnexpectedEnumCase")
6768
FUNC_DECL(DiagnoseUnexpectedEnumCaseValue, "_diagnoseUnexpectedEnumCaseValue")

lib/SILGen/SILGenExpr.cpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,7 +1269,7 @@ manageBufferForExprResult(SILValue buffer, const TypeLowering &bufferTL,
12691269
}
12701270

12711271
SILGenFunction::ForceTryEmission::ForceTryEmission(SILGenFunction &SGF,
1272-
Expr *loc)
1272+
ForceTryExpr *loc)
12731273
: SGF(SGF), Loc(loc), OldThrowDest(SGF.ThrowDest) {
12741274
assert(loc && "cannot pass a null location");
12751275

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

1295-
ASTContext &ctx = SGF.getASTContext();
1296-
auto error = catchBB->createPhiArgument(SILType::getExceptionType(ctx),
1297-
ValueOwnershipKind::Owned);
1298-
SGF.B.createBuiltin(Loc, ctx.getIdentifier("unexpectedError"),
1299-
SGF.SGM.Types.getEmptyTupleType(), {}, {error});
1295+
if (auto diagnoseError = SGF.getASTContext().getDiagnoseUnexpectedError(nullptr)) {
1296+
ASTContext &ctx = SGF.getASTContext();
1297+
auto error = SGF.B.createOwnedPhiArgument(SILType::getExceptionType(ctx));
1298+
auto args = SGF.emitSourceLocationArgs(Loc->getExclaimLoc(), Loc);
1299+
1300+
SGF.emitApplyOfLibraryIntrinsic(
1301+
Loc,
1302+
diagnoseError,
1303+
SubstitutionMap(),
1304+
{
1305+
error,
1306+
args.filenameStartPointer,
1307+
args.filenameLength,
1308+
args.filenameIsAscii,
1309+
args.line
1310+
},
1311+
SGFContext());
1312+
}
13001313
SGF.B.createUnreachable(Loc);
13011314
}
13021315

lib/SILGen/SILGenFunction.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1909,11 +1909,11 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
19091909

19101910
class ForceTryEmission {
19111911
SILGenFunction &SGF;
1912-
Expr *Loc;
1912+
ForceTryExpr *Loc;
19131913
JumpDest OldThrowDest;
19141914

19151915
public:
1916-
ForceTryEmission(SILGenFunction &SGF, Expr *loc);
1916+
ForceTryEmission(SILGenFunction &SGF, ForceTryExpr *loc);
19171917

19181918
ForceTryEmission(const ForceTryEmission &) = delete;
19191919
ForceTryEmission &operator=(const ForceTryEmission &) = delete;

stdlib/public/core/ErrorType.swift

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,20 @@ public func _bridgeErrorToNSError(_ error: __owned Error) -> AnyObject
178178
/// Invoked by the compiler when the subexpression of a `try!` expression
179179
/// throws an error.
180180
@_silgen_name("swift_unexpectedError")
181-
public func _unexpectedError(_ error: Error) {
182-
preconditionFailure("'try!' expression unexpectedly raised an error: \(String(reflecting: error))")
181+
public func _unexpectedError(
182+
_ error: __owned Error,
183+
filenameStart: Builtin.RawPointer,
184+
filenameLength: Builtin.Word,
185+
filenameIsASCII: Builtin.Int1,
186+
line: Builtin.Word
187+
) {
188+
preconditionFailure(
189+
"'try!' expression unexpectedly raised an error: \(String(reflecting: error))",
190+
file: StaticString(
191+
_start: filenameStart,
192+
utf8CodeUnitCount: filenameLength,
193+
isASCII: filenameIsASCII),
194+
line: UInt(line))
183195
}
184196

185197
/// Invoked by the compiler when code at top level throws an uncaught error.

stdlib/public/runtime/ErrorObject.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,11 @@ void swift_willThrow(SWIFT_CONTEXT void *unused,
214214
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_API LLVM_ATTRIBUTE_NORETURN
215215
void swift_errorInMain(SwiftError *object);
216216
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_API LLVM_ATTRIBUTE_NORETURN
217-
void swift_unexpectedError(SwiftError *object);
217+
void swift_unexpectedError(SwiftError *object,
218+
OpaqueValue *filenameStart,
219+
long filenameLength,
220+
bool isAscii,
221+
unsigned long line);
218222

219223
#if SWIFT_OBJC_INTEROP
220224

test/SILGen/errors.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ func testForceTry(_ fn: () -> Int) {
326326
// CHECK: [[FUNC:%.*]] = function_ref @$s6errors9createIntyySiyXEKF : $@convention(thin) (@noescape @callee_guaranteed () -> Int) -> @error Error
327327
// CHECK: try_apply [[FUNC]]([[ARG]])
328328
// CHECK: return
329-
// CHECK: builtin "unexpectedError"
329+
// CHECK: function_ref @swift_unexpectedError
330330
// CHECK: unreachable
331331

332332
func testForceTryMultiple() {
@@ -346,7 +346,8 @@ func testForceTryMultiple() {
346346
// CHECK-NEXT: [[VOID:%.+]] = tuple ()
347347
// CHECK-NEXT: return [[VOID]] : $()
348348
// CHECK: [[FAILURE:.+]]([[ERROR:%.+]] : @owned $Error):
349-
// CHECK-NEXT: = builtin "unexpectedError"([[ERROR]] : $Error)
349+
// CHECK: [[UNEXPECTED_ERROR:%.+]] = function_ref @swift_unexpectedError
350+
// CHECK-NEXT: apply [[UNEXPECTED_ERROR]]([[ERROR]]
350351
// CHECK-NEXT: unreachable
351352
// CHECK: [[CLEANUPS_1]]([[ERROR:%.+]] : @owned $Error):
352353
// CHECK-NEXT: br [[FAILURE]]([[ERROR]] : $Error)

test/SILOptimizer/stack-nesting-wrong-scope.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
// RUN: -sil-print-only-functions=$s3red19ThrowAddrOnlyStructV016throwsOptionalToG0ACyxGSgSi_tcfC \
44
// RUN: -Xllvm -sil-print-debuginfo -o /dev/null 2>&1 | %FileCheck %s
55

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

1111
protocol Patatino {
1212
init()

test/stdlib/Error.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,18 @@ ErrorTests.test("try!")
115115
.code {
116116
expectCrashLater()
117117
let _: () = try! { throw SillyError.JazzHands }()
118-
}
118+
}
119+
120+
ErrorTests.test("try!/location")
121+
.skip(.custom({ _isFastAssertConfiguration() },
122+
reason: "trap is not guaranteed to happen in -Ounchecked"))
123+
.crashOutputMatches(_isDebugAssertConfiguration()
124+
? "test/stdlib/Error.swift, line 128"
125+
: "")
126+
.code {
127+
expectCrashLater()
128+
let _: () = try! { throw SillyError.JazzHands }()
129+
}
119130

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

0 commit comments

Comments
 (0)