Skip to content

Commit e4b8a82

Browse files
committed
Remove properties from AST nodes
1 parent 35e028e commit e4b8a82

File tree

3 files changed

+24
-57
lines changed

3 files changed

+24
-57
lines changed

include/swift/AST/Expr.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,6 @@ class DeclRefExpr : public Expr {
11691169
ConcreteDeclRef D;
11701170
DeclNameLoc Loc;
11711171
ActorIsolation implicitActorHopTarget;
1172-
ValueDecl *DeclForUsageAnalysis = nullptr;
11731172

11741173
public:
11751174
DeclRefExpr(ConcreteDeclRef D, DeclNameLoc Loc, bool Implicit,
@@ -1248,20 +1247,6 @@ class DeclRefExpr : public Expr {
12481247
Bits.DeclRefExpr.FunctionRefKind = static_cast<unsigned>(refKind);
12491248
}
12501249

1251-
/// The decl that should be used for usage analysis of this DRE.
1252-
/// This is potentially different from `getDecl()`, to handle
1253-
/// cases where performing usage analysis on the actual decl
1254-
/// causes false-positive warnings.
1255-
ValueDecl *getDeclForUsageAnalysis() {
1256-
if (DeclForUsageAnalysis) {
1257-
return DeclForUsageAnalysis;
1258-
}
1259-
1260-
return getDecl();
1261-
}
1262-
1263-
void setDeclForUsageAnalysis(ValueDecl *vd) { DeclForUsageAnalysis = vd; }
1264-
12651250
static bool classof(const Expr *E) {
12661251
return E->getKind() == ExprKind::DeclRef;
12671252
}

include/swift/AST/Pattern.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,6 @@ class OptionalSomePattern : public Pattern {
608608
Pattern *SubPattern;
609609
SourceLoc QuestionLoc;
610610
EnumElementDecl *ElementDecl = nullptr;
611-
bool EnablesImplicitSelfForWeakSelfCapture = false;
612611

613612
public:
614613
explicit OptionalSomePattern(Pattern *SubPattern,
@@ -628,17 +627,6 @@ class OptionalSomePattern : public Pattern {
628627
EnumElementDecl *getElementDecl() const { return ElementDecl; }
629628
void setElementDecl(EnumElementDecl *d) { ElementDecl = d; }
630629

631-
/// Whether or not this pattern is used to enable implicit self
632-
/// for weak self captures in the following scope. This is used
633-
/// in Swift 5 language modes to prevent a false-positive
634-
/// "unused value" warning, and is not necessary in Swift 6+.
635-
bool getEnablesImplicitSelfForWeakSelfCapture() {
636-
return EnablesImplicitSelfForWeakSelfCapture;
637-
}
638-
void setEnablesImplicitSelfForWeakSelfCapture(bool value) {
639-
EnablesImplicitSelfForWeakSelfCapture = value;
640-
}
641-
642630
static bool classof(const Pattern *P) {
643631
return P->getKind() == PatternKind::OptionalSome;
644632
}

lib/Sema/MiscDiagnostics.cpp

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,6 +1526,15 @@ static void diagRecursivePropertyAccess(const Expr *E, const DeclContext *DC) {
15261526
const_cast<Expr *>(E)->walk(walker);
15271527
}
15281528

1529+
namespace DiagnosticsContext {
1530+
/// A mapping of replacement `ValueDecl`s to use in `VarDeclUsageAnalysis`
1531+
/// instead of the actual `ValueDecl` of the associated `DeclRefExpr`.
1532+
/// This lets `VarDeclUsageAnalysis` avoid emitting false-positive warnings
1533+
/// in certain circumstances.
1534+
static llvm::SmallDenseMap<DeclRefExpr *, ValueDecl *>
1535+
DeclMappingForUsageAnalysis;
1536+
}
1537+
15291538
/// Look for any property references in closures that lack a 'self.' qualifier.
15301539
/// Within a closure, we require that the source code contain 'self.' explicitly
15311540
/// (or that the closure explicitly capture 'self' in the capture list) because
@@ -1650,7 +1659,7 @@ static void diagnoseImplicitSelfUseInClosure(const Expr *E,
16501659
// When `VarDeclUsageChecker` checks this DRE, we need to use
16511660
// the base we looked up here instead, since otherwise
16521661
// we'll emit confusing false-positive warnings.
1653-
DRE->setDeclForUsageAnalysis(lookupResult);
1662+
DiagnosticsContext::DeclMappingForUsageAnalysis[DRE] = lookupResult;
16541663

16551664
selfDecl = lookupResult;
16561665
}
@@ -1677,17 +1686,7 @@ static void diagnoseImplicitSelfUseInClosure(const Expr *E,
16771686

16781687
if (auto LE = dyn_cast<LoadExpr>(cond.getInitializer())) {
16791688
if (auto selfDRE = dyn_cast<DeclRefExpr>(LE->getSubExpr())) {
1680-
if (selfDRE->getDecl()->getName().isSimpleName(Ctx.Id_self)) {
1681-
// Mark this `OptionalStatementPattern` as enabling implicit
1682-
// self for this weak self capture. This lets us avoid emitting
1683-
// a false-positive warning in `VarDeclUsageChecker` in
1684-
// Swift 5 mode (this is unnecessary in Swift 6).
1685-
if (!Ctx.LangOpts.isSwiftVersionAtLeast(6)) {
1686-
OSP->setEnablesImplicitSelfForWeakSelfCapture(true);
1687-
}
1688-
1689-
return true;
1690-
}
1689+
return selfDRE->getDecl()->getName().isSimpleName(Ctx.Id_self);
16911690
}
16921691
}
16931692
}
@@ -2636,7 +2635,15 @@ class VarDeclUsageChecker : public ASTWalker {
26362635

26372636
VarDecls[vd] |= Flag;
26382637
}
2639-
2638+
2639+
ValueDecl *getDecl(DeclRefExpr *DRE) {
2640+
if (auto decl = DiagnosticsContext::DeclMappingForUsageAnalysis[DRE]) {
2641+
return decl;
2642+
}
2643+
2644+
return DRE->getDecl();
2645+
}
2646+
26402647
void markBaseOfStorageUse(Expr *E, ConcreteDeclRef decl, unsigned flags);
26412648
void markBaseOfStorageUse(Expr *E, bool isMutating);
26422649

@@ -3318,19 +3325,6 @@ VarDeclUsageChecker::~VarDeclUsageChecker() {
33183325
if (initExpr->getStartLoc().isValid()) {
33193326
unsigned noParens = initExpr->canAppendPostfixExpression();
33203327

3321-
// Don't emit an "unused value" warning if this is a
3322-
// `let self = self` condition being used to enable
3323-
// implicit self for the remainder of this scope,
3324-
// since it would be a false positive. This is only
3325-
// necessary in Swift 5 mode, because in Swift 6
3326-
// this new self decl is correctly used as the base of
3327-
// implicit self calls for the remainder of the scope.
3328-
auto &ctx = var->getASTContext();
3329-
if (!ctx.LangOpts.isSwiftVersionAtLeast(6) &&
3330-
OSP->getEnablesImplicitSelfForWeakSelfCapture()) {
3331-
continue;
3332-
}
3333-
33343328
// If the subexpr is an "as?" cast, we can rewrite it to
33353329
// be an "is" test.
33363330
ConditionalCheckedCastExpr *CCE = nullptr;
@@ -3549,7 +3543,7 @@ void VarDeclUsageChecker::markStoredOrInOutExpr(Expr *E, unsigned Flags) {
35493543

35503544
// If we found a decl that is being assigned to, then mark it.
35513545
if (auto *DRE = dyn_cast<DeclRefExpr>(E)) {
3552-
addMark(DRE->getDeclForUsageAnalysis(), Flags);
3546+
addMark(getDecl(DRE), Flags);
35533547
return;
35543548
}
35553549

@@ -3634,10 +3628,10 @@ std::pair<bool, Expr *> VarDeclUsageChecker::walkToExprPre(Expr *E) {
36343628
// If this is a DeclRefExpr found in a random place, it is a load of the
36353629
// vardecl.
36363630
if (auto *DRE = dyn_cast<DeclRefExpr>(E)) {
3637-
addMark(DRE->getDeclForUsageAnalysis(), RK_Read);
3631+
addMark(getDecl(DRE), RK_Read);
36383632

36393633
// If the Expression is a read of a getter, track for diagnostics
3640-
if (auto VD = dyn_cast<VarDecl>(DRE->getDeclForUsageAnalysis())) {
3634+
if (auto VD = dyn_cast<VarDecl>(getDecl(DRE))) {
36413635
AssociatedGetterRefExpr.insert(std::make_pair(VD, DRE));
36423636
}
36433637
}
@@ -3711,7 +3705,7 @@ void VarDeclUsageChecker::handleIfConfig(IfConfigDecl *ICD) {
37113705
// conservatively mark it read and written. This will silence "variable
37123706
// unused" and "could be marked let" warnings for it.
37133707
if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3714-
VDUC.addMark(DRE->getDeclForUsageAnalysis(), RK_Read | RK_Written);
3708+
VDUC.addMark(VDUC.getDecl(DRE), RK_Read | RK_Written);
37153709
else if (auto *declRef = dyn_cast<UnresolvedDeclRefExpr>(E)) {
37163710
auto name = declRef->getName();
37173711
auto loc = declRef->getLoc();

0 commit comments

Comments
 (0)