Skip to content

Commit 48ab282

Browse files
author
ian
committed
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. Reviewed-on: https://go-review.googlesource.com/125299 git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@262908 138bc75d-0d04-0410-961f-82ee72b054a4
1 parent 0dbefa1 commit 48ab282

File tree

3 files changed

+225
-208
lines changed

3 files changed

+225
-208
lines changed

gcc/go/gofrontend/MERGE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
38850073f25f9de4f3daa33d799def3a33c942ab
1+
39d4d755db7d71b5e770ca435a8b1d1f08f53185
22

33
The first line of this file holds the git revision number of the last
44
merge done from the gofrontend repository.

gcc/go/gofrontend/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)