Skip to content

Walk auxiliary declarations when gathering stored properties #65509

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions lib/Sema/TypeCheckStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,23 @@ static void computeLoweredStoredProperties(NominalTypeDecl *decl,
static void enumerateStoredPropertiesAndMissing(
NominalTypeDecl *decl,
IterableDeclContext *implDecl,
llvm::function_ref<void(VarDecl *)> addStoredProperty,
llvm::function_ref<void(VarDecl *)> _addStoredProperty,
llvm::function_ref<void(MissingMemberDecl *)> addMissing) {
// Add a variable as a stored properties.
llvm::SmallSet<VarDecl *, 8> knownStoredProperties;
auto addStoredProperty = [&](VarDecl *var) {
if (!var->isStatic() && var->hasStorage()) {
if (knownStoredProperties.insert(var).second)
_addStoredProperty(var);
}
};

// If we have a distributed actor, find the id and actorSystem
// properties. We always want them first, and in a specific
// order.
VarDecl *distributedActorId = nullptr;
VarDecl *distributedActorSystem = nullptr;
if (decl->isDistributedActor()) {
VarDecl *distributedActorId = nullptr;
VarDecl *distributedActorSystem = nullptr;
ASTContext &ctx = decl->getASTContext();
for (auto *member : implDecl->getMembers()) {
if (auto *var = dyn_cast<VarDecl>(member)) {
Expand All @@ -201,17 +210,14 @@ static void enumerateStoredPropertiesAndMissing(

for (auto *member : implDecl->getMembers()) {
if (auto *var = dyn_cast<VarDecl>(member)) {
if (!var->isStatic() && var->hasStorage()) {
// Skip any properties that we already emitted explicitly
if (var == distributedActorId)
continue;
if (var == distributedActorSystem)
continue;

addStoredProperty(var);
}
addStoredProperty(var);
}

member->visitAuxiliaryDecls([&](Decl *auxDecl) {
if (auto auxVar = dyn_cast<VarDecl>(auxDecl))
addStoredProperty(auxVar);
});

if (auto missing = dyn_cast<MissingMemberDecl>(member))
if (missing->getNumberOfFieldOffsetVectorEntries() > 0)
addMissing(missing);
Expand Down
23 changes: 23 additions & 0 deletions test/Macros/Inputs/syntax_macro_definitions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,29 @@ extension PropertyWrapperMacro: AccessorMacro, Macro {
}
}

extension PropertyWrapperMacro: PeerMacro {
public static func expansion(
of node: AttributeSyntax,
providingPeersOf declaration: some DeclSyntaxProtocol,
in context: some MacroExpansionContext
) throws -> [DeclSyntax] {
guard let varDecl = declaration.as(VariableDeclSyntax.self),
let binding = varDecl.bindings.first,
let identifier = binding.pattern.as(IdentifierPatternSyntax.self)?.identifier,
binding.accessor == nil,
let type = binding.typeAnnotation?.type
else {
return []
}

return [
"""
var _\(raw: identifier.trimmedDescription): MyWrapperThingy<\(type)>
"""
]
}
}

public struct WrapAllProperties: MemberAttributeMacro {
public static func expansion(
of node: AttributeSyntax,
Expand Down
36 changes: 36 additions & 0 deletions test/Macros/macro_expand_peers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ struct Main {
}
print(result2)
// CHECK-EXEC: hahaha global

// CHECK-EXEC: MyWrapperThingy<Swift.Int>(storage: 5)
print(S3(x: 5))
}
}

Expand Down Expand Up @@ -156,3 +159,36 @@ struct S2 {
}
#endif
}

// Stored properties generated by a peer macro
@attached(accessor)
@attached(peer, names: prefixed(_))
macro myPropertyWrapper() =
#externalMacro(module: "MacroDefinition", type: "PropertyWrapperMacro")

struct Date { }

struct MyWrapperThingy<T> {
var storage: T

var wrappedValue: T {
get {
print("Getting value \(storage)")
return storage
}

set {
print("Setting value \(newValue)")
storage = newValue
}
}
}

struct S3 {
@myPropertyWrapper
var x: Int = 0

init(x: Int) {
self._x = MyWrapperThingy(storage: x)
}
}