Skip to content

Commit 29ca8b0

Browse files
committed
[AST] Adds 'hasValueSemantics()' to DeclContext and uses 'getSelfTypeInContext()' instead
1 parent f676dde commit 29ca8b0

File tree

6 files changed

+19
-29
lines changed

6 files changed

+19
-29
lines changed

include/swift/AST/DeclContext.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,10 @@ class alignas(1 << DeclContextAlignInBits) DeclContext {
261261

262262
/// Returns the kind of context this is.
263263
DeclContextKind getContextKind() const;
264-
264+
265+
/// Returns whether this context has value semantics.
266+
bool hasValueSemantics() const;
267+
265268
/// Determines whether this context is itself a local scope in a
266269
/// code block. A context that appears in such a scope, like a
267270
/// local type declaration, does not itself become a local context.

lib/AST/Decl.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5709,9 +5709,7 @@ void VarDecl::emitLetToVarNoteIfSimple(DeclContext *UseDC) const {
57095709
return;
57105710

57115711
// Do not suggest the fix-it if `Self` is a class type.
5712-
if (AD->getDeclContext()
5713-
->getSelfTypeInContext()
5714-
->isAnyClassReferenceType()) {
5712+
if (!AD->getDeclContext()->hasValueSemantics()) {
57155713
return;
57165714
}
57175715
}

lib/AST/DeclContext.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,14 @@ DeclContextKind DeclContext::getContextKind() const {
10241024
llvm_unreachable("Unhandled DeclContext ASTHierarchy");
10251025
}
10261026

1027+
bool DeclContext::hasValueSemantics() const {
1028+
if (auto contextTy = getSelfTypeInContext()) {
1029+
return !contextTy->hasReferenceSemantics();
1030+
}
1031+
1032+
return false;
1033+
}
1034+
10271035
SourceLoc swift::extractNearestSourceLoc(const DeclContext *dc) {
10281036
switch (dc->getContextKind()) {
10291037
case DeclContextKind::AbstractFunctionDecl:

lib/Sema/TypeCheckDecl.cpp

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1956,26 +1956,10 @@ void TypeChecker::validateDecl(OperatorDecl *OD) {
19561956
}
19571957
}
19581958

1959-
bool swift::doesContextHaveValueSemantics(DeclContext *dc) {
1960-
if (Type contextTy = dc->getDeclaredInterfaceType()) {
1961-
// If the decl context is an extension, then it could be imposing a class
1962-
// constraint (ex: where Self: SomeClass). Make sure we include that
1963-
// in our check as well.
1964-
auto extensionRequiresClass = false;
1965-
if (auto ED = dyn_cast<ExtensionDecl>(dc)) {
1966-
extensionRequiresClass =
1967-
ED->getGenericSignature()->requiresClass(ED->getSelfInterfaceType());
1968-
}
1969-
return !contextTy->hasReferenceSemantics() && !extensionRequiresClass;
1970-
}
1971-
return false;
1972-
}
1973-
19741959
llvm::Expected<SelfAccessKind>
19751960
SelfAccessKindRequest::evaluate(Evaluator &evaluator, FuncDecl *FD) const {
19761961
if (FD->getAttrs().getAttribute<MutatingAttr>(true)) {
1977-
if (!FD->isInstanceMember() ||
1978-
!doesContextHaveValueSemantics(FD->getDeclContext())) {
1962+
if (!FD->isInstanceMember() || !FD->getDeclContext()->hasValueSemantics()) {
19791963
return SelfAccessKind::NonMutating;
19801964
}
19811965
return SelfAccessKind::Mutating;
@@ -1997,8 +1981,7 @@ SelfAccessKindRequest::evaluate(Evaluator &evaluator, FuncDecl *FD) const {
19971981
case AccessorKind::MutableAddress:
19981982
case AccessorKind::Set:
19991983
case AccessorKind::Modify:
2000-
if (AD->isInstanceMember() &&
2001-
doesContextHaveValueSemantics(AD->getDeclContext()))
1984+
if (AD->isInstanceMember() && AD->getDeclContext()->hasValueSemantics())
20021985
return SelfAccessKind::Mutating;
20031986
break;
20041987

lib/Sema/TypeCheckDecl.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ class DeclContext;
2525
class ValueDecl;
2626
class Pattern;
2727

28-
bool doesContextHaveValueSemantics(DeclContext *dc);
29-
3028
/// Walks up the override chain for \p CD until it finds an initializer that is
3129
/// required and non-implicit. If no such initializer exists, returns the
3230
/// declaration where \c required was introduced (i.e. closest to the root

lib/Sema/TypeCheckStorage.cpp

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

274274
// 'lazy' overrides the normal accessor-based rules and heavily
275275
// restricts what accessors can be used. The getter is considered
@@ -323,8 +323,8 @@ IsSetterMutatingRequest::evaluate(Evaluator &evaluator,
323323
AbstractStorageDecl *storage) const {
324324
// By default, the setter is mutating if we have an instance member of a
325325
// value type, but this can be overridden below.
326-
bool result = (!storage->isStatic() &&
327-
doesContextHaveValueSemantics(storage->getDeclContext()));
326+
bool result =
327+
(!storage->isStatic() && storage->getDeclContext()->hasValueSemantics());
328328

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

0 commit comments

Comments
 (0)