This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[Patch, Fortran] PR 43362 - PURE contraint - fix ICE, add missing check
- From: Tobias Burnus <burnus at net-b dot de>
- To: gfortran <fortran at gcc dot gnu dot org>, gcc patches <gcc-patches at gcc dot gnu dot org>
- Date: Sun, 14 Mar 2010 13:38:23 +0100
- Subject: [Patch, Fortran] PR 43362 - PURE contraint - fix ICE, add missing check
Found when adding the equivalent check for coarrays. There are two
issues, one NULL pointer access for a valid case (item (4) does not
apply) as accessing "rhs->symtree->n.sym" causes a segfault if RHS is
not an EXPR_VARIABLE but, e.g., a structure constructor.
The other issue is that gfortran did not have a check for constraint (3).
Quoting Fortran 2003:
"C1272 In a pure subprogram any designator with a base object that is in
common or accessed by host or use association, is a dummy argument of a
pure function, is a dummy argument with INTENT (IN) of a pure
subroutine, or an object that is storage associated with any such
variable, shall not be used in the following contexts:
[...]
(3) As the expr corresponding to a component with the POINTER attribute
in a structure constructor.
(4) As the expr of an intrinsic assignment statement in which the
variable is of a derived type if the derived type has a pointer
component at any level of component selection; [...]"
Build and regtested on x86-64-linux.
OK for the trunk?
Tobias
2010-03-14 Tobias Burnus <burnus@net-b.de>
PR fortran/43362
* resolve.c (resolve_structure_cons): Add missing PURE constraint.
(resolve_ordinary_assign): Add check to avoid segfault.
2010-03-14 Tobias Burnus <burnus@net-b.de>
PR fortran/43362
* gfortran.dg/impure_constructor_1.f90: New test.
Index: gcc/fortran/resolve.c
===================================================================
--- gcc/fortran/resolve.c (Revision 157445)
+++ gcc/fortran/resolve.c (Arbeitskopie)
@@ -921,6 +921,16 @@ resolve_structure_cons (gfc_expr *expr)
"for pointer component '%s' should be a POINTER or "
"a TARGET", &cons->expr->where, comp->name);
}
+
+ /* F2003, C1272 (3). */
+ if (gfc_pure (NULL) && cons->expr->expr_type == EXPR_VARIABLE
+ && gfc_impure_variable (cons->expr->symtree->n.sym))
+ {
+ t = FAILURE;
+ gfc_error ("Invalid expression in the derived type constructor for pointer "
+ "component '%s' at %L in PURE procedure", comp->name,
+ &cons->expr->where);
+ }
}
return t;
@@ -7947,6 +7957,7 @@ resolve_ordinary_assign (gfc_code *code,
if (lhs->ts.type == BT_DERIVED
&& lhs->expr_type == EXPR_VARIABLE
&& lhs->ts.u.derived->attr.pointer_comp
+ && rhs->expr_type == EXPR_VARIABLE
&& gfc_impure_variable (rhs->symtree->n.sym))
{
gfc_error ("The impure variable at %L is assigned to "
Index: gcc/testsuite/gfortran.dg/impure_constructor_1.f90
===================================================================
--- gcc/testsuite/gfortran.dg/impure_constructor_1.f90 (Revision 0)
+++ gcc/testsuite/gfortran.dg/impure_constructor_1.f90 (Revision 0)
@@ -0,0 +1,30 @@
+! { dg-do compile }
+!
+! PR fortran/43362
+!
+module m
+ implicit none
+ type t
+ integer, pointer :: a
+ end type t
+ type t2
+ type(t) :: b
+ end type t2
+ type t3
+ type(t), pointer :: b
+ end type t3
+contains
+ pure subroutine foo(x)
+ type(t), target, intent(in) :: x
+ type(t2) :: y
+ type(t3) :: z
+
+ ! The following gave an ICE but is valid:
+ y = t2(x) ! Note: F2003, C1272 (3) and (4) do not apply
+
+ ! Variant which is invalid as C1272 (3) applies
+ z = t3(x) ! { dg-error "Invalid expression in the derived type constructor" }
+ end subroutine foo
+end module m
+
+