Skip to content

Commit 8d22e48

Browse files
authored
Merge pull request #70185 from hborla/sendable-extension-macro
[Concurrency] Check the outermost parent source file when diagnosing `Sendable` conformances outside the defining source file.
2 parents 5b4fb48 + 4f68514 commit 8d22e48

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5135,8 +5135,9 @@ bool swift::checkSendableConformance(
51355135
auto conformanceDecl = conformanceDC->getAsDecl();
51365136
auto behavior = SendableCheckContext(conformanceDC, check)
51375137
.defaultDiagnosticBehavior();
5138-
if (conformanceDC->getParentSourceFile() &&
5139-
conformanceDC->getParentSourceFile() != nominal->getParentSourceFile()) {
5138+
if (conformanceDC->getOutermostParentSourceFile() &&
5139+
conformanceDC->getOutermostParentSourceFile() !=
5140+
nominal->getOutermostParentSourceFile()) {
51405141
conformanceDecl->diagnose(diag::concurrent_value_outside_source_file,
51415142
nominal)
51425143
.limitBehavior(behavior);

test/Macros/Inputs/syntax_macro_definitions.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2057,6 +2057,31 @@ extension RequiredDefaultInitMacro: MemberMacro {
20572057
}
20582058
}
20592059

2060+
public struct SendableMacro: ExtensionMacro {
2061+
public static func expansion(
2062+
of node: AttributeSyntax,
2063+
attachedTo decl: some DeclGroupSyntax,
2064+
providingExtensionsOf type: some TypeSyntaxProtocol,
2065+
conformingTo protocols: [TypeSyntax],
2066+
in context: some MacroExpansionContext
2067+
) throws -> [ExtensionDeclSyntax] {
2068+
if protocols.isEmpty {
2069+
return []
2070+
}
2071+
2072+
let decl: DeclSyntax =
2073+
"""
2074+
extension \(type.trimmed): Sendable {
2075+
}
2076+
2077+
"""
2078+
2079+
return [
2080+
decl.cast(ExtensionDeclSyntax.self)
2081+
]
2082+
}
2083+
}
2084+
20602085
public struct FakeCodeItemMacro: DeclarationMacro, PeerMacro {
20612086
public static func expansion(
20622087
of node: some FreestandingMacroExpansionSyntax,

test/Macros/macro_expand_extensions.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,15 @@ func testStringFoo(s: String) {
244244
"Test".printFoo()
245245
s.printFoo()
246246
}
247+
248+
@attached(extension, conformances: Sendable)
249+
macro AddSendable() = #externalMacro(module: "MacroDefinition", type: "SendableMacro")
250+
251+
@AddSendable
252+
final class SendableClass {
253+
}
254+
255+
// expected-warning@+2 {{non-final class 'InvalidSendableClass' cannot conform to 'Sendable'; use '@unchecked Sendable'}}
256+
@AddSendable
257+
class InvalidSendableClass {
258+
}

0 commit comments

Comments
 (0)