Skip to content

Commit 2ae0c80

Browse files
authored
Merge pull request swiftlang#34340 from slavapestov/fix-effect-check-context-state
Sema: Fix failure to diagnose throwing expressions inside string interpolations
2 parents 907b069 + 5d6cf5c commit 2ae0c80

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

lib/Sema/TypeCheckEffects.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,13 @@ class Context {
878878
HandlesErrors(handlesErrors), HandlesAsync(handlesAsync) { }
879879

880880
public:
881+
bool shouldDiagnoseErrorOnTry() const {
882+
return DiagnoseErrorOnTry;
883+
}
884+
void setDiagnoseErrorOnTry(bool b) {
885+
DiagnoseErrorOnTry = b;
886+
}
887+
881888
/// Whether this is a function that rethrows.
882889
bool isRethrows() const {
883890
if (!HandlesErrors)
@@ -1375,6 +1382,7 @@ class CheckEffectsCoverage : public EffectsHandlingWalker<CheckEffectsCoverage>
13751382
DeclContext *OldRethrowsDC;
13761383
ContextFlags OldFlags;
13771384
ThrowingKind OldMaxThrowingKind;
1385+
13781386
public:
13791387
ContextScope(CheckEffectsCoverage &self, Optional<Context> newContext)
13801388
: Self(self), OldContext(self.CurContext),
@@ -1468,7 +1476,15 @@ class CheckEffectsCoverage : public EffectsHandlingWalker<CheckEffectsCoverage>
14681476
}
14691477

14701478
~ContextScope() {
1479+
// The "DiagnoseErrorOnTry" flag is a bit of mutable state
1480+
// in the Context itself, used to postpone diagnostic emission
1481+
// to a parent "try" expression. If something was diagnosed
1482+
// during this ContextScope, the flag may have been set, and
1483+
// we need to preseve its value when restoring the old Context.
1484+
bool DiagnoseErrorOnTry = Self.CurContext.shouldDiagnoseErrorOnTry();
14711485
Self.CurContext = OldContext;
1486+
Self.CurContext.setDiagnoseErrorOnTry(DiagnoseErrorOnTry);
1487+
14721488
Self.RethrowsDC = OldRethrowsDC;
14731489
Self.Flags = OldFlags;
14741490
Self.MaxThrowingKind = OldMaxThrowingKind;

test/expr/unary/async_await.swift

+9
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,12 @@ func testStringInterpolation() async throws {
133133
_ = "Eventually produces \(await getInt())"
134134
_ = await "Eventually produces \(getInt())"
135135
}
136+
137+
// Make sure try await works too
138+
func invalidAsyncFunction() async {
139+
_ = try await throwingAndAsync() // expected-error {{errors thrown from here are not handled}}
140+
}
141+
142+
func validAsyncFunction() async throws {
143+
_ = try await throwingAndAsync()
144+
}

test/stmt/errors.swift

+13
Original file line numberDiff line numberDiff line change
@@ -251,3 +251,16 @@ func sr_11402_func2(_ x: SR_11402_P) {
251251
print(y)
252252
}
253253
}
254+
255+
// https://bugs.swift.org/browse/SR-13654
256+
257+
func sr_13654_func() throws -> String {}
258+
259+
func sr_13654_invalid_interpolation() {
260+
_ = try "\(sr_13654_func())" // expected-error {{errors thrown from here are not handled}}
261+
_ = "\(try sr_13654_func())" // expected-error {{errors thrown from here are not handled}}
262+
}
263+
func sr_13654_valid_interpolation() throws {
264+
_ = try "\(sr_13654_func())"
265+
_ = "\(try sr_13654_func())"
266+
}

0 commit comments

Comments
 (0)