@@ -1671,6 +1671,17 @@ namespace {
16711671 }
16721672 }
16731673
1674+ // / Prints a `Type` if it is present, falling back to the `TypeRepr`
1675+ // / otherwise (and lastly, doing nothing if the `TypeRepr` was also null).
1676+ void printTypeOrTypeRepr (std::optional<Type> Ty, TypeRepr *Repr,
1677+ Label label) {
1678+ if (Ty.has_value ()) {
1679+ printTypeField (*Ty, label);
1680+ } else if (Repr) {
1681+ printRec (Repr, label);
1682+ }
1683+ }
1684+
16741685 void printThrowDest (ThrownErrorDestination throws, bool wantNothrow) {
16751686 if (!throws) {
16761687 if (wantNothrow)
@@ -1681,11 +1692,26 @@ namespace {
16811692
16821693 auto thrownError = throws.getThrownErrorType ();
16831694 auto contextError = throws.getContextErrorType ();
1695+ if (Writer.isParsable ()) {
1696+ // For parsable outputs, just print the full thrown and contextual error
1697+ // information as a nice structured object, even if they're the same.
1698+ printRecArbitrary (
1699+ [&](Label label) {
1700+ printHead (" thrown_error_destination" , IdentifierColor, label);
1701+ printTypeField (thrownError, Label::always (" thrown_type" ));
1702+ printTypeField (contextError, Label::always (" context_type" ));
1703+ printFoot ();
1704+ },
1705+ Label::always (" throws" ));
1706+ return ;
1707+ }
1708+
16841709 if (thrownError->isEqual (contextError)) {
1685- // No translation of the thrown error type is required, so ony print
1710+ // No translation of the thrown error type is required, so only print
16861711 // the thrown error type.
16871712 Type errorExistentialType =
16881713 contextError->getASTContext ().getErrorExistentialType ();
1714+
16891715 if (errorExistentialType && thrownError->isEqual (errorExistentialType))
16901716 printFlag (" throws" , ExprModifierColor);
16911717 else {
@@ -2456,29 +2482,20 @@ namespace {
24562482 if (auto *P = D->getImplicitSelfDecl ()) {
24572483 printRec (P, Label::optional (" implicit_self_decl" ));
24582484 }
2459- printRec (D->getParameters (), Label::optional (" params" ),
2460- &D->getASTContext ());
2461-
24622485 if (auto FD = dyn_cast<FuncDecl>(D)) {
2463- if (Writer.isParsable () && FD->hasInterfaceType ()) {
2464- printTypeField (FD->getResultInterfaceType (), Label::always (" result" ));
2465- if (auto opaque = FD->getOpaqueResultTypeDecl ()) {
2466- printRec (opaque, Label::always (" opaque_result_decl" ));
2467- }
2468- } else if (FD->getResultTypeRepr ()) {
2469- printRec (FD->getResultTypeRepr (), Label::always (" result" ));
2470- if (auto opaque = FD->getOpaqueResultTypeDecl ()) {
2471- printRec (opaque, Label::always (" opaque_result_decl" ));
2472- }
2486+ printTypeOrTypeRepr (FD->getCachedResultInterfaceType (),
2487+ FD->getResultTypeRepr (), Label::always (" result" ));
2488+ if (auto opaque = FD->getCachedOpaqueResultTypeDecl ();
2489+ opaque && *opaque != nullptr ) {
2490+ printRec (*opaque, Label::always (" opaque_result_decl" ));
24732491 }
24742492 }
24752493
2476- if (Writer.isParsable () && D->hasInterfaceType ()) {
2477- printTypeField (D->getThrownInterfaceType (),
2478- Label::always (" thrown_type" ));
2479- } else if (auto thrownTypeRepr = D->getThrownTypeRepr ()) {
2480- printRec (thrownTypeRepr,Label::always (" thrown_type" ));
2481- }
2494+ printTypeOrTypeRepr (D->getCachedThrownInterfaceType (),
2495+ D->getThrownTypeRepr (), Label::always (" thrown_type" ));
2496+
2497+ printRec (D->getParameters (), Label::optional (" params" ),
2498+ &D->getASTContext ());
24822499
24832500 if (auto fac = D->getForeignAsyncConvention ()) {
24842501 printRecArbitrary ([&](Label label) {
@@ -2653,10 +2670,8 @@ namespace {
26532670 printAttributes (MD);
26542671 printRec (MD->getParameterList (), Label::optional (" params" ),
26552672 &MD->getASTContext ());
2656- if (Writer.isParsable () && MD->getResultInterfaceType ())
2657- printTypeField (MD->getResultInterfaceType (), Label::always (" result" ));
2658- else if (MD->resultType .getTypeRepr ())
2659- printRec (MD->resultType .getTypeRepr (), Label::always (" result" ));
2673+ printTypeOrTypeRepr (MD->getCachedResultInterfaceType (),
2674+ MD->getResultTypeRepr (), Label::always (" result" ));
26602675 printRec (MD->definition , Label::always (" definition" ));
26612676 printFoot ();
26622677 }
@@ -4861,10 +4876,12 @@ class PrintAttribute : public AttributeVisitor<PrintAttribute, void, Label>,
48614876 }
48624877 void visitCustomAttr (CustomAttr *Attr, Label label) {
48634878 printCommon (Attr, " custom_attr" , label);
4864- if (Writer.isParsable ())
4865- printTypeField (Attr->getType (), Label::always (" type" ));
4866- else
4879+ printTypeField (Attr->getType (), Label::always (" type" ));
4880+ if (!Writer.isParsable ()) {
4881+ // The type has the semantic information we want for parsable outputs, so
4882+ // omit the `TypeRepr` there.
48674883 printRec (Attr->getTypeRepr (), Label::optional (" type_repr" ));
4884+ }
48684885 if (Attr->getArgs ())
48694886 printRec (Attr->getArgs (), Label::optional (" args" ));
48704887 printFoot ();
@@ -4925,8 +4942,11 @@ class PrintAttribute : public AttributeVisitor<PrintAttribute, void, Label>,
49254942 void visitImplementsAttr (ImplementsAttr *Attr, Label label) {
49264943 printCommon (Attr, " implements_attr" , label);
49274944 if (Writer.isParsable ()) {
4928- printFieldQuoted (declUSR (Attr->getProtocol (DC)),
4929- Label::always (" protocol" ));
4945+ // Print the resolved protocol's USR in parsable outputs, not the
4946+ // TypeRepr.
4947+ if (auto PD = Attr->getCachedProtocol (DC); PD && *PD != nullptr ) {
4948+ printFieldQuoted (declUSR (*PD), Label::always (" protocol" ));
4949+ }
49304950 } else {
49314951 printRec (Attr->getProtocolTypeRepr (), Label::always (" protocol" ));
49324952 }
0 commit comments