@@ -1889,13 +1889,10 @@ static ManagedValue emitBuiltinInjectEnumTag(SILGenFunction &SGF, SILLocation lo
18891889 return ManagedValue::forObjectRValueWithoutOwnership (bi);
18901890}
18911891
1892- static ExtensionDecl *distributedActorAsAnyActorExt = nullptr ;
1893-
18941892// / Find the extension on DistributedActor that defines __actorUnownedExecutor.
18951893static ExtensionDecl *findDistributedActorAsActorExtension (
18961894 ProtocolDecl *distributedActorProto, ModuleDecl *module ) {
18971895 ASTContext &ctx = distributedActorProto->getASTContext ();
1898- #if true
18991896 auto name = ctx.getIdentifier (" __actorUnownedExecutor" );
19001897 auto results = distributedActorProto->lookupDirect (
19011898 name, SourceLoc (),
@@ -1907,53 +1904,73 @@ static ExtensionDecl *findDistributedActorAsActorExtension(
19071904 }
19081905
19091906 return nullptr ;
1910- #else
1911- if (!distributedActorAsAnyActorExt) {
1912- auto ext = ExtensionDecl::create (
1913- ctx, SourceLoc (), nullptr , { }, module , nullptr );
1914- ctx.evaluator .cacheOutput (ExtendedTypeRequest{ext},
1915- distributedActorProto->getDeclaredInterfaceType ());
1916- ctx.evaluator .cacheOutput (ExtendedNominalRequest{ext},
1917- distributedActorProto);
1918-
1919- distributedActorAsAnyActorExt = ext;
1907+ }
1908+
1909+ ProtocolConformanceRef
1910+ SILGenModule::getDistributedActorAsActorConformance (SubstitutionMap subs) {
1911+ ASTContext &ctx = M.getASTContext ();
1912+ auto actorProto = ctx.getProtocol (KnownProtocolKind::Actor);
1913+ Type distributedActorType = subs.getReplacementTypes ()[0 ];
1914+
1915+ if (!distributedActorAsActorConformance) {
1916+ auto distributedActorProto = ctx.getProtocol (KnownProtocolKind::DistributedActor);
1917+ if (!distributedActorProto)
1918+ return ProtocolConformanceRef ();
1919+
1920+ auto ext = findDistributedActorAsActorExtension (
1921+ distributedActorProto, M.getSwiftModule ());
1922+ if (!ext)
1923+ return ProtocolConformanceRef ();
1924+
1925+ // Conformance of DistributedActor to Actor.
1926+ auto genericParam = subs.getGenericSignature ().getGenericParams ()[0 ];
1927+ distributedActorAsActorConformance = ctx.getNormalConformance (
1928+ Type (genericParam), actorProto, SourceLoc (), ext,
1929+ ProtocolConformanceState::Incomplete, /* isUnchecked=*/ false ,
1930+ /* isPreconcurrency=*/ false );
1931+ }
1932+
1933+ return ProtocolConformanceRef (
1934+ actorProto,
1935+ ctx.getSpecializedConformance (distributedActorType,
1936+ distributedActorAsActorConformance,
1937+ subs));
1938+ }
1939+
1940+ void SILGenModule::noteMemberRefExpr (MemberRefExpr *e) {
1941+ VarDecl *var = cast<VarDecl>(e->getMember ().getDecl ());
1942+
1943+ // If the member is the special `asLocalActor` operation on
1944+ // distributed actors, make sure we have the conformance needed
1945+ // for a builtin.
1946+ ASTContext &ctx = var->getASTContext ();
1947+ if (var->getName () == ctx.Id_asLocalActor &&
1948+ var->getDeclContext ()->getSelfProtocolDecl () &&
1949+ var->getDeclContext ()->getSelfProtocolDecl ()
1950+ ->isSpecificProtocol (KnownProtocolKind::DistributedActor)) {
1951+ auto conformance =
1952+ getDistributedActorAsActorConformance (
1953+ e->getMember ().getSubstitutions ());
1954+ useConformance (conformance);
19201955 }
19211956
1922- return distributedActorAsAnyActorExt;
1923- #endif
19241957}
19251958
19261959static ManagedValue emitBuiltinDistributedActorAsAnyActor (
19271960 SILGenFunction &SGF, SILLocation loc, SubstitutionMap subs,
19281961 ArrayRef<ManagedValue> args, SGFContext C) {
19291962 auto &ctx = SGF.getASTContext ();
19301963 auto distributedActor = args[0 ];
1931-
1932- auto builtinDecl = cast<FuncDecl>(getBuiltinValueDecl (
1933- ctx, ctx.getIdentifier (" distributedActorAsAnyActor" )));
1934- auto genericSignature = builtinDecl->getGenericSignature ();
1935- auto genericParam = genericSignature.getGenericParams ()[0 ];
1936-
1937- auto distributedActorProto = ctx.getProtocol (KnownProtocolKind::DistributedActor);
1938- auto ext = findDistributedActorAsActorExtension (
1939- distributedActorProto, SGF.getModule ().getSwiftModule ());
1940-
1941- // Conformance of DistributedActor to Actor.
1942- auto actorProto = ctx.getProtocol (KnownProtocolKind::Actor);
1943- CanType distributedActorType = distributedActor.getType ().getASTType ();
1944- RootProtocolConformance *daAsActorConformance = ctx.getNormalConformance (
1945- Type (genericParam), actorProto, SourceLoc (), ext,
1946- ProtocolConformanceState::Incomplete, /* isUnchecked=*/ false ,
1947- /* isPreconcurrency=*/ false );
1948- ProtocolConformanceRef conformance (
1949- actorProto,
1950- ctx.getSpecializedConformance (distributedActorType, daAsActorConformance,
1951- subs));
1952- ProtocolConformanceRef conformances[1 ] = { conformance };
1964+ ProtocolConformanceRef conformances[1 ] = {
1965+ SGF.SGM .getDistributedActorAsActorConformance (subs)
1966+ };
19531967
19541968 // Erase the distributed actor instance into an `any Actor` existential with
19551969 // the special conformance.
1970+ CanType distributedActorType =
1971+ subs.getReplacementTypes ()[0 ]->getCanonicalType ();
19561972 auto &distributedActorTL = SGF.getTypeLowering (distributedActorType);
1973+ auto actorProto = ctx.getProtocol (KnownProtocolKind::Actor);
19571974 auto &anyActorTL = SGF.getTypeLowering (actorProto->getDeclaredExistentialType ());
19581975 return SGF.emitExistentialErasure (
19591976 loc, distributedActorType, distributedActorTL, anyActorTL,
0 commit comments