Skip to content

Commit 9552442

Browse files
authored
Merge pull request swiftlang#36049 from DougGregor/simplify-marker-protocol-printing
2 parents 2eba8dd + 2fd1912 commit 9552442

File tree

2 files changed

+38
-52
lines changed

2 files changed

+38
-52
lines changed

lib/AST/ASTPrinter.cpp

Lines changed: 18 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -968,8 +968,21 @@ class PrintAST : public ASTVisitor<PrintAST> {
968968

969969
ASTVisitor::visit(D);
970970

971-
if (haveFeatureChecks)
971+
if (haveFeatureChecks) {
972+
// If we guarded a marker protocol, print an alternative typealias
973+
// for Any.
974+
if (auto proto = dyn_cast<ProtocolDecl>(D)) {
975+
if (proto->isMarkerProtocol()) {
976+
Printer.printNewline();
977+
Printer << "#else";
978+
Printer.printNewline();
979+
printAccess(proto);
980+
Printer << "typealias " << proto->getName() << " = Any";
981+
}
982+
}
983+
972984
printCompatibilityFeatureChecksPost(Printer);
985+
}
973986

974987
if (Synthesize) {
975988
Printer.setSynthesizedTarget({});
@@ -2426,53 +2439,16 @@ static bool usesFeatureAsyncAwait(Decl *decl) {
24262439
}
24272440

24282441
static bool usesFeatureMarkerProtocol(Decl *decl) {
2429-
// Check an inheritance clause for a marker protocol.
2430-
auto checkInherited = [&](ArrayRef<TypeLoc> inherited) -> bool {
2431-
for (const auto &inheritedEntry : inherited) {
2432-
if (auto inheritedType = inheritedEntry.getType()) {
2433-
if (inheritedType->isExistentialType()) {
2434-
auto layout = inheritedType->getExistentialLayout();
2435-
for (ProtocolType *protoTy : layout.getProtocols()) {
2436-
if (protoTy->getDecl()->isMarkerProtocol())
2437-
return true;
2438-
}
2439-
}
2440-
}
2441-
}
2442-
2443-
return false;
2444-
};
2445-
2446-
// Check generic requirements for a marker protocol.
2447-
auto checkRequirements = [&](ArrayRef<Requirement> requirements) -> bool {
2448-
for (const auto &req: requirements) {
2449-
if (req.getKind() == RequirementKind::Conformance &&
2450-
req.getSecondType()->castTo<ProtocolType>()->getDecl()
2451-
->isMarkerProtocol())
2452-
return true;
2453-
}
2454-
2455-
return false;
2456-
};
2457-
24582442
if (auto proto = dyn_cast<ProtocolDecl>(decl)) {
24592443
if (proto->isMarkerProtocol())
24602444
return true;
2461-
2462-
if (checkInherited(proto->getInherited()))
2463-
return true;
2464-
2465-
if (checkRequirements(proto->getRequirementSignature()))
2466-
return true;
24672445
}
24682446

24692447
if (auto ext = dyn_cast<ExtensionDecl>(decl)) {
2470-
if (checkRequirements(ext->getGenericRequirements()))
2471-
return true;
2472-
2473-
if (checkInherited(ext->getInherited()))
2474-
return true;
2475-
}
2448+
if (auto proto = ext->getSelfProtocolDecl())
2449+
if (proto->isMarkerProtocol())
2450+
return true;
2451+
}
24762452

24772453
return false;
24782454
}

test/ModuleInterface/features.swift

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,32 @@ public extension MyActor {
3232
public func globalAsync() async { }
3333

3434
// CHECK: #if compiler(>=5.3) && $MarkerProtocol
35-
// CHECK-NEXT: public protocol MP
35+
// CHECK-NEXT: public protocol MP {
36+
// CHECK-NEXT: }
37+
// CHECK-NEXT: #else
38+
// CHECK-NEXT: public typealias MP = Any
39+
// CHECK-NEXT: #endif
3640
@_marker public protocol MP { }
3741

3842
// CHECK: #if compiler(>=5.3) && $MarkerProtocol
3943
// CHECK-NEXT: @_marker public protocol MP2 : FeatureTest.MP {
4044
// CHECK-NEXT: }
45+
// CHECK-NEXT: #else
46+
// CHECK-NEXT: public typealias MP2 = Any
4147
// CHECK-NEXT: #endif
4248
@_marker public protocol MP2: MP { }
4349

44-
// CHECK: #if compiler(>=5.3) && $MarkerProtocol
45-
// CHECK-NEXT: public protocol MP3
50+
// CHECK-NOT: #if compiler(>=5.3) && $MarkerProtocol
51+
// CHECK: public protocol MP3 : FeatureTest.MP {
4652
// CHECK-NEXT: }
4753
public protocol MP3: MP { }
48-
// CHECK-NEXT: #endif
54+
55+
// CHECK: #if compiler(>=5.3) && $MarkerProtocol
56+
// CHECK-NEXT: extension MP2 {
57+
// CHECK-NEXT: func inMP2
58+
extension MP2 {
59+
public func inMP2() { }
60+
}
4961

5062
// CHECK: class OldSchool {
5163
public class OldSchool: MP {
@@ -55,17 +67,15 @@ public class OldSchool: MP {
5567
public func takeClass() async { }
5668
}
5769

58-
// CHECK: #if compiler(>=5.3) && $MarkerProtocol
59-
// CHECK-NEXT: extension Array : FeatureTest.MP where Element : FeatureTest.MP {
70+
// CHECK-NOT: #if compiler(>=5.3) && $MarkerProtocol
71+
// CHECK: extension Array : FeatureTest.MP where Element : FeatureTest.MP {
6072
extension Array: FeatureTest.MP where Element : FeatureTest.MP { }
6173
// CHECK-NEXT: }
62-
// CHECK-NEXT: #endif
6374

64-
// CHECK: #if compiler(>=5.3) && $MarkerProtocol
65-
// CHECK-NEXT: extension OldSchool : Swift.UnsafeConcurrentValue {
75+
// CHECK-NOT: #if compiler(>=5.3) && $MarkerProtocol
76+
// CHECK: extension OldSchool : Swift.UnsafeConcurrentValue {
6677
extension OldSchool: UnsafeConcurrentValue { }
6778
// CHECK-NEXT: }
68-
// CHECK-NEXT: #endif
6979

7080
// CHECK: #if compiler(>=5.3) && $AsyncAwait
7181
// CHECK-NEXT: func runSomethingSomewhere

0 commit comments

Comments
 (0)