Files
swift-mirror/test/decl/protocol/req/existentials_covariant_erasure.swift
Becca Royal-Gordon 8770c7f826 Rework ASTDumper (#68438)
This PR refactors the ASTDumper to make it more structured, less mistake-prone, and more amenable to future changes. For example:

```cpp
  // Before:
  void visitUnresolvedDotExpr(UnresolvedDotExpr *E) {
    printCommon(E, "unresolved_dot_expr")
      << " field '" << E->getName() << "'";
    PrintWithColorRAII(OS, ExprModifierColor)
      << " function_ref=" << getFunctionRefKindStr(E->getFunctionRefKind());
    if (E->getBase()) {
      OS << '\n';
      printRec(E->getBase());
    }
    PrintWithColorRAII(OS, ParenthesisColor) << ')';
  }

  // After:
  void visitUnresolvedDotExpr(UnresolvedDotExpr *E, StringRef label) {
    printCommon(E, "unresolved_dot_expr", label);

    printFieldQuoted(E->getName(), "field");
    printField(E->getFunctionRefKind(), "function_ref", ExprModifierColor);

    if (E->getBase()) {
      printRec(E->getBase());
    }

    printFoot();
  }
```

* Values are printed through calls to base class methods, rather than direct access to the underlying `raw_ostream`.
    * These methods tend to reduce the chances of bugs like missing/extra spaces or newlines, too much/too little indentation, etc.
    * More values are quoted, and unprintable/non-ASCII characters in quoted values are escaped before printing.
* Infrastructure to label child nodes now exists.
    * Some weird breaks from the normal "style", like `PatternBindingDecl`'s original and processed initializers, have been brought into line.
* Some types that previously used ad-hoc dumping functions, like conformances and substitution maps, are now structured similarly to the dumper classes.
* I've fixed the odd dumping bug along the way. For example, distributed actors were only marked `actor`, not `distributed actor`.

This PR doesn't change the overall style of AST dumps; they're still pseudo-S-expressions. But the logic that implements this style is now isolated into a relatively small base class, making it feasible to introduce e.g. JSON dumping in the future.
2023-09-11 23:56:38 -07:00

45 lines
1.8 KiB
Swift

// RUN: %target-swift-frontend -typecheck -dump-ast %s | %FileCheck %s
// Test that covariant "Self" references get erased to the existential base type
// when operating on existential values.
class C {}
protocol P {
func lotsOfSelfFunc(
_: (Self) -> Void,
_: (Self?) -> Void,
_: ([Self]) -> Void,
_: ([Array<Self>?]) -> Void
) -> [String : () -> Self]
var lotsOfSelfProp: (
_: (Self) -> Void,
_: (Self?) -> Void,
_: ([Self]) -> Void,
_: ([Array<Self>?]) -> Void
) -> [String : () -> Self] { get }
}
protocol Q {}
do {
class C {}
func testCovariantSelfErasure(p: P, pq: P & Q, pc: P & C) {
let x1 = p.lotsOfSelfFunc
let x2 = p.lotsOfSelfProp
let x3 = pq.lotsOfSelfFunc
let x4 = pq.lotsOfSelfProp
let x5 = pc.lotsOfSelfFunc
let x6 = pc.lotsOfSelfProp
// CHECK: (pattern_named type="((any P) -> Void, ((any P)?) -> Void, ([any P]) -> Void, ([Array<any P>?]) -> Void) -> [String : () -> any P]" "x1")
// CHECK: (pattern_named type="((any P) -> Void, ((any P)?) -> Void, ([any P]) -> Void, ([Array<any P>?]) -> Void) -> [String : () -> any P]" "x2")
// CHECK: (pattern_named type="((any P & Q) -> Void, ((any P & Q)?) -> Void, ([any P & Q]) -> Void, ([Array<any P & Q>?]) -> Void) -> [String : () -> any P & Q]" "x3")
// CHECK: (pattern_named type="((any P & Q) -> Void, ((any P & Q)?) -> Void, ([any P & Q]) -> Void, ([Array<any P & Q>?]) -> Void) -> [String : () -> any P & Q]" "x4")
// CHECK: (pattern_named type="((any C & P) -> Void, ((any C & P)?) -> Void, ([any C & P]) -> Void, ([Array<any C & P>?]) -> Void) -> [String : () -> any C & P]" "x5")
// CHECK: (pattern_named type="((any C & P) -> Void, ((any C & P)?) -> Void, ([any C & P]) -> Void, ([Array<any C & P>?]) -> Void) -> [String : () -> any C & P]" "x6")
}
}