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.
51 lines
1.6 KiB
Swift
51 lines
1.6 KiB
Swift
// RUN: %target-typecheck-verify-swift -dump-ast > %t.dump
|
|
// RUN: %FileCheck %s < %t.dump
|
|
|
|
// https://github.com/apple/swift/issues/56212
|
|
|
|
extension Optional {
|
|
func member1() -> S1? {}
|
|
static func member2() -> S1? {}
|
|
static func member3() -> S1? {}
|
|
static var member_wrongType: Int { get {} }
|
|
static var member_overload: S1 { get {} }
|
|
|
|
init(overloaded: Void) {}
|
|
}
|
|
|
|
protocol P1 {}
|
|
extension Optional: P1 where Wrapped: Equatable {
|
|
static func member4() {}
|
|
}
|
|
|
|
struct S1 {
|
|
static var member1: S1? = S1()
|
|
static var member2: S1? = S1()
|
|
static func member3() -> S1? {}
|
|
static var member4: S1? { get {} }
|
|
static var member_wrongType: S1? { get {} }
|
|
static var member_overload: S1? { get {} }
|
|
|
|
init(overloaded: Void) {}
|
|
init?(failable: Void) {}
|
|
init() {}
|
|
}
|
|
|
|
let _: S1? = .member1
|
|
let _: S1? = .member_wrongType
|
|
let _: S1? = .init()
|
|
let _: S1? = .member1() // expected-error {{instance member 'member1' cannot be used on type 'S1?'}}
|
|
let _: S1? = .member2()
|
|
let _: S1? = .init(S1())
|
|
let _: S1? = .init(overloaded: ())
|
|
// If members exist on Optional and Wrapped, always choose the one on optional
|
|
// CHECK: declref_expr {{.*}} location={{.*}}optional_overload.swift:40
|
|
// CHECK-SAME: decl="optional_overload.(file).Optional extension.init(overloaded:)@
|
|
let _: S1? = .member_overload
|
|
// Should choose the overload from Optional even if the Wrapped overload would otherwise have a better score
|
|
// CHECK: member_ref_expr {{.*}} location={{.*}}optional_overload.swift:44
|
|
// CHECK-SAME: decl="optional_overload.(file).Optional extension.member_overload@
|
|
let _: S1? = .init(failable: ())
|
|
let _: S1? = .member3()
|
|
let _: S1? = .member4
|