Skip to content

Commit c04a8a9

Browse files
authored
Merge pull request #30011 from AnthonyLatsis/mutating_dup_quick_help
[ASTPrinter] Fix duplicate «mutating» modifiers when printing file interface
2 parents 3462935 + f45c338 commit c04a8a9

File tree

3 files changed

+40
-26
lines changed

3 files changed

+40
-26
lines changed

lib/AST/ASTPrinter.cpp

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,7 @@ class PrintAST : public ASTVisitor<PrintAST> {
819819
bool shouldPrintPattern(const Pattern *P);
820820
void printPatternType(const Pattern *P);
821821
void printAccessors(const AbstractStorageDecl *ASD);
822-
void printMutatingModifiersIfNeeded(const AccessorDecl *accessor);
822+
void printMutabilityModifiersIfNeeded(const FuncDecl *FD);
823823
void printMembersOfDecl(Decl * NTD, bool needComma = false,
824824
bool openBracket = true, bool closeBracket = true);
825825
void printMembers(ArrayRef<Decl *> members, bool needComma = false,
@@ -1008,9 +1008,6 @@ void PrintAST::printAttributes(const Decl *D) {
10081008
#define CONTEXTUAL_SIMPLE_DECL_ATTR(X, Class, Y, Z) EXCLUDE_ATTR(Class)
10091009
#define CONTEXTUAL_DECL_ATTR_ALIAS(X, Class) EXCLUDE_ATTR(Class)
10101010
#include "swift/AST/Attr.def"
1011-
} else if (isa<FuncDecl>(D)) {
1012-
Options.ExcludeAttrList.push_back(DAK_Mutating);
1013-
Options.ExcludeAttrList.push_back(DAK_NonMutating);
10141011
}
10151012

10161013
// If the declaration is implicitly @objc, print the attribute now.
@@ -1026,19 +1023,23 @@ void PrintAST::printAttributes(const Decl *D) {
10261023
// clients, or if it inherits superclass convenience initializers,
10271024
// then print those attributes specially.
10281025
if (auto CD = dyn_cast<ClassDecl>(D)) {
1029-
if (Options.PrintImplicitAttrs) {
1030-
if (CD->inheritsSuperclassInitializers()) {
1031-
Printer.printAttrName("@_inheritsConvenienceInitializers");
1032-
Printer << " ";
1033-
}
1034-
if (CD->hasMissingDesignatedInitializers()) {
1035-
Printer.printAttrName("@_hasMissingDesignatedInitializers");
1036-
Printer << " ";
1037-
}
1026+
if (CD->inheritsSuperclassInitializers()) {
1027+
Printer.printAttrName("@_inheritsConvenienceInitializers");
1028+
Printer << " ";
1029+
}
1030+
if (CD->hasMissingDesignatedInitializers()) {
1031+
Printer.printAttrName("@_hasMissingDesignatedInitializers");
1032+
Printer << " ";
10381033
}
10391034
}
10401035
}
10411036

1037+
// We will handle 'mutating' and 'nonmutating' separately.
1038+
if (isa<FuncDecl>(D)) {
1039+
Options.ExcludeAttrList.push_back(DAK_Mutating);
1040+
Options.ExcludeAttrList.push_back(DAK_NonMutating);
1041+
}
1042+
10421043
D->getAttrs().print(Printer, Options, D);
10431044

10441045
// Print the implicit 'final' attribute.
@@ -1776,12 +1777,15 @@ void PrintAST::printBodyIfNecessary(const AbstractFunctionDecl *decl) {
17761777
printBraceStmt(decl->getBody(), /*newlineIfEmpty*/!isa<AccessorDecl>(decl));
17771778
}
17781779

1779-
void PrintAST::printMutatingModifiersIfNeeded(const AccessorDecl *accessor) {
1780-
if (accessor->isAssumedNonMutating() && accessor->isMutating() &&
1781-
!Options.excludeAttrKind(DAK_Mutating)) {
1782-
Printer.printKeyword("mutating", Options, " ");
1783-
} else if (accessor->isExplicitNonMutating() &&
1784-
!Options.excludeAttrKind(DAK_NonMutating)) {
1780+
void PrintAST::printMutabilityModifiersIfNeeded(const FuncDecl *FD) {
1781+
const auto *AD = dyn_cast<AccessorDecl>(FD);
1782+
1783+
if (FD->isMutating()) {
1784+
if (AD == nullptr || AD->isAssumedNonMutating())
1785+
if (!Options.excludeAttrKind(DAK_Mutating))
1786+
Printer.printKeyword("mutating", Options, " ");
1787+
} else if (AD && AD->isExplicitNonMutating() &&
1788+
!Options.excludeAttrKind(DAK_Mutating)) {
17851789
Printer.printKeyword("nonmutating", Options, " ");
17861790
}
17871791
}
@@ -1899,7 +1903,7 @@ void PrintAST::printAccessors(const AbstractStorageDecl *ASD) {
18991903
return true;
19001904
if (!PrintAccessorBody) {
19011905
Printer << " ";
1902-
printMutatingModifiersIfNeeded(Accessor);
1906+
printMutabilityModifiersIfNeeded(Accessor);
19031907
Printer.printKeyword(getAccessorLabel(Accessor->getAccessorKind()), Options);
19041908
} else {
19051909
{
@@ -2795,10 +2799,9 @@ bool PrintAST::printASTNodes(const ArrayRef<ASTNode> &Elements,
27952799
void PrintAST::visitAccessorDecl(AccessorDecl *decl) {
27962800
printDocumentationComment(decl);
27972801
printAttributes(decl);
2802+
// Explicitly print 'mutating' and 'nonmutating' if needed.
2803+
printMutabilityModifiersIfNeeded(decl);
27982804

2799-
// Explicitly print 'mutating' and 'nonmutating' before getters and setters
2800-
// for which that is true.
2801-
printMutatingModifiersIfNeeded(decl);
28022805
switch (auto kind = decl->getAccessorKind()) {
28032806
case AccessorKind::Get:
28042807
case AccessorKind::Address:
@@ -2855,9 +2858,9 @@ void PrintAST::visitFuncDecl(FuncDecl *decl) {
28552858
if (!Options.SkipIntroducerKeywords) {
28562859
if (decl->isStatic() && Options.PrintStaticKeyword)
28572860
printStaticKeyword(decl->getCorrectStaticSpelling());
2858-
if (decl->isMutating() && !Options.excludeAttrKind(DAK_Mutating)) {
2859-
Printer.printKeyword("mutating", Options, " ");
2860-
} else if (decl->isConsuming() && !decl->getAttrs().hasAttribute<ConsumingAttr>()) {
2861+
2862+
printMutabilityModifiersIfNeeded(decl);
2863+
if (decl->isConsuming() && !decl->getAttrs().hasAttribute<ConsumingAttr>()) {
28612864
Printer.printKeyword("__consuming", Options, " ");
28622865
}
28632866
Printer << tok::kw_func << " ";

test/IDE/print_source_file_interface_2.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,10 @@ extension MyClass {
1616
subscript(i: Int) -> Int { return 0 }
1717
}
1818

19+
/// Don't print `mutating` twice.
20+
struct MyStruct {
21+
mutating func foo() {}
22+
}
23+
1924
// RUN: %target-swift-ide-test -print-swift-file-interface -source-filename %s > %t.out
2025
// RUN: diff -u %s.result %t.out

test/IDE/print_source_file_interface_2.swift.result

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,9 @@ extension MyClass {
1717
/// and a nice subscript.
1818
internal subscript(i: Int) -> Int { get }
1919
}
20+
21+
/// Don't print `mutating` twice.
22+
internal struct MyStruct {
23+
24+
internal mutating func foo()
25+
}

0 commit comments

Comments
 (0)