|
25 | 25 |
|
26 | 26 | using namespace swift; |
27 | 27 |
|
| 28 | +/// Determine whether it makes sense to infer an attribute in the given |
| 29 | +/// context. |
| 30 | +static bool shouldInferAttributeInContext(const DeclContext *dc) { |
| 31 | + auto sourceFile = dc->getParentSourceFile(); |
| 32 | + if (!sourceFile) |
| 33 | + return false; |
| 34 | + |
| 35 | + switch (sourceFile->Kind) { |
| 36 | + case SourceFileKind::Interface: |
| 37 | + case SourceFileKind::SIL: |
| 38 | + return false; |
| 39 | + |
| 40 | + case SourceFileKind::Library: |
| 41 | + case SourceFileKind::Main: |
| 42 | + return true; |
| 43 | + } |
| 44 | +} |
| 45 | + |
28 | 46 | /// Check whether the @asyncHandler attribute can be applied to the given |
29 | 47 | /// function declaration. |
30 | 48 | /// |
@@ -108,7 +126,7 @@ bool IsAsyncHandlerRequest::evaluate( |
108 | 126 | return true; |
109 | 127 | } |
110 | 128 |
|
111 | | - if (!func->getASTContext().LangOpts.EnableExperimentalConcurrency) |
| 129 | + if (!shouldInferAttributeInContext(func->getDeclContext())) |
112 | 130 | return false; |
113 | 131 |
|
114 | 132 | // Are we in a context where inference is possible? |
@@ -1016,15 +1034,18 @@ static Optional<ActorIsolation> getIsolationFromAttributes(Decl *decl) { |
1016 | 1034 | // If the declaration is marked with a global actor, report it as being |
1017 | 1035 | // part of that global actor. |
1018 | 1036 | if (globalActorAttr) { |
1019 | | - TypeResolutionOptions options(TypeResolverContext::None); |
1020 | | - TypeResolution resolver = TypeResolution::forInterface( |
1021 | | - decl->getInnermostDeclContext(), options, nullptr); |
1022 | | - Type globalActorType = resolver.resolveType( |
1023 | | - globalActorAttr->first->getTypeRepr(), nullptr); |
| 1037 | + ASTContext &ctx = decl->getASTContext(); |
| 1038 | + auto dc = decl->getInnermostDeclContext(); |
| 1039 | + Type globalActorType = evaluateOrDefault( |
| 1040 | + ctx.evaluator, |
| 1041 | + CustomAttrTypeRequest{ |
| 1042 | + globalActorAttr->first, dc, CustomAttrTypeKind::GlobalActor}, |
| 1043 | + Type()); |
1024 | 1044 | if (!globalActorType || globalActorType->hasError()) |
1025 | 1045 | return ActorIsolation::forUnspecified(); |
1026 | 1046 |
|
1027 | | - return ActorIsolation::forGlobalActor(globalActorType); |
| 1047 | + return ActorIsolation::forGlobalActor( |
| 1048 | + globalActorType->mapTypeOutOfContext()); |
1028 | 1049 | } |
1029 | 1050 |
|
1030 | 1051 | llvm_unreachable("Forgot about an attribute?"); |
@@ -1138,23 +1159,32 @@ ActorIsolation ActorIsolationRequest::evaluate( |
1138 | 1159 | } |
1139 | 1160 |
|
1140 | 1161 | // Disable inference of actor attributes outside of normal Swift source files. |
1141 | | - if (auto sourceFile = value->getDeclContext()->getParentSourceFile()) { |
1142 | | - switch (sourceFile->Kind) { |
1143 | | - case SourceFileKind::Interface: |
1144 | | - case SourceFileKind::SIL: |
1145 | | - return defaultIsolation; |
1146 | | - |
1147 | | - case SourceFileKind::Library: |
1148 | | - case SourceFileKind::Main: |
1149 | | - // Attempt inference below. |
1150 | | - break; |
1151 | | - } |
1152 | | - } else { |
| 1162 | + if (!shouldInferAttributeInContext(value->getDeclContext())) |
1153 | 1163 | return defaultIsolation; |
1154 | | - } |
1155 | 1164 |
|
1156 | 1165 | // Function used when returning an inferred isolation. |
1157 | 1166 | auto inferredIsolation = [&](ActorIsolation inferred) { |
| 1167 | + // Add an implicit attribute to capture the actor isolation that was |
| 1168 | + // inferred, so that (e.g.) it will be printed and serialized. |
| 1169 | + ASTContext &ctx = value->getASTContext(); |
| 1170 | + switch (inferred) { |
| 1171 | + case ActorIsolation::Independent: |
| 1172 | + value->getAttrs().add(new (ctx) ActorIndependentAttr(true)); |
| 1173 | + break; |
| 1174 | + |
| 1175 | + case ActorIsolation::GlobalActor: { |
| 1176 | + auto typeExpr = TypeExpr::createImplicit(inferred.getGlobalActor(), ctx); |
| 1177 | + auto attr = CustomAttr::create( |
| 1178 | + ctx, SourceLoc(), typeExpr, /*implicit=*/true); |
| 1179 | + value->getAttrs().add(attr); |
| 1180 | + break; |
| 1181 | + } |
| 1182 | + |
| 1183 | + case ActorIsolation::ActorInstance: |
| 1184 | + case ActorIsolation::Unspecified: |
| 1185 | + // Nothing to do. |
| 1186 | + break; |
| 1187 | + } |
1158 | 1188 | return inferred; |
1159 | 1189 | }; |
1160 | 1190 |
|
|
0 commit comments