Files
swift-mirror/test/ImportResolution/import-resolution-overload.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

80 lines
2.4 KiB
Swift

// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -emit-module -o %t %S/Inputs/overload_intFunctions.swift
// RUN: %target-swift-frontend -emit-module -o %t %S/Inputs/overload_boolFunctions.swift
// RUN: %target-swift-frontend -emit-module -o %t %S/Inputs/overload_vars.swift
// RUN: %target-swift-frontend -typecheck %s -I %t -sdk "" -verify -swift-version 4
// RUN: %target-swift-frontend -typecheck %s -I %t -sdk "" -verify -swift-version 5
// RUN: not %target-swift-frontend -dump-ast %s -I %t -sdk "" > %t.astdump
// RUN: %FileCheck %s < %t.astdump
import overload_intFunctions
import overload_boolFunctions
import overload_vars
import var overload_vars.scopedVar
import func overload_boolFunctions.scopedFunction
struct LocalType {}
func something(_ obj: LocalType) -> LocalType { return obj }
func something(_ a: Int, _ b: Int, _ c: Int) -> () {}
var _ : Bool = something(true)
var _ : Int = something(1)
var _ : (Int, Int) = something(1, 2)
var _ : LocalType = something(LocalType())
var _ : () = something(1, 2, 3)
something = 42
let ambValue = ambiguousWithVar // no-warning - var preferred
let ambValueChecked: Int = ambValue
ambiguousWithVar = 42 // no-warning
ambiguousWithVar(true) // no-warning
var localVar : Bool
localVar = false
localVar = 42 // expected-error {{cannot assign value of type 'Int' to type 'Bool'}}
localVar(42) // expected-error {{cannot call value of non-function type 'Bool'}}
var _ : localVar // should still work
_ = scopedVar // no-warning
scopedVar(42)
var _ : Bool = scopedFunction(true)
var _ : Int = scopedFunction(42)
scopedFunction = 42
// FIXME: Should be an error -- a type name and a function cannot overload.
var _ : Int = TypeNameWins(42)
TypeNameWins = 42 // expected-error {{cannot assign to immutable expression of type 'Int'}}
var _ : TypeNameWins // no-warning
// rdar://problem/21739333
protocol HasFooSub : HasFoo { }
extension HasFooSub {
var foo: Int { return 0 }
}
// CHECK-LABEL: func_decl{{.*}}"testHasFooSub(_:)"
func testHasFooSub(_ hfs: HasFooSub) -> Int {
// CHECK: return_stmt
// CHECK-NOT: func_decl
// CHECK: member_ref_expr{{.*}}decl="overload_vars.(file).HasFoo.foo
return hfs.foo
}
extension HasBar {
var bar: Int { return 0 }
}
// CHECK-LABEL: func_decl{{.*}}"testHasBar(_:)"
func testHasBar(_ hb: HasBar) -> Int {
// CHECK: return_stmt
// CHECK-NOT: func_decl
// CHECK: member_ref_expr{{.*}}decl="overload_vars.(file).HasBar.bar
return hb.bar
}