Skip to content

Commit b4c5cfd

Browse files
committed
AST: Enumerate expanded attributes in TypeCheckAttr.
We need to avoid triggering semantic attribute requests in TypeCheckAttr because it happens too early in type checking to trigger some semantic attribute requests, and we only want to diagnose attributes that were written in source anyways.
1 parent 040e7a7 commit b4c5cfd

File tree

7 files changed

+11
-12
lines changed

7 files changed

+11
-12
lines changed

include/swift/AST/Decl.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -924,11 +924,10 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
924924
/// expansions.
925925
OrigDeclAttributes getOriginalAttrs() const;
926926

927-
/// Returns the semantic CustomAttrs attached to this declaration,
927+
/// Returns the attributes attached to this declaration,
928928
/// including attributes that are generated as the result of member
929929
/// attribute macro expansion.
930-
DeclAttributes::AttributeKindRange<CustomAttr, false>
931-
getSemanticCustomAttrs() const;
930+
DeclAttributes getExpandedAttrs() const;
932931

933932
/// Returns all semantic attributes attached to this declaration,
934933
/// including attributes that are generated as the result of member

lib/AST/Decl.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -375,13 +375,12 @@ OrigDeclAttributes Decl::getOriginalAttrs() const {
375375
return OrigDeclAttributes(getAttrs(), this);
376376
}
377377

378-
DeclAttributes::AttributeKindRange<CustomAttr, false>
379-
Decl::getSemanticCustomAttrs() const {
378+
DeclAttributes Decl::getExpandedAttrs() const {
380379
auto mutableThis = const_cast<Decl *>(this);
381380
(void)evaluateOrDefault(getASTContext().evaluator,
382381
ExpandMemberAttributeMacros{mutableThis}, {});
383382

384-
return getAttrs().getAttributes<CustomAttr>();
383+
return getAttrs();
385384
}
386385

387386
DeclAttributes Decl::getSemanticAttrs() const {
@@ -447,7 +446,7 @@ void Decl::visitAuxiliaryDecls(
447446

448447
void Decl::forEachAttachedMacro(MacroRole role,
449448
MacroCallback macroCallback) const {
450-
for (auto customAttrConst : getSemanticCustomAttrs()) {
449+
for (auto customAttrConst : getExpandedAttrs().getAttributes<CustomAttr>()) {
451450
auto customAttr = const_cast<CustomAttr *>(customAttrConst);
452451
auto *macroDecl = getResolvedMacro(customAttr);
453452

lib/AST/NameLookup.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1725,7 +1725,7 @@ void namelookup::forEachPotentialAttachedMacro(
17251725
// We intentionally avoid calling `forEachAttachedMacro` in order to avoid
17261726
// a request cycle.
17271727
auto moduleScopeCtx = decl->getDeclContext()->getModuleScopeContext();
1728-
for (auto attrConst : decl->getSemanticCustomAttrs()) {
1728+
for (auto attrConst : decl->getExpandedAttrs().getAttributes<CustomAttr>()) {
17291729
auto *attr = const_cast<CustomAttr *>(attrConst);
17301730
UnresolvedMacroReference macroRef(attr);
17311731
auto macroName = macroRef.getMacroName();

lib/Sema/TypeCheckAccess.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1281,7 +1281,7 @@ class AccessControlChecker : public AccessControlCheckerBase,
12811281
}
12821282

12831283
void checkAttachedMacrosAccess(const Decl *D) {
1284-
for (auto customAttrC : D->getSemanticCustomAttrs()) {
1284+
for (auto customAttrC : D->getExpandedAttrs().getAttributes<CustomAttr>()) {
12851285
auto customAttr = const_cast<CustomAttr *>(customAttrC);
12861286
auto *macroDecl = D->getResolvedMacro(customAttr);
12871287
if (macroDecl) {

lib/Sema/TypeCheckAttr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1579,7 +1579,7 @@ void TypeChecker::checkDeclAttributes(Decl *D) {
15791579
llvm::SmallVector<AvailableAttr *, 4> availableAttrs;
15801580
llvm::SmallVector<BackDeployedAttr *, 4> backDeployedAttrs;
15811581
llvm::SmallVector<OriginallyDefinedInAttr*, 4> ODIAttrs;
1582-
for (auto attr : D->getSemanticAttrs()) {
1582+
for (auto attr : D->getExpandedAttrs()) {
15831583
if (!attr->isValid()) continue;
15841584

15851585
// If Attr.def says that the attribute cannot appear on this kind of

lib/Sema/TypeCheckMacros.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1664,7 +1664,8 @@ ArrayRef<unsigned>
16641664
ExpandExtensionMacros::evaluate(Evaluator &evaluator,
16651665
NominalTypeDecl *nominal) const {
16661666
SmallVector<unsigned, 2> bufferIDs;
1667-
for (auto customAttrConst : nominal->getSemanticCustomAttrs()) {
1667+
for (auto customAttrConst :
1668+
nominal->getExpandedAttrs().getAttributes<CustomAttr>()) {
16681669
auto customAttr = const_cast<CustomAttr *>(customAttrConst);
16691670
auto *macro = nominal->getResolvedMacro(customAttr);
16701671

lib/Sema/TypeCheckPropertyWrapper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ AttachedPropertyWrappersRequest::evaluate(Evaluator &evaluator,
438438
auto dc = var->getDeclContext();
439439
llvm::TinyPtrVector<CustomAttr *> result;
440440

441-
for (auto attr : var->getSemanticCustomAttrs()) {
441+
for (auto attr : var->getExpandedAttrs().getAttributes<CustomAttr>()) {
442442
auto mutableAttr = const_cast<CustomAttr *>(attr);
443443
// Figure out which nominal declaration this custom attribute refers to.
444444
auto *nominal = evaluateOrDefault(

0 commit comments

Comments
 (0)