mirror of https://github.com/golang/go.git
go spec: constant divisors must not be zero
Both gc and gccgo always checked this for constant expressions but the spec only mentions run-time exceptions. This CL also requires that constant divisors must not be zero in non-constant integer expressions: This is consistent with the spirit of the most recent changes and it is consistent with constant expressions. We don't want to specify the effect for non-integer expressions (f/0.0 where f is a float or complex number) because there the result f/g is not further specified if a non-constant g is 0. R=r, rsc, iant, ken, andybalholm, iant CC=golang-dev https://golang.org/cl/6710045
This commit is contained in:
parent
3bde00033b
commit
ddddd39fc8
|
|
@ -3001,7 +3001,8 @@ int64 -9223372036854775808
|
|||
</pre>
|
||||
|
||||
<p>
|
||||
If the divisor is zero, a <a href="#Run_time_panics">run-time panic</a> occurs.
|
||||
If the divisor is a <a href="#Constants">constant</a>, it must not be zero.
|
||||
If the divisor is zero at run time, a <a href="#Run_time_panics">run-time panic</a> occurs.
|
||||
If the dividend is positive and the divisor is a constant power of 2,
|
||||
the division may be replaced by a right shift, and computing the remainder may
|
||||
be replaced by a bitwise AND operation:
|
||||
|
|
@ -3692,6 +3693,14 @@ const Huge = 1 << 100
|
|||
const Four int8 = Huge >> 98
|
||||
</pre>
|
||||
|
||||
<p>
|
||||
The divisor of a constant division or remainder operation must not be zero:
|
||||
</p>
|
||||
|
||||
<pre>
|
||||
3.14 / 0.0 // illegal: division by zero
|
||||
</pre>
|
||||
|
||||
<p>
|
||||
The values of <i>typed</i> constants must always be accurately representable as values
|
||||
of the constant type. The following constant expressions are illegal:
|
||||
|
|
@ -4759,8 +4768,6 @@ the function completes.
|
|||
(See also the section on <a href="#Handling_panics">handling panics</a>.)
|
||||
</p>
|
||||
|
||||
</p>
|
||||
|
||||
<pre>
|
||||
lock(l)
|
||||
defer unlock(l) // unlocking happens before surrounding function returns
|
||||
|
|
|
|||
Loading…
Reference in New Issue