- 
                Notifications
    You must be signed in to change notification settings 
- Fork 125
[WIP] Rethink how we capture expectation conditions and their subexpressions. #840
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Draft
      
      
            grynspan
  wants to merge
  29
  commits into
  main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
jgrynspan/162-redesign-value-capture
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
      
        
          +2,562
        
        
          −1,909
        
        
          
        
      
    
  
Conversation
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
    | @swift-ci test | 
31785d8    to
    8c19484      
    Compare
  
    | @swift-ci test | 
| This PR is blocked by swiftlang/swift-syntax#2724 (or some alternate solution) because the following expressions would have previously compiled while they will fail with this change: try #expect(await f())
try #expect(x[123]) | 
| @swift-ci test | 
7930583    to
    f23d8a3      
    Compare
  
    | @swift-ci test | 
4be404f    to
    6608954      
    Compare
  
    | @swift-ci test | 
    
      
        1 similar comment
      
    
  
    | @swift-ci test | 
9aad9f5    to
    58a7c82      
    Compare
  
    | @swift-ci test | 
58a7c82    to
    5bc8b84      
    Compare
  
    191352a    to
    da992cb      
    Compare
  
    | @swift-ci test | 
958457c    to
    9655fe4      
    Compare
  
    | @swift-ci test | 
  This was referenced Jun 13, 2025 
      
    
  grynspan 
      added a commit
      that referenced
      this pull request
    
      Jun 17, 2025 
    
    
      
  
    
      
    
  
… the lexical context. This PR changes the behaviour of the testing library for expressions such as: ```swift try #expect(a == b) ``` Currently, we don't expand that expression correctly because we can't tell where the `try` keyword should be applied. It sometimes expands and sometimes doesn't. This PR detects the presence of those keywords (with a recent-enough toolchain) and, if found, disables the fancy expansion in favour of a simpler one that is less likely to fail to compile. More thorough support for effectful expressions in expectations is tracked by #840 which involves fully refactoring the implementation of the `#expect()` macro.
  This was referenced Jun 17, 2025 
      
     Merged
  
    
  grynspan 
      added a commit
      that referenced
      this pull request
    
      Jun 18, 2025 
    
    
      
  
    
      
    
  
… the lexical context. (#1161) This PR changes the behaviour of the testing library for expressions such as: ```swift try #expect(a == b) ``` Currently, we don't expand that expression correctly because we can't tell where the `try` keyword should be applied. It sometimes expands and sometimes doesn't. This PR detects the presence of those keywords (with a recent-enough toolchain) and, if found, disables the fancy expansion in favour of a simpler one that is less likely to fail to compile. More thorough support for effectful expressions in expectations is tracked by #840 which involves fully refactoring the implementation of the `#expect()` macro. See also #162 for some more context. ### 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.
| @swift-ci test | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
      Labels
      
    enhancement
  New feature or request 
  
    issue-handling
  Related to Issue handling within the testing library 
  
    public-api
  Affects public API 
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
This PR completely rewrites how we capture expectation conditions.
Explanation
For example, given the following expectation:
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
tryorawaitkeywords) 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
Expressionthat 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…
… previously expanded to…
… but will now expand to:
More interestingly, an expression with side effects or complex nested operations can also be translated. For example, this throwing expression…
… was…
… but now becomes:
Caveats
There remain a few caveats (that also generally affect the current implementation):
Expressions where theThis is now resolved.tryorawaitkeyword is outside the#expectmacro cannot be expanded correctly because the macro cannot see those keywords during expansion.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: