Skip to content

Commit 20f51b2

Browse files
authored
Merge pull request #63075 from hborla/macro-attribute-serialization
[Serialization] Don't serialize macro custom attributes.
2 parents 4e7d311 + ae1f824 commit 20f51b2

File tree

6 files changed

+77
-7
lines changed

6 files changed

+77
-7
lines changed

include/swift/AST/Attr.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,6 +1702,10 @@ class CustomAttr final : public DeclAttribute {
17021702
bool isArgUnsafe() const;
17031703
void setArgIsUnsafe(bool unsafe) { isArgUnsafeBit = unsafe; }
17041704

1705+
/// Whether this custom attribute is a macro attached to the given
1706+
/// declaration.
1707+
bool isAttachedMacro(const Decl *decl) const;
1708+
17051709
Expr *getSemanticInit() const { return semanticInit; }
17061710
void setSemanticInit(Expr *expr) { semanticInit = expr; }
17071711

lib/AST/Attr.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "swift/AST/IndexSubset.h"
2424
#include "swift/AST/LazyResolver.h"
2525
#include "swift/AST/Module.h"
26+
#include "swift/AST/NameLookupRequests.h"
2627
#include "swift/AST/ParameterList.h"
2728
#include "swift/AST/TypeCheckRequests.h"
2829
#include "swift/AST/TypeRepr.h"
@@ -2336,6 +2337,21 @@ bool CustomAttr::isArgUnsafe() const {
23362337
return isArgUnsafeBit;
23372338
}
23382339

2340+
bool CustomAttr::isAttachedMacro(const Decl *decl) const {
2341+
auto &ctx = decl->getASTContext();
2342+
auto *dc = decl->getInnermostDeclContext();
2343+
2344+
auto attrDecl = evaluateOrDefault(
2345+
ctx.evaluator,
2346+
CustomAttrDeclRequest{const_cast<CustomAttr *>(this), dc},
2347+
nullptr);
2348+
2349+
if (!attrDecl)
2350+
return false;
2351+
2352+
return attrDecl.dyn_cast<MacroDecl *>();
2353+
}
2354+
23392355
DeclarationAttr::DeclarationAttr(SourceLoc atLoc, SourceRange range,
23402356
MacroRole role,
23412357
ArrayRef<MacroIntroducedDeclName> peerNames,

lib/Serialization/Serialization.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2855,6 +2855,11 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
28552855
case DAK_Custom: {
28562856
auto abbrCode = S.DeclTypeAbbrCodes[CustomDeclAttrLayout::Code];
28572857
auto theAttr = cast<CustomAttr>(DA);
2858+
2859+
// Macro attributes are not serialized.
2860+
if (theAttr->isAttachedMacro(D))
2861+
return;
2862+
28582863
auto typeID = S.addTypeRef(theAttr->getType());
28592864
if (!typeID && !S.allowCompilerErrors()) {
28602865
llvm::PrettyStackTraceString message("CustomAttr has no type");
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import SwiftSyntax
2+
import SwiftSyntaxBuilder
3+
import _SwiftSyntaxMacros
4+
5+
public struct StringifyMacro: ExpressionMacro {
6+
public static func expansion(
7+
of macro: MacroExpansionExprSyntax, in context: inout MacroExpansionContext
8+
) -> ExprSyntax {
9+
guard let argument = macro.argumentList.first?.expression else {
10+
// FIXME: Create a diagnostic for the missing argument?
11+
return ExprSyntax(macro)
12+
}
13+
14+
return "(\(argument), \(StringLiteralExprSyntax(content: argument.description)))"
15+
}
16+
}
17+
18+
public struct MyWrapperMacro: AccessorDeclarationMacro {
19+
public static func expansion(
20+
of node: AttributeSyntax,
21+
attachedTo declaration: DeclSyntax,
22+
in context: inout MacroExpansionContext
23+
) throws -> [AccessorDeclSyntax] {
24+
return []
25+
}
26+
}
27+
28+
public struct WrapAllProperties: MemberAttributeMacro {
29+
public static func expansion(
30+
of node: AttributeSyntax,
31+
attachedTo parent: DeclSyntax,
32+
annotating member: DeclSyntax,
33+
in context: inout MacroExpansionContext
34+
) throws -> [AttributeSyntax] {
35+
return []
36+
}
37+
}
38+
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1-
@expression public macro publicStringify<T>(_ value: T) -> (T, String) = SomeModule.StringifyMacro
1+
@expression public macro publicStringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "MacroDefinition", type: "StringifyMacro")
22

3-
@expression macro internalStringify<T>(_ value: T) -> (T, String) = SomeModule.StringifyMacro
3+
@expression macro internalStringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "MacroDefinition", type: "StringifyMacro")
44

5-
@attached(accessor) public macro myWrapper: Void = SomeModule.MyWrapperMacro
5+
@attached(accessor) public macro myWrapper: Void = #externalMacro(module: "MacroDefinition", type: "MyWrapperMacro")
6+
7+
@attached(memberAttributes) public macro wrapAllProperties() = #externalMacro(module: "MacroDefinition", type: "WrapAllProperties")
8+
9+
// Make sure that macro custom attributes are not serialized.
10+
@wrapAllProperties
11+
public struct S {
12+
public var value: Int
13+
}

test/Serialization/macros.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,20 @@
22

33
// RUN: %empty-directory(%t)
44
// RUN: %empty-directory(%t-scratch)
5-
// RUN: %target-swift-frontend -emit-module -o %t/def_macros.swiftmodule %S/Inputs/def_macros.swift -module-name def_macros -enable-experimental-feature Macros
6-
// RUN: %target-swift-frontend -typecheck -I%t -verify %s -verify-ignore-unknown -enable-experimental-feature Macros
5+
// RUN: %target-build-swift -I %swift-host-lib-dir -L %swift-host-lib-dir -emit-library -o %t/%target-library-name(MacroDefinition) -module-name=MacroDefinition %S/Inputs/def_macro_plugin.swift -g -no-toolchain-stdlib-rpath
6+
// RUN: %target-swift-frontend -emit-module -o %t/def_macros.swiftmodule %S/Inputs/def_macros.swift -module-name def_macros -enable-experimental-feature Macros -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir
7+
// RUN: %target-swift-frontend -typecheck -I%t -verify %s -verify-ignore-unknown -enable-experimental-feature Macros -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir
78
// REQUIRES: OS=macosx
89

910
import def_macros
1011

1112
func test(a: Int, b: Int) {
1213
_ = #publicStringify(a + b)
13-
// expected-error@-1{{external macro implementation type 'SomeModule.StringifyMacro' could not be found for macro 'publicStringify'; the type must be public and provided via '-load-plugin-library'}}
1414

1515
_ = #internalStringify(a + b)
1616
// expected-error@-1{{no macro named 'internalStringify'}}
1717
}
1818

1919
struct TestStruct {
2020
@myWrapper var x: Int
21-
// expected-error@-1{{external macro implementation type 'SomeModule.MyWrapperMacro' could not be found for macro 'myWrapper'}}
2221
}

0 commit comments

Comments
 (0)