public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug fortran/47474] New: Wrong code with allocatable scalar, allocatable components as function result
@ 2011-01-26 13:43 burnus at gcc dot gnu.org
  2011-01-26 15:20 ` [Bug fortran/47474] " burnus at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: burnus at gcc dot gnu.org @ 2011-01-26 13:43 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47474

           Summary: Wrong code with allocatable scalar, allocatable
                    components as function result
           Product: gcc
           Version: 4.6.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: fortran
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: burnus@gcc.gnu.org
                CC: janus@gcc.gnu.org
            Blocks: 47455


Found as part of PR 47455. For the following program

   function find_y() result(res)
     type(tx), allocatable :: res
     ! do something sensible such as "allocate(res)"
   end function find_y

the dump looks as follows

  find_y ()
  {
    struct tx * res;

    res.i.data = 0B;  /* <<<< WRONG.  */
    res = 0B;
    /* some code.  */
    return res;
  }

If one does not use a RESULT variable but "find_y" as result variable, the dump
looks as follows:

  find_y ()
  {
     __result_find_y.i.data = 0B;  /* Note: 1.  */
     return __result_find_y;
  }

Note 1: Unless "find_y" is used (e.g. "allocate(find_y)") the function is
generated with an empty body.


For some reason, the example program below does not crash here with gfortran
4.5/4.6, but the dump is wrong and I am sure it can cause problems in certain
cases. The example of bug 47455 comment 4 does crash - and I believe(d) that it
is due to this bug.

program test
    type :: tx
        integer, dimension(:), allocatable :: i
    end type tx
    type(tx) :: x
    x = find_y()
    if (allocated(x%i)) call abort()
contains
    function find_y() result(res)
        type(tx), allocatable :: res
        allocate(res)
   end function find_y
end program test


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug fortran/47474] Wrong code with allocatable scalar, allocatable components as function result
  2011-01-26 13:43 [Bug fortran/47474] New: Wrong code with allocatable scalar, allocatable components as function result burnus at gcc dot gnu.org
@ 2011-01-26 15:20 ` burnus at gcc dot gnu.org
  2011-01-26 17:30 ` burnus at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: burnus at gcc dot gnu.org @ 2011-01-26 15:20 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47474

--- Comment #1 from Tobias Burnus <burnus at gcc dot gnu.org> 2011-01-26 15:04:53 UTC ---
Patch. The order was wrong; additionally, if there is an allocatable RESULT
variable, it seems to get zero initialized via sym->value or similarly. Thus
the sym->result == result check prevents that two "res = 0B;" lines get added.


--- a/gcc/fortran/trans-decl.c
+++ b/gcc/fortran/trans-decl.c
@@ -4602,16 +4716,18 @@ gfc_generate_function_code (gfc_namespace * ns)
            && sym->attr.function
            && !sym->attr.pointer)
        {
-         if (sym->ts.type == BT_DERIVED
-             && sym->ts.u.derived->attr.alloc_comp)
+         if (sym->attr.allocatable && sym->attr.dimension == 0
+             && sym->result == sym)
+           gfc_add_modify (&init, result, fold_convert (TREE_TYPE (result),
+                                                        null_pointer_node));
+         else if (sym->ts.type == BT_DERIVED
+             && sym->ts.u.derived->attr.alloc_comp
+             && !sym->attr.allocatable)
            {
              rank = sym->as ? sym->as->rank : 0;
              tmp = gfc_nullify_alloc_comp (sym->ts.u.derived, result, rank);
              gfc_add_expr_to_block (&init, tmp);
            }
-         else if (sym->attr.allocatable && sym->attr.dimension == 0)
-           gfc_add_modify (&init, result, fold_convert (TREE_TYPE (result),
-                                                        null_pointer_node));
        }

       if (result == NULL_TREE)


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug fortran/47474] Wrong code with allocatable scalar, allocatable components as function result
  2011-01-26 13:43 [Bug fortran/47474] New: Wrong code with allocatable scalar, allocatable components as function result burnus at gcc dot gnu.org
  2011-01-26 15:20 ` [Bug fortran/47474] " burnus at gcc dot gnu.org
