Discussion:
for-loops and subrange types
Jonas Maebe
2013-04-26 19:01:34 UTC
Permalink
Hi,

Can anyone here offer some insights regarding the standard conformance of the test program submitted at http://bugs.freepascal.org/view.php?id=24318 ? (range.p)

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.


Jonas
Phil Nelson
2013-04-26 19:33:52 UTC
Permalink
Post by Jonas Maebe
Can anyone here offer some insights regarding the standard conformance of the test program submitted at http://bugs.freepascal.org/view.php?id=24318 ? (range.p)
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.
This program is clearly how I expect pascal to work. If the
initial value is already past the final value, it should just
do nothing.

I can't speak directly to the standards, but if the require this
to be a range error, the standards clearly broke many of my
pascal programs.

I also verified that a very old version of gcp (2.95.3) did not
cause a range error on this kind of construct.

--Phil
--
Phil Nelson (phil at cs.wwu.edu) http://www.cs.wwu.edu/nelson
NetBSD: http://www.NetBSD.org Coda: http://www.coda.cs.cmu.edu
Maurice Lombardi
2013-04-27 10:50:40 UTC
Permalink
Post by Phil Nelson
I also verified that a very old version of gcp (2.95.3) did not
cause a range error on this kind of construct.
old versions of *GPC* had not implemented range check. What was the
*GPC* (not *GCC*) version of that compiler ?

Maurice
--
Maurice Lombardi
Laboratoire Interdisciplinaire de Physique,
(ex Spectrometrie Physique)
Universite Joseph Fourier de Grenoble, BP87
38402 Saint Martin d'Heres Cedex FRANCE
Tel: 33 (0)4 76 51 47 51
Fax: 33 (0)4 76 63 54 95
mailto:Maurice.Lombardi at ujf-grenoble.fr
Jonas Maebe
2013-04-26 20:28:00 UTC
Permalink
Post by Jonas Maebe
Hi,
Can anyone here offer some insights regarding the standard conformance of the test program submitted at http://bugs.freepascal.org/view.php?id=24318 ? (range.p)
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.
: 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.
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.


Jonas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.g-n-u.de/pipermail/gpc/attachments/20130426/36e1806b/attachment.html>
Waldek Hebisch
2013-04-26 20:22:58 UTC
Permalink
Post by Jonas Maebe
Hi,
Can anyone here offer some insights regarding the standard conformance of the test program submitted at http://bugs.freepascal.org/view.php?id=24318 ? (range.p)
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.
AFAICS he is right. ISO-10206 clause 6.9.3.9.2 says:

: 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.

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.
--
Waldek Hebisch
hebisch at math.uni.wroc.pl
Contestcen
2013-04-26 22:03:32 UTC
Permalink
This compatibility requirement seems to imply that the control variable
need not be an integer type. Does this mean that

Var letter: char;

for letter := 'A' to 'Z' do

is legitimate, or that the definers of the language expected that might
eventually be allowed? Or does it merely mean that the magnitudes must match
the range, so that

Var num: smallint;

for num := 20000 to 40000 do

is illegal?



In a message dated 4/26/2013 4:28:41 P.M. Eastern Daylight Time,
jonas.maebe at elis.ugent.be writes:

: 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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.g-n-u.de/pipermail/gpc/attachments/20130426/99796b7a/attachment.html>
Jonas Maebe
2013-04-27 06:37:35 UTC
Permalink
This compatibility requirement seems to imply that the control variable need not be an integer type. Does this mean that
Var letter: char;
for letter := 'A' to 'Z' do
is legitimate, or that the definers of the language expected that might eventually be allowed?
It certainly is valid. The same goes for boolean and enumeration types.


