Skip to content

Commit 708fb13

Browse files
committed
Extract decl in macro added extensions
1 parent 87bde40 commit 708fb13

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

lib/ConstExtract/ConstExtract.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,11 @@ gatherConstValuesForPrimary(const std::unordered_set<std::string> &Protocols,
560560
ConformanceDecls);
561561
for (auto D : SF->getTopLevelDecls())
562562
D->walk(ConformanceCollector);
563+
// Visit macro expanded extensions
564+
if (auto *synthesizedSF = SF->getSynthesizedFile())
565+
for (auto D : synthesizedSF->getTopLevelDecls())
566+
if (isa<ExtensionDecl>(D))
567+
D->walk(ConformanceCollector);
563568

564569
for (auto *CD : ConformanceDecls)
565570
Result.emplace_back(evaluateOrDefault(

test/ConstExtraction/ExtractFromMacroExpansion.swift

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ macro AddMacroAddedStruct() = #externalMacro(module: "MacroDefinition", type: "A
1515
@freestanding(declaration, names: named(macroAddedVar))
1616
macro AddMacroAddedVar() = #externalMacro(module: "MacroDefinition", type: "AddVarDeclMacro")
1717

18-
@attached(extension, conformances: MyProto, names: prefixed(_extension_))
18+
@attached(extension, conformances: MyProto, names: prefixed(_extension_), named(_Extension_MyProto))
1919
macro AddExtension() = #externalMacro(module: "MacroDefinition", type: "AddExtensionMacro")
2020

2121
@attached(peer, names: prefixed(_peer_))
@@ -175,3 +175,42 @@ extension MyStruct {
175175
// CHECK: "type": "Swift.Int",
176176
// CHECK: "valueKind": "RawLiteral",
177177
// CHECK: "value": "3"
178+
179+
180+
// CHECK: "typeName": "ExtractFromMacroExpansion.MacroAddedStruct._Extension_MyProto",
181+
// CHECK: "properties": [
182+
// CHECK: "label": "nested",
183+
// CHECK: "type": "Swift.Int",
184+
// CHECK: "valueKind": "RawLiteral",
185+
// CHECK: "value": "8"
186+
187+
188+
// CHECK: "typeName": "ExtractFromMacroExpansion._Peer_MyStruct._Extension_MyProto",
189+
// CHECK: "properties": [
190+
// CHECK: "label": "nested",
191+
// CHECK: "type": "Swift.Int",
192+
// CHECK: "valueKind": "RawLiteral",
193+
// CHECK: "value": "8"
194+
195+
// CHECK: "typeName": "ExtractFromMacroExpansion.MyStruct._Extension_MyProto",
196+
// CHECK: "properties": [
197+
// CHECK: "label": "nested",
198+
// CHECK: "type": "Swift.Int",
199+
// CHECK: "valueKind": "RawLiteral",
200+
// CHECK: "value": "8"
201+
202+
203+
// CHECK: "typeName": "ExtractFromMacroExpansion.MyStruct._Peer_Inner._Extension_MyProto",
204+
// CHECK: "properties": [
205+
// CHECK: "label": "nested",
206+
// CHECK: "type": "Swift.Int",
207+
// CHECK: "valueKind": "RawLiteral",
208+
// CHECK: "value": "8"
209+
210+
211+
// CHECK: "typeName": "ExtractFromMacroExpansion.MyStruct.Inner._Extension_MyProto",
212+
// CHECK: "properties": [
213+
// CHECK: "label": "nested",
214+
// CHECK: "type": "Swift.Int",
215+
// CHECK: "valueKind": "RawLiteral",
216+
// CHECK: "value": "8"

test/ConstExtraction/Inputs/Macros.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,14 @@ public struct AddExtensionMacro: ExtensionMacro {
4343
) throws -> [ExtensionDeclSyntax] {
4444
let typeName = declaration.declGroupName
4545
return protocols.map {
46-
("extension \(typeName): \($0) { }" as DeclSyntax)
47-
.cast(ExtensionDeclSyntax.self)
46+
("""
47+
extension \(typeName): \($0) {
48+
struct _Extension_\($0): \($0) {
49+
var nested = 8
50+
}
51+
}
52+
""" as DeclSyntax)
53+
.cast(ExtensionDeclSyntax.self)
4854
} + [
4955
("""
5056
extension \(typeName) {

0 commit comments

Comments
 (0)