<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;"><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;">Hello,<div class=""><br class=""></div><div class="">I’d like to propose improving the quality, consistency, and future preparedness of diagnostics through simplification. Right now, a lot of diagnostics output tends to look like this (for example):</div><div class=""><br class=""></div><div class=""><font face="Courier" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>type ‘Foo’ cannot subclass type ‘Bar’</font></div><div class=""><br class=""></div><div class="">Instead of hard coding the word “type” into the message, I’d like to change the diagnostics engine to dynamically introspect the types and print out the type kind instead. Therefore, after this change, the message output would look like this:</div><div class=""><br class=""></div><div class=""><font face="Courier" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>class ‘Foo’ cannot subclass struct ‘Bar’<br class=""></font><br class=""></div><div class="">Which is clearly more better. And it requires less work and specialization on behalf of diagnostics clients because the messages within the compiler “defs” files would change from:</div><div class=""><br class=""></div><div class=""><font face="Courier" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>“type %0 cannot subclass type %1”<br class=""></font><br class=""></div><div class="">To:</div><div class=""><br class=""></div><div class=""><font face="Courier" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>“%0 cannot subclass %1”<br class=""></font></div><div class=""><br class=""></div><div class="">Which is obviously simpler, better, and more future proof. In particular, clients of the diagnostics engine would no longer need to haphazardly customize messages to acknowledge optionals, metatypes, etc as they do today. The engine itself would deal with that.</div><div class=""><br class=""></div><div class="">What do people think? The meat of the change is tiny and below. The vast majority of the work is the long and painful fallout in the test suite, which I’m cautiously (and perhaps foolishly) stepping up to doing.</div><div class=""><br class=""></div><div class="">Cheers,</div><div class="">Dave</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><font face="Courier" class="">diff --git c/lib/AST/DiagnosticEngine.cpp i/lib/AST/DiagnosticEngine.cpp<br class="">index c99833d373..32ba196f04 100644<br class="">--- c/lib/AST/DiagnosticEngine.cpp<br class="">+++ i/lib/AST/DiagnosticEngine.cpp<br class="">@@ -373,6 +373,121 @@ static bool shouldShowAKA(Type type, StringRef typeName) {<br class="">&nbsp; &nbsp;return true;<br class="">&nbsp;}<br class="">&nbsp;<br class="">+static std::string computeDiagnosticTypeKindString(Type &amp;type) {<br class="">+&nbsp;&nbsp;switch (type-&gt;getKind()) {<br class="">+#define TYPE(id, parent)<br class="">+#define BUILTIN_TYPE(id, parent) \<br class="">+&nbsp;&nbsp;case TypeKind::id: return "builtin ";<br class="">+#include &lt;swift/AST/TypeNodes.def&gt;<br class="">+&nbsp;&nbsp;case TypeKind::InOut:<br class="">+&nbsp; &nbsp;&nbsp;type = type-&gt;getInOutObjectType();<br class="">+&nbsp; &nbsp;&nbsp;return "inout " + computeDiagnosticTypeKindString(type);<br class="">+&nbsp;&nbsp;case TypeKind::Function:&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return "function ";<br class="">+&nbsp;&nbsp;case TypeKind::GenericFunction:&nbsp; &nbsp; &nbsp;&nbsp;return "function ";<br class="">+&nbsp;&nbsp;case TypeKind::Struct:&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return "struct ";<br class="">+&nbsp;&nbsp;case TypeKind::BoundGenericStruct:&nbsp;&nbsp;&nbsp;return "struct ";<br class="">+&nbsp;&nbsp;case TypeKind::Class:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return "class ";<br class="">+&nbsp;&nbsp;case TypeKind::BoundGenericClass:&nbsp; &nbsp;&nbsp;return "class ";<br class="">+&nbsp;&nbsp;case TypeKind::Enum:&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return "enum ";<br class="">+&nbsp;&nbsp;case TypeKind::BoundGenericEnum:&nbsp;&nbsp; &nbsp;&nbsp;return "enum ";<br class="">+&nbsp;&nbsp;case TypeKind::ProtocolComposition:&nbsp;&nbsp;return "protocol ";<br class="">+&nbsp;&nbsp;case TypeKind::Protocol:&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return "protocol ";<br class="">+&nbsp;&nbsp;case TypeKind::Module:&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return "module ";<br class="">+&nbsp;&nbsp;case TypeKind::Unresolved:&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return "unresolved type ";<br class="">+&nbsp;&nbsp;case TypeKind::DynamicSelf:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return "dynamic type ";<br class="">+&nbsp;&nbsp;case TypeKind::TypeVariable:&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return "type-variable ";<br class="">+&nbsp;&nbsp;case TypeKind::SILFunction:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return "SIL function ";<br class="">+&nbsp;&nbsp;case TypeKind::SILBlockStorage:&nbsp; &nbsp; &nbsp;&nbsp;return "SIL block storage ";<br class="">+&nbsp;&nbsp;case TypeKind::SILBox:&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return "SIL box ";<br class="">+&nbsp;&nbsp;case TypeKind::GenericTypeParam:&nbsp;&nbsp; &nbsp;&nbsp;return "generic parameter ";<br class="">+&nbsp;&nbsp;case TypeKind::Archetype: {<br class="">+&nbsp; &nbsp;&nbsp;auto archetypeTy = dyn_cast&lt;ArchetypeType&gt;(type.getPointer());<br class="">+&nbsp; &nbsp;&nbsp;if (auto openedTy = archetypeTy-&gt;getOpenedExistentialType()) {<br class="">+&nbsp; &nbsp; &nbsp;&nbsp;type = openedTy;<br class="">+&nbsp; &nbsp; &nbsp;&nbsp;return computeDiagnosticTypeKindString(type);<br class="">+&nbsp; &nbsp;&nbsp;}<br class="">+&nbsp; &nbsp;&nbsp;if (archetypeTy-&gt;getAssocType())<br class="">+&nbsp; &nbsp; &nbsp;&nbsp;return "associated type ";<br class="">+&nbsp; &nbsp;&nbsp;return "generic parameter ";<br class="">+&nbsp;&nbsp;}<br class="">+&nbsp;&nbsp;case TypeKind::DependentMember: {<br class="">+&nbsp; &nbsp;&nbsp;while (auto depTy = dyn_cast&lt;DependentMemberType&gt;(type.getPointer()))<br class="">+&nbsp; &nbsp; &nbsp;&nbsp;type = depTy-&gt;getBase();<br class="">+&nbsp; &nbsp;&nbsp;return "dependent " + computeDiagnosticTypeKindString(type);<br class="">+&nbsp;&nbsp;}<br class="">+&nbsp;&nbsp;case TypeKind::Metatype: {<br class="">+&nbsp; &nbsp;&nbsp;auto metaTy = cast&lt;MetatypeType&gt;(type.getPointer());<br class="">+&nbsp; &nbsp;&nbsp;type = metaTy-&gt;getInstanceType();<br class="">+&nbsp; &nbsp;&nbsp;if (!type-&gt;isExistentialType())<br class="">+&nbsp; &nbsp; &nbsp;&nbsp;return "metatype of " + computeDiagnosticTypeKindString(type);<br class="">+&nbsp; &nbsp;&nbsp;return "concrete metatype of " + computeDiagnosticTypeKindString(type);<br class="">+&nbsp;&nbsp;}<br class="">+&nbsp;&nbsp;case TypeKind::ExistentialMetatype: {<br class="">+&nbsp; &nbsp;&nbsp;auto metaTy = cast&lt;ExistentialMetatypeType&gt;(type.getPointer());<br class="">+&nbsp; &nbsp;&nbsp;type = metaTy-&gt;getInstanceType();<br class="">+&nbsp; &nbsp;&nbsp;return "metatype of " + computeDiagnosticTypeKindString(type);<br class="">+&nbsp;&nbsp;}<br class="">+&nbsp;&nbsp;case TypeKind::ArraySlice:<br class="">+&nbsp; &nbsp;&nbsp;// we could in dig further, but then diag messages might become unwieldy<br class="">+&nbsp; &nbsp;&nbsp;return "array slice ";<br class="">+&nbsp;&nbsp;case TypeKind::Dictionary:<br class="">+&nbsp; &nbsp;&nbsp;// we could in dig further, but then diag messages would become unwieldy<br class="">+&nbsp; &nbsp;&nbsp;return "dictionary ";<br class="">+&nbsp;&nbsp;case TypeKind::Optional:<br class="">+&nbsp; &nbsp;&nbsp;type = type-&gt;getAnyOptionalObjectType();<br class="">+&nbsp; &nbsp;&nbsp;return "optional " + computeDiagnosticTypeKindString(type);<br class="">+&nbsp;&nbsp;case TypeKind::ImplicitlyUnwrappedOptional:<br class="">+&nbsp; &nbsp;&nbsp;type = type-&gt;getAnyOptionalObjectType();<br class="">+&nbsp; &nbsp;&nbsp;return "implicitly unwrapped optional "<br class="">+&nbsp; &nbsp; &nbsp;&nbsp;+ computeDiagnosticTypeKindString(type);<br class="">+&nbsp;&nbsp;case TypeKind::Paren:<br class="">+&nbsp; &nbsp;&nbsp;type = type-&gt;getWithoutParens();<br class="">+&nbsp; &nbsp;&nbsp;return computeDiagnosticTypeKindString(type);<br class="">+&nbsp;&nbsp;case TypeKind::Tuple: {<br class="">+&nbsp; &nbsp;&nbsp;auto tempTy = type-&gt;getWithoutImmediateLabel();<br class="">+&nbsp; &nbsp;&nbsp;if (tempTy-&gt;isEqual(type))<br class="">+&nbsp; &nbsp; &nbsp;&nbsp;return "tuple ";<br class="">+&nbsp; &nbsp;&nbsp;type = tempTy;<br class="">+&nbsp; &nbsp;&nbsp;return computeDiagnosticTypeKindString(type);<br class="">+&nbsp;&nbsp;}<br class="">+&nbsp;&nbsp;case TypeKind::NameAlias: {<br class="">+&nbsp; &nbsp;&nbsp;// We need to stop updating our caller's 'type' at this point.<br class="">+&nbsp; &nbsp;&nbsp;auto aliasTy = cast&lt;NameAliasType&gt;(type.getPointer());<br class="">+&nbsp; &nbsp;&nbsp;Type discardTy = aliasTy-&gt;getSinglyDesugaredType();<br class="">+&nbsp; &nbsp;&nbsp;return computeDiagnosticTypeKindString(discardTy);<br class="">+&nbsp;&nbsp;}<br class="">+&nbsp;&nbsp;case TypeKind::Error: {<br class="">+&nbsp; &nbsp;&nbsp;auto errorTy = cast&lt;ErrorType&gt;(type.getPointer());<br class="">+&nbsp; &nbsp;&nbsp;if (auto origTy = errorTy-&gt;getOriginalType()) {<br class="">+&nbsp; &nbsp; &nbsp;&nbsp;type = origTy;<br class="">+&nbsp; &nbsp; &nbsp;&nbsp;return "invalid " + computeDiagnosticTypeKindString(type);<br class="">+&nbsp; &nbsp;&nbsp;}<br class="">+&nbsp; &nbsp;&nbsp;return "invalid type ";<br class="">+&nbsp;&nbsp;}</font></div><div class=""><span style="font-family: Courier;" class="">+</span><span style="font-family: Courier;" class="">&nbsp;&nbsp;</span><span style="font-family: Courier;" class="">case TypeKind::UnboundGeneric:</span><br style="font-family: Courier;" class=""><span style="font-family: Courier;" class="">+</span><span style="font-family: Courier;" class="">&nbsp; &nbsp;&nbsp;</span><span style="font-family: Courier;" class="">switch (type-&gt;castTo&lt;UnboundGenericType&gt;()-&gt;getDecl()-&gt;getKind()) {</span><br style="font-family: Courier;" class=""><span style="font-family: Courier;" class="">+</span><span style="font-family: Courier;" class="">&nbsp; &nbsp;&nbsp;</span><span style="font-family: Courier;" class="">case DeclKind::TypeAlias: return "typealias ";</span><br style="font-family: Courier;" class=""><span style="font-family: Courier;" class="">+</span><span style="font-family: Courier;" class="">&nbsp; &nbsp;&nbsp;</span><span style="font-family: Courier;" class="">case DeclKind::Protocol:</span><span style="font-family: Courier;" class="">&nbsp;&nbsp;</span><span style="font-family: Courier;" class="">return "protocol ";</span><br style="font-family: Courier;" class=""><span style="font-family: Courier;" class="">+</span><span style="font-family: Courier;" class="">&nbsp; &nbsp;&nbsp;</span><span style="font-family: Courier;" class="">case DeclKind::Struct:</span><span style="font-family: Courier;" class="">&nbsp; &nbsp;&nbsp;</span><span style="font-family: Courier;" class="">return "struct ";</span><br style="font-family: Courier;" class=""><span style="font-family: Courier;" class="">+</span><span style="font-family: Courier;" class="">&nbsp; &nbsp;&nbsp;</span><span style="font-family: Courier;" class="">case DeclKind::Class:&nbsp;</span><span style="font-family: Courier;" class="">&nbsp; &nbsp;&nbsp;</span><span style="font-family: Courier;" class="">return "class ";</span><br style="font-family: Courier;" class=""><span style="font-family: Courier;" class="">+</span><span style="font-family: Courier;" class="">&nbsp; &nbsp;&nbsp;</span><span style="font-family: Courier;" class="">case DeclKind::Enum:</span><span style="font-family: Courier;" class="">&nbsp; &nbsp; &nbsp;&nbsp;</span><span style="font-family: Courier;" class="">return "enum ";</span><br style="font-family: Courier;" class=""><span style="font-family: Courier;" class="">+</span><span style="font-family: Courier;" class="">&nbsp; &nbsp;&nbsp;</span><span style="font-family: Courier;" class="">default:</span><span style="font-family: Courier;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="font-family: Courier;" class="">break;</span><br style="font-family: Courier;" class=""><span style="font-family: Courier;" class="">+</span><span style="font-family: Courier;" class="">&nbsp; &nbsp;&nbsp;</span><span style="font-family: Courier;" class="">}</span><br style="font-family: Courier;" class=""><span style="font-family: Courier;" class="">+</span><span style="font-family: Courier;" class="">&nbsp; &nbsp;&nbsp;</span><span style="font-family: Courier;" class="">llvm_unreachable("Unknown unbound generic");</span><br style="font-family: Courier;" class=""><font face="Courier" class="">+&nbsp;&nbsp;}</font></div><div class=""><span style="font-family: Courier;" class="">+</span><span style="font-family: Courier;" class="">&nbsp;&nbsp;</span><span style="font-family: Courier;" class="">case TypeKind::LValue: {</span><br style="font-family: Courier;" class=""><span style="font-family: Courier;" class="">+</span><span style="font-family: Courier;" class="">&nbsp; &nbsp;&nbsp;</span><span style="font-family: Courier;" class="">// While LValues are not a part of the user visible type system, they do</span><br style="font-family: Courier;" class=""><span style="font-family: Courier;" class="">+</span><span style="font-family: Courier;" class="">&nbsp; &nbsp;&nbsp;</span><span style="font-family: Courier;" class="">// sometimes appear inside of tuple expressions. Just ignore them.</span><br style="font-family: Courier;" class=""><span style="font-family: Courier;" class="">+</span><span style="font-family: Courier;" class="">&nbsp; &nbsp;&nbsp;</span><span style="font-family: Courier;" class="">auto lvTy = cast&lt;LValueType&gt;(type.getPointer());</span><br style="font-family: Courier;" class=""><span style="font-family: Courier;" class="">+</span><span style="font-family: Courier;" class="">&nbsp; &nbsp;&nbsp;</span><span style="font-family: Courier;" class="">type = lvTy-&gt;getObjectType();</span><br style="font-family: Courier;" class=""><span style="font-family: Courier;" class="">+</span><span style="font-family: Courier;" class="">&nbsp; &nbsp;&nbsp;</span><span style="font-family: Courier;" class="">return computeDiagnosticTypeKindString(type);</span><br style="font-family: Courier;" class=""><span style="font-family: Courier;" class="">+</span><span style="font-family: Courier;" class="">&nbsp;&nbsp;</span><span style="font-family: Courier;" class="">}</span><br style="font-family: Courier;" class=""><span style="font-family: Courier;" class="">+</span><span style="font-family: Courier;" class="">&nbsp;&nbsp;</span><span style="font-family: Courier;" class="">case TypeKind::WeakStorage:</span><br style="font-family: Courier;" class=""><span style="font-family: Courier;" class="">+</span><span style="font-family: Courier;" class="">&nbsp;&nbsp;</span><span style="font-family: Courier;" class="">case TypeKind::UnownedStorage:</span><br style="font-family: Courier;" class=""><span style="font-family: Courier;" class="">+</span><span style="font-family: Courier;" class="">&nbsp;&nbsp;</span><span style="font-family: Courier;" class="">case TypeKind::UnmanagedStorage:</span><br style="font-family: Courier;" class=""><span style="font-family: Courier;" class="">+</span><span style="font-family: Courier;" class="">&nbsp; &nbsp;&nbsp;</span><span style="font-family: Courier;" class="">llvm_unreachable("Not a part of the user facing type system");</span><br style="font-family: Courier;" class=""><font face="Courier" class="">+}<br class="">+<br class="">&nbsp;/// \brief Format a single diagnostic argument and write it to the given<br class="">&nbsp;/// stream.<br class="">&nbsp;static void formatDiagnosticArgument(StringRef Modifier,&nbsp;<br class="">@@ -439,16 +554,18 @@ static void formatDiagnosticArgument(StringRef Modifier,<br class="">&nbsp; &nbsp; &nbsp;<br class="">&nbsp; &nbsp; &nbsp;// Strip extraneous parentheses; they add no value.<br class="">&nbsp; &nbsp; &nbsp;auto type = Arg.getAsType()-&gt;getWithoutParens();<br class="">+&nbsp; &nbsp;&nbsp;auto origType = type-&gt;getInOutObjectType();<br class="">+&nbsp; &nbsp;&nbsp;std::string declKind = computeDiagnosticTypeKindString(type);<br class="">&nbsp; &nbsp; &nbsp;std::string typeName = type-&gt;getString();<br class="">&nbsp;<br class="">&nbsp; &nbsp; &nbsp;if (shouldShowAKA(type, typeName)) {<br class="">&nbsp; &nbsp; &nbsp; &nbsp;llvm::SmallString&lt;256&gt; AkaText;<br class="">&nbsp; &nbsp; &nbsp; &nbsp;llvm::raw_svector_ostream OutAka(AkaText);<br class="">-&nbsp; &nbsp; &nbsp;&nbsp;OutAka &lt;&lt; type-&gt;getCanonicalType();<br class="">-&nbsp; &nbsp; &nbsp;&nbsp;Out &lt;&lt; llvm::format(FormatOpts.AKAFormatString.c_str(), typeName.c_str(),<br class="">-&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;AkaText.c_str());<br class="">+&nbsp; &nbsp; &nbsp;&nbsp;OutAka &lt;&lt; origType-&gt;getCanonicalType();<br class="">+&nbsp; &nbsp; &nbsp;&nbsp;Out &lt;&lt; declKind &lt;&lt; llvm::format(FormatOpts.AKAFormatString.c_str(),<br class="">+&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;typeName.c_str(), AkaText.c_str());<br class="">&nbsp; &nbsp; &nbsp;} else {<br class="">-&nbsp; &nbsp; &nbsp;&nbsp;Out &lt;&lt; FormatOpts.OpeningQuotationMark &lt;&lt; typeName<br class="">+&nbsp; &nbsp; &nbsp;&nbsp;Out &lt;&lt; declKind &lt;&lt; FormatOpts.OpeningQuotationMark &lt;&lt; typeName<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;&lt; FormatOpts.ClosingQuotationMark;<br class="">&nbsp; &nbsp; &nbsp;}<br class="">&nbsp; &nbsp; &nbsp;break;</font><br class=""></div></div></div></div></div></div></body></html>