Skip to content

Commit 0ce97b8

Browse files
committed
Diagnostics: noformat modifier for Type
Sometimes, quotes or an "aka" for a type are not desirable in a diagnostic, such as one describing a Requirement. This modifier suppresses such additional formatting a Type in a diagnostic.
1 parent 215c4e1 commit 0ce97b8

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7646,8 +7646,8 @@ ERROR(copyable_illegal_deinit, none,
76467646
(const ValueDecl *))
76477647
ERROR(inverse_cannot_be_conditional_on_requirement, none,
76487648
"conditional conformance to suppressible %kind0 cannot depend on "
7649-
"'%1%select{:|:| ==|:| has same shape as}2 %3'",
7650-
(const ProtocolDecl *, StringRef, unsigned, StringRef))
7649+
"'%noformat1%select{:|:| ==|:| has same shape as}2 %noformat3'",
7650+
(const ProtocolDecl *, Type, unsigned, Type))
76517651
ERROR(inverse_type_member_in_conforming_type,none,
76527652
"%select{stored property %2|associated value %2}1 of "
76537653
"'%4'-conforming %kind3 has non-%4 type %0",

lib/AST/DiagnosticEngine.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,13 @@ static void formatDiagnosticArgument(StringRef Modifier,
757757
case DiagnosticArgumentKind::FullyQualifiedType:
758758
case DiagnosticArgumentKind::Type:
759759
case DiagnosticArgumentKind::WitnessType: {
760-
assert(Modifier.empty() && "Improper modifier for Type argument");
760+
std::optional<DiagnosticFormatOptions> TypeFormatOpts;
761+
if (Modifier == "noformat") {
762+
TypeFormatOpts.emplace(DiagnosticFormatOptions::formatForFixIts());
763+
} else {
764+
assert(Modifier.empty() && "Improper modifier for Type argument");
765+
TypeFormatOpts.emplace(FormatOpts);
766+
}
761767

762768
// Strip extraneous parentheses; they add no value.
763769
Type type;
@@ -829,7 +835,7 @@ static void formatDiagnosticArgument(StringRef Modifier,
829835

830836
auto descriptiveKind = opaqueTypeDecl->getDescriptiveKind();
831837

832-
Out << llvm::format(FormatOpts.OpaqueResultFormatString.c_str(),
838+
Out << llvm::format(TypeFormatOpts->OpaqueResultFormatString.c_str(),
833839
type->getString(printOptions).c_str(),
834840
Decl::getDescriptiveKindName(descriptiveKind).data(),
835841
NamingDeclText.c_str());
@@ -843,11 +849,11 @@ static void formatDiagnosticArgument(StringRef Modifier,
843849
llvm::raw_svector_ostream OutAka(AkaText);
844850

845851
getAkaTypeForDisplay(type)->print(OutAka, printOptions);
846-
Out << llvm::format(FormatOpts.AKAFormatString.c_str(),
852+
Out << llvm::format(TypeFormatOpts->AKAFormatString.c_str(),
847853
typeName.c_str(), AkaText.c_str());
848854
} else {
849-
Out << FormatOpts.OpeningQuotationMark << typeName
850-
<< FormatOpts.ClosingQuotationMark;
855+
Out << TypeFormatOpts->OpeningQuotationMark << typeName
856+
<< TypeFormatOpts->ClosingQuotationMark;
851857
}
852858
}
853859
break;

lib/Sema/TypeCheckInvertible.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,13 @@ static void checkInvertibleConformanceCommon(DeclContext *dc,
154154
// In theory, it could depend on any invertible protocol, but it may be
155155
// confusing if we permitted that and this simplifies the model a bit.
156156
for (auto req : condReqs) {
157-
std::optional<StringRef> illegalSecondTypeStr;
157+
Type illegalSecondType;
158158

159159
// If we are diagnosing, fill-in the second-type string of this req.
160160
switch (req.getKind()) {
161161
case RequirementKind::Layout:
162-
illegalSecondTypeStr = req.getLayoutConstraint().getString();
162+
assert(req.getLayoutConstraint()->isClass());
163+
illegalSecondType = ctx.getAnyObjectType();
163164
break;
164165
case RequirementKind::Conformance:
165166
if (req.getProtocolDecl() == thisProto
@@ -169,19 +170,19 @@ static void checkInvertibleConformanceCommon(DeclContext *dc,
169170
case RequirementKind::Superclass:
170171
case RequirementKind::SameType:
171172
case RequirementKind::SameShape:
172-
illegalSecondTypeStr = req.getSecondType().getString();
173+
illegalSecondType = req.getSecondType();
173174
break;
174175
}
175176

176177
static_assert((unsigned)RequirementKind::LAST_KIND == 4,
177178
"update %select in diagnostic!");
178-
if (illegalSecondTypeStr) {
179-
ctx.Diags.diagnose(conformanceLoc,
179+
if (illegalSecondType) {
180+
auto t = ctx.Diags.diagnose(conformanceLoc,
180181
diag::inverse_cannot_be_conditional_on_requirement,
181182
thisProto,
182-
req.getFirstType().getString(),
183+
req.getFirstType(),
183184
static_cast<unsigned>(req.getKind()),
184-
*illegalSecondTypeStr);
185+
illegalSecondType);
185186
}
186187
}
187188
}

0 commit comments

Comments
 (0)