Files
swift-mirror/test/decl/var/property_wrappers_synthesis.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

91 lines
2.3 KiB
Swift

// RUN: %target-swift-frontend -typecheck -dump-ast %s | %FileCheck %s
@propertyWrapper
struct Wrapper<T> {
var wrappedValue: T
}
protocol DefaultInit {
init()
}
// CHECK: struct_decl{{.*}}"UseWrapper"
struct UseWrapper<T: DefaultInit> {
// CHECK: var_decl{{.*}}"wrapped"
// CHECK: accessor_decl{{.*}}get for="wrapped"
// CHECK: member_ref_expr{{.*}}UseWrapper._wrapped
// CHECK: accessor_decl{{.*}}set for="wrapped"
// CHECK: member_ref_expr{{.*}}UseWrapper._wrapped
// CHECK: accessor_decl{{.*}}_modify for="wrapped"
// CHECK: yield_stmt
// CHECK: member_ref_expr{{.*}}Wrapper.wrappedValue
@Wrapper
var wrapped = T()
// CHECK: pattern_binding_decl implicit
// CHECK-NEXT: pattern_typed implicit type="Wrapper<T>"
// CHECK-NEXT: pattern_named implicit type="Wrapper<T>" "_wrapped"
// CHECK: constructor_ref_call_expr
// CHECK-NEXT: declref_expr{{.*}}Wrapper.init(wrappedValue:)
init() { }
}
struct UseWillSetDidSet {
// CHECK: var_decl{{.*}}"z"
// CHECK: accessor_decl{{.*}}set for="z"
// CHECK: member_ref_expr{{.*}}UseWillSetDidSet._z
@Wrapper
var z: Int {
willSet {
print(newValue)
}
didSet {
print(oldValue)
}
}
}
@propertyWrapper
struct Observable<Value> {
private var stored: Value
init(initialValue: Value) {
self.stored = initialValue
}
var wrappedValue: Value {
get { fatalError("called wrappedValue getter") }
set { fatalError("called wrappedValue setter") }
}
static subscript<EnclosingSelf>(
_enclosingInstance observed: EnclosingSelf,
wrapped wrappedKeyPath: ReferenceWritableKeyPath<EnclosingSelf, Value>,
storage storageKeyPath: ReferenceWritableKeyPath<EnclosingSelf, Self>
) -> Value {
get {
observed[keyPath: storageKeyPath].stored
}
set {
observed[keyPath: storageKeyPath].stored = newValue
}
}
}
// CHECK-LABEL: class_decl{{.*}}"MyObservedType"
class MyObservedType {
@Observable var observedProperty = 17
// CHECK: accessor_decl{{.*}}get for="observedProperty"
// CHECK: subscript_expr implicit type="@lvalue Int" decl="{{.*}}.Observable.subscript(_enclosingInstance:wrapped:storage:)@
// CHECK: accessor_decl{{.*}}set for="observedProperty"
// CHECK: subscript_expr implicit type="@lvalue Int" decl="{{.*}}.Observable.subscript(_enclosingInstance:wrapped:storage:)@
}