Skip to content

Commit ad97157

Browse files
committed
AST: Compute actor isolation when expanding semantic attributes.
Declarations may have semantic actor isolation requirements that were not written in source. For example, the default implementation of a protocol requirement must be `@MainActor` isolated if the protocol declaration itself is `@MainActor` isolated. When computing the semantic attributes of a declaration lazily, we must compute the actor isolation of the declaration to ensure that any global actor constraint attributes are added. Also, avoid request cycles and recursive diagnostic printing by only triggering the computation of semantic attributes when printing for swiftinterfaces. Resolves rdar://118277555
1 parent 353d04f commit ad97157

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

lib/AST/ASTPrinter.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ static bool isInObjCImpl(const ValueDecl *VD) {
146146
/// be complete (for example, when printing a .swiftinterface). In other
147147
/// contexts, though, triggering type checking could cause re-entrancy and
148148
/// should be avoided.
149-
static bool shouldTypeCheck(const PrintOptions &options) {
149+
static bool shouldPrintAllSemanticDetails(const PrintOptions &options) {
150150
if (options.IsForSwiftInterface)
151151
return true;
152152

@@ -1150,7 +1150,11 @@ void PrintAST::printAttributes(const Decl *D) {
11501150
if (Options.SkipAttributes)
11511151
return;
11521152

1153-
auto attrs = D->getSemanticAttrs();
1153+
// Force semantic attrs to be computed if appropriate.
1154+
if (shouldPrintAllSemanticDetails(Options))
1155+
(void)D->getSemanticAttrs();
1156+
1157+
auto attrs = D->getAttrs();
11541158

11551159
// Save the current number of exclude attrs to restore once we're done.
11561160
unsigned originalExcludeAttrCount = Options.ExcludeAttrList.size();
@@ -2259,7 +2263,7 @@ void PrintAST::printAccessors(const AbstractStorageDecl *ASD) {
22592263
}
22602264

22612265
// Force implicit accessors to be created if they haven't been already.
2262-
if (shouldTypeCheck(Options)) {
2266+
if (shouldPrintAllSemanticDetails(Options)) {
22632267
ASD->visitEmittedAccessors([](AccessorDecl *accessor) {
22642268
(void)accessor;
22652269
});
@@ -3900,7 +3904,7 @@ void PrintAST::visitPatternBindingDecl(PatternBindingDecl *decl) {
39003904
auto *pattern = decl->getPattern(idx);
39013905

39023906
// Force the entry to be typechecked before attempting to print.
3903-
if (shouldTypeCheck(Options) && !pattern->hasType())
3907+
if (shouldPrintAllSemanticDetails(Options) && !pattern->hasType())
39043908
(void)decl->getCheckedPatternBindingEntry(idx);
39053909

39063910
if (!shouldPrintPattern(pattern))

lib/AST/TypeCheckRequests.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2050,7 +2050,8 @@ DeclAttributes SemanticDeclAttrsRequest::evaluate(Evaluator &evaluator,
20502050
{});
20512051

20522052
// Trigger requests that cause additional semantic attributes to be added.
2053-
if (auto vd = dyn_cast<ValueDecl>(decl)) {
2053+
if (auto vd = dyn_cast<ValueDecl>(mutableDecl)) {
2054+
(void)getActorIsolation(vd);
20542055
(void)vd->isDynamic();
20552056
(void)vd->isFinal();
20562057
}

test/Inputs/lazy_typecheck.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ public protocol PublicProtoWithAssociatedType {
115115
func req() throws -> Int
116116
}
117117

118+
extension MainActorProtocol {
119+
public func req() throws -> Int {
120+
return 1
121+
}
122+
}
123+
118124
protocol InternalProtoWithAssociatedType {
119125
associatedtype A
120126
func internalReq() -> A

0 commit comments

Comments
 (0)