Skip to content

Commit 39d4d75

Browse files
cherrymuiianlancetaylor
authored andcommitted
compiler: do order_evaluations before remove_shortcuts
In remove_shortcuts, the shortcut expressions (&&, ||) are rewritten to if statements, which are lifted out before the statement containing the shortcut expression. If the containing statement has other (sub)expressions that should be evaluated before the shortcut expression, which has not been lifted out, this will result in wrong evaluation order. For example, F() + G(A() && B()), the evaluation order per spec is F, A, B (if A returns true), G. If we lift A() and B() out first, they will be called before F, which is wrong. To fix this, we split order_evaluations to two phases. The first phase, which runs before remove_shortcuts, skips shortcut expressions' components. So it won't lift out subexpressions that are evaluated conditionally. The shortcut expression itself is ordered, since it may have side effects. Then we run remove_shortcuts. At this point the subexpressions that should be evaluated before the shortcut expression are already lifted out. remove_shortcuts also runs the second phase of order_evaluations to order the components of shortcut expressions, which were skipped during the first phase. Reorder the code blocks of remove_shortcuts and order_evaluations, since remove_shortcuts now calls Order_eval. Fixes golang/go#26495. Change-Id: Ifabd6b69c7d23e711a810d6bd36dc815a8023ebc Reviewed-on: https://go-review.googlesource.com/125299 Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 3885007 commit 39d4d75

File tree

2 files changed

+224
-207
lines changed

2 files changed

+224
-207
lines changed

go/go.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,12 @@ go_parse_input_files(const char** filenames, unsigned int filename_count,
143143
// Export global identifiers as appropriate.
144144
::gogo->do_exports();
145145

146-
// Turn short-cut operators (&&, ||) into explicit if statements.
147-
::gogo->remove_shortcuts();
148-
149146
// Use temporary variables to force order of evaluation.
150147
::gogo->order_evaluations();
151148

149+
// Turn short-cut operators (&&, ||) into explicit if statements.
150+
::gogo->remove_shortcuts();
151+
152152
// Convert named types to backend representation.
153153
::gogo->convert_named_types();
154154

0 commit comments

Comments
 (0)