@ 2011-01-26 17:30 ` burnus at gcc dot gnu.org
  2011-01-27 23:17 ` burnus at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: burnus at gcc dot gnu.org @ 2011-01-26 17:30 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47474

--- Comment #2 from Tobias Burnus <burnus at gcc dot gnu.org> 2011-01-26 16:58:28 UTC ---
Submitted patch: http://gcc.gnu.org/ml/fortran/2011-01/msg00235.html


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug fortran/47474] Wrong code with allocatable scalar, allocatable components as function result
  2011-01-26 13:43 [Bug fortran/47474] New: Wrong code with allocatable scalar, allocatable components as function result burnus at gcc dot gnu.org
  2011-01-26 15:20 ` [Bug fortran/47474] " burnus at gcc dot gnu.org
  2011-01-26 17:30 ` burnus at gcc dot gnu.org
@ 2011-01-27 23:17 ` burnus at gcc dot gnu.org
  2011-01-27 23:59 ` burnus at gcc dot gnu.org
  2011-02-02 18:22 ` dnovillo at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: burnus at gcc dot gnu.org @ 2011-01-27 23:17 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47474

--- Comment #3 from Tobias Burnus <burnus at gcc dot gnu.org> 2011-01-27 22:47:14 UTC ---
Author: burnus
Date: Thu Jan 27 22:47:08 2011
New Revision: 169340

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=169340
Log:
2011-01-27  Tobias Burnus  <burnus@net-b.de>

        PR fortran/47474
        * trans-decl.c (gfc_generate_function_code): Fix init
        of allocatable result variable with allocatable components.


Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/trans-decl.c


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug fortran/47474] Wrong code with allocatable scalar, allocatable components as function result
  2011-01-26 13:43 [Bug fortran/47474] New: Wrong code with allocatable scalar, allocatable components as function result burnus at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2011-01-27 23:17 ` burnus at gcc dot gnu.org
@ 2011-01-27 23:59 ` burnus at gcc dot gnu.org
  2011-02-02 18:22 ` dnovillo at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: burnus at gcc dot gnu.org @ 2011-01-27 23:59 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47474

Tobias Burnus <burnus at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |FIXED

--- Comment #4 from Tobias Burnus <burnus at gcc dot gnu.org> 2011-01-27 22:50:25 UTC ---
FIXED on the trunk (4.6)


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug fortran/47474] Wrong code with allocatable scalar, allocatable components as function result
  2011-01-26 13:43 [Bug fortran/47474] New: Wrong code with allocatable scalar, allocatable components as function result burnus at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2011-01-27 23:59 ` burnus at gcc dot gnu.org
@ 2011-02-02 18:22 ` dnovillo at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: dnovillo at gcc dot gnu.org @ 2011-02-02 18:22 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47474

--- Comment #5 from Diego Novillo <dnovillo at gcc dot gnu.org> 2011-02-02 17:58:22 UTC ---
Author: dnovillo
Date: Wed Feb  2 17:58:18 2011
New Revision: 169657

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=169657
Log:
2011-01-27  Tobias Burnus  <burnus@net-b.de>

        PR fortran/47474
        * trans-decl.c (gfc_generate_function_code): Fix init
        of allocatable result variable with allocatable components.

Modified:
    branches/google/integration/gcc/fortran/ChangeLog
    branches/google/integration/gcc/fortran/trans-decl.c


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2011-02-02 18:22 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-01-26 13:43 [Bug fortran/47474] New: Wrong code with allocatable scalar, allocatable components as function result burnus at gcc dot gnu.org
2011-01-26 15:20 ` [Bug fortran/47474] " burnus at gcc dot gnu.org
2011-01-26 17:30 ` burnus at gcc dot gnu.org
2011-01-27 23:17 ` burnus at gcc dot gnu.org
2011-01-27 23:59 ` burnus at gcc dot gnu.org
2011-02-02 18:22 ` dnovillo at gcc dot gnu.org

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).