@@ -613,6 +613,122 @@ main() {
613613 });
614614 });
615615
616+ test ('tryCatchStatement_bodyEnd() restores pre-try state' , () {
617+ var h = _Harness ();
618+ var x = h.addVar ('x' , 'int?' );
619+ var y = h.addVar ('y' , 'int?' );
620+ h.run ((flow) {
621+ h.declare (x, initialized: true );
622+ h.declare (y, initialized: true );
623+ h.promote (y, 'int' );
624+ flow.tryCatchStatement_bodyBegin ();
625+ h.promote (x, 'int' );
626+ expect (flow.promotedType (x).type, 'int' );
627+ expect (flow.promotedType (y).type, 'int' );
628+ flow.tryCatchStatement_bodyEnd ({});
629+ flow.tryCatchStatement_catchBegin ();
630+ expect (flow.promotedType (x), isNull);
631+ expect (flow.promotedType (y).type, 'int' );
632+ flow.tryCatchStatement_catchEnd ();
633+ flow.tryCatchStatement_end ();
634+ });
635+ });
636+
637+ test ('tryCatchStatement_bodyEnd() un-promotes variables assigned in body' ,
638+ () {
639+ var h = _Harness ();
640+ var x = h.addVar ('x' , 'int?' );
641+ h.run ((flow) {
642+ h.declare (x, initialized: true );
643+ h.promote (x, 'int' );
644+ expect (flow.promotedType (x).type, 'int' );
645+ flow.tryCatchStatement_bodyBegin ();
646+ flow.write (x);
647+ h.promote (x, 'int' );
648+ expect (flow.promotedType (x).type, 'int' );
649+ flow.tryCatchStatement_bodyEnd ({x});
650+ flow.tryCatchStatement_catchBegin ();
651+ expect (flow.promotedType (x), isNull);
652+ flow.tryCatchStatement_catchEnd ();
653+ flow.tryCatchStatement_end ();
654+ });
655+ });
656+
657+ test ('tryCatchStatement_catchBegin() restores previous post-body state' ,
658+ () {
659+ var h = _Harness ();
660+ var x = h.addVar ('x' , 'int?' );
661+ h.run ((flow) {
662+ h.declare (x, initialized: true );
663+ flow.tryCatchStatement_bodyBegin ();
664+ flow.tryCatchStatement_bodyEnd ({});
665+ flow.tryCatchStatement_catchBegin ();
666+ h.promote (x, 'int' );
667+ expect (flow.promotedType (x).type, 'int' );
668+ flow.tryCatchStatement_catchEnd ();
669+ flow.tryCatchStatement_catchBegin ();
670+ expect (flow.promotedType (x), isNull);
671+ flow.tryCatchStatement_catchEnd ();
672+ flow.tryCatchStatement_end ();
673+ });
674+ });
675+
676+ test ('tryCatchStatement_catchEnd() joins catch state with after-try state' ,
677+ () {
678+ var h = _Harness ();
679+ var x = h.addVar ('x' , 'int?' );
680+ var y = h.addVar ('y' , 'int?' );
681+ var z = h.addVar ('z' , 'int?' );
682+ h.run ((flow) {
683+ h.declare (x, initialized: true );
684+ h.declare (y, initialized: true );
685+ h.declare (z, initialized: true );
686+ flow.tryCatchStatement_bodyBegin ();
687+ h.promote (x, 'int' );
688+ h.promote (y, 'int' );
689+ flow.tryCatchStatement_bodyEnd ({});
690+ flow.tryCatchStatement_catchBegin ();
691+ h.promote (x, 'int' );
692+ h.promote (z, 'int' );
693+ flow.tryCatchStatement_catchEnd ();
694+ flow.tryCatchStatement_end ();
695+ // Only x should be promoted, because it's the only variable promoted
696+ // in both the try body and the catch handler.
697+ expect (flow.promotedType (x).type, 'int' );
698+ expect (flow.promotedType (y), isNull);
699+ expect (flow.promotedType (z), isNull);
700+ });
701+ });
702+
703+ test ('tryCatchStatement_catchEnd() joins catch states' , () {
704+ var h = _Harness ();
705+ var x = h.addVar ('x' , 'int?' );
706+ var y = h.addVar ('y' , 'int?' );
707+ var z = h.addVar ('z' , 'int?' );
708+ h.run ((flow) {
709+ h.declare (x, initialized: true );
710+ h.declare (y, initialized: true );
711+ h.declare (z, initialized: true );
712+ flow.tryCatchStatement_bodyBegin ();
713+ flow.handleExit ();
714+ flow.tryCatchStatement_bodyEnd ({});
715+ flow.tryCatchStatement_catchBegin ();
716+ h.promote (x, 'int' );
717+ h.promote (y, 'int' );
718+ flow.tryCatchStatement_catchEnd ();
719+ flow.tryCatchStatement_catchBegin ();
720+ h.promote (x, 'int' );
721+ h.promote (z, 'int' );
722+ flow.tryCatchStatement_catchEnd ();
723+ flow.tryCatchStatement_end ();
724+ // Only x should be promoted, because it's the only variable promoted
725+ // in both catch handlers.
726+ expect (flow.promotedType (x).type, 'int' );
727+ expect (flow.promotedType (y), isNull);
728+ expect (flow.promotedType (z), isNull);
729+ });
730+ });
731+
616732 test ('whileStatement_conditionBegin() un-promotes' , () {
617733 var h = _Harness ();
618734 var x = h.addVar ('x' , 'int?' );
0 commit comments