mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
Merge pull request #74698 from artemcm/ConstExtractConformanceExtractionFix
[Compile Time Constant Extraction] Query nominal type *conformances* to get at the protocols, instead of `getAllProtocols()`.
This commit is contained in:
@@ -1081,12 +1081,13 @@ void writeProperties(llvm::json::OStream &JSON,
|
||||
void writeConformances(llvm::json::OStream &JSON,
|
||||
const NominalTypeDecl &NomTypeDecl) {
|
||||
JSON.attributeArray("conformances", [&] {
|
||||
for (auto *Protocol : NomTypeDecl.getAllProtocols()) {
|
||||
for (auto *Conformance : NomTypeDecl.getAllConformances()) {
|
||||
auto Proto = Conformance->getProtocol();
|
||||
// FIXME(noncopyable_generics): Should these be included?
|
||||
if (Protocol->getInvertibleProtocolKind())
|
||||
if (Proto->getInvertibleProtocolKind())
|
||||
continue;
|
||||
|
||||
JSON.value(toFullyQualifiedProtocolNameString(*Protocol));
|
||||
JSON.value(toFullyQualifiedProtocolNameString(*Proto));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
28
test/ConstExtraction/ExtractMacroExpandedConformances.swift
Normal file
28
test/ConstExtraction/ExtractMacroExpandedConformances.swift
Normal file
@@ -0,0 +1,28 @@
|
||||
// REQUIRES: swift_swift_parser
|
||||
// RUN: %empty-directory(%t)
|
||||
// RUN: echo "[MyProto]" > %t/protocols.json
|
||||
|
||||
// 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
|
||||
|
||||
// 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)
|
||||
// RUN: cat %t/ExtractFromMacroExpansion.swiftconstvalues 2>&1 | %FileCheck %s
|
||||
|
||||
protocol MyProto { }
|
||||
protocol MyExtraProto { }
|
||||
|
||||
@attached(extension, conformances: MyProto, MyExtraProto)
|
||||
macro specificExtensionMacro() = #externalMacro(module: "MacroDefinition", type: "AddSpecificExtensionMacro")
|
||||
|
||||
@specificExtensionMacro
|
||||
struct MyStruct {
|
||||
struct Inner { }
|
||||
}
|
||||
|
||||
// CHECK: "typeName": "ExtractMacroExpandedConformances.MyStruct",
|
||||
// CHECK: "mangledTypeName": "32ExtractMacroExpandedConformances8MyStructV",
|
||||
// CHECK: "kind": "struct",
|
||||
// CHECK: "conformances": [
|
||||
// CHECK-DAG: "Swift.Sendable",
|
||||
// CHECK-DAG: "Swift.BitwiseCopyable",
|
||||
// CHECK-DAG: "ExtractMacroExpandedConformances.MyProto"
|
||||
// CHECK-NOT: "ExtractMacroExpandedConformances.MyExtraProto"
|
||||
@@ -61,6 +61,23 @@ public struct AddExtensionMacro: ExtensionMacro {
|
||||
}
|
||||
}
|
||||
|
||||
public struct AddSpecificExtensionMacro: ExtensionMacro {
|
||||
public static func expansion(
|
||||
of node: AttributeSyntax,
|
||||
attachedTo declaration: some DeclGroupSyntax,
|
||||
providingExtensionsOf type: some TypeSyntaxProtocol,
|
||||
conformingTo protocols: [TypeSyntax],
|
||||
in context: some MacroExpansionContext
|
||||
) throws -> [ExtensionDeclSyntax] {
|
||||
var extensions = [ExtensionDeclSyntax]()
|
||||
let protocolNames = Set(protocols.compactMap { $0.as(IdentifierTypeSyntax.self)?.name.text })
|
||||
if protocolNames.contains("MyProto") {
|
||||
extensions.append(try ExtensionDeclSyntax("extension \(type.trimmed): MyProto") { })
|
||||
}
|
||||
return extensions
|
||||
}
|
||||
}
|
||||
|
||||
public struct AddPeerVarMacro: PeerMacro {
|
||||
public static func expansion(
|
||||
of node: AttributeSyntax,
|
||||
|
||||
Reference in New Issue
Block a user