Skip to content

Commit 93961aa

Browse files
committed
[AST] Implement printing suppression for ~Sendable
1 parent c50602b commit 93961aa

File tree

4 files changed

+69
-3
lines changed

4 files changed

+69
-3
lines changed

include/swift/AST/Decl.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ namespace swift {
8585
struct ExternalSourceLocs;
8686
class CaptureListExpr;
8787
class DeclRefExpr;
88+
class InverseTypeRepr;
8889
class LiteralExpr;
8990
class BraceStmt;
9091
class DeclAttributes;
@@ -1970,6 +1971,12 @@ class InheritedTypes {
19701971
Type getResolvedType(unsigned i, TypeResolutionStage stage =
19711972
TypeResolutionStage::Interface) const;
19721973

1974+
/// If the given index corresponds to a "suppressed" entry, returns the
1975+
/// information associated with it, and `std::nullopt` otherwise.
1976+
std::optional<std::pair<Type, InverseTypeRepr *>> getAsSuppressed(
1977+
unsigned i,
1978+
TypeResolutionStage stage = TypeResolutionStage::Interface) const;
1979+
19731980
/// Returns the underlying array of inherited type entries.
19741981
///
19751982
/// NOTE: The `Type` associated with an entry may not be resolved yet.

lib/AST/ASTPrinter.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8223,9 +8223,19 @@ swift::getInheritedForPrinting(
82238223
}
82248224
}
82258225

8226-
if (options.SuppressConformanceSuppression &&
8227-
inherited.getEntry(i).isSuppressed()) {
8228-
continue;
8226+
if (inherited.getEntry(i).isSuppressed()) {
8227+
// All `~<<Protocol>>` inheritances are suppressed.
8228+
if (options.SuppressConformanceSuppression)
8229+
continue;
8230+
8231+
if (options.SuppressTildeSendable) {
8232+
if (auto type = inherited.getAsSuppressed(i)->first) {
8233+
auto *sendable =
8234+
decl->getASTContext().getProtocol(KnownProtocolKind::Sendable);
8235+
if (sendable && sendable == type->getAnyNominal())
8236+
continue;
8237+
}
8238+
}
82298239
}
82308240

82318241
Results.push_back(inherited.getEntry(i));

lib/AST/Decl.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2034,6 +2034,20 @@ Type InheritedTypes::getResolvedType(unsigned i,
20342034
.getInheritedTypeOrNull(getASTContext());
20352035
}
20362036

2037+
std::optional<std::pair<Type, InverseTypeRepr *>>
2038+
InheritedTypes::getAsSuppressed(unsigned i, TypeResolutionStage stage) const {
2039+
ASTContext &ctx = isa<const ExtensionDecl *>(Decl)
2040+
? cast<const ExtensionDecl *>(Decl)->getASTContext()
2041+
: cast<const TypeDecl *>(Decl)->getASTContext();
2042+
auto result =
2043+
evaluateOrDefault(ctx.evaluator, InheritedTypeRequest{Decl, i, stage},
2044+
InheritedTypeResult::forDefault());
2045+
if (result != InheritedTypeResult::Kind::Suppressed)
2046+
return std::nullopt;
2047+
2048+
return result.getSuppressed();
2049+
}
2050+
20372051
ExtensionDecl::ExtensionDecl(SourceLoc extensionLoc,
20382052
TypeRepr *extendedType,
20392053
ArrayRef<InheritedEntry> inherited,
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-emit-module-interface(%t/Library.swiftinterface) %s -module-name Library -enable-experimental-feature TildeSendable
3+
// RUN: %target-swift-typecheck-module-from-interface(%t/Library.swiftinterface) -module-name Library
4+
// RUN: %FileCheck %s < %t/Library.swiftinterface
5+
6+
// REQUIRES: swift_feature_TildeSendable
7+
8+
// CHECK: #if compiler(>=5.3) && $TildeSendable
9+
// CHECK: public class A : ~Swift.Sendable {
10+
// CHECK: }
11+
// CHECK: #else
12+
// CHECK: public class A {
13+
// CHECK: }
14+
// CHECK: #endif
15+
public class A: ~Sendable {
16+
public init() {}
17+
}
18+
19+
protocol P {
20+
}
21+
22+
// CHECK: #if compiler(>=5.3) && $TildeSendable
23+
// CHECK: public struct S : ~Swift.Sendable {
24+
// CHECK: public let x: Swift.Int
25+
// CHECK: }
26+
// CHECK: #else
27+
// CHECK: public struct S {
28+
// CHECK: public let x: Swift.Int
29+
// CHECK: }
30+
// CHECK: #endif
31+
32+
// CHECK-NOT: extension Library.S : Swift.Sendable {}
33+
public struct S: P, ~Sendable {
34+
public let x: Int
35+
}

0 commit comments

Comments
 (0)