Skip to content

Commit 1e27f21

Browse files
authored
Merge pull request #21727 from slavapestov/error-handling-interpolation-crash
Sema: Fix crashes when a call of a closure value is missing a 'try'
2 parents 5282e92 + f34d202 commit 1e27f21

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

lib/Sema/TypeCheckError.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "swift/AST/DiagnosticsSema.h"
2121
#include "swift/AST/Initializer.h"
2222
#include "swift/AST/Pattern.h"
23+
#include "swift/AST/PrettyStackTrace.h"
2324

2425
using namespace swift;
2526

@@ -902,7 +903,7 @@ class Context {
902903
Kind TheKind;
903904
bool DiagnoseErrorOnTry = false;
904905
DeclContext *RethrowsDC = nullptr;
905-
InterpolatedStringLiteralExpr * InterpolatedString;
906+
InterpolatedStringLiteralExpr *InterpolatedString = nullptr;
906907

907908
explicit Context(Kind kind) : TheKind(kind) {}
908909

@@ -1060,7 +1061,9 @@ class Context {
10601061
insertLoc = loc;
10611062
highlight = e->getSourceRange();
10621063

1063-
if (InterpolatedString && e->getCalledValue()->getBaseName() ==
1064+
if (InterpolatedString &&
1065+
e->getCalledValue() &&
1066+
e->getCalledValue()->getBaseName() ==
10641067
TC.Context.Id_appendInterpolation) {
10651068
message = diag::throwing_interpolation_without_try;
10661069
insertLoc = InterpolatedString->getLoc();
@@ -1648,6 +1651,10 @@ void TypeChecker::checkFunctionErrorHandling(AbstractFunctionDecl *fn) {
16481651
// by the time we got here.
16491652
if (!fn->hasInterfaceType()) return;
16501653

1654+
#ifndef NDEBUG
1655+
PrettyStackTraceDecl debugStack("checking error handling for", fn);
1656+
#endif
1657+
16511658
CheckErrorCoverage checker(*this, Context::forFunction(fn));
16521659

16531660
// If this is a debugger function, suppress 'try' marking at the top level.

test/decl/func/throwing_functions.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,3 +258,23 @@ struct IllegalContext {
258258
}
259259
}
260260
}
261+
262+
// Crash in 'uncovered try' diagnostic when calling a function value - rdar://46973064
263+
struct FunctionHolder {
264+
let fn: () throws -> ()
265+
func receive() {
266+
do {
267+
_ = fn()
268+
// expected-error@-1 {{call can throw but is not marked with 'try'}}
269+
// expected-note@-2 {{did you mean to use 'try'?}}
270+
// expected-note@-3 {{did you mean to handle error as optional value?}}
271+
// expected-note@-4 {{did you mean to disable error propagation?}}
272+
_ = "\(fn())"
273+
// expected-error@-1 {{call can throw but is not marked with 'try'}}
274+
// expected-note@-2 {{did you mean to use 'try'?}}
275+
// expected-note@-3 {{did you mean to handle error as optional value?}}
276+
// expected-note@-4 {{did you mean to disable error propagation?}}
277+
} catch {}
278+
}
279+
}
280+

0 commit comments

Comments
 (0)