Skip to content

Commit f34d202

Browse files
committed
Sema: Fix crashes when a call of a closure value is missing a 'try'
Two problems here: - The InterpolatedString instance variable was not always initialized before being checked for null - In the non-null case, we were assuming the result of CallExpr::getCalledValue() was non-null, but it's null if the callee is not a function declaration Fixes <rdar://problem/46973064>.
1 parent ea64aa3 commit f34d202

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

lib/Sema/TypeCheckError.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,7 @@ class Context {
903903
Kind TheKind;
904904
bool DiagnoseErrorOnTry = false;
905905
DeclContext *RethrowsDC = nullptr;
906-
InterpolatedStringLiteralExpr * InterpolatedString;
906+
InterpolatedStringLiteralExpr *InterpolatedString = nullptr;
907907

908908
explicit Context(Kind kind) : TheKind(kind) {}
909909

@@ -1061,7 +1061,9 @@ class Context {
10611061
insertLoc = loc;
10621062
highlight = e->getSourceRange();
10631063

1064-
if (InterpolatedString && e->getCalledValue()->getBaseName() ==
1064+
if (InterpolatedString &&
1065+
e->getCalledValue() &&
1066+
e->getCalledValue()->getBaseName() ==
10651067
TC.Context.Id_appendInterpolation) {
10661068
message = diag::throwing_interpolation_without_try;
10671069
insertLoc = InterpolatedString->getLoc();

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)