public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Marc Poulhiès" <poulhies@adacore.com>
To: gcc-patches@gcc.gnu.org
Cc: Eric Botcazou <ebotcazou@adacore.com>
Subject: [Ada] Fix crash on declaration of overaligned array with constraints
Date: Fri, 2 Sep 2022 09:53:28 +0200	[thread overview]
Message-ID: <20220902075328.GA1121076@poulhies-Precision-5550> (raw)

[-- Attachment #1: Type: text/plain, Size: 737 bytes --]

The semantic analyzer was setting the Is_Constr_Subt_For_UN_Aliased flag on
the actual subtype of the object, which is incorrect because the nominal
subtype is constrained.  This also adjusts a recent related change.

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

gcc/ada/

	* exp_util.adb (Expand_Subtype_From_Expr): Check for the presence
	of the Is_Constr_Subt_For_U_Nominal flag instead of the absence
	of the Is_Constr_Subt_For_UN_Aliased flag on the subtype of the
	expression of an object declaration before reusing this subtype.
	* sem_ch3.adb (Analyze_Object_Declaration): Do not incorrectly
	set the Is_Constr_Subt_For_UN_Aliased flag on the actual subtype
	of an array with definite nominal subtype.  Remove useless test.

[-- Attachment #2: patch.diff --]
[-- Type: text/x-diff, Size: 2567 bytes --]

diff --git a/gcc/ada/exp_util.adb b/gcc/ada/exp_util.adb
--- a/gcc/ada/exp_util.adb
+++ b/gcc/ada/exp_util.adb
@@ -5732,14 +5732,17 @@ package body Exp_Util is
       then
          if Is_Itype (Exp_Typ)
 
-           --  If Exp_Typ was created for a previous declaration whose nominal
-           --  subtype is unconstrained, and that declaration is aliased,
-           --  we need to generate a new subtype, because otherwise the
-           --  Is_Constr_Subt_For_U_Nominal flag will be set on the wrong
-           --  subtype, causing failure to detect non-statically-matching
-           --  subtypes on 'Access of the previously-declared object.
-
-           and then not Is_Constr_Subt_For_UN_Aliased (Exp_Typ)
+           --  When this is for an object declaration, the caller may want to
+           --  set Is_Constr_Subt_For_U_Nominal on the subtype, so we must make
+           --  sure that either the subtype has been built for the expression,
+           --  typically for an aggregate, or the flag is already set on it;
+           --  otherwise it could end up being set on the nominal constrained
+           --  subtype of an object and thus later cause the failure to detect
+           --  non-statically-matching subtypes on 'Access of this object.
+
+           and then (Nkind (N) /= N_Object_Declaration
+                      or else Nkind (Exp) = N_Aggregate
+                      or else Is_Constr_Subt_For_U_Nominal (Exp_Typ))
          then
             --  Within an initialization procedure, a selected component
             --  denotes a component of the enclosing record, and it appears as


diff --git a/gcc/ada/sem_ch3.adb b/gcc/ada/sem_ch3.adb
--- a/gcc/ada/sem_ch3.adb
+++ b/gcc/ada/sem_ch3.adb
@@ -4770,20 +4770,13 @@ package body Sem_Ch3 is
          if not Is_Entity_Name (Object_Definition (N)) then
             Act_T := Etype (E);
             Check_Compile_Time_Size (Act_T);
-
-            if Aliased_Present (N) then
-               Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
-            end if;
          end if;
 
          --  When the given object definition and the aggregate are specified
          --  independently, and their lengths might differ do a length check.
          --  This cannot happen if the aggregate is of the form (others =>...)
 
-         if not Is_Constrained (T) then
-            null;
-
-         elsif Nkind (E) = N_Raise_Constraint_Error then
+         if Nkind (E) = N_Raise_Constraint_Error then
 
             --  Aggregate is statically illegal. Place back in declaration
 



                 reply	other threads:[~2022-09-02  7:53 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220902075328.GA1121076@poulhies-Precision-5550 \
    --to=poulhies@adacore.com \
    --cc=ebotcazou@adacore.com \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).