@@ -531,7 +531,7 @@ final class LexicalContextTests: XCTestCase {
531531 struct S {
532532 let arg: C
533533 var contextDescription: String {
534- #lexicalContextDescription
534+ unsafe try await #lexicalContextDescription
535535 }
536536 }
537537 return S(arg: c)
@@ -542,7 +542,10 @@ final class LexicalContextTests: XCTestCase {
542542 struct S {
543543 let arg: C
544544 var contextDescription: String {
545- """
545+ unsafe try await """
546+ await _
547+ try _
548+ unsafe _
546549 contextDescription: String
547550 struct S {}
548551 { c in
@@ -551,7 +554,7 @@ final class LexicalContextTests: XCTestCase {
551554 struct S {
552555 let arg: C
553556 var contextDescription: String {
554- #lexicalContextDescription
557+ unsafe try await #lexicalContextDescription
555558 }
556559 }
557560 return S(arg: c)
@@ -565,4 +568,182 @@ final class LexicalContextTests: XCTestCase {
565568 macros: [ " lexicalContextDescription " : LexicalContextDescriptionMacro . self]
566569 )
567570 }
571+
572+ func testEffectMarkersInSequenceLexicalContext( ) {
573+ // Valid cases.
574+ assertMacroExpansion (
575+ " unsafe try await #lexicalContextDescription + #lexicalContextDescription " ,
576+ expandedSource: #"""
577+ unsafe try await """
578+ await _
579+ try _
580+ unsafe _
581+ """ + """
582+ await _
583+ try _
584+ unsafe _
585+ """
586+ """# ,
587+ macros: [ " lexicalContextDescription " : LexicalContextDescriptionMacro . self]
588+ )
589+ assertMacroExpansion (
590+ " try unsafe await 0 + 1 + foo(#lexicalContextDescription) + 2 " ,
591+ expandedSource: #"""
592+ try unsafe await 0 + 1 + foo("""
593+ await _
594+ unsafe _
595+ try _
596+ """) + 2
597+ """# ,
598+ macros: [ " lexicalContextDescription " : LexicalContextDescriptionMacro . self]
599+ )
600+ assertMacroExpansion (
601+ " x = try await unsafe 0 + 1 + foo(#lexicalContextDescription) + 2 " ,
602+ expandedSource: #"""
603+ x = try await unsafe 0 + 1 + foo("""
604+ unsafe _
605+ await _
606+ try _
607+ """) + 2
608+ """# ,
609+ macros: [ " lexicalContextDescription " : LexicalContextDescriptionMacro . self]
610+ )
611+ // `unsafe try await` in the 'then' branch doesn't cover condition or else.
612+ assertMacroExpansion (
613+ " #lexicalContextDescription ? unsafe try await #lexicalContextDescription : #lexicalContextDescription " ,
614+ expandedSource: #"""
615+ """
616+ """ ? unsafe try await """
617+ await _
618+ try _
619+ unsafe _
620+ """ : """
621+ """
622+ """# ,
623+ macros: [ " lexicalContextDescription " : LexicalContextDescriptionMacro . self]
624+ )
625+ // Same for else.
626+ assertMacroExpansion (
627+ " #lexicalContextDescription ? #lexicalContextDescription : unsafe try await #lexicalContextDescription " ,
628+ expandedSource: #"""
629+ """
630+ """ ? """
631+ """ : unsafe try await """
632+ await _
633+ try _
634+ unsafe _
635+ """
636+ """# ,
637+ macros: [ " lexicalContextDescription " : LexicalContextDescriptionMacro . self]
638+ )
639+ // 'unsafe try await' in the condition here covers the entire expression
640+ assertMacroExpansion (
641+ " unsafe try await #lexicalContextDescription ? #lexicalContextDescription : #lexicalContextDescription ~~ #lexicalContextDescription " ,
642+ expandedSource: #"""
643+ unsafe try await """
644+ await _
645+ try _
646+ unsafe _
647+ """ ? """
648+ await _
649+ try _
650+ unsafe _
651+ """ : """
652+ await _
653+ try _
654+ unsafe _
655+ """ ~~ """
656+ await _
657+ try _
658+ unsafe _
659+ """
660+ """# ,
661+ macros: [ " lexicalContextDescription " : LexicalContextDescriptionMacro . self]
662+ )
663+ assertMacroExpansion (
664+ " x = unsafe try try! await 0 + #lexicalContextDescription " ,
665+ expandedSource: #"""
666+ x = unsafe try try! await 0 + """
667+ await _
668+ try! _
669+ try _
670+ unsafe _
671+ """
672+ """# ,
673+ macros: [ " lexicalContextDescription " : LexicalContextDescriptionMacro . self]
674+ )
675+
676+ // Invalid cases
677+ assertMacroExpansion (
678+ " 0 + unsafe try await #lexicalContextDescription " ,
679+ expandedSource: #"""
680+ 0 + unsafe try await """
681+ await _
682+ try _
683+ unsafe _
684+ """
685+ """# ,
686+ macros: [ " lexicalContextDescription " : LexicalContextDescriptionMacro . self]
687+ )
688+ // The `unsafe try await` may not actually cover `lexicalContextDescription`
689+ // here, but this will be rejected by the compiler.
690+ assertMacroExpansion (
691+ " 0 + unsafe try await 1 ^ #lexicalContextDescription " ,
692+ expandedSource: #"""
693+ 0 + unsafe try await 1 ^ """
694+ await _
695+ try _
696+ unsafe _
697+ """
698+ """# ,
699+ macros: [ " lexicalContextDescription " : LexicalContextDescriptionMacro . self]
700+ )
701+ // Invalid if '^' has a lower precedence than '='.
702+ assertMacroExpansion (
703+ " x = unsafe try await 0 ^ #lexicalContextDescription " ,
704+ expandedSource: #"""
705+ x = unsafe try await 0 ^ """
706+ await _
707+ try _
708+ unsafe _
709+ """
710+ """# ,
711+ macros: [ " lexicalContextDescription " : LexicalContextDescriptionMacro . self]
712+ )
713+ // Unassignable
714+ assertMacroExpansion (
715+ " #lexicalContextDescription = unsafe try await 0 + 1 " ,
716+ expandedSource: #"""
717+ """
718+ """ = unsafe try await 0 + 1
719+ """# ,
720+ macros: [ " lexicalContextDescription " : LexicalContextDescriptionMacro . self]
721+ )
722+ assertMacroExpansion (
723+ " unsafe try await #lexicalContextDescription = 0 + #lexicalContextDescription " ,
724+ expandedSource: #"""
725+ unsafe try await """
726+ await _
727+ try _
728+ unsafe _
729+ """ = 0 + """
730+ await _
731+ try _
732+ unsafe _
733+ """
734+ """# ,
735+ macros: [ " lexicalContextDescription " : LexicalContextDescriptionMacro . self]
736+ )
737+ assertMacroExpansion (
738+ " unsafe try await foo() ? 0 : 1 = #lexicalContextDescription " ,
739+ expandedSource: #"""
740+ unsafe try await foo() ? 0 : 1 = """
741+ await _
742+ try _
743+ unsafe _
744+ """
745+ """# ,
746+ macros: [ " lexicalContextDescription " : LexicalContextDescriptionMacro . self]
747+ )
748+ }
568749}
0 commit comments