[ nice-Bugs-797569 ] byte var wraps - no runtime error

"SourceForge.net" <[email protected]>
Newsgroups gmane.comp.lang.nice.devel
Message-ID <[email protected]>
Bugs item #797569, was opened at 2003-08-30 00:24
Message generated for change (Comment added) made by arjanb
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=112788&aid=797569&group_id=12788

Category: None
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Isaac Gouy (igouy)
Assigned to: Nobody/Anonymous (nobody)
Summary: byte var wraps - no runtime error

Initial Comment:
>> Looks like you found a bug in the code generation. 

>> The incrementation should typecheck, but the value 

>> should wrap.  

> This is now fixed.



Using the development nice.jar



the loop never terminates, I think because i is a byte 

value which wraps rather than an int. 



I guess this error will go away with the "special rule" for 

type inference on integer types.



In these circumstances would it be better to raise an 

exception at runtime, rather than silently continue?





void main(String[] args){

    var count = 0;

    for (var i=0; i<10000; i++) count++;

    println(count);

 }

----------------------------------------------------------------------

Comment By: Arjan Boeijink (arjanb)
Date: 2003-09-01 22:18

Message:
Logged In: YES 
user_id=688815

The warning is implemented and will be in the next 

development version.

----------------------------------------------------------------------

Comment By: Daniel Bonniot (bonniot)
Date: 2003-09-01 12:02

Message:
Logged In: YES 
user_id=88952

I think it would be good to isue a compile-time warning when

comparing an expression with a literal value outside of its

range.



Arjan, would you look at that? It should be similar to the

"== null" warning.



----------------------------------------------------------------------

Comment By: Isaac Gouy (igouy)
Date: 2003-08-31 17:57

Message:
Logged In: YES 
user_id=536291

No compiler warning, fails with runtime error:

   #RTS: unhandled exception #1: expression out of bounds



MODULE MainTest;

   PROCEDURE Test (n: LONGINT);

   VAR count: LONGINT; i: SHORTINT; 

   BEGIN count := 0;  FOR i := 1 TO n DO INC(count); END;

   END Test;



BEGIN

   Test(128); 

END MainTest.

----------------------------------------------------------------------

Comment By: Daniel Bonniot (bonniot)
Date: 2003-08-31 10:40

Message:
Logged In: YES 
user_id=88952

I'm not sure about the syntax for functions, but something like:



void test(LONGINT n)

VAR count: LONGINT; i: SHORTINT; 

   BEGIN

      count := 0; 

      FOR i := 1 TO n DO INC(count); END;

   END



----------------------------------------------------------------------

Comment By: Isaac Gouy (igouy)
Date: 2003-08-31 03:40

Message:
Logged In: YES 
user_id=536291

>Concerning warnings: would oberon-2 warn if n was not a

>compile time constant ? 



give me some example code and I'll test it

----------------------------------------------------------------------

Comment By: Daniel Bonniot (bonniot)
Date: 2003-08-31 03:30

Message:
Logged In: YES 
user_id=88952

The change to 'var x = ...' is implemented.



Concerning warnings: would oberon-2 warn if n was not a

compile time constant ? (n := 127;  n := n + 1; can be

computed at compile time)

----------------------------------------------------------------------

Comment By: Isaac Gouy (igouy)
Date: 2003-08-30 16:06

Message:
Logged In: YES 
user_id=536291

> for (byte i=0; i<10000; i++) count++;

> is an infinite loop in Java (and in Nice)



Aaaaagh! Please pardon my screaming fit ;-)

Which shows that 'for (int i=0;...' is a very strong cliche - I 

doubt many people have tried to use anything but an int as 

the type of the loop var.



Still that doesn't seem like a good excuse for Java not 

providing a warning for the constant comparison.

   (Oberon-2)

   VAR count: LONGINT; i: SHORTINT

      count := 0;

      FOR i := 1 TO 10000 DO INC(count); END;



   * expression out of bounds

      FOR i := 1 TO $10000 DO INC(count); END;





And although this will compile with comparison on a variable:

   VAR count: LONGINT; i: SHORTINT; n: LONGINT;

   BEGIN

      count := 0; n := 127;

      FOR i := 1 TO n DO INC(count); END;



if we set the value to big we get a compile time warning:

   VAR count: LONGINT; i: SHORTINT; n: LONGINT;

   BEGIN

      count := 0; n := 127;  n := n + 1;

      FOR i := 1 TO n DO INC(count); END;



* rangeException will be raised here

      FOR i := 1 TO $n DO INC(count); END;



best wishes, Isaac

----------------------------------------------------------------------

Comment By: Daniel Bonniot (bonniot)
Date: 2003-08-30 11:02

Message:
Logged In: YES 
user_id=88952

Yes, this particular situation will be handled by the change

for 'var i = 0'.



Wrapping is part of the semantics of integer numbers. For

instance,

    for (byte i=0; i<10000; i++) count++;

is an infinite loop in Java (and in Nice).

I think it would be extremely inefficient to add checking

when compiling for the JVM. If you don't want wrapping,

either make sure you use a large enough type (int, long) or

BigInteger (which has operators +,-,*,... in Nice).



As arjan said, it would be possible to report a warning when

comparing a byte/short integer with a constant outside its

range (this is already done for comparison with null).



It's surely a good thing to get reports about what you think

is broken. It often means something needs to be done (change

of the compiler, documentation, ...). :-)



----------------------------------------------------------------------

Comment By: Isaac Gouy (igouy)
Date: 2003-08-30 02:03

Message:
Logged In: YES 
user_id=536291

>I don't see that as an urgency when



I hope my approach of reporting whatever seems like a bug is 

OK with you guys ;-) I don't believe I'm smart enough to 

figure out the other circumstances in which the problem 

might occur - so I just try to report what seems to happen.

----------------------------------------------------------------------

Comment By: Arjan Boeijink (arjanb)
Date: 2003-08-30 01:03

Message:
Logged In: YES 
user_id=688815

>the loop never terminates, I think because i is a byte 

>value which wraps rather than an int. 

indeed

>I guess this error will go away with the "special rule" for 

>type inference on integer types.

Yes

>In these circumstances would it be better to raise an 

>exception at runtime, rather than silently continue?

That's a possibility and/or it could be checked that a byte is 

compared with 10000. 

But I don't see that as an urgency when the inference for 

integer literals has changed because it will almost never 

happen in code that doesn't use inference.







----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=112788&aid=797569&group_id=12788


-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.