Skip to content
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
4 changes: 4 additions & 0 deletions include/swift/AST/Attr.h
Original file line number Diff line number Diff line change
Expand Up @@ -2359,6 +2359,10 @@ class CustomAttr final : public DeclAttribute {

ASTContext &getASTContext() const;

/// If \c true, we should prefer a property wrapper if one exists for the
/// given attribute over a macro.
bool shouldPreferPropertyWrapperOverMacro() const;

/// Retrieve the NominalTypeDecl the CustomAttr refers to, or \c nullptr if
/// it doesn't refer to one (which can be the case for e.g macro attrs).
NominalTypeDecl *getNominalDecl() const;
Expand Down
14 changes: 14 additions & 0 deletions lib/AST/Attr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3167,6 +3167,20 @@ ASTContext &CustomAttr::getASTContext() const {
return getOwner().getDeclContext()->getASTContext();
}

bool CustomAttr::shouldPreferPropertyWrapperOverMacro() const {
// If we have a VarDecl in a local context, prefer to use a property wrapper
// if one exists. This is necessary since we don't properly support peer
// declarations in local contexts, so want to use a property wrapper if one
// exists.
if (auto *D = getOwner().getAsDecl()) {
if ((isa<VarDecl>(D) || isa<PatternBindingDecl>(D)) &&
D->getDeclContext()->isLocalContext()) {
return true;
}
}
return false;
}

NominalTypeDecl *CustomAttr::getNominalDecl() const {
auto &eval = getASTContext().evaluator;
auto *mutThis = const_cast<CustomAttr *>(this);
Expand Down
16 changes: 1 addition & 15 deletions lib/AST/NameLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4020,20 +4020,6 @@ GenericParamListRequest::evaluate(Evaluator &evaluator, GenericContext *value) c
parsedGenericParams->getRAngleLoc());
}

static bool shouldPreferPropertyWrapperOverMacro(CustomAttrOwner owner) {
// If we have a VarDecl in a local context, prefer to use a property wrapper
// if one exists. This is necessary since we don't properly support peer
// declarations in local contexts, so want to use a property wrapper if one
// exists.
if (auto *D = owner.getAsDecl()) {
if ((isa<VarDecl>(D) || isa<PatternBindingDecl>(D)) &&
D->getDeclContext()->isLocalContext()) {
return true;
}
}
return false;
}

NominalTypeDecl *CustomAttrNominalRequest::evaluate(Evaluator &evaluator,
CustomAttr *attr) const {
auto owner = attr->getOwner();
Expand All @@ -4048,7 +4034,7 @@ NominalTypeDecl *CustomAttrNominalRequest::evaluate(Evaluator &evaluator,
auto macroName = (macro) ? macro->getNameRef() : DeclNameRef();
auto macros = namelookup::lookupMacros(dc, moduleName, macroName,
getAttachedMacroRoles());
auto shouldPreferPropWrapper = shouldPreferPropertyWrapperOverMacro(owner);
auto shouldPreferPropWrapper = attr->shouldPreferPropertyWrapperOverMacro();
if (!macros.empty() && !shouldPreferPropWrapper)
return nullptr;

Expand Down
8 changes: 6 additions & 2 deletions lib/Sema/TypeCheckMacros.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2242,8 +2242,12 @@ ResolveMacroRequest::evaluate(Evaluator &evaluator,
// So bail out to prevent diagnostics from the contraint system.
if (auto *attr = macroRef.getAttr()) {
// If we already resolved this CustomAttr to a nominal, this isn't for a
// macro.
if (attr->getNominalDecl())
// macro. This can only currently be the case for property wrappers, so
// limit the check here to avoid request cycles for member attribute macros
// in cases where the attribute refers to a nested type in the type it's
// attached to, since the qualified lookup there needs to expand member
// attributes.
if (attr->shouldPreferPropertyWrapperOverMacro() && attr->getNominalDecl())
return ConcreteDeclRef();

auto foundMacros = namelookup::lookupMacros(dc, macroRef.getModuleName(),
Expand Down
10 changes: 10 additions & 0 deletions test/NameLookup/rdar163961797.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// RUN: %target-typecheck-verify-swift

// Make sure we don't end up with a request cycle here, since looking up 'S.Actor'
// requires expanding member attribute macros for 'S'.
@S.Actor
struct S {
@globalActor actor Actor: GlobalActor {
public static let shared = S.Actor()
}
}