[Ada] Spurious dimensionality errors on multitidimensional aggregates.

Arnaud Charlet charlet@adacore.com
Wed Apr 27 12:38:00 GMT 2016


This patch removes spurious dimensionality errors on aggregates for multi-
dimensional arrays of scalar types with dimension specifications.

Compiling dimbug.ads below must yield:

   dimbug.ads:8:33: dimensions mismatch in array aggregate
   dimbug.ads:8:33: expected dimension [L], found [M]

---
with system.Dim.Mks; use System.Dim.Mks;
package dimbug is

 test_array_2 : array (1 .. 3, 1 .. 3) of Length :=
      (others => (others => 3.0 * m));    --  OK

 Bad_Array : array (1 .. 3, 1 .. 3) of Length :=
      (others => (others => 3.0 * kg));   --  ERROR
end;

Tested on x86_64-pc-linux-gnu, committed on trunk

2016-04-27  Ed Schonberg  <schonberg@adacore.com>

	* sem_dim.ads, sem_dim.adb (Check_Expression_Dimensions): New
	procedure to compute the dimension vector of a scalar expression
	and compare it with the dimensions if its expected subtype. Used
	for the ultimate components of a multidimensional aggregate,
	whose components typically are themselves aggregates that are
	expanded separately. Previous to this patch, dimensionality
	checking on such aggregates generated spurious errors.
	* sem_aggr.adb (Resolve_Array_Aggregate): Use
	Check_Expression_Dimensions when needed.

-------------- next part --------------
Index: sem_aggr.adb
===================================================================
--- sem_aggr.adb	(revision 235481)
+++ sem_aggr.adb	(working copy)
@@ -6,7 +6,7 @@
 --                                                                          --
 --                                 B o d y                                  --
 --                                                                          --
---          Copyright (C) 1992-2015, Free Software Foundation, Inc.         --
+--          Copyright (C) 1992-2016, Free Software Foundation, Inc.         --
 --                                                                          --
 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
 -- terms of the  GNU General Public License as published  by the Free Soft- --
@@ -2052,6 +2052,13 @@
                      Set_Parent (Expr, Parent (Expression (Assoc)));
                      Analyze (Expr);
 
+                     --  Compute its dimensions now, rather than at the end
+                     --  of resolution, because in the case of multidimensional
+                     --  aggregates subsequent expansion may lead to spurious
+                     --  errors.
+
+                     Check_Expression_Dimensions (Expr, Component_Typ);
+
                      --  If the expression is a literal, propagate this info
                      --  to the expression in the association, to enable some
                      --  optimizations downstream.
Index: sem_dim.adb
===================================================================
--- sem_dim.adb	(revision 235481)
+++ sem_dim.adb	(working copy)
@@ -6,7 +6,7 @@
 --                                                                          --
 --                                 B o d y                                  --
 --                                                                          --
---          Copyright (C) 2011-2015, Free Software Foundation, Inc.         --
+--          Copyright (C) 2011-2016, Free Software Foundation, Inc.         --
 --                                                                          --
 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
 -- terms of the  GNU General Public License as published  by the Free Soft- --
@@ -1235,10 +1235,12 @@
          --  since it may not be decorated at this point. We also don't want to
          --  issue the same error message multiple times on the same expression
          --  (may happen when an aggregate is converted into a positional
-         --  aggregate).
+         --  aggregate). We also must verify that this is a scalar component,
+         --  and not a subaggregate of a multidimensional aggregate.
 
          if Comes_From_Source (Original_Node (Expr))
            and then Present (Etype (Expr))
+           and then Is_Numeric_Type (Etype (Expr))
            and then Dimensions_Of (Expr) /= Dims_Of_Comp_Typ
            and then Sloc (Comp) /= Sloc (Prev (Comp))
          then
@@ -2270,6 +2272,27 @@
       end case;
    end Analyze_Dimension_Unary_Op;
 
+   ---------------------------------
+   -- Check_Expression_Dimensions --
+   ---------------------------------
+
+   procedure Check_Expression_Dimensions
+      (Expr : Node_Id;
+       Typ  : Entity_Id)
+   is
+   begin
+      if Is_Floating_Point_Type (Etype (Expr)) then
+         Analyze_Dimension (Expr);
+
+         if Dimensions_Of (Expr) /= Dimensions_Of (Typ) then
+            Error_Msg_N ("dimensions mismatch in array aggregate", Expr);
+            Error_Msg_N
+              ("\expected dimension " & Dimensions_Msg_Of (Typ)
+               & ", found " & Dimensions_Msg_Of (Expr), Expr);
+         end if;
+      end if;
+   end Check_Expression_Dimensions;
+
    ---------------------
    -- Copy_Dimensions --
    ---------------------
Index: sem_dim.ads
===================================================================
--- sem_dim.ads	(revision 235481)
+++ sem_dim.ads	(working copy)
@@ -6,7 +6,7 @@
 --                                                                          --
 --                                 S p e c                                  --
 --                                                                          --
---          Copyright (C) 2011-2015, Free Software Foundation, Inc.         --
+--          Copyright (C) 2011-2016, Free Software Foundation, Inc.         --
 --                                                                          --
 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
 -- terms of the  GNU General Public License as published  by the Free Soft- --
@@ -164,6 +164,16 @@
    --  For sub spec N, issue a warning for each dimensioned formal with a
    --  literal default value in the list of formals Formals.
 
+   procedure Check_Expression_Dimensions
+      (Expr : Node_Id;
+       Typ  : Entity_Id);
+   --  Compute dimensions of a floating-point expression and compare them
+   --  with the dimensions of a the given type. Used to verify dimensions
+   --  of the components of a multidimensional array type, for which components
+   --  are typically themselves arrays. The resolution of such arrays delays
+   --  the resolution of the ultimate components to a separate phase, which
+   --  forces this separate dimension verification.
+
    procedure Copy_Dimensions (From, To : Node_Id);
    --  Copy dimension vector of node From to node To. Note that To must be a
    --  node that is allowed to contain a dimension (see OK_For_Dimension in


More information about the Gcc-patches mailing list