@@ -2147,32 +2147,42 @@ void TranslateToFuzzReader::fixAfterChanges(Function* func) {
21472147 }
21482148
21492149 void visitRethrow (Rethrow* curr) {
2150- if (!isValidRethrow (curr->target )) {
2150+ if (!isValidTryRef (curr->target , curr )) {
21512151 replace ();
21522152 }
21532153 }
21542154
2155- bool isValidRethrow (Name target) {
2156- // The rethrow must be on top.
2155+ void visitTry (Try* curr) {
2156+ if (curr->delegateTarget .is () &&
2157+ !isValidTryRef (curr->delegateTarget , curr)) {
2158+ replace ();
2159+ }
2160+ }
2161+
2162+ // Check if a reference to a try is valid.
2163+ bool isValidTryRef (Name target, Expression* curr) {
2164+ // The rethrow or try must be on top.
21572165 assert (!expressionStack.empty ());
2158- assert (expressionStack.back ()-> is <Rethrow>() );
2166+ assert (expressionStack.back () == curr );
21592167 if (expressionStack.size () < 2 ) {
2160- // There must be a try for this rethrow to be valid.
2168+ // There must be a parent try for this to be valid.
21612169 return false ;
21622170 }
21632171 Index i = expressionStack.size () - 2 ;
2172+ // Rethrows and try-delegates must target a try. Find it.
21642173 while (1 ) {
2165- auto * curr = expressionStack[i];
2166- if (auto * tryy = curr->dynCast <Try>()) {
2167- // The rethrow must target a try, and must be nested in a catch of
2168- // that try (not the body). Look at the child above us to check, when
2169- // we find the proper try.
2174+ if (auto * tryy = expressionStack[i]->dynCast <Try>()) {
2175+ // A rethrow must be nested in a catch of that try, not the body. A
2176+ // try-delegate is the reverse. Look at the child above us to check,
2177+ // when we find the proper try.
21702178 if (tryy->name == target) {
2171- if (i + 1 >= expressionStack.size ()) {
2172- return false ;
2173- }
2179+ assert (i + 1 < expressionStack.size ());
21742180 auto * child = expressionStack[i + 1 ];
2175- return child != tryy->body ;
2181+ if (curr->is <Rethrow>()) {
2182+ return child != tryy->body ;
2183+ }
2184+ assert (curr->is <Try>());
2185+ return child == tryy->body ;
21762186 }
21772187 }
21782188 if (i == 0 ) {
0 commit comments