Project

General

Profile

Bug #107

Methods of POINTER TO RECORD with CONST receiver still allowed to change receiver's fields

Added by Anton Dmitriev about 1 year ago. Updated 13 days ago.

Status:
Feedback
Priority:
Normal
Assignee:
-
Category:
-

Description

I was told (but have not confirmed) that receiver fields should be read-only with the following declarations, but they are not:

MODULE M;
TYPE Reader = POINTER TO RECORD r: Files.Reader; eof: BOOLEAN END;
PROCEDURE (r: Reader) ReadByte (VAR b: UNSIGNED8); (* receiver is CONST by default, right? *)
VAR ch: CHAR;
BEGIN r.r.Char(ch); r.eof := r.r.res # Streams.Ok; IF ~r.eof THEN b := SYSTEM.VAL END
END ReadByte;
END M.

Compiles without errors. Should it?

History

#1 Updated by Felix Friedrich 13 days ago

  • Status changed from New to Feedback

Reader is a pointer and the receiver is a pointer as well. The constness of the pointer is not transitive: the record can be changed via the pointer.
The following example shows that constness works with records (where a receiver is, by default, const)

MODULE M;
IMPORT Files, Streams;
TYPE Reader = RECORD r: Files.Reader; eof: BOOLEAN END;
PROCEDURE (r: Reader) ReadByte (VAR b: UNSIGNED8); (* receiver is CONST by default, right? *)
VAR ch: CHAR;
BEGIN r.r.Char(ch); r.eof := r.r.res # Streams.Ok; IF ~r.eof THEN b := 1 END
END ReadByte;
END M.

Also available in: Atom PDF