Jonas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.g-n-u.de/pipermail/gpc/attachments/20130427/aafc9177/attachment.html>
Waldek Hebisch
2013-04-27 18:47:03 UTC
Permalink
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
=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...
--
Waldek Hebisch
hebisch at math.uni.wroc.pl
Maurice Lombardi
2013-04-28 17:38:36 UTC
Permalink
Post by Waldek Hebisch
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.
This is how gpc should work, but not how it actually works with current
gpc version (20070904 with either 3.4.5 or 4.3.5 gcc backends).
To check a suggestion of Florian Kaempfl on the fpc bug list about
signed/unsigned underlying type, I changed ttlow to -1 to be sure that
the underlying "full" type is integer, not cardinal, and put ttop - 2
for the upper limit of the loop, which is out of bounds for assignment,
but not for the underlying integer type, giving
------------------------------------------------------------------
program range ( output );

const ttlow = -1; tthigh = 800;

type ttx = ttlow .. tthigh;

var ttop : ttx;

procedure p ( low : ttx );
var high : ttx;
begin
for high := low to ttop - 2 do
writeln( high, ' ', low, ' ', ttop, ' ', ttop - 1 );
end;

begin
ttop := 0;
p( 1 );
end.
--------------------------------------------------------------------
I obtain the following out of bounds message:

MinGW: C:\Lombardi\mingw\gpc\bugRange>range
range: value out of range (error #300 at 40131d)

So the assignment of high:=ttop-2 has been done, even if the loop is not
executed.

Maurice
--
Maurice Lombardi
Laboratoire Interdisciplinaire de Physique,
(ex Spectrometrie Physique)
Universite Joseph Fourier de Grenoble, BP87
38402 Saint Martin d'Heres Cedex FRANCE
Tel: 33 (0)4 76 51 47 51
Fax: 33 (0)4 76 63 54 95
mailto:Maurice.Lombardi at ujf-grenoble.fr
Jonas Maebe
2013-04-29 07:07:23 UTC
Permalink
On 27 Apr 2013, at 20:47, Waldek Hebisch wrote:

[snip]
Post by Waldek Hebisch
BTW4: The reporter wrote this clearly in the comment to the
FPC bug report...
The poster was obviously heavily invested in getting his code accepted by FPC. Given that GPC is known for its standards compliance and had the same behaviour as FPC, and given the fact that as a non-standards-expert I did not know that for-statement always solely refers to "loop body", I thought the standard was possibly in part self-contradicting or ambiguous (it wouldn't be the first such standard). That's why I asked here rather than just closing the bug report and be done with it, because I knew there are people here who know way more about the Pascal standard than I do.


Jonas
Jonas Maebe
2013-04-29 07:19:58 UTC
Permalink
Post by Jonas Maebe
[snip]
Post by Waldek Hebisch
BTW4: The reporter wrote this clearly in the comment to the
FPC bug report...
The poster was obviously heavily invested in getting his code accepted by FPC. Given that GPC is known for its standards compliance and had the same behaviour as FPC, and given the fact that as a non-standards-expert I did not know that for-statement always solely refers to "loop body"
Or rather "statement of the for-statement". I thought it was just awkward standardese wording .


Jonas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.g-n-u.de/pipermail/gpc/attachments/20130429/f453651f/attachment.html>
Waldek Hebisch
2013-04-28 18:06:41 UTC
Permalink
Post by Maurice Lombardi
Post by Waldek Hebisch
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.
This is how gpc should work, but not how it actually works with current
gpc version (20070904 with either 3.4.5 or 4.3.5 gcc backends).
Yes, it is a gpc bug.
--
Waldek Hebisch
hebisch at math.uni.wroc.pl
Adriaan van Os
2013-04-29 06:24:36 UTC
Permalink
Post by Jonas Maebe
Hi,
Can anyone here offer some insights regarding the standard conformance of the test program submitted at http://bugs.freepascal.org/view.php?id=24318 ? (range.p)
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.
I second with what Waldek Hebisch and submitter have said and argued in detail.

Regards,

Adriaan van Os
Continue reading on narkive:
Loading...