[comp.lang.pascal] TP Scope violation bug

roth@oasys.dt.navy.mil (Pete Roth) (04/22/91)

{
        The enclosed program shows that Turbo Pascal 6.0 violates
the scope rules of Pascal by allowing the compilation and incorrect
execution of the program.  Giving wrong answers, of course.

        It looks to me like the table of function identifiers is not
searched when variable identifiers are "compiled."

}

PROGRAM aBug ;

(* demonstrates the violation of scope rules *)

FUNCTION x : LONGINT ;
   VAR x : REAL ;
   BEGIN
      x := Trunc( 1.0 )
   END ;

BEGIN
   WriteLn( 'output: ', x )
END.
- - - - - - - - - - - - - - - - - - - - - - - - - -
Peter N Roth      roth@oasys.dt.navy.mil
Objects in this office are closer than they appear.

milne@ics.uci.edu (Alastair Milne) (04/23/91)

In <7232@oasys.dt.navy.mil> roth@oasys.dt.navy.mil (Pete Roth) writes:

>{
>        The enclosed program shows that Turbo Pascal 6.0 violates
>the scope rules of Pascal by allowing the compilation and incorrect
>execution of the program.  Giving wrong answers, of course.
>        It looks to me like the table of function identifiers is not
>searched when variable identifiers are "compiled."
>}

>PROGRAM aBug ;
>(* demonstrates the violation of scope rules *)
>FUNCTION x : LONGINT ;
>   VAR x : REAL ;
        ^^^^^^^^^^^^^ This is a more local occurrence than the function's
		      name, which belongs to the enclosing scope.
>   BEGIN
>      x := Trunc( 1.0 )    This assigns to the most local version of 'x',
			    which is of course the local variable.
>   END ;

>BEGIN
>   WriteLn( 'output: ', x )
>END.

    This works exactly as I would expect:
    the variable x gets the assignment, the function remains unassigned,
    and the writeln reports an uninitialised value.

    No bug that I can see.


    Alastair Milne