Skip to content

Commit 148897a

Browse files
committed
[nfc] refactor ValueDecl::isMoveOnly
I've renamed the method to `TypeDecl::isNoncopyable`, because the query doesn't make sense for many other kinds of `ValueDecl`'s beyond the `TypeDecl`'s. In fact, it looks like no one was relying on that anyway. Thus, we now have a distinction where in Sema, you ask whether a `Type` or `TypeDecl` is "Noncopyable". But within SIL, we still preserve the notion of "move-only" since there is additionally the move-only type wrapper for types that otherwise support copying.
1 parent e364b23 commit 148897a

25 files changed

+83
-76
lines changed

include/swift/AST/Decl.h

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2606,12 +2606,6 @@ class ValueDecl : public Decl {
26062606
/// optional result.
26072607
unsigned isIUO : 1;
26082608

2609-
/// Whether the "isMoveOnly" bit has been computed yet.
2610-
unsigned isMoveOnlyComputed : 1;
2611-
2612-
/// Whether this declaration can not be copied and thus is move only.
2613-
unsigned isMoveOnly : 1;
2614-
26152609
/// Whether the "isEscapable" bit has been computed yet.
26162610
unsigned isEscapable : 1;
26172611

@@ -2623,7 +2617,6 @@ class ValueDecl : public Decl {
26232617
friend class OverriddenDeclsRequest;
26242618
friend class IsObjCRequest;
26252619
friend class IsFinalRequest;
2626-
friend class IsMoveOnlyRequest;
26272620
friend class IsEscapableRequest;
26282621
friend class IsDynamicRequest;
26292622
friend class IsImplicitlyUnwrappedOptionalRequest;
@@ -2927,9 +2920,6 @@ class ValueDecl : public Decl {
29272920
/// Is this declaration 'final'?
29282921
bool isFinal() const;
29292922

2930-
/// Is this declaration 'moveOnly'?
2931-
bool isMoveOnly() const;
2932-
29332923
/// Is this declaration escapable?
29342924
bool isEscapable() const;
29352925

@@ -3105,8 +3095,18 @@ class ValueDecl : public Decl {
31053095

31063096
/// This is a common base class for declarations which declare a type.
31073097
class TypeDecl : public ValueDecl {
3098+
private:
31083099
ArrayRef<InheritedEntry> Inherited;
31093100

3101+
struct {
3102+
/// Whether the "isNoncopyable" bit has been computed yet.
3103+
unsigned isNoncopyableComputed : 1;
3104+
3105+
/// Whether this declaration supports copying.
3106+
unsigned isNoncopyable : 1;
3107+
} LazySemanticInfo = { };
3108+
friend class IsNoncopyableRequest;
3109+
31103110
protected:
31113111
TypeDecl(DeclKind K, llvm::PointerUnion<DeclContext *, ASTContext *> context,
31123112
Identifier name, SourceLoc NameLoc,
@@ -3134,6 +3134,9 @@ class TypeDecl : public ValueDecl {
31343134

31353135
void setInherited(ArrayRef<InheritedEntry> i) { Inherited = i; }
31363136

3137+
/// Is this declaration noncopyable?
3138+
bool isNoncopyable() const;
3139+
31373140
static bool classof(const Decl *D) {
31383141
return D->getKind() >= DeclKind::First_TypeDecl &&
31393142
D->getKind() <= DeclKind::Last_TypeDecl;

include/swift/AST/TypeCheckRequests.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -407,9 +407,9 @@ class IsFinalRequest :
407407
void cacheResult(bool value) const;
408408
};
409409

410-
/// Determine whether the given declaration is 'moveOnly'.
411-
class IsMoveOnlyRequest
412-
: public SimpleRequest<IsMoveOnlyRequest, bool(ValueDecl *),
410+
/// Determine whether the given declaration is noncopyable
411+
class IsNoncopyableRequest
412+
: public SimpleRequest<IsNoncopyableRequest, bool(TypeDecl *),
413413
RequestFlags::SeparatelyCached> {
414414
public:
415415
using SimpleRequest::SimpleRequest;
@@ -418,7 +418,7 @@ class IsMoveOnlyRequest
418418
friend SimpleRequest;
419419

420420
// Evaluation.
421-
bool evaluate(Evaluator &evaluator, ValueDecl *decl) const;
421+
bool evaluate(Evaluator &evaluator, TypeDecl *decl) const;
422422

423423
public:
424424
// Separate caching.

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ SWIFT_REQUEST(TypeChecker, IsDynamicRequest, bool(ValueDecl *),
210210
SeparatelyCached, NoLocationInfo)
211211
SWIFT_REQUEST(TypeChecker, IsFinalRequest, bool(ValueDecl *), SeparatelyCached,
212212
NoLocationInfo)
213-
SWIFT_REQUEST(TypeChecker, IsMoveOnlyRequest, bool(ValueDecl *), SeparatelyCached,
213+
SWIFT_REQUEST(TypeChecker, IsNoncopyableRequest, bool(TypeDecl *), SeparatelyCached,
214214
NoLocationInfo)
215215
SWIFT_REQUEST(TypeChecker, IsEscapableRequest, bool(ValueDecl *),
216216
SeparatelyCached, NoLocationInfo)

lib/AST/ASTPrinter.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3327,14 +3327,16 @@ static bool usesFeatureFlowSensitiveConcurrencyCaptures(Decl *decl) {
33273327
static bool usesFeatureMoveOnly(Decl *decl) {
33283328
if (auto *extension = dyn_cast<ExtensionDecl>(decl)) {
33293329
if (auto *nominal = extension->getSelfNominalTypeDecl())
3330-
if (nominal->isMoveOnly())
3330+
if (nominal->isNoncopyable())
33313331
return true;
33323332
}
33333333

3334-
if (auto value = dyn_cast<ValueDecl>(decl)) {
3335-
if (value->isMoveOnly())
3336-
return true;
3334+
if (auto typeDecl = dyn_cast<TypeDecl>(decl)) {
3335+
if (typeDecl->isNoncopyable())
3336+
return true;
3337+
}
33373338

3339+
if (auto value = dyn_cast<ValueDecl>(decl)) {
33383340
// Check for move-only types in the types of this declaration.
33393341
if (Type type = value->getInterfaceType()) {
33403342
bool hasMoveOnly = type.findIf([](Type type) {

lib/AST/ASTVerifier.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3238,7 +3238,7 @@ class Verifier : public ASTWalker {
32383238
PrettyStackTraceDecl debugStack("verifying DestructorDecl", DD);
32393239

32403240
auto *ND = DD->getDeclContext()->getSelfNominalTypeDecl();
3241-
if (!isa<ClassDecl>(ND) && !ND->isMoveOnly() && !DD->isInvalid()) {
3241+
if (!isa<ClassDecl>(ND) && !ND->isNoncopyable() && !DD->isInvalid()) {
32423242
Out << "DestructorDecls outside classes/move only types should be "
32433243
"marked invalid\n";
32443244
abort();

lib/AST/Decl.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3556,12 +3556,6 @@ bool ValueDecl::isFinal() const {
35563556
getAttrs().hasAttribute<FinalAttr>());
35573557
}
35583558

3559-
bool ValueDecl::isMoveOnly() const {
3560-
return evaluateOrDefault(getASTContext().evaluator,
3561-
IsMoveOnlyRequest{const_cast<ValueDecl *>(this)},
3562-
getAttrs().hasAttribute<MoveOnlyAttr>());
3563-
}
3564-
35653559
bool ValueDecl::isEscapable() const {
35663560
return evaluateOrDefault(getASTContext().evaluator,
35673561
IsEscapableRequest{const_cast<ValueDecl *>(this)},
@@ -4786,6 +4780,12 @@ GenericParameterReferenceInfo ValueDecl::findExistentialSelfReferences(
47864780
llvm::None);
47874781
}
47884782

4783+
bool TypeDecl::isNoncopyable() const {
4784+
return evaluateOrDefault(getASTContext().evaluator,
4785+
IsNoncopyableRequest{const_cast<TypeDecl *>(this)},
4786+
getAttrs().hasAttribute<MoveOnlyAttr>());
4787+
}
4788+
47894789
Type TypeDecl::getDeclaredInterfaceType() const {
47904790
if (auto *NTD = dyn_cast<NominalTypeDecl>(this))
47914791
return NTD->getDeclaredInterfaceType();

lib/AST/Module.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2002,7 +2002,7 @@ LookupConformanceInModuleRequest::evaluate(
20022002
}
20032003
} else if (protocol->isSpecificProtocol(KnownProtocolKind::Copyable)) {
20042004
// Only move-only nominals are not Copyable
2005-
if (nominal->isMoveOnly()) {
2005+
if (nominal->isNoncopyable()) {
20062006
return ProtocolConformanceRef::forInvalid();
20072007
} else {
20082008
// Specifically do not create a concrete conformance to Copyable. At

lib/AST/ProtocolConformance.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1027,7 +1027,7 @@ void NominalTypeDecl::prepareConformanceTable() const {
10271027
return;
10281028

10291029
// No synthesized conformances for move-only nominals.
1030-
if (isMoveOnly()) {
1030+
if (isNoncopyable()) {
10311031
// assumption is Sendable gets synthesized elsewhere.
10321032
assert(!proto->isSpecificProtocol(KnownProtocolKind::Sendable));
10331033
return;

lib/AST/SwiftNameTranslation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ swift::cxx_translation::getDeclRepresentation(const ValueDecl *VD) {
234234
if (isa<ProtocolDecl>(typeDecl))
235235
return {Unsupported, UnrepresentableProtocol};
236236
// Swift's consume semantics are not yet supported in C++.
237-
if (typeDecl->isMoveOnly())
237+
if (typeDecl->isNoncopyable())
238238
return {Unsupported, UnrepresentableMoveOnly};
239239
if (typeDecl->isGeneric()) {
240240
if (isa<ClassDecl>(VD))

lib/AST/Type.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ bool TypeBase::isMarkerExistential() {
159159

160160
bool TypeBase::isNoncopyable() {
161161
if (auto *nom = getAnyNominal())
162-
return nom->isMoveOnly();
162+
return nom->isNoncopyable();
163163

164164
if (auto *expansion = getAs<PackExpansionType>()) {
165165
return expansion->getPatternType()->isNoncopyable();

0 commit comments

Comments
 (0)