Bug 54992 - [4.8 Regression] [OOP] Wrong offset in the array offset calculation when using nonclass%class(index)%nonclass
Summary: [4.8 Regression] [OOP] Wrong offset in the array offset calculation when usin...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.8.0
: P4 normal
Target Milestone: 4.8.0
Assignee: Paul Thomas
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2012-10-19 20:33 UTC by Tobias Burnus
Modified: 2013-01-07 19:10 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2012-10-19 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tobias Burnus 2012-10-19 20:33:48 UTC
As reported by Andrew Benson in the thread starting at http://gcc.gnu.org/ml/fortran/2012-10/msg00087.html

The problem is that gfortran generates the wrong code for:

    targetNode%cBh(2)%hostNode => targetNode

where "cBh" is a polymorphic array. The offset is calculated as

  ((base_type *)targetNode->_data->cBh->_data)[ index ].host


Instead of the proper:

  targetNode->_data->cBh->_data.data
               + (index * targetNode->_data->cBh->_vptr_size)


Test case:  http://gcc.gnu.org/ml/fortran/2012-10/msg00100.html

Analysis:   http://gcc.gnu.org/ml/fortran/2012-10/msg00101.html
Comment 1 janus 2012-10-19 21:23:36 UTC
(In reply to comment #0)
> Test case:  http://gcc.gnu.org/ml/fortran/2012-10/msg00100.html

Note: While this test case fails with trunk, it seems to work with 4.7.
Comment 2 Dominique d'Humieres 2012-10-19 21:37:08 UTC
Revision 187190 is OK, revision 187198 is not -> likely r187192.
Comment 3 janus 2012-10-25 11:36:00 UTC
Related (or duplicate): PR 55057.
Comment 4 Paul Thomas 2012-12-26 17:56:31 UTC
This is another result of the underlying bug unearthed in PR54990.

Index: gcc/fortran/trans-array.c
===================================================================
*** gcc/fortran/trans-array.c	(revision 194721)
--- gcc/fortran/trans-array.c	(working copy)
*************** static tree
*** 3099,3112 ****
  build_array_ref (tree desc, tree offset, tree decl)
  {
    tree tmp;
  
    /* Class array references need special treatment because the assigned
       type size needs to be used to point to the element.  */ 
    if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc))
  	&& TREE_CODE (desc) == COMPONENT_REF
! 	&& GFC_CLASS_TYPE_P (TREE_TYPE (TREE_OPERAND (desc, 0))))
      {
!       tree type = gfc_get_element_type (TREE_TYPE (desc));
        tmp = TREE_OPERAND (desc, 0);
        tmp = gfc_get_class_array_ref (offset, tmp);
        tmp = fold_convert (build_pointer_type (type), tmp);
--- 3099,3118 ----
  build_array_ref (tree desc, tree offset, tree decl)
  {
    tree tmp;
+   tree type;
+ 
+   type = TREE_TYPE (TREE_OPERAND (desc, 0));
+   if (TYPE_CANONICAL (type)
+       && GFC_CLASS_TYPE_P (TYPE_CANONICAL (type)))
+     type = TYPE_CANONICAL (type);
  
    /* Class array references need special treatment because the assigned
       type size needs to be used to point to the element.  */ 
    if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc))
  	&& TREE_CODE (desc) == COMPONENT_REF
! 	&& GFC_CLASS_TYPE_P (type))
      {
!       type = gfc_get_element_type (TREE_TYPE (desc));
        tmp = TREE_OPERAND (desc, 0);
        tmp = gfc_get_class_array_ref (offset, tmp);
        tmp = fold_convert (build_pointer_type (type), tmp);

fixes it.  As remarked in PR54990, I do not know why this regression has occurred.  Evidently, GFC_CLASS_TYPE_P is not being transferred from the canonical type, whereas this was happening before the regression (or a new type was not being generated?).  I will investigate more.

I have taken this PR.

Cheers

Paul
Comment 5 Paul Thomas 2013-01-07 09:05:10 UTC
Fixed by http://gcc.gnu.org/viewcvs?view=revision&revision=194953

It did not register here because I screwed up on the ChangeLog Format (I really am rusty  :-) ).  I'll fix this tonight.

Thanks for the report.

Paul
Comment 6 Paul Thomas 2013-01-07 19:10:40 UTC
Author: pault
Date: Mon Jan  7 19:10:32 2013
New Revision: 194994

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=194994
Log:
2013-01-07  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/53876
	PR fortran/54990
	PR fortran/54992
	* ChangeLog: Correct format error in revision 194953

2013-01-07  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/53876
	PR fortran/54990
	PR fortran/54992
	* ChangeLog: Correct format error in revision 194953

Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/testsuite/ChangeLog