Skip to content

Commit 71b256d

Browse files
committed
use new noncopyable types infrastructure
The infrastructure underpinning the new feature NoncopyableGenerics is mature enough to be used.
1 parent ad01574 commit 71b256d

33 files changed

+178
-497
lines changed

include/swift/AST/Decl.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4392,10 +4392,6 @@ class NominalTypeDecl : public GenericTypeDecl, public IterableDeclContext {
43924392
/// Type if it `isEscapable` instead of using this.
43934393
CanBeInvertible::Result canBeEscapable() const;
43944394

4395-
/// Determine whether this type has ~<target>` stated on
4396-
/// itself, one of its inherited types or `Self` requirements.
4397-
InverseMarking::Mark hasInverseMarking(InvertibleProtocolKind target) const;
4398-
43994395
// Implement isa/cast/dyncast/etc.
44004396
static bool classof(const Decl *D) {
44014397
return D->getKind() >= DeclKind::First_NominalTypeDecl &&

include/swift/AST/DiagnosticsSema.def

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7676,9 +7676,6 @@ ERROR(bitwise_copyable_outside_module,none,
76767676
(const ValueDecl *))
76777677

76787678
// -- older ones below --
7679-
ERROR(noncopyable_cannot_conform_to_type, none,
7680-
"noncopyable %kind0 cannot conform to %1",
7681-
(const ValueDecl *, Type))
76827679
ERROR(noncopyable_parameter_requires_ownership, none,
76837680
"parameter of noncopyable type %0 must specify ownership", (Type))
76847681
ERROR(noncopyable_parameter_subscript_unsupported, none,

include/swift/Frontend/ModuleInterfaceLoader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ struct RequireNoncopyableGenerics_t {
494494
RequireNoncopyableGenerics_t(const ASTContext &ctx)
495495
: RequireNoncopyableGenerics_t(ctx.LangOpts) {}
496496
RequireNoncopyableGenerics_t(const LangOptions &opts)
497-
: value(opts.hasFeature(Feature::NoncopyableGenerics)) {}
497+
: value(true) {}
498498

499499
explicit operator bool() const { return value; }
500500
};

lib/AST/ASTPrinter.cpp

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7701,17 +7701,15 @@ static void getSyntacticInheritanceClause(const ProtocolDecl *proto,
77017701
InvertibleProtocolSet inverses = InvertibleProtocolSet::full();
77027702

77037703
for (auto *inherited : proto->getInheritedProtocols()) {
7704-
if (ctx.LangOpts.hasFeature(Feature::NoncopyableGenerics)) {
7705-
if (auto ip = inherited->getInvertibleProtocolKind()) {
7706-
inverses.remove(*ip);
7707-
continue;
7708-
}
7704+
if (auto ip = inherited->getInvertibleProtocolKind()) {
7705+
inverses.remove(*ip);
7706+
continue;
7707+
}
77097708

7710-
for (auto ip : InvertibleProtocolSet::full()) {
7711-
auto *proto = ctx.getProtocol(getKnownProtocolKind(ip));
7712-
if (inherited->inheritsFrom(proto))
7713-
inverses.remove(ip);
7714-
}
7709+
for (auto ip : InvertibleProtocolSet::full()) {
7710+
auto *proto = ctx.getProtocol(getKnownProtocolKind(ip));
7711+
if (inherited->inheritsFrom(proto))
7712+
inverses.remove(ip);
77157713
}
77167714

77177715
Results.emplace_back(TypeLoc::withoutLoc(inherited->getDeclaredInterfaceType()),
@@ -7720,19 +7718,17 @@ static void getSyntacticInheritanceClause(const ProtocolDecl *proto,
77207718
/*isPreconcurrency=*/false);
77217719
}
77227720

7723-
if (ctx.LangOpts.hasFeature(Feature::NoncopyableGenerics)) {
7724-
for (auto ip : inverses) {
7725-
InvertibleProtocolSet singleton;
7726-
singleton.insert(ip);
7721+
for (auto ip : inverses) {
7722+
InvertibleProtocolSet singleton;
7723+
singleton.insert(ip);
77277724

7728-
auto inverseTy = ProtocolCompositionType::get(
7729-
ctx, ArrayRef<Type>(), singleton,
7730-
/*hasExplicitAnyObject=*/false);
7731-
Results.emplace_back(TypeLoc::withoutLoc(inverseTy),
7732-
/*isUnchecked=*/false,
7733-
/*isRetroactive=*/false,
7734-
/*isPreconcurrency=*/false);
7735-
}
7725+
auto inverseTy = ProtocolCompositionType::get(
7726+
ctx, ArrayRef<Type>(), singleton,
7727+
/*hasExplicitAnyObject=*/false);
7728+
Results.emplace_back(TypeLoc::withoutLoc(inverseTy),
7729+
/*isUnchecked=*/false,
7730+
/*isRetroactive=*/false,
7731+
/*isPreconcurrency=*/false);
77367732
}
77377733
}
77387734

lib/AST/Builtins.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -296,12 +296,6 @@ struct CollectGenericParams {
296296
auto protocolType = synthesizeType(SC, conf.Protocol);
297297
Requirement req = {RequirementKind::Conformance, type, protocolType};
298298

299-
// If it's an invertible protocol and NoncopyableGenerics is disabled
300-
// then skip the requirement.
301-
if (req.getProtocolDecl()->getInvertibleProtocolKind())
302-
if (!SC.Context.LangOpts.hasFeature(Feature::NoncopyableGenerics))
303-
return;
304-
305299
AddedRequirements.push_back(req);
306300
}
307301

@@ -736,13 +730,6 @@ namespace {
736730
Requirement req(RequirementKind::Conformance,
737731
generator.build(*this),
738732
proto->getDeclaredInterfaceType());
739-
740-
// If it's an invertible protocol and NoncopyableGenerics is disabled
741-
// then skip the requirement.
742-
if (req.getProtocolDecl()->getInvertibleProtocolKind())
743-
if (!Context.LangOpts.hasFeature(Feature::NoncopyableGenerics))
744-
return;
745-
746733
addedRequirements.push_back(req);
747734
}
748735

lib/AST/ConformanceLookup.cpp

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -912,26 +912,10 @@ static bool conformsToInvertible(CanType type, InvertibleProtocolKind ip) {
912912
/// \returns true iff this type lacks conformance to Copyable.
913913
bool TypeBase::isNoncopyable() {
914914
auto canType = preprocessTypeForInvertibleQuery(this);
915-
auto &ctx = canType->getASTContext();
916-
917-
// for legacy-mode queries that are not dependent on conformances to Copyable
918-
if (!ctx.LangOpts.hasFeature(Feature::NoncopyableGenerics))
919-
return alwaysNoncopyable(canType);
920-
921915
return !conformsToInvertible(canType, InvertibleProtocolKind::Copyable);
922916
}
923917

924918
bool TypeBase::isEscapable() {
925919
auto canType = preprocessTypeForInvertibleQuery(this);
926-
auto &ctx = canType->getASTContext();
927-
928-
// for legacy-mode queries that are not dependent on conformances to Escapable
929-
if (!ctx.LangOpts.hasFeature(Feature::NoncopyableGenerics)) {
930-
if (auto nom = canType.getAnyNominal())
931-
return nom->canBeEscapable();
932-
else
933-
return true;
934-
}
935-
936920
return conformsToInvertible(canType, InvertibleProtocolKind::Escapable);
937921
}

lib/AST/ConformanceLookupTable.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -552,9 +552,8 @@ void ConformanceLookupTable::expandImpliedConformances(NominalTypeDecl *nominal,
552552
for (auto *inherited : conformingProtocol->getInheritedProtocols()) {
553553
// Conforming a ~Copyable nominal to a protocol that inherits Copyable
554554
// should not imply a Copyable conformance on the nominal.
555-
if (ctx.LangOpts.hasFeature(Feature::NoncopyableGenerics))
556-
if (inherited->getInvertibleProtocolKind())
557-
continue;
555+
if (inherited->getInvertibleProtocolKind())
556+
continue;
558557

559558
addProtocol(inherited, SourceLoc(), source);
560559
}

lib/AST/Decl.cpp

Lines changed: 4 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,12 +1324,10 @@ static GenericSignature getPlaceholderGenericSignature(
13241324
auto type = genericParam->getDeclaredInterfaceType();
13251325
genericParams.push_back(type->castTo<GenericTypeParamType>());
13261326

1327-
if (ctx.LangOpts.hasFeature(Feature::NoncopyableGenerics)) {
1328-
for (auto ip : InvertibleProtocolSet::full()) {
1329-
auto proto = ctx.getProtocol(getKnownProtocolKind(ip));
1330-
requirements.emplace_back(RequirementKind::Conformance, type,
1331-
proto->getDeclaredInterfaceType());
1332-
}
1327+
for (auto ip : InvertibleProtocolSet::full()) {
1328+
auto proto = ctx.getProtocol(getKnownProtocolKind(ip));
1329+
requirements.emplace_back(RequirementKind::Conformance, type,
1330+
proto->getDeclaredInterfaceType());
13331331
}
13341332
}
13351333
}
@@ -4947,22 +4945,10 @@ conformanceExists(TypeDecl const *decl, InvertibleProtocolKind ip) {
49474945
}
49484946

49494947
TypeDecl::CanBeInvertible::Result NominalTypeDecl::canBeCopyable() const {
4950-
if (!getASTContext().LangOpts.hasFeature(Feature::NoncopyableGenerics)) {
4951-
return !hasInverseMarking(InvertibleProtocolKind::Copyable)
4952-
? CanBeInvertible::Always
4953-
: CanBeInvertible::Never;
4954-
}
4955-
49564948
return conformanceExists(this, InvertibleProtocolKind::Copyable);
49574949
}
49584950

49594951
TypeDecl::CanBeInvertible::Result NominalTypeDecl::canBeEscapable() const {
4960-
if (!getASTContext().LangOpts.hasFeature(Feature::NoncopyableGenerics)) {
4961-
return !hasInverseMarking(InvertibleProtocolKind::Escapable)
4962-
? CanBeInvertible::Always
4963-
: CanBeInvertible::Never;
4964-
}
4965-
49664952
return conformanceExists(this, InvertibleProtocolKind::Escapable);
49674953
}
49684954

@@ -6665,47 +6651,6 @@ findInverseInInheritance(InheritedTypes inherited,
66656651
return inverse;
66666652
}
66676653

6668-
InverseMarking::Mark
6669-
NominalTypeDecl::hasInverseMarking(InvertibleProtocolKind target) const {
6670-
switch (target) {
6671-
case InvertibleProtocolKind::Copyable:
6672-
// Handle the legacy '@_moveOnly' for types they can validly appear.
6673-
// TypeCheckAttr handles the illegal situations for us.
6674-
if (auto attr = getAttrs().getAttribute<MoveOnlyAttr>())
6675-
if (isa<StructDecl, EnumDecl, ClassDecl>(this))
6676-
return InverseMarking::Mark(InverseMarking::Kind::LegacyExplicit,
6677-
attr->getLocation());
6678-
break;
6679-
6680-
case InvertibleProtocolKind::Escapable:
6681-
// Handle the legacy '@_nonEscapable' attribute
6682-
if (auto attr = getAttrs().getAttribute<NonEscapableAttr>()) {
6683-
assert((isa<ClassDecl, StructDecl, EnumDecl>(this)));
6684-
return InverseMarking::Mark(InverseMarking::Kind::LegacyExplicit,
6685-
attr->getLocation());
6686-
}
6687-
break;
6688-
}
6689-
6690-
auto &ctx = getASTContext();
6691-
6692-
// Legacy support stops here.
6693-
if (!ctx.LangOpts.hasFeature(Feature::NoncopyableGenerics))
6694-
return InverseMarking::Mark(InverseMarking::Kind::None);
6695-
6696-
// Claim that the tuple decl has an explicit ~TARGET marking.
6697-
if (isa<BuiltinTupleDecl>(this))
6698-
return InverseMarking::Mark(InverseMarking::Kind::Explicit);
6699-
6700-
assert(!isa<ProtocolDecl>(this));
6701-
6702-
// Search the inheritance clause first.
6703-
if (auto inverse = findInverseInInheritance(getInherited(), target))
6704-
return inverse;
6705-
6706-
return InverseMarking::Mark();
6707-
}
6708-
67096654
bool ProtocolDecl::requiresClass() const {
67106655
return evaluateOrDefault(getASTContext().evaluator,
67116656
ProtocolRequiresClassRequest{const_cast<ProtocolDecl *>(this)}, false);

lib/AST/GenericSignature.cpp

Lines changed: 18 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -660,23 +660,19 @@ Type GenericSignatureImpl::getUpperBound(Type type,
660660
// we didn't have a superclass or require AnyObject.
661661
InvertibleProtocolSet inverses;
662662

663-
if (ctx.LangOpts.hasFeature(Feature::NoncopyableGenerics)) {
664-
if (!superclass && !hasExplicitAnyObject) {
665-
for (auto ip : InvertibleProtocolSet::full()) {
666-
auto *kp = ctx.getProtocol(::getKnownProtocolKind(ip));
667-
if (!requiresProtocol(type, kp))
668-
inverses.insert(ip);
669-
}
663+
if (!superclass && !hasExplicitAnyObject) {
664+
for (auto ip : InvertibleProtocolSet::full()) {
665+
auto *kp = ctx.getProtocol(::getKnownProtocolKind(ip));
666+
if (!requiresProtocol(type, kp))
667+
inverses.insert(ip);
670668
}
671669
}
672670

673671
for (auto *proto : getRequiredProtocols(type)) {
674-
if (ctx.LangOpts.hasFeature(Feature::NoncopyableGenerics)) {
675-
// Don't add invertible protocols to the composition, because we recorded
676-
// their absence above.
677-
if (proto->getInvertibleProtocolKind())
678-
continue;
679-
}
672+
// Don't add invertible protocols to the composition, because we recorded
673+
// their absence above.
674+
if (proto->getInvertibleProtocolKind())
675+
continue;
680676

681677
if (proto->requiresClass())
682678
hasExplicitAnyObject = false;
@@ -1254,11 +1250,6 @@ void GenericSignatureImpl::getRequirementsWithInverses(
12541250
SmallVector<InverseRequirement, 2> &inverses) const {
12551251
auto &ctx = getASTContext();
12561252

1257-
if (!ctx.LangOpts.hasFeature(Feature::NoncopyableGenerics)) {
1258-
reqs.append(getRequirements().begin(), getRequirements().end());
1259-
return;
1260-
}
1261-
12621253
// Record the absence of conformances to invertible protocols.
12631254
for (auto gp : getGenericParams()) {
12641255
// Any generic parameter with a superclass bound or concrete type does not
@@ -1306,11 +1297,9 @@ RequirementSignature RequirementSignature::getPlaceholderRequirementSignature(
13061297
inheritedProtos.push_back(inheritedProto);
13071298
}
13081299

1309-
if (ctx.LangOpts.hasFeature(Feature::NoncopyableGenerics)) {
1310-
for (auto ip : InvertibleProtocolSet::full()) {
1311-
auto *otherProto = ctx.getProtocol(getKnownProtocolKind(ip));
1312-
inheritedProtos.push_back(otherProto);
1313-
}
1300+
for (auto ip : InvertibleProtocolSet::full()) {
1301+
auto *otherProto = ctx.getProtocol(getKnownProtocolKind(ip));
1302+
inheritedProtos.push_back(otherProto);
13141303
}
13151304

13161305
ProtocolType::canonicalizeProtocols(inheritedProtos);
@@ -1323,14 +1312,12 @@ RequirementSignature RequirementSignature::getPlaceholderRequirementSignature(
13231312
inheritedProto->getDeclaredInterfaceType());
13241313
}
13251314

1326-
if (ctx.LangOpts.hasFeature(Feature::NoncopyableGenerics)) {
1327-
for (auto *assocTypeDecl : proto->getAssociatedTypeMembers()) {
1328-
for (auto ip : InvertibleProtocolSet::full()) {
1329-
auto *otherProto = ctx.getProtocol(getKnownProtocolKind(ip));
1330-
requirements.emplace_back(RequirementKind::Conformance,
1331-
assocTypeDecl->getDeclaredInterfaceType(),
1332-
otherProto->getDeclaredInterfaceType());
1333-
}
1315+
for (auto *assocTypeDecl : proto->getAssociatedTypeMembers()) {
1316+
for (auto ip : InvertibleProtocolSet::full()) {
1317+
auto *otherProto = ctx.getProtocol(getKnownProtocolKind(ip));
1318+
requirements.emplace_back(RequirementKind::Conformance,
1319+
assocTypeDecl->getDeclaredInterfaceType(),
1320+
otherProto->getDeclaredInterfaceType());
13341321
}
13351322
}
13361323

@@ -1349,12 +1336,6 @@ void RequirementSignature::getRequirementsWithInverses(
13491336
SmallVector<Requirement, 2> &reqs,
13501337
SmallVector<InverseRequirement, 2> &inverses) const {
13511338
auto &ctx = owner->getASTContext();
1352-
1353-
if (!ctx.LangOpts.hasFeature(Feature::NoncopyableGenerics)) {
1354-
reqs.append(getRequirements().begin(), getRequirements().end());
1355-
return;
1356-
}
1357-
13581339
auto sig = owner->getGenericSignature();
13591340

13601341
llvm::SmallDenseSet<CanType, 2> assocTypes;

lib/AST/NameLookup.cpp

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3285,31 +3285,29 @@ InheritedProtocolsRequest::evaluate(Evaluator &evaluator,
32853285
}
32863286

32873287
// Apply inverses.
3288-
if (ctx.LangOpts.hasFeature(Feature::NoncopyableGenerics)) {
3289-
bool skipInverses = false;
3290-
3291-
// ... except for these protocols, so that Copyable does not have to
3292-
// inherit ~Copyable, etc.
3293-
if (auto kp = PD->getKnownProtocolKind()) {
3294-
switch (*kp) {
3295-
case KnownProtocolKind::Sendable:
3296-
case KnownProtocolKind::Copyable:
3297-
case KnownProtocolKind::Escapable:
3298-
skipInverses = true;
3299-
break;
3288+
bool skipInverses = false;
3289+
3290+
// ... except for these protocols, so that Copyable does not have to
3291+
// inherit ~Copyable, etc.
3292+
if (auto kp = PD->getKnownProtocolKind()) {
3293+
switch (*kp) {
3294+
case KnownProtocolKind::Sendable:
3295+
case KnownProtocolKind::Copyable:
3296+
case KnownProtocolKind::Escapable:
3297+
skipInverses = true;
3298+
break;
33003299

3301-
default:
3302-
break;
3303-
}
3300+
default:
3301+
break;
33043302
}
3303+
}
33053304

3306-
if (!skipInverses) {
3307-
for (auto ip : InvertibleProtocolSet::full()) {
3308-
// Unless the user wrote ~P in the syntactic inheritance clause, the
3309-
// semantic inherited list includes P.
3310-
if (!inverses.contains(ip))
3311-
inherited.insert(ctx.getProtocol(getKnownProtocolKind(ip)));
3312-
}
3305+
if (!skipInverses) {
3306+
for (auto ip : InvertibleProtocolSet::full()) {
3307+
// Unless the user wrote ~P in the syntactic inheritance clause, the
3308+
// semantic inherited list includes P.
3309+
if (!inverses.contains(ip))
3310+
inherited.insert(ctx.getProtocol(getKnownProtocolKind(ip)));
33133311
}
33143312
}
33153313

0 commit comments

Comments
 (0)