[Ada] improve warning on CE raised at run-time

Arnaud Charlet charlet@adacore.com
Thu Aug 30 08:57:00 GMT 2007


Tested on i686-linux, committed on trunk.

When the right hand side of an assignment to a NOT NULL variable is known
to be null, a warning is properly generated that this will raise CE at
run-time. However, this is not considered a possible modification of the
left hand side. This can result in a blow up at compile time (as in the
below example, where Is_True_Constant gets set for Local_4, and can also
result in false warnings about no assignments to an OUT parameter.

The following example should compile with the indicated warnings:

Compiling: n-main.adb
	
     1.
     2. procedure N.Main is
     3.    Local_1 : not null Ref;
           |
        >>> warning: (Ada 2005) null-excluding objects must
            be initialized, "Constraint_Error" will be
            raised at run time
        >>> warning: variable "Local_1" is never read and
            never assigned

     4.    Local_2 : not null Ref := null;
                                     |
        >>> warning: (Ada 2005) null not allowed in
            null-excluding objects, "Constraint_Error" will
            be raised at run time

     5.    Local_3 : Ref := null;
     6.    Local_4 : not null Ref :=
     7.                Int'Access;
     8. begin
     9.    Local_3 := null;
    10.    Local_4 := null;
                      |
        >>> warning: (Ada 2005) null not allowed in
            null-excluding objects, "Constraint_Error" will
            be raised at run time

    11.    P (null);
              |
        >>> warning: (Ada 2005) null not allowed in
            null-excluding formal, "Constraint_Error" will
            be raised at run time

    12. end N.Main;

2007-08-16  Robert Dewar  <dewar@adacore.com>

	* sem_ch5.adb (Analyze_Assignment): Make sure we still note update in
	exception case

-------------- next part --------------
Index: sem_ch5.adb
===================================================================
--- sem_ch5.adb	(revision 127427)
+++ sem_ch5.adb	(working copy)
@@ -574,22 +574,31 @@ package body Sem_Ch5 is
          Analyze_And_Resolve (Rhs, T1);
       end if;
 
-      --  Ada 2005 (AI-231)
+      --  Ada 2005 (AI-231): Assignment to not null variable
 
       if Ada_Version >= Ada_05
         and then Can_Never_Be_Null (T1)
         and then not Assignment_OK (Lhs)
       then
+         --  Case where we know the right hand side is null
+
          if Known_Null (Rhs) then
             Apply_Compile_Time_Constraint_Error
               (N   => Rhs,
                Msg => "(Ada 2005) null not allowed in null-excluding objects?",
                Reason => CE_Null_Not_Allowed);
+
+            --  We still mark this as a possible modification, that's necessary
+            --  to reset Is_True_Constant, and desirable for xref purposes.
+
+            Note_Possible_Modification (Lhs);
             return;
 
+         --  If we know the right hand side is non-null, then we convert to the
+         --  target type, since we don't need a run time check in that case.
+
          elsif not Can_Never_Be_Null (T2) then
-            Rewrite (Rhs,
-              Convert_To (T1, Relocate_Node (Rhs)));
+            Rewrite (Rhs, Convert_To (T1, Relocate_Node (Rhs)));
             Analyze_And_Resolve (Rhs, T1);
          end if;
       end if;


More information about the Gcc-patches mailing list