Skip to content

Commit d9c8ae4

Browse files
committed
[AST] Remove isTypeContext() check from hasValueSemantics() and move the responsibility to the caller
1 parent 0d66b0c commit d9c8ae4

File tree

4 files changed

+15
-11
lines changed

4 files changed

+15
-11
lines changed

lib/AST/Decl.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5708,8 +5708,9 @@ void VarDecl::emitLetToVarNoteIfSimple(DeclContext *UseDC) const {
57085708
if (AD->isGetter() && !AD->getAccessorKeywordLoc().isValid())
57095709
return;
57105710

5711+
auto accessorDC = AD->getDeclContext();
57115712
// Do not suggest the fix-it if `Self` is a class type.
5712-
if (!AD->getDeclContext()->hasValueSemantics()) {
5713+
if (accessorDC->isTypeContext() && !accessorDC->hasValueSemantics()) {
57135714
return;
57145715
}
57155716
}

lib/AST/DeclContext.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,9 +1025,6 @@ DeclContextKind DeclContext::getContextKind() const {
10251025
}
10261026

10271027
bool DeclContext::hasValueSemantics() const {
1028-
if (!isTypeContext())
1029-
return false;
1030-
10311028
if (auto contextTy = getSelfTypeInContext()) {
10321029
return !contextTy->hasReferenceSemantics();
10331030
}

lib/Sema/TypeCheckDecl.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1959,7 +1959,9 @@ void TypeChecker::validateDecl(OperatorDecl *OD) {
19591959
llvm::Expected<SelfAccessKind>
19601960
SelfAccessKindRequest::evaluate(Evaluator &evaluator, FuncDecl *FD) const {
19611961
if (FD->getAttrs().getAttribute<MutatingAttr>(true)) {
1962-
if (!FD->isInstanceMember() || !FD->getDeclContext()->hasValueSemantics()) {
1962+
auto functionDC = FD->getDeclContext();
1963+
if (!FD->isInstanceMember() ||
1964+
(functionDC->isTypeContext() && !functionDC->hasValueSemantics())) {
19631965
return SelfAccessKind::NonMutating;
19641966
}
19651967
return SelfAccessKind::Mutating;
@@ -1981,7 +1983,9 @@ SelfAccessKindRequest::evaluate(Evaluator &evaluator, FuncDecl *FD) const {
19811983
case AccessorKind::MutableAddress:
19821984
case AccessorKind::Set:
19831985
case AccessorKind::Modify:
1984-
if (AD->isInstanceMember() && AD->getDeclContext()->hasValueSemantics())
1986+
auto accessorDC = AD->getDeclContext();
1987+
if (AD->isInstanceMember() && accessorDC->isTypeContext() &&
1988+
accessorDC->hasValueSemantics())
19851989
return SelfAccessKind::Mutating;
19861990
break;
19871991

lib/Sema/TypeCheckStorage.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,9 @@ void swift::validatePatternBindingEntries(TypeChecker &tc,
268268
llvm::Expected<bool>
269269
IsGetterMutatingRequest::evaluate(Evaluator &evaluator,
270270
AbstractStorageDecl *storage) const {
271-
bool result =
272-
(!storage->isStatic() && storage->getDeclContext()->hasValueSemantics());
271+
auto storageDC = storage->getDeclContext();
272+
bool result = (!storage->isStatic() && storageDC->isTypeContext() &&
273+
storageDC->hasValueSemantics());
273274

274275
// 'lazy' overrides the normal accessor-based rules and heavily
275276
// restricts what accessors can be used. The getter is considered
@@ -297,7 +298,7 @@ IsGetterMutatingRequest::evaluate(Evaluator &evaluator,
297298

298299
// Protocol requirements are always written as '{ get }' or '{ get set }';
299300
// the @_borrowed attribute determines if getReadImpl() becomes Get or Read.
300-
if (isa<ProtocolDecl>(storage->getDeclContext()))
301+
if (isa<ProtocolDecl>(storageDC))
301302
return checkMutability(AccessorKind::Get);
302303

303304
switch (storage->getReadImpl()) {
@@ -323,8 +324,9 @@ IsSetterMutatingRequest::evaluate(Evaluator &evaluator,
323324
AbstractStorageDecl *storage) const {
324325
// By default, the setter is mutating if we have an instance member of a
325326
// value type, but this can be overridden below.
326-
bool result =
327-
(!storage->isStatic() && storage->getDeclContext()->hasValueSemantics());
327+
auto storageDC = storage->getDeclContext();
328+
bool result = (!storage->isStatic() && storageDC->isTypeContext() &&
329+
storageDC->hasValueSemantics());
328330

329331
// If we have an attached property wrapper, the setter is mutating
330332
// or not based on the composition of the wrappers.

0 commit comments

Comments
 (0)