From 2e0bffaf8cc85cd8f5dd4a7b31ca354e0017b14e Mon Sep 17 00:00:00 2001 From: Tobias Burnus Date: Tue, 10 Jan 2012 12:22:16 +0100 Subject: [PATCH] re PR fortran/51652 (Allocate with type-spec and source-expr: check whether length type-parameter is the same is lacking) 2012-01-10 Tobias Burnus PR fortran/51652 * resolve.c (resolve_allocate_expr): For non-deferred char lengths, check whether type-spec matches declaration. 2012-01-10 Tobias Burnus PR fortran/51652 * gfortran.dg/allocate_with_typespec_5.f90: New. From-SVN: r183061 --- gcc/fortran/ChangeLog | 6 +++++ gcc/fortran/resolve.c | 13 ++++++++++ gcc/testsuite/ChangeLog | 5 ++++ .../gfortran.dg/allocate_with_typespec_5.f90 | 26 +++++++++++++++++++ 4 files changed, 50 insertions(+) create mode 100644 gcc/testsuite/gfortran.dg/allocate_with_typespec_5.f90 diff --git a/gcc/fortran/ChangeLog b/gcc/fortran/ChangeLog index bc9abc1f175..04c83448705 100644 --- a/gcc/fortran/ChangeLog +++ b/gcc/fortran/ChangeLog @@ -1,3 +1,9 @@ +2012-01-10 Tobias Burnus + + PR fortran/51652 + * resolve.c (resolve_allocate_expr): For non-deferred char lengths, + check whether type-spec matches declaration. + 2012-01-10 Tobias Burnus * resolve.c (resolve_ordinary_assign): Improve error wording. diff --git a/gcc/fortran/resolve.c b/gcc/fortran/resolve.c index 1c5fea3752c..7f476b83400 100644 --- a/gcc/fortran/resolve.c +++ b/gcc/fortran/resolve.c @@ -6989,6 +6989,19 @@ resolve_allocate_expr (gfc_expr *e, gfc_code *code) goto failure; } + if (code->ext.alloc.ts.type == BT_CHARACTER && !e->ts.deferred) + { + int cmp = gfc_dep_compare_expr (e->ts.u.cl->length, + code->ext.alloc.ts.u.cl->length); + if (cmp == 1 || cmp == -1 || cmp == -3) + { + gfc_error ("Allocating %s at %L with type-spec requires the same " + "character-length parameter as in the declaration", + sym->name, &e->where); + goto failure; + } + } + /* In the variable definition context checks, gfc_expr_attr is used on the expression. This is fooled by the array specification present in e, thus we have to eliminate that one temporarily. */ diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index a6df9b0fc33..1eccbaa7c17 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2012-01-10 Tobias Burnus + + PR fortran/51652 + * gfortran.dg/allocate_with_typespec_5.f90: New. + 2012-01-10 Tobias Burnus * gfortran.dg/class_39.f03: Update dg-error string. diff --git a/gcc/testsuite/gfortran.dg/allocate_with_typespec_5.f90 b/gcc/testsuite/gfortran.dg/allocate_with_typespec_5.f90 new file mode 100644 index 00000000000..1f8b3d6c4d2 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/allocate_with_typespec_5.f90 @@ -0,0 +1,26 @@ +! { dg-do compile } +! +! PR fortran/51652 +! +! Contributed by David Kinniburgh +! +module settings + +type keyword + character(60), allocatable :: c(:) +end type keyword + +type(keyword) :: kw(10) + +contains + +subroutine save_kw + allocate(character(80) :: kw(1)%c(10)) ! { dg-error "with type-spec requires the same character-length parameter" } +end subroutine save_kw + +subroutine foo(n) + character(len=n+2), allocatable :: x + allocate (character(len=n+3) :: x) ! { dg-error "type-spec requires the same character-length parameter" } +end subroutine foo + +end module settings -- 2.30.2