[Ada] Warn if old applied to constant
Arnaud Charlet
charlet@adacore.com
Wed May 28 13:30:00 GMT 2008
Applying Old to a constant object is dubious since a constant does not
change in value, so Old has no effect. This patch gives a warning for
this case, as shown in this test program:
1. procedure Useless_Old (X : Integer) is
2. Y : Integer;
3. Z : constant Integer := 3;
4.
5. procedure Inner is
6. begin
7. Y := X'Old;
|
>>> warning: attribute Old applied to constant has no effect
8. Y := Z'Old;
|
>>> warning: attribute Old applied to constant has no effect
9. end;
10. begin
11. null;
12. end;
Existing tests in gnat.dg have also been updated to take this new warning
into account.
Tested on i686-pc-linux-gnu, committed on trunk
2008-05-28 Robert Dewar <dewar@adacore.com>
* gnat_rm.texi: Add note on Old giving warning when applied to constant
* sem_attr.adb (Analyze_Attribute, case Old): Give warning if prefix is
a constant
-------------- next part --------------
Index: gnat_rm.texi
===================================================================
--- gnat_rm.texi (revision 136071)
+++ gnat_rm.texi (working copy)
@@ -5803,6 +5803,10 @@ package body Old_Pkg is
end Old_Pkg;
@end smallexample
+@noindent
+Note that it is allowed to apply 'Old to a constant entity, but this will
+result in a warning, since the old and new values will always be the same.
+
@node Passed_By_Reference
@unnumberedsec Passed_By_Reference
@cindex Parameters, when passed by reference
Index: sem_attr.adb
===================================================================
--- sem_attr.adb (revision 136071)
+++ sem_attr.adb (working copy)
@@ -3501,6 +3501,13 @@ package body Sem_Attr is
Error_Attr ("attribute % cannot apply to limited objects", P);
end if;
+ if Is_Entity_Name (P)
+ and then Is_Constant_Object (Entity (P))
+ then
+ Error_Msg_N
+ ("?attribute Old applied to constant has no effect", P);
+ end if;
+
-- Check that the expression does not refer to local entities
Check_Local : declare
More information about the Gcc-patches
mailing list