File tree Expand file tree Collapse file tree 3 files changed +49
-3
lines changed Expand file tree Collapse file tree 3 files changed +49
-3
lines changed Original file line number Diff line number Diff line change @@ -949,12 +949,13 @@ void writeProperties(llvm::json::OStream &JSON,
949949void writeConformances (llvm::json::OStream &JSON,
950950 const NominalTypeDecl &NomTypeDecl) {
951951 JSON.attributeArray (" conformances" , [&] {
952- for (auto *Protocol : NomTypeDecl.getAllProtocols ()) {
952+ for (auto *Conformance : NomTypeDecl.getAllConformances ()) {
953+ auto Proto = Conformance->getProtocol ();
953954 // FIXME(noncopyable_generics): Should these be included?
954- if (Protocol ->getInvertibleProtocolKind ())
955+ if (Proto ->getInvertibleProtocolKind ())
955956 continue ;
956957
957- JSON.value (toFullyQualifiedProtocolNameString (*Protocol ));
958+ JSON.value (toFullyQualifiedProtocolNameString (*Proto ));
958959 }
959960 });
960961}
Original file line number Diff line number Diff line change 1+ // REQUIRES: swift_swift_parser
2+ // RUN: %empty-directory(%t)
3+ // RUN: echo "[MyProto]" > %t/protocols.json
4+
5+ // RUN: %host-build-swift -swift-version 5 -emit-library -o %t/%target-library-name(MacroDefinition) -module-name=MacroDefinition %S/Inputs/Macros.swift -g -no-toolchain-stdlib-rpath
6+
7+ // RUN: %target-swift-frontend -typecheck -emit-const-values-path %t/ExtractFromMacroExpansion.swiftconstvalues -const-gather-protocols-file %t/protocols.json -primary-file %s -load-plugin-library %t/%target-library-name(MacroDefinition)
8+ // RUN: cat %t/ExtractFromMacroExpansion.swiftconstvalues 2>&1 | %FileCheck %s
9+
10+ protocol MyProto { }
11+ protocol MyExtraProto { }
12+
13+ @attached ( extension, conformances: MyProto, MyExtraProto)
14+ macro specificExtensionMacro( ) = #externalMacro( module: " MacroDefinition " , type: " AddSpecificExtensionMacro " )
15+
16+ @specificExtensionMacro
17+ struct MyStruct {
18+ struct Inner { }
19+ }
20+
21+ // CHECK: "typeName": "ExtractMacroExpandedConformances.MyStruct",
22+ // CHECK: "mangledTypeName": "32ExtractMacroExpandedConformances8MyStructV",
23+ // CHECK: "kind": "struct",
24+ // CHECK: "conformances": [
25+ // CHECK-DAG: "Swift.Sendable",
26+ // CHECK-DAG: "Swift.BitwiseCopyable",
27+ // CHECK-DAG: "ExtractMacroExpandedConformances.MyProto"
28+ // CHECK-NOT: "ExtractMacroExpandedConformances.MyExtraProto"
Original file line number Diff line number Diff line change @@ -61,6 +61,23 @@ public struct AddExtensionMacro: ExtensionMacro {
6161 }
6262}
6363
64+ public struct AddSpecificExtensionMacro : ExtensionMacro {
65+ public static func expansion(
66+ of node: AttributeSyntax ,
67+ attachedTo declaration: some DeclGroupSyntax ,
68+ providingExtensionsOf type: some TypeSyntaxProtocol ,
69+ conformingTo protocols: [ TypeSyntax ] ,
70+ in context: some MacroExpansionContext
71+ ) throws -> [ ExtensionDeclSyntax ] {
72+ var extensions = [ ExtensionDeclSyntax] ( )
73+ let protocolNames = Set ( protocols. compactMap { $0. as ( IdentifierTypeSyntax . self) ? . name. text } )
74+ if protocolNames. contains ( " MyProto " ) {
75+ extensions. append ( try ExtensionDeclSyntax ( " extension \( type. trimmed) : MyProto " ) { } )
76+ }
77+ return extensions
78+ }
79+ }
80+
6481public struct AddPeerVarMacro : PeerMacro {
6582 public static func expansion(
6683 of node: AttributeSyntax ,
You can’t perform that action at this time.
0 commit comments