Commit c9d3cec
committed
This PR completely rewrites how we capture expectation conditions.
### Explanation
For example, given the following expectation:
```swift
#expect(x.f() == 123)
```
We currently detect that there is a binary operation and emit code that calls the binary operator as a closure and passes the left-hand value and right-hand value, then checks that the result of the operation is `true`.
This is sufficient for simpler expressions like that one, but more complex ones (including any that involve `try` or `await` keywords) cannot be expanded correctly. With this PR, such expressions _can_ generally be expanded correctly.
The change involves rewriting the macro condition as a closure to which is passed a local, mutable "context" value. Subexpressions of the condition expression are then rewritten by walking the syntax tree of the expression (using typical swift-syntax API) and replacing them with calls into the context value that pass in the value and related state.
If the expectation ultimately fails, the collected data is transformed into an instance of the SPI type `Expression` that contains the source code of the expression and interesting subexpressions as well as the runtime values of those subexpressions.
Nodes in the syntax tree are identified by a unique ID which is composed of the swift-syntax ID for that node as well as all its parent nodes in a compact bitmask format. These IDs can be transformed into graph/trie key paths when expression/subexpression relationships need to be reconstructed on failure, meaning that a single rewritten node doesn't otherwise need to know its "place" in the overall expression.
### Examples
As an example, this expectation…
```swift
#expect(g() > 500)
```
… previously expanded to…
```swift
Testing.__checkBinaryOperation(
g(),
{ $0 > $1() },
500,
expression: .__fromBinaryOperation(
.__fromSyntaxNode("g()"),
">",
.__fromSyntaxNode("500")
),
comments: [],
isRequired: false,
sourceLocation: Testing.SourceLocation.__here()
).__expected()
```
… but will now expand to:
```swift
Testing.__checkCondition(
{ (__ec: inout Testing.__ExpectationContext) -> Swift.Bool in
__ec(__ec(g(),0x2) > 500,0x0)
},
sourceCode: [
0x0: "g() > 500",
0x2: "g()"
],
comments: [],
isRequired: false,
sourceLocation: Testing.SourceLocation.__here()
).__expected()
```
More interestingly, an expression with side effects or complex nested operations can also be translated. For example, this throwing expression…
```swift
#expect((try g() > 500) && true)
```
… was…
```swift
Testing.__checkValue(
(try g() > 500) && true,
expression: .__fromSyntaxNode("(try g() > 500) && true"),
comments: [],
isRequired: false,
sourceLocation: Testing.SourceLocation.__here()
).__expected()
```
… but now becomes:
```swift
try Testing.__checkCondition(
{ (__ec: inout Testing.__ExpectationContext) -> Swift.Bool in
try Testing.__requiringTry(__ec(__ec((try __ec(__ec(g(),0x1ba) > 500,0xba)),0x2) && __ec(true,0x400000),0x0))
},
sourceCode: [
0x0: "(try g() > 500) && true",
0x2: "(try g() > 500)",
0xba: "g() > 500",
0x1ba: "g()",
0x400000: "true"
],
comments: [],
isRequired: false,
sourceLocation: Testing.SourceLocation.__here()
).__expected()
```
### Caveats
There remain a few caveats (that also generally affect the current implementation):
- Mutating member functions are syntactically indistinguishable from non-mutating ones and miscompile when rewritten;
- Expressions involving move-only types are also indistinguishable, but need lifetime management to be rewritten correctly; and
- ~~Expressions where the `try` or `await` keyword is _outside_ the `#expect` macro cannot be expanded correctly because the macro cannot see those keywords during expansion.~~ This is now resolved.
- Operations that cause us to capture move-only values (which is disallowed in most contexts) are syntactically indistinguishable from "normal" operations, which causes compilation of such code to fail obscurely.
The first issue might be resolvable in the future using pointer tricks, although I don't hold a lot of hope for it. The second issue is probably resolved by non-escaping types. The third issue is an area of active exploration for us and the macros/swift-syntax team.
### Resolved Issues
Resolves #162.
Resolves rdar://135437448.
### Checklist:
- [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md).
- [x] If public symbols are renamed or modified, DocC references should be updated.1 parent 06a94d4 commit c9d3cec
File tree
39 files changed
+2343
-1702
lines changed- Sources
- TestingMacros
- Support
- Additions
- Testing
- Events/Recorder
- ExitTests
- Expectations
- Issues
- SourceAttribution
- Support
- Additions
- Testing.docc
- _TestingInternals/include
- Tests
- SubexpressionShowcase
- TestingMacrosTests
- TestSupport
- TestingTests
- Traits
39 files changed
+2343
-1702
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
186 | 186 | | |
187 | 187 | | |
188 | 188 | | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
189 | 196 | | |
190 | 197 | | |
191 | 198 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
52 | 52 | | |
53 | 53 | | |
54 | 54 | | |
| 55 | + | |
55 | 56 | | |
56 | 57 | | |
57 | 58 | | |
| |||
74 | 75 | | |
75 | 76 | | |
76 | 77 | | |
77 | | - | |
| 78 | + | |
78 | 79 | | |
79 | 80 | | |
80 | 81 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
187 | 187 | | |
188 | 188 | | |
189 | 189 | | |
190 | | - | |
| 190 | + | |
191 | 191 | | |
192 | 192 | | |
193 | 193 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
464 | 464 | | |
465 | 465 | | |
466 | 466 | | |
467 | | - | |
| 467 | + | |
468 | 468 | | |
469 | 469 | | |
470 | 470 | | |
471 | 471 | | |
472 | | - | |
| 472 | + | |
473 | 473 | | |
474 | 474 | | |
475 | | - | |
476 | 475 | | |
477 | | - | |
| 476 | + | |
478 | 477 | | |
479 | 478 | | |
480 | 479 | | |
| |||
527 | 526 | | |
528 | 527 | | |
529 | 528 | | |
530 | | - | |
531 | | - | |
532 | | - | |
533 | | - | |
534 | | - | |
535 | | - | |
536 | | - | |
537 | | - | |
538 | | - | |
539 | | - | |
540 | | - | |
541 | | - | |
542 | | - | |
543 | | - | |
544 | | - | |
545 | | - | |
546 | | - | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
547 | 534 | | |
548 | | - | |
549 | | - | |
550 | | - | |
| 535 | + | |
| 536 | + | |
551 | 537 | | |
552 | 538 | | |
553 | 539 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
66 | 66 | | |
67 | 67 | | |
68 | 68 | | |
69 | | - | |
| 69 | + | |
70 | 70 | | |
71 | 71 | | |
72 | | - | |
| 72 | + | |
73 | 73 | | |
74 | 74 | | |
75 | 75 | | |
| |||
89 | 89 | | |
90 | 90 | | |
91 | 91 | | |
92 | | - | |
| 92 | + | |
93 | 93 | | |
94 | 94 | | |
95 | 95 | | |
| |||
118 | 118 | | |
119 | 119 | | |
120 | 120 | | |
121 | | - | |
| 121 | + | |
122 | 122 | | |
123 | 123 | | |
124 | 124 | | |
125 | 125 | | |
126 | 126 | | |
127 | | - | |
| 127 | + | |
128 | 128 | | |
129 | 129 | | |
130 | | - | |
| 130 | + | |
131 | 131 | | |
132 | 132 | | |
133 | 133 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | | - | |
| 15 | + | |
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
22 | | - | |
| 22 | + | |
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
39 | 34 | | |
40 | 35 | | |
41 | 36 | | |
| |||
100 | 95 | | |
101 | 96 | | |
102 | 97 | | |
103 | | - | |
| 98 | + | |
104 | 99 | | |
105 | 100 | | |
106 | 101 | | |
| |||
0 commit comments