@@ -567,4 +567,165 @@ final class LexicalContextTests: XCTestCase {
567567 macros: [ " lexicalContextDescription " : LexicalContextDescriptionMacro . self]
568568 )
569569 }
570+
571+ func testTryAwaitInSequenceLexicalContext( ) {
572+ // Valid cases.
573+ assertMacroExpansion (
574+ " try await #lexicalContextDescription + #lexicalContextDescription " ,
575+ expandedSource: #"""
576+ try await """
577+ await _
578+ try _
579+ """ + """
580+ await _
581+ try _
582+ """
583+ """# ,
584+ macros: [ " lexicalContextDescription " : LexicalContextDescriptionMacro . self]
585+ )
586+ assertMacroExpansion (
587+ " try await 0 + 1 + foo(#lexicalContextDescription) + 2 " ,
588+ expandedSource: #"""
589+ try await 0 + 1 + foo("""
590+ await _
591+ try _
592+ """) + 2
593+ """# ,
594+ macros: [ " lexicalContextDescription " : LexicalContextDescriptionMacro . self]
595+ )
596+ assertMacroExpansion (
597+ " x = try await 0 + 1 + foo(#lexicalContextDescription) + 2 " ,
598+ expandedSource: #"""
599+ x = try await 0 + 1 + foo("""
600+ await _
601+ try _
602+ """) + 2
603+ """# ,
604+ macros: [ " lexicalContextDescription " : LexicalContextDescriptionMacro . self]
605+ )
606+ // `try await` in the 'then' branch doesn't cover condition or else.
607+ assertMacroExpansion (
608+ " #lexicalContextDescription ? try await #lexicalContextDescription : #lexicalContextDescription " ,
609+ expandedSource: #"""
610+ """
611+ """ ? try await """
612+ await _
613+ try _
614+ """ : """
615+ """
616+ """# ,
617+ macros: [ " lexicalContextDescription " : LexicalContextDescriptionMacro . self]
618+ )
619+ // Same for else.
620+ assertMacroExpansion (
621+ " #lexicalContextDescription ? #lexicalContextDescription : try await #lexicalContextDescription " ,
622+ expandedSource: #"""
623+ """
624+ """ ? """
625+ """ : try await """
626+ await _
627+ try _
628+ """
629+ """# ,
630+ macros: [ " lexicalContextDescription " : LexicalContextDescriptionMacro . self]
631+ )
632+ // 'try await' in the condition here covers the entire expression
633+ assertMacroExpansion (
634+ " try await #lexicalContextDescription ? #lexicalContextDescription : #lexicalContextDescription ~~ #lexicalContextDescription " ,
635+ expandedSource: #"""
636+ try await """
637+ await _
638+ try _
639+ """ ? """
640+ await _
641+ try _
642+ """ : """
643+ await _
644+ try _
645+ """ ~~ """
646+ await _
647+ try _
648+ """
649+ """# ,
650+ macros: [ " lexicalContextDescription " : LexicalContextDescriptionMacro . self]
651+ )
652+ assertMacroExpansion (
653+ " x = unsafe try try! await 0 + #lexicalContextDescription " ,
654+ expandedSource: #"""
655+ x = unsafe try try! await 0 + """
656+ await _
657+ try! _
658+ try _
659+ """
660+ """# ,
661+ macros: [ " lexicalContextDescription " : LexicalContextDescriptionMacro . self]
662+ )
663+
664+ // Invalid cases
665+ assertMacroExpansion (
666+ " 0 + try await #lexicalContextDescription " ,
667+ expandedSource: #"""
668+ 0 + try await """
669+ await _
670+ try _
671+ """
672+ """# ,
673+ macros: [ " lexicalContextDescription " : LexicalContextDescriptionMacro . self]
674+ )
675+ // The 'try await' may not actually cover 'lexicalContextDescription' here,
676+ // but this will be rejected by the compiler.
677+ assertMacroExpansion (
678+ " 0 + try await 1 ^ #lexicalContextDescription " ,
679+ expandedSource: #"""
680+ 0 + try await 1 ^ """
681+ await _
682+ try _
683+ """
684+ """# ,
685+ macros: [ " lexicalContextDescription " : LexicalContextDescriptionMacro . self]
686+ )
687+ // Invalid if '^' has a lower precedence than '='.
688+ assertMacroExpansion (
689+ " x = try await 0 ^ #lexicalContextDescription " ,
690+ expandedSource: #"""
691+ x = try await 0 ^ """
692+ await _
693+ try _
694+ """
695+ """# ,
696+ macros: [ " lexicalContextDescription " : LexicalContextDescriptionMacro . self]
697+ )
698+ // Unassignable
699+ assertMacroExpansion (
700+ " #lexicalContextDescription = try await 0 + 1 " ,
701+ expandedSource: #"""
702+ """
703+ """ = try await 0 + 1
704+ """# ,
705+ macros: [ " lexicalContextDescription " : LexicalContextDescriptionMacro . self]
706+ )
707+ assertMacroExpansion (
708+ " try await #lexicalContextDescription = 0 + #lexicalContextDescription " ,
709+ expandedSource: #"""
710+ try await """
711+ await _
712+ try _
713+ """ = 0 + """
714+ await _
715+ try _
716+ """
717+ """# ,
718+ macros: [ " lexicalContextDescription " : LexicalContextDescriptionMacro . self]
719+ )
720+ assertMacroExpansion (
721+ " try await foo() ? 0 : 1 = #lexicalContextDescription " ,
722+ expandedSource: #"""
723+ try await foo() ? 0 : 1 = """
724+ await _
725+ try _
726+ """
727+ """# ,
728+ macros: [ " lexicalContextDescription " : LexicalContextDescriptionMacro . self]
729+ )
730+ }
570731}
0 commit comments