Skip to content

[Distributed] Only synthesize Codable for DA where the ID is Codable #72081

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 8, 2024

Conversation

ktoso
Copy link
Contributor

@ktoso ktoso commented Mar 5, 2024

This is a bug in synthesis we only noticed recently. The design is such that a distributed actor shall be Codable when the ID is Codable. But we wrongly always added the conformance and thus synthesis would fail when IDs were not Codable.

This forced adopters to make their ID types Codable even though they were not using Codable at all.

The implementations come from:

@available(SwiftStdlib 5.7, *)
extension DistributedActor /*: implicitly Decodable */ where Self.ID: Decodable { ... }
@available(SwiftStdlib 5.7, *)
extension DistributedActor /*: implicitly Encodable */ where Self.ID: Encodable { ... } 

We are not extending this implicit conformance to other types, so this is just a fix of behavior.

resolves rdar://122930345

@ktoso
Copy link
Contributor Author

ktoso commented Mar 5, 2024

I wonder if @ser-0xff @hassila have hit this issue in https://github.com/ordo-one/package-distributed-system since it seems you use your own serialization requirement but had to make the actor and id codable anyway.

This should remove forcing people into this un-necessary conformance.

@ktoso
Copy link
Contributor Author

ktoso commented Mar 5, 2024

@swift-ci please smoke test

if (!systemTy->getAnyNominal())
return false;

auto idTy = getDistributedActorSystemActorIDType(systemTy->getAnyNominal());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actor system doesn't have to be a concrete nominal type for ActorID to be available (i.e. I think it could be stated as a same-type requirement?), maybe better use getAssociatedTypeOfDistributedSystemOfActor or directly substitute ActorSystem.ActorID dependent member?...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, I'll add a test like that and cover it. Same type requirement should be the case you're mentioning yeah

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heh weirdly enough facing term verification crashes that I don't quite understand when trying to use the getAssociatedTypeOfDistributedSystemOfActor here... I dug around for a while but will need to ask @slavapestov for a hint I think

@ktoso ktoso force-pushed the wip-fix-implicit-codable-da branch from e66378d to 15cfbe2 Compare March 5, 2024 08:30
@ser-0xff
Copy link

ser-0xff commented Mar 5, 2024

In the our implementation the ID type was codable from the very beginning because we transmitted it between server and client, but recently we made some changes so do not transmit the whole ID any more. Probably will make sense to remove that conformance.
Thank you for the update.

@ktoso
Copy link
Contributor Author

ktoso commented Mar 5, 2024

@swift-ci please smoke test

@ktoso
Copy link
Contributor Author

ktoso commented Mar 5, 2024

I see, thanks @ser-0xff -- I thought you had run into this issue and were forced to do Codable there.
For what it's worth this should enable you to remove the Codable conformance then.

@ktoso
Copy link
Contributor Author

ktoso commented Mar 5, 2024

@swift-ci please smoke test Linux

1 similar comment
@ktoso
Copy link
Contributor Author

ktoso commented Mar 5, 2024

@swift-ci please smoke test Linux

@ktoso
Copy link
Contributor Author

ktoso commented Mar 5, 2024

@swift-ci please smoke test Windows

@ktoso ktoso added the distributed Feature → concurrency: distributed actor label Mar 6, 2024
@ktoso ktoso force-pushed the wip-fix-implicit-codable-da branch from 15cfbe2 to 738a018 Compare March 7, 2024 11:40
@ktoso
Copy link
Contributor Author

ktoso commented Mar 7, 2024

This doesn't handle the

protocol Protocol_SerNotCodable_IdCodable: DistributedActor
  where ActorSystem == FakeCustomSerializationRoundtripActorSystem { }

yet but I'm working on it.

Seems the new code in getAssociatedTypeOfDistributedSystemOfActor:

  auto *actorType = actorOrExtension->getSelfNominalTypeDecl();
  if (isa<ProtocolDecl>(actorType))
    return memberTy->getReducedType(sig);

still isn't quite good enough:

(lldb) e sig->getReducedType(memberTy)
Term verification failed
Initial term:    τ_0_0.[DistributedActor:ActorSystem]
Erased term:     τ_0_0.ActorSystem
Simplified term: τ_0_0.[Protocol_SerNotCodable_IdCodable:ActorSystem]

Requirement machine for <τ_0_0 where τ_0_0 : Protocol_SerNotCodable_IdCodable>
Rewrite system: {
- [Protocol_SerNotCodable_IdCodable].[Protocol_SerNotCodable_IdCodable] => [Protocol_SerNotCodable_IdCodable] [permanent]
- [Protocol_SerNotCodable_IdCodable].ActorSystem => [Protocol_SerNotCodable_IdCodable:ActorSystem] [permanent]
- [Protocol_SerNotCodable_IdCodable].SerializationRequirement => [Protocol_SerNotCodable_IdCodable:SerializationRequirement] [permanent]
- [Protocol_SerNotCodable_IdCodable].ID => [Protocol_SerNotCodable_IdCodable:ID] [permanent]
- [Protocol_SerNotCodable_IdCodable].[DistributedActor] => [Protocol_SerNotCodable_IdCodable] [explicit]
- [Protocol_SerNotCodable_IdCodable].[Protocol_SerNotCodable_IdCodable:ActorSystem] => [Protocol_SerNotCodable_IdCodable:ActorSystem]
- [Protocol_SerNotCodable_IdCodable].[Protocol_SerNotCodable_IdCodable:SerializationRequirement] => [Protocol_SerNotCodable_IdCodable:SerializationRequirement]
- [Protocol_SerNotCodable_IdCodable].[Protocol_SerNotCodable_IdCodable:ID] => [Protocol_SerNotCodable_IdCodable:ID]
- τ_0_0.[Protocol_SerNotCodable_IdCodable] => τ_0_0 [explicit]
- τ_0_0.ActorSystem => τ_0_0.[Protocol_SerNotCodable_IdCodable:ActorSystem]
- τ_0_0.SerializationRequirement => τ_0_0.[Protocol_SerNotCodable_IdCodable:SerializationRequirement]
- τ_0_0.ID => τ_0_0.[Protocol_SerNotCodable_IdCodable:ID]
- τ_0_0.[DistributedActor] => τ_0_0
}
Property map: {
  [Protocol_SerNotCodable_IdCodable] => { conforms_to: [Protocol_SerNotCodable_IdCodable DistributedActor] }
  τ_0_0 => { conforms_to: [Protocol_SerNotCodable_IdCodable DistributedActor] }
}
Conformance paths: {
}

@ktoso
Copy link
Contributor Author

ktoso commented Mar 7, 2024

@swift-ci please smoke test

@ktoso
Copy link
Contributor Author

ktoso commented Mar 8, 2024

Turns out we actually don't want to support the inference of Codable for a protocol anyway -- and it would have been cyclic.

So we're good as soon as CI is happy from unrelated issues

@ktoso
Copy link
Contributor Author

ktoso commented Mar 8, 2024

@swift-ci please smoke test macOS

@ktoso ktoso enabled auto-merge (squash) March 8, 2024 04:06
@ktoso ktoso merged commit b7ff16b into swiftlang:main Mar 8, 2024
@ktoso ktoso deleted the wip-fix-implicit-codable-da branch March 8, 2024 06:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
distributed Feature → concurrency: distributed actor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants