Recent

Author Topic: For loops with typed 'subset of integer' variables - bug?  (Read 3177 times)

RememberTP

  • New Member
  • *
  • Posts: 12
For loops with typed 'subset of integer' variables - bug?
« on: October 18, 2021, 06:53:28 pm »
I was wondering why one program wasn't working: the contents of a for loop was being executed despite the start value being higher than the end value. If I changed the loop variable to be an integer, rather than of a type that's a subset of integers, it worked.

The attached test program works with TurboPascal 5.5 in DOSBOX.

Output with TP:

Integers
(1,2) (1,3) (1,4)
(2,1) (2,3) (2,4)
(3,1) (3,2) (3,4)
(4,1) (4,2) (4,3)
Tinys
(1,2) (1,3) (1,4)
(2,1) (2,3) (2,4)
(3,1) (3,2) (3,4)
(4,1) (4,2) (4,3)

Output when compiled with fpc under Linux, even with -Mtp command line option:

Integers
(1,2) (1,3) (1,4)
(2,1) (2,3) (2,4)
(3,1) (3,2) (3,4)
(4,1) (4,2) (4,3)
Tinys
(1,1) (1,2) (1,3) (1,4)
(2,1) (2,3) (2,4)
(3,1) (3,2) (3,4)
(4,1) (4,2) (4,3) (4,5)

.. i.e. the 'for' loops with the typed variables - but not integers - are always run once.

RememberTP

  • New Member
  • *
  • Posts: 12
Re: For loops with typed 'subset of integer' variables - bug?
« Reply #1 on: October 18, 2021, 07:07:13 pm »
Oh, this is Free Pascal Compiler version 3.2.2+dfsg-1ubuntu2 [2021/08/20] for x86_64

https://lazarus-ccr.sourceforge.io/fpcdoc/ref/refsu58.html says

"In the case To is used, if the initial value is larger than the final value then Statement will never be executed" and it's that which is not happening for me with typed variables here.



« Last Edit: October 18, 2021, 07:09:49 pm by RememberTP »

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 1058
Re: For loops with typed 'subset of integer' variables - bug?
« Reply #2 on: October 18, 2021, 07:12:17 pm »
If you compile it with range checking enabled, the program will terminate with a range error. The behaviour of programs with range errors in them is undefined.

RememberTP

  • New Member
  • *
  • Posts: 12
Re: For loops with typed 'subset of integer' variables - bug?
« Reply #3 on: October 18, 2021, 07:35:43 pm »
Mmm, true, but it could be undefined in the same way that TP handles them if you specify -Mtp when compiling.

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 1058
Re: For loops with typed 'subset of integer' variables - bug?
« Reply #4 on: October 18, 2021, 08:34:30 pm »
That is impossible. The meaning of "undefined" literally is "it is impossible to define what the behaviour will be". "The same behaviour as TP" would not be "undefined".

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: For loops with typed 'subset of integer' variables - bug?
« Reply #5 on: October 18, 2021, 10:34:42 pm »
"The same behaviour as TP" would not be "undefined".

That made me giggle  ;)

Bart

RememberTP

  • New Member
  • *
  • Posts: 12
Re: For loops with typed 'subset of integer' variables - bug?
« Reply #6 on: October 20, 2021, 10:44:17 am »
I meant it as a joke - obviously defining behaviour means it's not undefined - but I still think that not following TP's behaviour when compiled in TP mode is a bug.

If the second loop is changed to be

Code: Pascal  [Select][+][-]
  1.     if n > 1 then
  2.       for m := 1 to n-1 do begin
  3.         Write ('(', n, ',', m, ') ') ;
  4.         end ;
  5.  
  6.     if n < NoOfLoops then
  7.       for m := n+1 to NoOfLoops do begin
  8.         Write ('(', n, ',', m, ') ') ;
  9.         end ;
  10.  

.. it works, even with range checking on, as m is not evaluated if n is too small / large.


Jonas Maebe

  • Hero Member
  • *****
  • Posts: 1058
Re: For loops with typed 'subset of integer' variables - bug?
« Reply #7 on: October 20, 2021, 12:43:50 pm »
I meant it as a joke - obviously defining behaviour means it's not undefined - but I still think that not following TP's behaviour when compiled in TP mode is a bug.
For correct programs, yes. For incorrect programs, no.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: For loops with typed 'subset of integer' variables - bug?
« Reply #8 on: October 21, 2021, 10:01:52 pm »
I meant it as a joke - obviously defining behaviour means it's not undefined - but I still think that not following TP's behaviour when compiled in TP mode is a bug.

If you enable range checks in TP ({$R+}) your example will fail there as well with an error (I tested with TP 6.0). This shows that your code is wrong in TP, too. That it does not fail without range checks enabled is precisely the undefined behavior that Jonas spoke off.

 

TinyPortal © 2005-2018