public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Pierre-Marie de Rodat <derodat@adacore.com>
To: gcc-patches@gcc.gnu.org
Cc: Eric Botcazou <ebotcazou@adacore.com>
Subject: [Ada] Fix dispatching call to primitive function with controlling tagged result
Date: Mon, 4 Jul 2022 07:50:28 +0000	[thread overview]
Message-ID: <20220704075028.GA99387@adacore.com> (raw)

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

When a dispatching call is made to a primitive function with a controlling
tagged result, the call is dispatching on result and thus must return the
class-wide type of the tagged type to accommodate all possible results.

This was ensured by Expand_Dispatching_Call only in the common case where
the result type is the type of the controlling argument, which does not
cover the case of a primitive function inherited from an ancestor type.

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

gcc/ada/

	* exp_disp.adb (Expand_Dispatching_Call): Fix detection of calls
	that are dispatching on tagged result.

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

diff --git a/gcc/ada/exp_disp.adb b/gcc/ada/exp_disp.adb
--- a/gcc/ada/exp_disp.adb
+++ b/gcc/ada/exp_disp.adb
@@ -896,8 +896,14 @@ package body Exp_Disp is
       Copy_Strub_Mode (Subp_Typ, Subp);
       Set_Convention  (Subp_Typ, Convention (Subp));
 
-      if Etype (Subp) = Typ then
-         Set_Etype          (Subp_Typ, CW_Typ);
+      --  If this is a function and it has a controlling tagged result, then
+      --  the call is dispatching on result and returns the class-wide type.
+
+      if Ekind (Subp) = E_Function
+        and then Has_Controlling_Result (Subp)
+        and then Is_Tagged_Type (Etype (Subp))
+      then
+         Set_Etype          (Subp_Typ, Class_Wide_Type (Etype (Subp)));
          Set_Returns_By_Ref (Subp_Typ, True);
       else
          Set_Etype          (Subp_Typ, Etype (Subp));



                 reply	other threads:[~2022-07-04  7:50 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=20220704075028.GA99387@adacore.com \
    --to=derodat@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).