Skip to content

Print type inheritances and dictionary types without a space before the colon by default #81204

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
8 changes: 8 additions & 0 deletions include/swift/AST/ASTPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,14 @@ class ASTPrinter {
PendingNewlines++;
}

void printColonForType(const PrintOptions &options)
{
if (options.PrintSpaceBeforeInheritance) {
*this << ' ';
}
*this << ": ";
}

void forceNewlines() {
if (PendingNewlines > 0) {
llvm::SmallString<16> Str;
Expand Down
6 changes: 3 additions & 3 deletions include/swift/AST/GenericParamList.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ class RequirementRepr {
}

SWIFT_DEBUG_DUMP;
void print(raw_ostream &OS) const;
void print(ASTPrinter &Printer) const;
void print(raw_ostream &OS, const PrintOptions &Options = PrintOptions()) const;
void print(ASTPrinter &Printer, const PrintOptions &Options = PrintOptions()) const;
};

/// GenericParamList - A list of generic parameters that is part of a generic
Expand Down Expand Up @@ -400,7 +400,7 @@ class alignas(RequirementRepr) TrailingWhereClause final :
return SourceRange(WhereLoc, EndLoc);
}

void print(llvm::raw_ostream &OS, bool printWhereKeyword) const;
void print(llvm::raw_ostream &OS, const PrintOptions &PO, bool printWhereKeyword) const;

};

Expand Down
3 changes: 2 additions & 1 deletion include/swift/AST/PrintOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ struct PrintOptions {

/// Whether to print a space before the `:` of an inheritance list in a type
/// decl.
bool PrintSpaceBeforeInheritance = true;
bool PrintSpaceBeforeInheritance = false;

/// Whether to print feature checks for compatibility with older Swift
/// compilers that might parse the result.
Expand Down Expand Up @@ -771,6 +771,7 @@ struct PrintOptions {
static PrintOptions printQualifiedSILType() {
PrintOptions result = PrintOptions::printSIL();
result.FullyQualifiedTypesIfAmbiguous = true;
result.PrintSpaceBeforeInheritance = true;
return result;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/AST/ASTDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1471,7 +1471,7 @@ namespace {
}, label);
} else {
printFieldQuotedRaw([&](raw_ostream &OS) {
Where->print(OS, /*printWhereKeyword*/ false);
Where->print(OS, PrintOptions(), /*printWhereKeyword*/ false);
}, label);
}
};
Expand Down
68 changes: 37 additions & 31 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,7 @@ class PrintAST : public ASTVisitor<PrintAST> {
FreshOptions.TransformContext = options.TransformContext;
FreshOptions.CurrentModule = options.CurrentModule;
FreshOptions.FullyQualifiedTypesIfAmbiguous = options.FullyQualifiedTypesIfAmbiguous;
FreshOptions.PrintSpaceBeforeInheritance = options.PrintSpaceBeforeInheritance;
T.print(Printer, FreshOptions);
return;
}
Expand Down Expand Up @@ -1979,7 +1980,7 @@ void PrintAST::printSingleDepthOfGenericSignature(
PrintNameContext::GenericParameter);

if (param->isValue()) {
Printer << " : ";
Printer.printColonForType(Options);
printType(param->getValueType());
}

