Skip to content

Commit 16b0538

Browse files
committed
Generalize declaration-reference checking in the effects checker
1 parent 37bfa19 commit 16b0538

File tree

1 file changed

+45
-19
lines changed

1 file changed

+45
-19
lines changed

lib/Sema/TypeCheckEffects.cpp

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,11 @@ class EffectsHandlingWalker : public ASTWalker {
576576
} else if (auto lookup = dyn_cast<LookupExpr>(E)) {
577577
recurse = asImpl().checkLookup(lookup);
578578
} else if (auto declRef = dyn_cast<DeclRefExpr>(E)) {
579-
recurse = asImpl().checkDeclRef(declRef);
579+
recurse = asImpl().checkDeclRef(declRef,
580+
declRef->getDeclRef(),
581+
declRef->getLoc(),
582+
declRef->isImplicitlyAsync().has_value(),
583+
declRef->isImplicitlyThrows());
580584
} else if (auto interpolated = dyn_cast<InterpolatedStringLiteralExpr>(E)) {
581585
recurse = asImpl().checkInterpolatedStringLiteral(interpolated);
582586
} else if (auto macroExpansionExpr = dyn_cast<MacroExpansionExpr>(E)) {
@@ -1363,43 +1367,50 @@ class ApplyClassifier {
13631367
return classification;
13641368
}
13651369

1366-
Classification classifyDeclRef(DeclRefExpr *E) {
1367-
if (!E->getDecl())
1370+
Classification classifyDeclRef(ConcreteDeclRef declRef, SourceLoc loc,
1371+
bool isImplicitlyAsync,
1372+
bool isImplicitlyThrows) {
1373+
if (!declRef)
13681374
return Classification::forInvalidCode();
13691375

13701376
Classification classification;
13711377
PotentialEffectReason reason = PotentialEffectReason::forPropertyAccess();
1372-
ConcreteDeclRef declRef = E->getDeclRef();
13731378

13741379
if (auto getter = getEffectfulGetOnlyAccessor(declRef)) {
13751380
reason = getKindOfEffectfulProp(declRef);
13761381
classification = Classification::forDeclRef(
1377-
getter, ConditionalEffectKind::Always, reason, E->getLoc());
1382+
getter, ConditionalEffectKind::Always, reason, loc);
13781383
} else if (isa<VarDecl>(declRef.getDecl())) {
13791384
// Handle async let.
13801385
reason = PotentialEffectReason::forAsyncLet();
13811386
classification = Classification::forDeclRef(
1382-
declRef, ConditionalEffectKind::Always, reason, E->getLoc());
1387+
declRef, ConditionalEffectKind::Always, reason, loc);
13831388
}
13841389

13851390
classification.setDowngradeToWarning(
1386-
downgradeAsyncAccessToWarning(E->getDecl()));
1391+
downgradeAsyncAccessToWarning(declRef.getDecl()));
13871392

13881393
classification.mergeImplicitEffects(
1389-
E->getDeclRef().getDecl()->getASTContext(),
1390-
E->isImplicitlyAsync().has_value(), E->isImplicitlyThrows(),
1394+
declRef.getDecl()->getASTContext(),
1395+
isImplicitlyAsync, isImplicitlyThrows,
13911396
reason);
13921397

13931398
if (!classification.hasUnsafe()) {
13941399
classification.merge(
13951400
Classification::forDeclRef(
1396-
declRef, ConditionalEffectKind::Always, reason, E->getLoc(),
1401+
declRef, ConditionalEffectKind::Always, reason, loc,
13971402
EffectKind::Unsafe));
13981403
}
13991404

14001405
return classification;
14011406
}
14021407

1408+
Classification classifyDeclRef(DeclRefExpr *E) {
1409+
return classifyDeclRef(
1410+
E->getDeclRef(), E->getLoc(), E->isImplicitlyAsync().has_value(),
1411+
E->isImplicitlyThrows());
1412+
}
1413+
14031414
Classification classifyThrow(ThrowStmt *S) {
14041415
Expr *thrownValue = S->getSubExpr();
14051416
if (!thrownValue)
@@ -1860,8 +1871,14 @@ class ApplyClassifier {
18601871
classification.merge(Self.classifyLookup(E).onlyThrowing());
18611872
return ShouldRecurse;
18621873
}
1863-
ShouldRecurse_t checkDeclRef(DeclRefExpr *E) {
1864-
classification.merge(Self.classifyDeclRef(E).onlyThrowing());
1874+
ShouldRecurse_t checkDeclRef(Expr *expr,
1875+
ConcreteDeclRef declRef, SourceLoc loc,
1876+
bool isImplicitlyAsync,
1877+
bool isImplicitlyThrows) {
1878+
classification.merge(
1879+
Self.classifyDeclRef(
1880+
declRef, loc, isImplicitlyAsync, isImplicitlyThrows
1881+
).onlyThrowing());
18651882
return ShouldNotRecurse;
18661883
}
18671884
ShouldRecurse_t checkAsyncLet(PatternBindingDecl *patternBinding) {
@@ -1978,10 +1995,13 @@ class ApplyClassifier {
19781995

19791996
return ShouldRecurse;
19801997
}
1981-
ShouldRecurse_t checkDeclRef(DeclRefExpr *E) {
1982-
if (E->isImplicitlyAsync()) {
1998+
ShouldRecurse_t checkDeclRef(Expr *expr,
1999+
ConcreteDeclRef declRef, SourceLoc loc,
2000+
bool isImplicitlyAsync,
2001+
bool isImplicitlyThrows) {
2002+
if (isImplicitlyAsync) {
19832003
AsyncKind = ConditionalEffectKind::Always;
1984-
} else if (auto getter = getEffectfulGetOnlyAccessor(E->getDeclRef())) {
2004+
} else if (auto getter = getEffectfulGetOnlyAccessor(declRef)) {
19852005
if (cast<AccessorDecl>(getter.getDecl())->hasAsync())
19862006
AsyncKind = ConditionalEffectKind::Always;
19872007
}
@@ -3601,11 +3621,17 @@ class CheckEffectsCoverage : public EffectsHandlingWalker<CheckEffectsCoverage>
36013621
return ShouldRecurse;
36023622
}
36033623

3604-
ShouldRecurse_t checkDeclRef(DeclRefExpr *E) {
3605-
if (auto classification = getApplyClassifier().classifyDeclRef(E)) {
3624+
ShouldRecurse_t checkDeclRef(Expr *expr,
3625+
ConcreteDeclRef declRef, SourceLoc loc,
3626+
bool isImplicitlyAsync,
3627+
bool isImplicitlyThrows) {
3628+
if (auto classification = getApplyClassifier().classifyDeclRef(
3629+
declRef, loc, isImplicitlyAsync, isImplicitlyThrows)) {
36063630
auto throwDest = checkEffectSite(
3607-
E, classification.hasThrows(), classification);
3608-
E->setThrows(throwDest);
3631+
expr, classification.hasThrows(), classification);
3632+
3633+
if (auto declRefExpr = dyn_cast<DeclRefExpr>(expr))
3634+
declRefExpr->setThrows(throwDest);
36093635
}
36103636

36113637
return ShouldNotRecurse;

0 commit comments

Comments
 (0)