Files
swift-mirror/test/decl/protocol/override.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

160 lines
3.6 KiB
Swift

// RUN: %target-typecheck-verify-swift -dump-ast > %t.ast
// RUN: %FileCheck %s < %t.ast
// Test overriding of protocol members.
// CHECK: protocol{{.*}}"P0"
protocol P0 {
associatedtype A
// expected-note@+1{{potential overridden instance method 'foo()' here}}
func foo()
// expected-note@+1{{attempt to override property here}}
var prop: A { get }
}
// CHECK: protocol{{.*}}"P1"
protocol P1: P0 {
// CHECK: associated_type_decl
// CHECK-SAME: overridden="P0"
associatedtype A
// CHECK: func_decl{{.*}}foo(){{.*}}Self : P1{{.*}}override={{.*}}P0.foo
func foo()
// CHECK: var_decl
// CHECK-SAME: "prop"
// CHECK-SAME: override="override.(file).P0.prop
var prop: A { get }
}
// CHECK: protocol{{.*}}"P2"
protocol P2 {
associatedtype A
func foo()
var prop: A { get }
}
// CHECK: protocol{{.*}}"P3"
protocol P3: P1, P2 {
// CHECK: associated_type_decl
// CHECK-SAME: "A"
// CHECK-SAME: override="override.(file).P1.A
// CHECK-SAME: override.(file).P2.A
associatedtype A
// CHECK: func_decl{{.*}}foo(){{.*}}Self : P3{{.*}}override={{.*}}P2.foo{{.*,.*}}P1.foo
func foo()
// CHECK: var_decl
// CHECK-SAME: "prop"
// CHECK-SAME: override="override.(file).P2.prop
// CHECK-SAME: override.(file).P1.prop
var prop: A { get }
}
// CHECK: protocol{{.*}}"P4"
protocol P4: P0 {
// CHECK: func_decl{{.*}}foo(){{.*}}Self : P4
// CHECK-NOT: override=
// CHECK-SAME: )
func foo() -> Int
// CHECK: var_decl
// CHECK-SAME: "prop"
// CHECK-NOT: override=
// CHECK-SAME: immutable
var prop: Int { get }
}
// CHECK: protocol{{.*}}"P5"
protocol P5: P0 where Self.A == Int {
// CHECK: func_decl{{.*}}foo(){{.*}}Self : P5
// CHECK-NOT: override=
// CHECK-SAME: )
func foo() -> Int
// CHECK: var_decl
// CHECK-SAME: "prop"
// CHECK-SAME: override="override.(file).P0.prop
var prop: Int { get }
}
// Allow the 'override' keyword on protocol members (it is not required).
// CHECK: protocol{{.*}}"P6"
protocol P6: P0 {
// CHECK: func_decl{{.*}}foo(){{.*}}Self : P6{{.*}}override={{.*}}P0.foo
override func foo()
// CHECK: var_decl
// CHECK-SAME: "prop"
// CHECK-SAME: override="override.(file).P0.prop
override var prop: A { get }
}
// Complain if 'override' is present but there is no overridden declaration.
protocol P7: P0 {
// expected-error@+1{{method does not override any method from its parent protocol}}
override func foo() -> Int
// expected-error@+1{{property 'prop' with type 'Int' cannot override a property with type 'Self.A'}}
override var prop: Int { get }
}
// Suppress overrides.
// CHECK: protocol{{.*}}"P8"
protocol P8: P0 {
// CHECK: associated_type_decl
// CHECK-SAME: "A"
// CHECK-NOT: override
// CHECK-SAME: )
@_nonoverride
associatedtype A
// CHECK: func_decl{{.*}}foo(){{.*}}Self : P8
// CHECK-NOT: override=
// CHECK-SAME: )
@_nonoverride
func foo()
// CHECK: var_decl
// CHECK-SAME: "prop"
// CHECK-NOT: override=
// CHECK-SAME: immutable
@_nonoverride
var prop: A { get }
}
class Base { }
class Derived : Base { }
// Protocol overrides require exact type matches.
protocol SubtypingP0 {
init?()
func foo() -> Base
}
// CHECK: protocol{{.*}}"SubtypingP1"
protocol SubtypingP1: SubtypingP0 {
// CHECK: constructor_decl{{.*}}Self : SubtypingP1
// CHECK-NOT: override=
// CHECK-SAME: designated
init()
// CHECK: func_decl{{.*}}foo(){{.*}}Self : SubtypingP1
// CHECK-NOT: override=
// CHECK-SAME: )
func foo() -> Derived
}
// CHECK: protocol{{.*}}"SubtypingP2"
protocol SubtypingP2: SubtypingP0 {
// CHECK: constructor_decl{{.*}}Self : SubtypingP2
// CHECK: override={{.*}}SubtypingP0.init
// CHECK-SAME: designated
init?()
}