@@ -1671,6 +1671,17 @@ namespace {
1671
1671
}
1672
1672
}
1673
1673
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
+
1674
1685
void printThrowDest (ThrownErrorDestination throws, bool wantNothrow) {
1675
1686
if (!throws) {
1676
1687
if (wantNothrow)
@@ -1681,11 +1692,26 @@ namespace {
1681
1692
1682
1693
auto thrownError = throws.getThrownErrorType ();
1683
1694
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
+
1684
1709
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
1686
1711
// the thrown error type.
1687
1712
Type errorExistentialType =
1688
1713
contextError->getASTContext ().getErrorExistentialType ();
1714
+
1689
1715
if (errorExistentialType && thrownError->isEqual (errorExistentialType))
1690
1716
printFlag (" throws" , ExprModifierColor);
1691
1717
else {
@@ -2456,29 +2482,20 @@ namespace {
2456
2482
if (auto *P = D->getImplicitSelfDecl ()) {
2457
2483
printRec (P, Label::optional (" implicit_self_decl" ));
2458
2484
}
2459
- printRec (D->getParameters (), Label::optional (" params" ),
2460
- &D->getASTContext ());
2461
-
2462
2485
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" ));
2473
2491
}
2474
2492
}
2475
2493
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 ());
2482
2499
2483
2500
if (auto fac = D->getForeignAsyncConvention ()) {
2484
2501
printRecArbitrary ([&](Label label) {
@@ -2653,10 +2670,8 @@ namespace {
2653
2670
printAttributes (MD);
2654
2671
printRec (MD->getParameterList (), Label::optional (" params" ),
2655
2672
&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" ));
2660
2675
printRec (MD->definition , Label::always (" definition" ));
2661
2676
printFoot ();
2662
2677
}
@@ -4861,10 +4876,12 @@ class PrintAttribute : public AttributeVisitor<PrintAttribute, void, Label>,
4861
4876
}
4862
4877
void visitCustomAttr (CustomAttr *Attr, Label label) {
4863
4878
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.
4867
4883
printRec (Attr->getTypeRepr (), Label::optional (" type_repr" ));
4884
+ }
4868
4885
if (Attr->getArgs ())
4869
4886
printRec (Attr->getArgs (), Label::optional (" args" ));
4870
4887
printFoot ();
@@ -4925,8 +4942,11 @@ class PrintAttribute : public AttributeVisitor<PrintAttribute, void, Label>,
4925
4942
void visitImplementsAttr (ImplementsAttr *Attr, Label label) {
4926
4943
printCommon (Attr, " implements_attr" , label);
4927
4944
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
+ }
4930
4950
} else {
4931
4951
printRec (Attr->getProtocolTypeRepr (), Label::always (" protocol" ));
4932
4952
}
0 commit comments