Affects versions down to at least r5 : $ cat z1.f90 program p type t real :: a(3,2:-1) end type type t2 type(t) :: b(2) end type type(t2) :: c integer, parameter :: n = sizeof(c%b) end $ cat z2.f90 program p type t real :: a(3,2:0) end type type t2 type(t) :: b(2) end type type(t2) :: c integer :: n = sizeof(c%b) end $ cat z3.f90 program p type t real :: a(3,2:-1) end type type t2 type(t) :: b end type type(t2) :: c integer, parameter :: n = sizeof(c%b) end $ gfortran-13-20220612 -c z1.f90 f951: internal compiler error: in gfc_element_size, at fortran/target-memory.cc:132 0x79f574 gfc_element_size(gfc_expr*, unsigned long*) ../../gcc/fortran/target-memory.cc:132 0x79f5b3 gfc_target_expr_size(gfc_expr*, unsigned long*) ../../gcc/fortran/target-memory.cc:165 0x79340b gfc_simplify_sizeof(gfc_expr*) ../../gcc/fortran/simplify.cc:7637 0x70e392 do_simplify ../../gcc/fortran/intrinsic.cc:4663 0x7192da gfc_intrinsic_func_interface(gfc_expr*, int) ../../gcc/fortran/intrinsic.cc:5056 0x76de18 resolve_unknown_f ../../gcc/fortran/resolve.cc:2990 0x76de18 resolve_function ../../gcc/fortran/resolve.cc:3347 0x76de18 gfc_resolve_expr(gfc_expr*) ../../gcc/fortran/resolve.cc:7187 0x6fe2b4 gfc_reduce_init_expr(gfc_expr*) ../../gcc/fortran/expr.cc:3163 0x701240 gfc_match_init_expr(gfc_expr**) ../../gcc/fortran/expr.cc:3211 0x6eb584 variable_decl ../../gcc/fortran/decl.cc:3004 0x6eb584 gfc_match_data_decl() ../../gcc/fortran/decl.cc:6299 0x756333 match_word ../../gcc/fortran/parse.cc:67 0x756333 decode_statement ../../gcc/fortran/parse.cc:378 0x757d7a next_free ../../gcc/fortran/parse.cc:1397 0x757d7a next_statement ../../gcc/fortran/parse.cc:1629 0x75930b parse_spec ../../gcc/fortran/parse.cc:4168 0x75c4ac parse_progunit ../../gcc/fortran/parse.cc:6210 0x75db71 gfc_parse_file() ../../gcc/fortran/parse.cc:6755 0x7ab8cf gfc_be_parse_file ../../gcc/fortran/f95-lang.cc:229
For reference : $ cat zz1.f90 program p real :: a(3,2:-1) real :: b(3,2:0) print *, size(a), sizeof(a) print *, size(b), sizeof(b) end $ gfortran-13-20220612 zz1.f90 ; a.out 0 0 0 0
Confirmed. Reduced testcase: program p implicit none integer, parameter :: n = 0 !-1 real :: a(3,2:n) type t real :: a(3,2:n) end type type(t) :: d integer, parameter :: k = sizeof(a) ! OK integer, parameter :: m = sizeof(d) ! ICE for n < 1 end
(In reply to anlauf from comment #2) > Reduced testcase: > > integer, parameter :: m = sizeof(d) ! ICE for n < 1 In target-memory.cc we run into int_size_in_bytes(), which returns -12 for n=0, and -24 for n=-1, and so on... (gdb) l 127 gfc_typespec ts; 128 HOST_WIDE_INT size; 129 ts = e->ts; 130 type = gfc_typenode_for_spec (&ts); 131 size = int_size_in_bytes (type); 132 gcc_assert (size >= 0); 133 *siz = size; 134 } It is strange that -fdump-fortran-original shows the same output for n=0 and n=-1 when commenting out the ICEing line, so I wonder where this difference comes from. Some help interpreting tree type here might be helpful...
Anyway, there's likely an ordering issue looking at array bounds and using them. Moving the type decl to a module, the problem seems to disappear: module m implicit none integer, parameter :: n = -1 real :: a(3,2:n) type t real :: b(3,2:n) end type end module m program p use m implicit none type(t) :: d integer, parameter :: k = sizeof(a) integer, parameter :: j = sizeof(d) integer, parameter :: l = storage_size(d) end This compiles and produces the right numbers.
The following patch fixes the ICE and regtests OK: diff --git a/gcc/fortran/decl.cc b/gcc/fortran/decl.cc index bd586e75008..3e04f45e9ac 100644 --- a/gcc/fortran/decl.cc +++ b/gcc/fortran/decl.cc @@ -2775,6 +2775,17 @@ variable_decl (int elem) else gfc_free_expr (n); } + /* For bounds of an array spec that are both constant, ensure + that the upper bound is not lower than lower bound minus + one. Otherwise fix it. */ + if (as->lower[i]->expr_type == EXPR_CONSTANT + && as->upper[i]->expr_type == EXPR_CONSTANT + && as->lower[i]->ts.type == BT_INTEGER + && as->upper[i]->ts.type == BT_INTEGER + && mpz_cmp (as->upper[i]->value.integer, + as->lower[i]->value.integer) < 0) + mpz_sub_ui (as->upper[i]->value.integer, + as->lower[i]->value.integer, 1); } } }
Submitted: https://gcc.gnu.org/pipermail/fortran/2022-June/057939.html
The master branch has been updated by Harald Anlauf <anlauf@gcc.gnu.org>: https://gcc.gnu.org/g:a312407bd715647f7c11b67e0a52effc94d0f15d commit r13-1276-ga312407bd715647f7c11b67e0a52effc94d0f15d Author: Harald Anlauf <anlauf@gmx.de> Date: Mon Jun 20 20:59:55 2022 +0200 Fortran: handle explicit-shape specs with constant bounds [PR105954] gcc/fortran/ChangeLog: PR fortran/105954 * decl.cc (variable_decl): Adjust upper bounds for explicit-shape specs with constant bound expressions to ensure non-negative extents. gcc/testsuite/ChangeLog: PR fortran/105954 * gfortran.dg/pr105954.f90: New test.
Fixed. Thanks for the report!
The releases/gcc-12 branch has been updated by Harald Anlauf <anlauf@gcc.gnu.org>: https://gcc.gnu.org/g:6bcab64467d7393d69bf5abd7b2a0aba22d2896e commit r12-8531-g6bcab64467d7393d69bf5abd7b2a0aba22d2896e Author: Harald Anlauf <anlauf@gmx.de> Date: Mon Jun 20 20:59:55 2022 +0200 Fortran: handle explicit-shape specs with constant bounds [PR105954] gcc/fortran/ChangeLog: PR fortran/105954 * decl.cc (variable_decl): Adjust upper bounds for explicit-shape specs with constant bound expressions to ensure non-negative extents. gcc/testsuite/ChangeLog: PR fortran/105954 * gfortran.dg/pr105954.f90: New test. (cherry picked from commit a312407bd715647f7c11b67e0a52effc94d0f15d)
The releases/gcc-11 branch has been updated by Harald Anlauf <anlauf@gcc.gnu.org>: https://gcc.gnu.org/g:7296a7c9aa35a9731a73e33c29f6bbc488a0c837 commit r11-10103-g7296a7c9aa35a9731a73e33c29f6bbc488a0c837 Author: Harald Anlauf <anlauf@gmx.de> Date: Mon Jun 20 20:59:55 2022 +0200 Fortran: handle explicit-shape specs with constant bounds [PR105954] gcc/fortran/ChangeLog: PR fortran/105954 * decl.c (variable_decl): Adjust upper bounds for explicit-shape specs with constant bound expressions to ensure non-negative extents. gcc/testsuite/ChangeLog: PR fortran/105954 * gfortran.dg/pr105954.f90: New test. (cherry picked from commit a312407bd715647f7c11b67e0a52effc94d0f15d)
The releases/gcc-10 branch has been updated by Harald Anlauf <anlauf@gcc.gnu.org>: https://gcc.gnu.org/g:bac42273343bb9a198704900e2118ed4e84cb23a commit r10-10878-gbac42273343bb9a198704900e2118ed4e84cb23a Author: Harald Anlauf <anlauf@gmx.de> Date: Mon Jun 20 20:59:55 2022 +0200 Fortran: handle explicit-shape specs with constant bounds [PR105954] gcc/fortran/ChangeLog: PR fortran/105954 * decl.c (variable_decl): Adjust upper bounds for explicit-shape specs with constant bound expressions to ensure non-negative extents. gcc/testsuite/ChangeLog: PR fortran/105954 * gfortran.dg/pr105954.f90: New test. (cherry picked from commit a312407bd715647f7c11b67e0a52effc94d0f15d)