[AST] NFC: Move shouldPreferPropertyWrapperOverMacro onto CustomAttr

This commit is contained in:
Hamish Knight
2025-11-06 14:39:52 +00:00
parent 3e0ea3ac68
commit 9981dc8126
3 changed files with 19 additions and 15 deletions

View File

@@ -2359,6 +2359,10 @@ public:
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;

View File

@@ -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 = owner.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);

View File

@@ -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();
@@ -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;