]> Cypherpunks.ru repositories - gostls13.git/commit
spec: specify evaluation order for binary logical operations
authorMatthew Dempsky <mdempsky@google.com>
Fri, 25 Aug 2023 23:24:54 +0000 (16:24 -0700)
committerMatthew Dempsky <mdempsky@google.com>
Thu, 14 Sep 2023 05:14:44 +0000 (05:14 +0000)
commit856cf23a8acfa14756a6e9b82ace76f5604262c9
treebf8c904c62eb307abdbddc8a8b4cd4b95220edfa
parent495830acd6976c8a2b39dd4aa4fdc105ad72de52
spec: specify evaluation order for binary logical operations

This CL clarifies the order of evaluation of the binary logical
operators, && and ||. The clarified semantics matches what cmd/compile
and x/tools/go/ssa already implement, and prohibit some optimizations
that are arguably allowed today but risk surprising users.

First, it specifies that the left operand is evaluated before the
right operand. This prohibits "(f() || true) && *p" from evaluating
"*p" before "f()".

Second, it specifies that binary logical operations are also ordered
lexically left-to-right with regard to function calls and receive
operations. This prohibits "h(*p || true || f(), g())" from evaluating
"*p" after "g()".

Finally, the "order of evaluation of [...] is not specified" wording
in the example is clarified to acknowledge that there are still some
other orderings that are implied lexically; e.g., x must be evaluated
and indexed before g(), and z now must be evaluated before h(). (Note:
Whether z is evaluated before or after f() remains unspecified, as
there's no lexical dependency.)

Change-Id: I9d316a7f1fbc83be663e116380a2cc7a4ace623d
Reviewed-on: https://go-review.googlesource.com/c/go/+/522938
Reviewed-by: Robert Griesemer <gri@google.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
doc/go_spec.html