Skip to content

Commit fdbaedc

Browse files
committed
Plumb await location through to diagnostics
We want to be able to put the `try` before the `await`, so we need to make it available where we emit the diagnostics and fix-its. The easiest way to do that was through the context object.
1 parent 21fb139 commit fdbaedc

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

lib/Sema/TypeCheckEffects.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1424,6 +1424,9 @@ class Context {
14241424
DiagnoseErrorOnTry = b;
14251425
}
14261426

1427+
/// Stores the location of the innermost await
1428+
SourceLoc awaitLoc = SourceLoc();
1429+
14271430
/// Whether this is a function that rethrows.
14281431
bool hasPolymorphicEffect(EffectKind kind) const {
14291432
if (!Function)
@@ -1683,7 +1686,7 @@ class Context {
16831686
auto loc = E.getStartLoc();
16841687
SourceLoc insertLoc;
16851688
SourceRange highlight;
1686-
1689+
16871690
// Generate more specific messages in some cases.
16881691
if (auto e = dyn_cast_or_null<ApplyExpr>(E.dyn_cast<Expr*>())) {
16891692
if (isa<PrefixUnaryExpr>(e) || isa<PostfixUnaryExpr>(e) ||
@@ -1693,7 +1696,7 @@ class Context {
16931696
}
16941697
insertLoc = loc;
16951698
highlight = e->getSourceRange();
1696-
1699+
16971700
if (InterpolatedString &&
16981701
e->getCalledValue() &&
16991702
e->getCalledValue()->getBaseName() ==
@@ -1702,7 +1705,7 @@ class Context {
17021705
insertLoc = InterpolatedString->getLoc();
17031706
}
17041707
}
1705-
1708+
17061709
Diags.diagnose(loc, message).highlight(highlight);
17071710
maybeAddRethrowsNote(Diags, loc, reason);
17081711

@@ -2115,14 +2118,16 @@ class CheckEffectsCoverage : public EffectsHandlingWalker<CheckEffectsCoverage>
21152118
DeclContext *OldReasyncDC;
21162119
ContextFlags OldFlags;
21172120
ConditionalEffectKind OldMaxThrowingKind;
2121+
SourceLoc OldAwaitLoc;
21182122

21192123
public:
21202124
ContextScope(CheckEffectsCoverage &self, Optional<Context> newContext)
21212125
: Self(self), OldContext(self.CurContext),
21222126
OldRethrowsDC(self.RethrowsDC),
21232127
OldReasyncDC(self.ReasyncDC),
21242128
OldFlags(self.Flags),
2125-
OldMaxThrowingKind(self.MaxThrowingKind) {
2129+
OldMaxThrowingKind(self.MaxThrowingKind),
2130+
OldAwaitLoc(self.CurContext.awaitLoc) {
21262131
if (newContext) self.CurContext = *newContext;
21272132
}
21282133

@@ -2140,9 +2145,10 @@ class CheckEffectsCoverage : public EffectsHandlingWalker<CheckEffectsCoverage>
21402145
Self.Flags.clear(ContextFlags::HasTryThrowSite);
21412146
}
21422147

2143-
void enterAwait() {
2148+
void enterAwait(SourceLoc awaitLoc) {
21442149
Self.Flags.set(ContextFlags::IsAsyncCovered);
21452150
Self.Flags.clear(ContextFlags::HasAnyAsyncSite);
2151+
Self.CurContext.awaitLoc = awaitLoc;
21462152
}
21472153

21482154
void enterAsyncLet() {
@@ -2239,6 +2245,7 @@ class CheckEffectsCoverage : public EffectsHandlingWalker<CheckEffectsCoverage>
22392245
Self.ReasyncDC = OldReasyncDC;
22402246
Self.Flags = OldFlags;
22412247
Self.MaxThrowingKind = OldMaxThrowingKind;
2248+
Self.CurContext.awaitLoc = OldAwaitLoc;
22422249
}
22432250
};
22442251

@@ -2647,12 +2654,13 @@ class CheckEffectsCoverage : public EffectsHandlingWalker<CheckEffectsCoverage>
26472654
break;
26482655
}
26492656
}
2657+
26502658
ShouldRecurse_t checkAwait(AwaitExpr *E) {
26512659

26522660
// Walk the operand.
26532661
ContextScope scope(*this, None);
2654-
scope.enterAwait();
2655-
2662+
scope.enterAwait(E->getAwaitLoc());
2663+
26562664
E->getSubExpr()->walk(*this);
26572665

26582666
// Warn about 'await' expressions that weren't actually needed, unless of
@@ -2665,7 +2673,7 @@ class CheckEffectsCoverage : public EffectsHandlingWalker<CheckEffectsCoverage>
26652673
CurContext.diagnoseUnhandledAsyncSite(Ctx.Diags, E, None,
26662674
/*forAwait=*/ true);
26672675
}
2668-
2676+
26692677
// Inform the parent of the walk that an 'await' exists here.
26702678
scope.preserveCoverageFromAwaitOperand();
26712679
return ShouldNotRecurse;

0 commit comments

Comments
 (0)