Skip to content

Commit ac7f05b

Browse files
committed
[Macros] Apply expanded attributes to the member's semantic attribute list.
This change enables the first end-to-end test for member attribute macros!
1 parent ffa4755 commit ac7f05b

5 files changed

+46
-11
lines changed

lib/Sema/TypeCheckAttr.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -3609,6 +3609,10 @@ void AttributeChecker::visitCustomAttr(CustomAttr *attr) {
36093609
// If the nominal type is a property wrapper type, we can be delegating
36103610
// through a property.
36113611
if (nominal->getAttrs().hasAttribute<PropertyWrapperAttr>()) {
3612+
// FIXME: We shouldn't be type checking missing decls.
3613+
if (isa<MissingDecl>(D))
3614+
return;
3615+
36123616
// property wrappers can only be applied to variables
36133617
if (!isa<VarDecl>(D)) {
36143618
diagnose(attr->getLocation(),
@@ -7414,10 +7418,7 @@ AttachedSemanticAttrsRequest::evaluate(Evaluator &evaluator, Decl *decl) const {
74147418
if (!macroDecl->getMacroRoles().contains(MacroRole::MemberAttribute))
74157419
continue;
74167420

7417-
// Expand the attributes.
7418-
expandAttributes(customAttr, macroDecl, decl);
7419-
7420-
// TODO: append the expanded attributes to 'semanticAttrs'.
7421+
expandAttributes(customAttr, macroDecl, decl, semanticAttrs);
74217422
}
74227423

74237424
return semanticAttrs;

lib/Sema/TypeCheckDeclPrimary.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -1993,7 +1993,10 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
19931993
}
19941994

19951995
void visitMissingDecl(MissingDecl *missing) {
1996-
// nothing to do for missing decls.
1996+
// FIXME: Expanded attribute lists should be type checked against
1997+
// the real declaration they will be attached to. Attempting to
1998+
// type check a missing decl should produce an error.
1999+
TypeChecker::checkDeclAttributes(missing);
19972000
}
19982001

19992002
void visitMissingMemberDecl(MissingMemberDecl *MMD) {

lib/Sema/TypeCheckMacros.cpp

+14-3
Original file line numberDiff line numberDiff line change
@@ -842,8 +842,8 @@ void swift::expandAccessors(
842842
// FIXME: Almost entirely duplicated code from `expandAccessors`.
843843
// Factor this out into an `expandAttachedMacro` function, with
844844
// arguments for the PrettyStackTrace string, 'attachedTo' decl, etc.
845-
void swift::expandAttributes(
846-
CustomAttr *attr, MacroDecl *macro, Decl *member) {
845+
void swift::expandAttributes(CustomAttr *attr, MacroDecl *macro, Decl *member,
846+
SemanticDeclAttributes &result) {
847847
auto *dc = member->getInnermostDeclContext();
848848
ASTContext &ctx = dc->getASTContext();
849849
SourceManager &sourceMgr = ctx.SourceMgr;
@@ -1004,6 +1004,17 @@ void swift::expandAttributes(
10041004
"type checking expanded declaration macro", member);
10051005

10061006
auto topLevelDecls = macroSourceFile->getTopLevelDecls();
1007+
for (auto decl : topLevelDecls) {
1008+
// FIXME: We want to type check decl attributes applied to
1009+
// the real declaration, ideally by appending the new attributes
1010+
// to the result and changing TypeChecker::checkDeclAttributes
1011+
// to use the semantic attribute list.
1012+
decl->setDeclContext(dc);
1013+
TypeChecker::typeCheckDecl(decl);
10071014

1008-
// TODO: Return the attribute lists attached to top-level decls.
1015+
// Add the new attributes to the semantic attribute list.
1016+
for (auto *attr : decl->getAttrs()) {
1017+
result.add(attr);
1018+
}
1019+
}
10091020
}

lib/Sema/TypeCheckMacros.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#ifndef SWIFT_SEMA_TYPECHECKMACROS_H
1717
#define SWIFT_SEMA_TYPECHECKMACROS_H
1818

19+
#include "swift/AST/Attr.h"
1920
#include "swift/AST/ConcreteDeclRef.h"
2021
#include "swift/AST/Type.h"
2122

@@ -50,8 +51,8 @@ void expandAccessors(
5051

5152
/// Expand the attributes for the given member declaration based
5253
/// on the custom attribute that references the given macro.
53-
void expandAttributes(
54-
CustomAttr *attr, MacroDecl *macro, Decl *member);
54+
void expandAttributes(CustomAttr *attr, MacroDecl *macro, Decl *member,
55+
SemanticDeclAttributes &result);
5556

5657
} // end namespace swift
5758

test/Macros/macro_expand_attributes.swift

+20-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// 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/syntax_macro_definitions.swift -g -no-toolchain-stdlib-rpath
33
// RUNx: %target-swift-frontend -dump-ast -enable-experimental-feature Macros -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir %s -module-name MacroUser 2>&1 | %FileCheck --check-prefix CHECK-AST %s
44
// RUN: %target-typecheck-verify-swift -enable-experimental-feature Macros -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir -module-name MacroUser -DTEST_DIAGNOSTICS -swift-version 5
5+
// RUN: %target-build-swift -enable-experimental-feature Macros -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir -L %swift-host-lib-dir %s -o %t/main -module-name MacroUser -swift-version 5
6+
// RUN: %target-run %t/main | %FileCheck %s
7+
// REQUIRES: executable_test
58

69
// FIXME: Swift parser is not enabled on Linux CI yet.
710
// REQUIRES: OS=macosx
@@ -10,11 +13,27 @@
1013

1114
@propertyWrapper
1215
struct Wrapper<T> {
13-
var wrappedValue: T
16+
init(wrappedValue: T) {
17+
print("initializing wrapper with \(wrappedValue)")
18+
self.wrappedValue = wrappedValue
19+
}
20+
var wrappedValue: T {
21+
willSet { print("setting \(newValue)") }
22+
}
1423
}
1524

1625
@wrapAllProperties
1726
struct S {
1827
var x: Int
1928
var y: Int
2029
}
30+
31+
// CHECK: initializing wrapper with 0
32+
// CHECK: initializing wrapper with 1
33+
var s = S(x: 0, y: 1)
34+
35+
// CHECK: setting 10
36+
s.x = 10
37+
38+
// CHECK: setting 100
39+
s.y = 100

0 commit comments

Comments
 (0)