Bug 49562

Summary: [4.6/4.7 Regression] [OOP] assigning value to type-bound function
Product: gcc Reporter: Hans-Werner Boschmann <boschmann>
Component: fortranAssignee: janus
Status: RESOLVED FIXED    
Severity: normal CC: burnus, janus
Priority: P3 Keywords: ice-on-valid-code
Version: 4.7.0   
Target Milestone: 4.6.2   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2011-06-28 09:43:24

Description Hans-Werner Boschmann 2011-06-28 08:26:11 UTC
The code:

module ice
  type::ice_type
   contains
     procedure::ice_func
  end type ice_type
contains
  pure integer function ice_func(this)
    class(ice_type),intent(in)::this
    ice_func=1
  end function ice_func
  subroutine ice_sub(a)
    class(ice_type)::a
    a%ice_func()=1           ! This is the invalid line
  end subroutine ice_sub
end module ice

The marked line is obviously not allowed. When I replace class(ice_type)::a by type(ice_type)::a then get a proper error message, but the class statement results in an internal compiler error.

My gfortran version is 4.7.0 20110620
Comment 1 janus 2011-06-28 09:43:24 UTC
In fact this is a regression: With 4.5 one gets the correct error message.
Comment 2 janus 2011-06-28 11:05:08 UTC
The fix is pretty trivial:


Index: gcc/fortran/expr.c
===================================================================
--- gcc/fortran/expr.c	(revision 175562)
+++ gcc/fortran/expr.c	(working copy)
@@ -4395,7 +4395,7 @@ gfc_check_vardef_context (gfc_expr* e, bool pointe
     }
 
   if (!pointer && e->expr_type == EXPR_FUNCTION
-      && sym->result->attr.pointer)
+      && sym->result && sym->result->attr.pointer)
     {
       if (!(gfc_option.allow_std & GFC_STD_F2008))
 	{


With this one gets:

    a%ice_func()=1           ! This is the invalid line
    1
Error: Non-variable expression in variable definition context (assignment) at (1)
Comment 3 janus 2011-06-28 11:06:29 UTC
Also: Mine. [Will commit as obvious after regtesting.]
Comment 4 janus 2011-06-28 11:18:47 UTC
For the record: The regression is probably due to r165749:

http://gcc.gnu.org/viewcvs?view=revision&revision=165749
Comment 5 janus 2011-06-28 12:52:58 UTC
Note: You can get the same ICE on *valid* code, if you give the return value of 'ice_func' the POINTER attribute (allowed by F08):


module ice
  type::ice_type
   contains
     procedure::ice_func
  end type ice_type
contains
  pure function ice_func(this)
    integer, pointer :: ice_func
    class(ice_type),intent(in)::this
    ice_func=1
  end function ice_func
  subroutine ice_sub(a)
    class(ice_type)::a
    a%ice_func()=1           ! This is valid now
  end subroutine ice_sub
end module ice
 

Unfortunately this test case is rejected with the patch in comment #2.
Comment 6 janus 2011-06-28 12:59:20 UTC
(In reply to comment #5)
> Unfortunately this test case is rejected with the patch in comment #2.

However, it is accepted with this improved patch:


Index: gcc/fortran/expr.c
===================================================================
--- gcc/fortran/expr.c	(revision 175580)
+++ gcc/fortran/expr.c	(working copy)
@@ -4394,8 +4394,8 @@ gfc_check_vardef_context (gfc_expr* e, bool pointe
       sym = e->value.function.esym ? e->value.function.esym : e->symtree->n.sym;
     }
 
-  if (!pointer && e->expr_type == EXPR_FUNCTION
-      && sym->result->attr.pointer)
+  attr = gfc_expr_attr (e);
+  if (!pointer && e->expr_type == EXPR_FUNCTION && attr.pointer)
     {
       if (!(gfc_option.allow_std & GFC_STD_F2008))
 	{
@@ -4432,7 +4432,6 @@ gfc_check_vardef_context (gfc_expr* e, bool pointe
 
   /* Find out whether the expr is a pointer; this also means following
      component references to the last one.  */
-  attr = gfc_expr_attr (e);
   is_pointer = (attr.pointer || attr.proc_pointer);
   if (pointer && !is_pointer)
     {
Comment 7 janus 2011-07-02 11:08:45 UTC
Author: janus
Date: Sat Jul  2 11:08:41 2011
New Revision: 175779

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=175779
Log:
2011-07-02  Janus Weil  <janus@gcc.gnu.org>

	PR fortran/49562
	* expr.c (gfc_check_vardef_context): Handle type-bound procedures.


2011-07-02  Janus Weil  <janus@gcc.gnu.org>

	PR fortran/49562
	* gfortran.dg/typebound_proc_23.f90: New.

Added:
    trunk/gcc/testsuite/gfortran.dg/typebound_proc_23.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/expr.c
    trunk/gcc/testsuite/ChangeLog
Comment 8 Tobias Burnus 2011-07-09 18:37:31 UTC
Janus, what's the status of this PR?
I think only backporting is missing, is that correct?
Comment 9 janus 2011-07-10 09:31:31 UTC
(In reply to comment #8)
> Janus, what's the status of this PR?
> I think only backporting is missing, is that correct?

Right. I'm just about to apply the backport ...
Comment 10 janus 2011-07-10 11:50:09 UTC
Author: janus
Date: Sun Jul 10 11:50:04 2011
New Revision: 176117

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=176117
Log:
2011-07-10  Janus Weil  <janus@gcc.gnu.org>

	PR fortran/49562
	* expr.c (gfc_check_vardef_context): Handle type-bound procedures.


2011-07-10  Janus Weil  <janus@gcc.gnu.org>

	PR fortran/49562
	* gfortran.dg/typebound_proc_23.f90: New.

Added:
    branches/gcc-4_6-branch/gcc/testsuite/gfortran.dg/typebound_proc_23.f90
Modified:
    branches/gcc-4_6-branch/gcc/fortran/ChangeLog
    branches/gcc-4_6-branch/gcc/fortran/expr.c
    branches/gcc-4_6-branch/gcc/testsuite/ChangeLog
Comment 11 janus 2011-07-10 11:52:35 UTC
Fixed on trunk and 4.6. Closing.

Also: Thanks for reporting, Hans-Werner!