Skip to content

Commit 9c5bd44

Browse files
committed
AST: Push ValueDecl::isSettable() down to AbstractStorageDecl
1 parent d3f65e7 commit 9c5bd44

File tree

4 files changed

+40
-38
lines changed

4 files changed

+40
-38
lines changed

include/swift/AST/Decl.h

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2588,12 +2588,6 @@ class ValueDecl : public Decl {
25882588
void setInterfaceType(Type type);
25892589

25902590
bool hasValidSignature() const;
2591-
2592-
/// isSettable - Determine whether references to this decl may appear
2593-
/// on the left-hand side of an assignment or as the operand of a
2594-
/// `&` or 'inout' operator.
2595-
bool isSettable(const DeclContext *UseDC,
2596-
const DeclRefExpr *base = nullptr) const;
25972591

25982592
/// isInstanceMember - Determine whether this value is an instance member
25992593
/// of an enum or protocol.
@@ -4546,6 +4540,12 @@ class AbstractStorageDecl : public ValueDecl {
45464540
return getImplInfo().supportsMutation();
45474541
}
45484542

4543+
/// isSettable - Determine whether references to this decl may appear
4544+
/// on the left-hand side of an assignment or as the operand of a
4545+
/// `&` or 'inout' operator.
4546+
bool isSettable(const DeclContext *UseDC,
4547+
const DeclRefExpr *base = nullptr) const;
4548+
45494549
/// Are there any accessors for this declaration, including implicit ones?
45504550
bool hasAnyAccessors() const {
45514551
return !getAllAccessors().empty();
@@ -7099,14 +7099,13 @@ class MissingMemberDecl : public Decl {
70997099
}
71007100
};
71017101

7102-
inline bool ValueDecl::isSettable(const DeclContext *UseDC,
7103-
const DeclRefExpr *base) const {
7104-
if (auto vd = dyn_cast<VarDecl>(this)) {
7102+
inline bool AbstractStorageDecl::isSettable(const DeclContext *UseDC,
7103+
const DeclRefExpr *base) const {
7104+
if (auto vd = dyn_cast<VarDecl>(this))
71057105
return vd->isSettable(UseDC, base);
7106-
} else if (auto sd = dyn_cast<SubscriptDecl>(this)) {
7107-
return sd->supportsMutation();
7108-
} else
7109-
return false;
7106+
7107+
auto sd = cast<SubscriptDecl>(this);
7108+
return sd->supportsMutation();
71107109
}
71117110

71127111
inline void

lib/SILGen/SILGenProlog.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ static void emitCaptureArguments(SILGenFunction &SGF,
342342
CapturedValue capture,
343343
uint16_t ArgNo) {
344344

345-
auto *VD = capture.getDecl();
345+
auto *VD = cast<VarDecl>(capture.getDecl());
346346
SILLocation Loc(VD);
347347
Loc.markAsPrologue();
348348

lib/Sema/TypeCheckDeclOverride.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -893,19 +893,20 @@ static void checkOverrideAccessControl(ValueDecl *baseDecl, ValueDecl *decl,
893893
bool shouldDiagnose = !decl->isAccessibleFrom(scopeDC);
894894

895895
bool shouldDiagnoseSetter = false;
896-
if (!shouldDiagnose && baseDecl->isSettable(dc)){
897-
auto matchASD = cast<AbstractStorageDecl>(baseDecl);
898-
if (matchASD->isSetterAccessibleFrom(dc)) {
899-
auto matchSetterAccessScope =
900-
matchASD->getSetterFormalAccessScope(dc);
901-
auto requiredSetterAccessScope =
902-
matchSetterAccessScope.intersectWith(classAccessScope);
903-
auto setterScopeDC = requiredSetterAccessScope->getDeclContext();
904-
905-
const auto *ASD = cast<AbstractStorageDecl>(decl);
906-
shouldDiagnoseSetter =
907-
ASD->isSettable(setterScopeDC) &&
908-
!ASD->isSetterAccessibleFrom(setterScopeDC);
896+
if (auto matchASD = dyn_cast<AbstractStorageDecl>(baseDecl)) {
897+
if (!shouldDiagnose && matchASD->isSettable(dc)){
898+
if (matchASD->isSetterAccessibleFrom(dc)) {
899+
auto matchSetterAccessScope =
900+
matchASD->getSetterFormalAccessScope(dc);
901+
auto requiredSetterAccessScope =
902+
matchSetterAccessScope.intersectWith(classAccessScope);
903+
auto setterScopeDC = requiredSetterAccessScope->getDeclContext();
904+
905+
const auto *ASD = cast<AbstractStorageDecl>(decl);
906+
shouldDiagnoseSetter =
907+
ASD->isSettable(setterScopeDC) &&
908+
!ASD->isSetterAccessibleFrom(setterScopeDC);
909+
}
909910
}
910911
}
911912

@@ -1594,7 +1595,7 @@ static bool checkSingleOverride(ValueDecl *override, ValueDecl *base) {
15941595
// Make sure we're not overriding a settable property with a non-settable
15951596
// one. The only reasonable semantics for this would be to inherit the
15961597
// setter but override the getter, and that would be surprising at best.
1597-
if (baseIsSettable && !override->isSettable(override->getDeclContext())) {
1598+
if (baseIsSettable && !overrideASD->isSettable(override->getDeclContext())) {
15981599
diags.diagnose(overrideASD, diag::override_mutable_with_readonly_property,
15991600
overrideASD->getBaseName().getIdentifier());
16001601
diags.diagnose(baseASD, diag::property_override_here);

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -398,16 +398,16 @@ swift::matchWitness(
398398
return RequirementMatch(witness, MatchKind::StaticNonStaticConflict);
399399

400400
// If the requirement is settable and the witness is not, reject it.
401-
if (req->isSettable(req->getDeclContext()) &&
402-
!witness->isSettable(witness->getDeclContext()))
401+
if (reqASD->isSettable(req->getDeclContext()) &&
402+
!witnessASD->isSettable(witness->getDeclContext()))
403403
return RequirementMatch(witness, MatchKind::SettableConflict);
404404

405405
// Validate that the 'mutating' bit lines up for getters and setters.
406406
if (!reqASD->isGetterMutating() && witnessASD->isGetterMutating())
407407
return RequirementMatch(getStandinForAccessor(witnessASD, AccessorKind::Get),
408408
MatchKind::MutatingConflict);
409409

410-
if (req->isSettable(req->getDeclContext())) {
410+
if (reqASD->isSettable(req->getDeclContext())) {
411411
if (!reqASD->isSetterMutating() && witnessASD->isSetterMutating())
412412
return RequirementMatch(getStandinForAccessor(witnessASD, AccessorKind::Set),
413413
MatchKind::MutatingConflict);
@@ -1197,15 +1197,17 @@ bool WitnessChecker::checkWitnessAccess(ValueDecl *requirement,
11971197
return true;
11981198
}
11991199

1200-
if (requirement->isSettable(DC)) {
1201-
*isSetter = true;
1200+
if (auto *requirementASD = dyn_cast<AbstractStorageDecl>(requirement)) {
1201+
if (requirementASD->isSettable(DC)) {
1202+
*isSetter = true;
12021203

1203-
auto ASD = cast<AbstractStorageDecl>(witness);
1204+
auto witnessASD = cast<AbstractStorageDecl>(witness);
12041205

1205-
// See above about the forConformance flag.
1206-
if (!ASD->isSetterAccessibleFrom(actualScopeToCheck.getDeclContext(),
1207-
/*forConformance=*/true))
1208-
return true;
1206+
// See above about the forConformance flag.
1207+
if (!witnessASD->isSetterAccessibleFrom(actualScopeToCheck.getDeclContext(),
1208+
/*forConformance=*/true))
1209+
return true;
1210+
}
12091211
}
12101212

12111213
return false;

0 commit comments

Comments
 (0)