Skip to content
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

[Macros] Reproduce issue with peer+extension macro extension's methods not being checked as witnesses #71717

Merged
merged 2 commits into from Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 26 additions & 16 deletions lib/AST/NameLookup.cpp
Expand Up @@ -1844,13 +1844,18 @@ PotentialMacroExpansions PotentialMacroExpansionsInContextRequest::evaluate(
auto containerDecl = container.getAsDecl();
forEachPotentialAttachedMacro(containerDecl, MacroRole::Member, nameTracker);

// If the container is an extension that was created from an extension macro,
// look at the nominal declaration to find any extension macros.
if (auto ext = dyn_cast<ExtensionDecl>(containerDecl)) {
if (auto nominal = nominalForExpandedExtensionDecl(ext)) {
forEachPotentialAttachedMacro(
nominal, MacroRole::Extension, nameTracker);
}
// Extension macros on the type or extension.
{
NominalTypeDecl *nominal = nullptr;
// If the container is an extension that was created from an extension
// macro, look at the nominal declaration to find any extension macros.
if (auto ext = dyn_cast<ExtensionDecl>(containerDecl))
nominal = nominalForExpandedExtensionDecl(ext);
else
nominal = container.getBaseNominal();

if (nominal)
forEachPotentialAttachedMacro(nominal, MacroRole::Extension, nameTracker);
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Awesome, thanks Pavel :)


// Peer and freestanding declaration macros.
Expand Down Expand Up @@ -1911,15 +1916,20 @@ populateLookupTableEntryFromMacroExpansions(ASTContext &ctx,
// names match.
{
MacroIntroducedNameTracker nameTracker;
if (auto ext = dyn_cast<ExtensionDecl>(container.getAsDecl())) {
if (auto nominal = nominalForExpandedExtensionDecl(ext)) {
forEachPotentialAttachedMacro(nominal, MacroRole::Extension, nameTracker);
if (nameTracker.shouldExpandForName(name)) {
(void)evaluateOrDefault(
ctx.evaluator,
ExpandExtensionMacros{nominal},
false);
}
NominalTypeDecl *nominal = nullptr;
// If the container is an extension that was created from an extension
// macro, look at the nominal declaration to find any extension macros.
if (auto ext = dyn_cast<ExtensionDecl>(container.getAsDecl()))
nominal = nominalForExpandedExtensionDecl(ext);
else
nominal = container.getBaseNominal();

if (nominal) {
forEachPotentialAttachedMacro(nominal,
MacroRole::Extension, nameTracker);
if (nameTracker.shouldExpandForName(name)) {
(void)evaluateOrDefault(ctx.evaluator, ExpandExtensionMacros{nominal},
false);
}
}
}
Expand Down
1 change: 0 additions & 1 deletion stdlib/public/Distributed/DistributedMacros.swift
Expand Up @@ -17,7 +17,6 @@ import _Concurrency

#if $Macros

// FIXME: the fact tha this is peer and extension makes the extension's methods not found as witnesses
@attached(peer, names: prefixed(`$`)) // provides $Greeter concrete stub type
@attached(extension, names: arbitrary) // provides extension for Greeter & _DistributedActorStub
public macro _DistributedProtocol() =
Expand Down
Expand Up @@ -11,29 +11,26 @@

import Distributed

// FIXME: the errors below are bugs: the added methods should be considered witnesses rdar://123012943
// expected-note@+1{{in expansion of macro '_DistributedProtocol' on protocol 'Greeter' here}}
@_DistributedProtocol
protocol Greeter: DistributedActor where ActorSystem: DistributedActorSystem<any Codable> {
// FIXME: this is a bug
// expected-note@+1{{protocol requires function 'greet(name:)' with type '(String) -> String'}}
distributed func greet(name: String) -> String
}

// @_DistributedProtocol ->

// CHECK: distributed actor $Greeter<ActorSystem>: Greeter, _DistributedActorStub
// CHECK: where ActorSystem: DistributedActorSystem<any Codable>,
// CHECK: ActorSystem.ActorID: Codable
// CHECK: {
// CHECK: }
// CHECK: distributed actor $Greeter<ActorSystem>: Greeter,
// CHECK-NEXT: Distributed._DistributedActorStub
// CHECK-NEXT: where ActorSystem: DistributedActorSystem<any Codable>,
// CHECK-NEXT: ActorSystem.ActorID: Codable
// CHECK-NEXT: {
// CHECK-NEXT: }

// CHECK: extension Greeter {
// CHECK: distributed func greet(name: String) -> String {
// CHECK: if #available (SwiftStdlib 5.11, *) {
// CHECK: Distributed._distributedStubFatalError()
// CHECK: } else {
// CHECK: fatalError()
// CHECK: }
// CHECK: }
// CHECK: }
// CHECK: extension Greeter where Self: Distributed._DistributedActorStub {
// CHECK-NEXT: distributed func greet(name: String) -> String {
// CHECK-NEXT: if #available (SwiftStdlib 5.11, *) {
// CHECK-NEXT: Distributed._distributedStubFatalError()
// CHECK-NEXT: } else {
// CHECK-NEXT: fatalError()
// CHECK-NEXT: }
// CHECK-NEXT: }
// CHECK-NEXT: }
5 changes: 3 additions & 2 deletions test/Macros/macro_expand_synthesized_members.swift
Expand Up @@ -158,9 +158,10 @@ func callNestedExpansionMember() {
NestedMacroExpansion.member()
}

@attached(peer, names: arbitrary) // introduces `__GenerateStubsForProtocolRequirements
@attached(peer, names: prefixed(`__`)) // introduces `__GenerateStubsForProtocolRequirements
@attached(extension, names: arbitrary) // introduces `extension GenerateStubsForProtocolRequirements`
macro GenerateStubsForProtocolRequirements() = #externalMacro(module: "MacroDefinition", type: "GenerateStubsForProtocolRequirementsMacro")

protocol _TestStub {} // used by 'GenerateStubsForProtocolRequirements'

@GenerateStubsForProtocolRequirements
Expand All @@ -173,4 +174,4 @@ protocol MacroExpansionRequirements {
func testWitnessStub() {
let stub: any MacroExpansionRequirements = __MacroExpansionRequirements()
_ = stub.hello(name: "Caplin")
}
}