Expand All @@ -1992,7 +1993,7 @@ void PrintAST::printSingleDepthOfGenericSignature(
printType(param);

if (param->isValue()) {
Printer << " : ";
Printer.printColonForType(Options);
printType(param->getValueType());
}
}
Expand Down Expand Up @@ -2036,7 +2037,7 @@ void PrintAST::printSingleDepthOfGenericSignature(
if (printRequirements)
Printer << " " << tok::kw_where << " ";
else
Printer << " : ";
Printer.printColonForType(Options);

isFirstReq = false;
} else {
Expand Down Expand Up @@ -2085,7 +2086,8 @@ void PrintAST::printSingleDepthOfGenericSignature(
if (printRequirements)
Printer << " " << tok::kw_where << " ";
else
Printer << " : ";

Printer.printColonForType(Options);

isFirstReq = false;
} else {
Expand Down Expand Up @@ -2171,21 +2173,23 @@ void PrintAST::printRequirement(const Requirement &req) {
printTransformedType(req.getFirstType());
Printer << ", ";
printTransformedType(req.getSecondType());
Printer << ")) : Any";
Printer << "))";
Printer.printColonForType(Options);
Printer << "Any";
return;
case RequirementKind::Layout:
if (isPackRequirement)
Printer << "repeat ";
printTransformedType(req.getFirstType());
Printer << " : ";
Printer.printColonForType(Options);
req.getLayoutConstraint()->print(Printer, Options);
return;
case RequirementKind::Conformance:
case RequirementKind::Superclass:
if (isPackRequirement)
Printer << "repeat ";
printTransformedType(req.getFirstType());
Printer << " : ";
Printer.printColonForType(Options);
break;
case RequirementKind::SameType:
if (isPackRequirement)
Expand All @@ -2202,7 +2206,7 @@ void PrintAST::printRequirement(const InverseRequirement &inverse,
if (!forInherited) {
Printer.callPrintStructurePre(PrintStructureKind::GenericRequirement);
printTransformedType(inverse.subject);
Printer << " : ";
Printer.printColonForType(Options);
Printer.printStructurePost(PrintStructureKind::GenericRequirement);
}

Expand Down Expand Up @@ -2930,10 +2934,7 @@ void PrintAST::printInherited(const Decl *decl) {
if (TypesToPrint.empty())
return;

if (Options.PrintSpaceBeforeInheritance) {
Printer << " ";
}
Printer << ": ";
Printer.printColonForType(Options);

interleave(TypesToPrint, [&](InheritedEntry inherited) {
printTypeLoc(inherited, [&] {
Expand Down Expand Up @@ -4627,8 +4628,10 @@ void PrintAST::visitInfixOperatorDecl(InfixOperatorDecl *decl) {
[&]{
Printer.printName(decl->getName());
});
if (auto *group = decl->getPrecedenceGroup())
Printer << " : " << group->getName();
if (auto *group = decl->getPrecedenceGroup()) {
Printer.printColonForType(Options);
Printer << group->getName();
}
}

void PrintAST::visitPrecedenceGroupDecl(PrecedenceGroupDecl *decl) {
Expand Down Expand Up @@ -6336,7 +6339,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
if (T->isDictionary()) {
Printer << "[";
visit(T->getGenericArgs()[0]);
Printer << " : ";
Printer.printColonForType(Options);
visit(T->getGenericArgs()[1]);
Printer << "]";
return;
Expand Down Expand Up @@ -7123,7 +7126,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
} else {
Printer << "[";
visit(T->getKeyType());
Printer << " : ";
Printer << ": ";
visit(T->getValueType());
Printer << "]";
}
Expand Down Expand Up @@ -8160,43 +8163,43 @@ swift::getInheritedForPrinting(
// Generic param list printing.
//===----------------------------------------------------------------------===//

void RequirementRepr::print(raw_ostream &out) const {
void RequirementRepr::print(raw_ostream &out, const PrintOptions &options) const {
StreamPrinter printer(out);
print(printer);
print(printer, options);
}

void RequirementRepr::print(ASTPrinter &out) const {
void RequirementRepr::print(ASTPrinter &out, const PrintOptions &options) const {
auto printLayoutConstraint =
[&](const LayoutConstraintLoc &LayoutConstraintLoc) {
LayoutConstraintLoc.getLayoutConstraint()->print(out, PrintOptions());
LayoutConstraintLoc.getLayoutConstraint()->print(out, options);
};

switch (getKind()) {
case RequirementReprKind::LayoutConstraint:
if (auto *repr = getSubjectRepr()) {
repr->print(out, PrintOptions());
repr->print(out, options);
}
out << " : ";
out.printColonForType(options);
printLayoutConstraint(getLayoutConstraintLoc());
break;

case RequirementReprKind::TypeConstraint:
if (auto *repr = getSubjectRepr()) {
repr->print(out, PrintOptions());
repr->print(out, options);
}
out << " : ";
out.printColonForType(options);
if (auto *repr = getConstraintRepr()) {
repr->print(out, PrintOptions());
repr->print(out, options);
}
break;

case RequirementReprKind::SameType:
if (auto *repr = getFirstTypeRepr()) {
repr->print(out, PrintOptions());
repr->print(out, options);
}
out << " == ";
if (auto *repr = getSecondTypeRepr()) {
repr->print(out, PrintOptions());
repr->print(out, options);
}
break;
}
Expand All @@ -8209,6 +8212,7 @@ void GenericParamList::print(raw_ostream &out, const PrintOptions &PO) const {

static void printTrailingRequirements(ASTPrinter &Printer,
ArrayRef<RequirementRepr> Reqs,
const PrintOptions &Options,
bool printWhereKeyword) {
if (Reqs.empty())
return;
Expand All @@ -8219,7 +8223,7 @@ static void printTrailingRequirements(ASTPrinter &Printer,
Reqs,
[&](const RequirementRepr &req) {
Printer.callPrintStructurePre(PrintStructureKind::GenericRequirement);
req.print(Printer);
req.print(Printer, Options);
Printer.printStructurePost(PrintStructureKind::GenericRequirement);
},
[&] { Printer << ", "; });
Expand All @@ -8233,7 +8237,7 @@ void GenericParamList::print(ASTPrinter &Printer,
[&](const GenericTypeParamDecl *P) {
Printer << P->getName();
if (!P->getInherited().empty()) {
Printer << " : ";
Printer.printColonForType(PO);

auto loc = P->getInherited().getEntry(0);
if (willUseTypeReprPrinting(loc, nullptr, PO)) {
Expand All @@ -8245,13 +8249,15 @@ void GenericParamList::print(ASTPrinter &Printer,
},
[&] { Printer << ", "; });

printTrailingRequirements(Printer, getRequirements(),
printTrailingRequirements(Printer, getRequirements(), PO,
/*printWhereKeyword*/ true);
Printer << '>';
}

void TrailingWhereClause::print(llvm::raw_ostream &OS,
const PrintOptions &PO,
bool printWhereKeyword) const {
StreamPrinter Printer(OS);
printTrailingRequirements(Printer, getRequirements(), printWhereKeyword);
printTrailingRequirements(Printer, getRequirements(), PO,
printWhereKeyword);
}
2 changes: 1 addition & 1 deletion lib/AST/RequirementMachine/RequirementLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1195,7 +1195,7 @@ TypeAliasRequirementsRequest::evaluate(Evaluator &evaluator,
if (!assocType->getInherited().empty())
out << ", ";

whereClause->print(out, /*printWhereKeyword*/false);
whereClause->print(out, PrintOptions(), /*printWhereKeyword*/false);
}
}
return result;
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/TypeRepr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ void DictionaryTypeRepr::printImpl(ASTPrinter &Printer,
const PrintOptions &Opts) const {
Printer << "[";
printTypeRepr(Key, Printer, Opts);
Printer << " : ";
Printer.printColonForType(Opts);
printTypeRepr(Value, Printer, Opts);
Printer << "]";
}
Expand Down
12 changes: 9 additions & 3 deletions lib/Frontend/ModuleInterfaceSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -822,16 +822,22 @@ class InheritedProtocolCollector {
out << "@_spi(" << DummyProtocolName << ")\n";
out << "@available(*, unavailable)\nextension ";
nominal->getDeclaredType().print(out, printOptions);
out << " : ";
if (printOptions.PrintSpaceBeforeInheritance) {
out << ' ';
}
out << ": ";
llvm::interleave(
conformanceProtos,
[&out, &printOptions](const ProtocolType *protoTy) {
protoTy->print(out, printOptions);
},
[&out] { out << ", "; });
out << " where "
<< nominal->getGenericSignature().getGenericParams()[0]->getName()
<< " : " << DummyProtocolName << " {}\n";
<< nominal->getGenericSignature().getGenericParams()[0]->getName();
if (printOptions.PrintSpaceBeforeInheritance) {
out << ' ';
}
out << ": " << DummyProtocolName << " {}\n";
};

// We have to print conformances for invertible protocols in separate
Expand Down
1 change: 1 addition & 0 deletions lib/SIL/IR/SILPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4844,6 +4844,7 @@ PrintOptions PrintOptions::printSIL(const SILPrintContext *ctx) {
result.AbstractAccessors = false;
result.PrintForSIL = true;
result.PrintInSILBody = true;
result.PrintSpaceBeforeInheritance = true;
result.PreferTypeRepr = false;
result.OpaqueReturnTypePrinting =
OpaqueReturnTypePrintingMode::StableReference;
Expand Down
4 changes: 2 additions & 2 deletions test/APINotes/properties.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// REQUIRES: objc_interop


// CHECK-BOTH-LABEL: class TestProperties : Base {
// CHECK-BOTH-LABEL: class TestProperties: Base {

// CHECK-BOTH-DAG: func accessorsOnly() -> Any
// CHECK-BOTH-DAG: func setAccessorsOnly(_ accessorsOnly: Any)
Expand All @@ -34,7 +34,7 @@

// CHECK-BOTH: {{^}$}}

// CHECK-BOTH-LABEL: class TestPropertiesSub : TestProperties {
// CHECK-BOTH-LABEL: class TestPropertiesSub: TestProperties {
// CHECK-BOTH-DAG: func accessorsOnly() -> Any
// CHECK-BOTH-DAG: func setAccessorsOnly(_ accessorsOnly: Any)
// CHECK-BOTH-DAG: class func accessorsOnlyForClass() -> Any
Expand Down
Loading