Jonas Maebe wrote:
On 26 Apr 2013, at 22:22, Waldek Hebisch wrote:
Hi, =20 Can anyone here offer some insights regarding the standard =
conformance of the test program submitted at = http://bugs.freepascal.org/view.php?id=3D24318 ? (range.p)
=20 Both FPC and GPC cause the program to abort with a range check error, =
while the submitter is of the opinion that the for-loop should merely = short-circuit and never executed instead.
=20
=20 AFAICS he is right. ISO-10206 clause 6.9.3.9.2 says: =20 : The initial-value and the final-value of a sequence-iteration : of an iteration-clause of a for-statement shall be of a type : compatible with the type of control-variable of the for-statement. : The initial-value and the final-value shall be : assignment-compatible with the type possesed by the : control-variable if the statement of the for-statement : is executed. =20 Compatible type for integer subrange means that any integer value is OK. Assignment-compatible means that values must be in range, but is only required when loop body is actually executed.
The for-statement includes the initial-value assignment to the = control-variable and the comparison of the control-variable with the = final-value. That part of the for-statement is executed any time the = control flow of the program reaches the for-loop. That's why I believe = that the assignment-compatibility rule applies.
The 'statement of the for-statement' is standarese for loop body, so the quoted part is quite clear: assignment-compatibility only applies when body is executed.
BTW: Standard also includes "equivalent" code and before this code states that the clause I cited takes precedence over what you would normally expect from assignment-compatibility.
BTW2: In Pascal expressions are normally computed using "full" types and range restrictions only play role for assignment. The "equivalent" code will perform assignment to control variable only when body will be execute.
BTW3: Given that standard usually leaves many thing up to general rules it is clear that the intent of wording was to allow examples like the one reported.
BTW4: The reporter wrote this clearly in the comment to the FPC bug report...