mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
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.
82 lines
2.9 KiB
Swift
82 lines
2.9 KiB
Swift
// RUN: %target-swift-frontend -typecheck -dump-ast %s | %FileCheck %s
|
|
|
|
// FIXME: Make this a SILGen test instead.
|
|
// Even though redundant conversions are eventually optimized away, test from
|
|
// the get-go that we build these implicit conversions only when necessary.
|
|
protocol P {}
|
|
|
|
class A {
|
|
required init() {}
|
|
|
|
func method() -> Self { self }
|
|
var property: Self { self }
|
|
subscript() -> Self { self }
|
|
|
|
static func staticMethod() -> Self { .init() }
|
|
static var staticProperty: Self { .init() }
|
|
static subscript() -> Self { .init() }
|
|
}
|
|
|
|
class B: A {
|
|
func test() -> Self {
|
|
// CHECK: covariant_return_conversion_expr implicit type="Self" location={{.*}}.swift:[[@LINE+1]]
|
|
_ = super.method()
|
|
// CHECK: covariant_function_conversion_expr implicit type="() -> Self" location={{.*}}.swift:[[@LINE+1]]
|
|
_ = super.method
|
|
// CHECK: covariant_return_conversion_expr implicit type="Self" location={{.*}}.swift:[[@LINE+1]]
|
|
_ = super.property
|
|
// CHECK: covariant_return_conversion_expr implicit type="Self" location={{.*}}.swift:[[@LINE+1]]
|
|
_ = super[]
|
|
|
|
return super.property
|
|
}
|
|
|
|
static func testStatic() -> Self {
|
|
// CHECK: covariant_return_conversion_expr implicit type="Self" location={{.*}}.swift:[[@LINE+1]]
|
|
_ = super.staticMethod()
|
|
// CHECK: covariant_function_conversion_expr implicit type="() -> Self" location={{.*}}.swift:[[@LINE+1]]
|
|
_ = super.staticMethod
|
|
// CHECK-NOT: function_conversion_expr {{.*}} location={{.*}}.swift:[[@LINE+3]]
|
|
// CHECK-NOT: covariant_function_conversion_expr {{.*}} location={{.*}}.swift:[[@LINE+2]]
|
|
// CHECK: covariant_return_conversion_expr implicit type="Self" location={{.*}}.swift:[[@LINE+1]]
|
|
_ = self.method
|
|
// CHECK: covariant_return_conversion_expr implicit type="Self" location={{.*}}.swift:[[@LINE+1]]
|
|
_ = self.init()
|
|
// CHECK: covariant_return_conversion_expr implicit type="Self" location={{.*}}.swift:[[@LINE+1]]
|
|
_ = super.staticProperty
|
|
// CHECK: covariant_return_conversion_expr implicit type="Self" location={{.*}}.swift:[[@LINE+1]]
|
|
_ = super[]
|
|
|
|
return super.staticProperty
|
|
}
|
|
}
|
|
|
|
func testOnExistential(arg: P & A) {
|
|
// FIXME: This could be a single conversion.
|
|
// CHECK: function_conversion_expr implicit type="() -> any A & P" location={{.*}}.swift:[[@LINE+2]]
|
|
// CHECK-NEXT: covariant_function_conversion_expr implicit type="() -> any A & P" location={{.*}}.swift:[[@LINE+1]]
|
|
_ = arg.method
|
|
}
|
|
|
|
class Generic<T> {}
|
|
extension Generic where T == Never {
|
|
func method() -> Self { self }
|
|
var property: Self { self }
|
|
subscript() -> Self { self }
|
|
|
|
func test() {
|
|
// CHECK-NOT: conversion_expr {{.*}} location={{.*}}.swift:{{[[@LINE+1]]|[[@LINE+2]]|[[@LINE+3]]|[[@LINE+4]]}}
|
|
_ = Generic().method()
|
|
_ = Generic().method
|
|
_ = Generic().property
|
|
_ = Generic()[]
|
|
}
|
|
}
|
|
|
|
final class Final {
|
|
static func useSelf(_ body: (Self) -> ()) {}
|
|
}
|
|
func testNoErasure(_ body: (Final) -> ()) {
|
|
return Final.useSelf(body)
|
|
}
|