I believe I have run into a case where ubound is giving the wrong result. Given: program test integer, dimension (1:3,1:6) :: array print *, "ubound = ", ubound (array (1, 1:4)) end program test I see the output: ubound = 1 When I would have expected: ubound = 4 Due to array (1, 1:4) being a 4 element 1-dimensional array. I am seeing this behaviour in my default Fedora compiler: GNU Fortran (GCC) 9.3.1 20200408 (Red Hat 9.3.1-2) But I also build current HEAD from git (26a3f288f18) and am still seeing this issue: GNU Fortran (GCC) 11.0.0 20210209 (experimental) Interestingly, if I change the test program to: program test integer, dimension (1:3,1:6) :: array print *, "ubound = ", ubound (array (1:2, 1:4)) end program test Then I do now see what I would expect for this case: ubound = 2 4
Confirmed since at least GCC7.
Untested patch: diff --git a/gcc/fortran/simplify.c b/gcc/fortran/simplify.c index 23317a2e2d9..6a180ff40f2 100644 --- a/gcc/fortran/simplify.c +++ b/gcc/fortran/simplify.c @@ -4170,3 +4170,13 @@ simplify_bound_dim (gfc_expr *array, gfc_expr *kind, int d, int upper, { - if (!gfc_ref_dimen_size (&ref->u.ar, d - 1, &result->value.integer, NULL)) + int d2 = 0, cnt = 0; + for (int idx = 0; idx < ref->u.ar.dimen; ++idx) + { + if (ref->u.ar.dimen_type == DIMEN_ELEMENT) + d2++; + else if (cnt < d - 1) + cnt++; + else + break; + } + if (!gfc_ref_dimen_size (&ref->u.ar, d2 + d - 1, &result->value.integer, NULL)) goto returnNull;
The master branch has been updated by Tobias Burnus <burnus@gcc.gnu.org>: https://gcc.gnu.org/g:f600f271b10d0214b111f2aa52a3d5740477e139 commit r11-7292-gf600f271b10d0214b111f2aa52a3d5740477e139 Author: Tobias Burnus <tobias@codesourcery.com> Date: Fri Feb 19 10:42:15 2021 +0100 Fortran: Fix ubound simplifcation [PR99027] gcc/fortran/ChangeLog: PR fortran/99027 * simplify.c (simplify_bound_dim): Honor DIMEN_ELEMENT when using dim=. gcc/testsuite/ChangeLog: PR fortran/99027 * gfortran.dg/ubound_1.f90: New test.
Can confirm that with latest GCC HEAD I am now seeing the results that I expect. Thanks for the quick fix.
The releases/gcc-10 branch has been updated by Tobias Burnus <burnus@gcc.gnu.org>: https://gcc.gnu.org/g:700dcc60b5646cc64ae3ba72a79a7542b4902b50 commit r10-9380-g700dcc60b5646cc64ae3ba72a79a7542b4902b50 Author: Tobias Burnus <tobias@codesourcery.com> Date: Fri Feb 19 10:42:15 2021 +0100 Fortran: Fix ubound simplifcation [PR99027] gcc/fortran/ChangeLog: PR fortran/99027 * simplify.c (simplify_bound_dim): Honor DIMEN_ELEMENT when using dim=. gcc/testsuite/ChangeLog: PR fortran/99027 * gfortran.dg/ubound_1.f90: New test. (cherry picked from commit f600f271b10d0214b111f2aa52a3d5740477e139)
Now also FIXED for the GCC 10 branch. (In reply to Andrew Burgess from comment #4) > Can confirm that with latest GCC HEAD I am now seeing the results that I > expect. Thanks for the quick fix. :-) You are welcome. Thanks for the report!