See attached patch. IS_CONTIGUOUS of an array section with a vector
subscript, such as A([1,3]), is not simplified at compile time, so
the run-time test is applied to the contiguous temporary that is
built for the section and returns .true. The fix simplifies the result
to .false. whenever the argument has a vector subscript, using
gfc_has_vector_subscript.

The fix is one condition in gfc_simplify_is_contiguous in simplify.cc;
the rest is the new test, which covers explicit-shape, allocatable,
assumed-shape and character arrays, with and without a substring.

Regression tested on x86_64.

I plan to commit this as simple and obvious.

Regards,

Jerry

---

fortran: IS_CONTIGUOUS true for a section with a vector subscript

IS_CONTIGUOUS returned .true. for an array section with a vector
subscript, such as A([1,3]), because the run-time test inspected the
contiguous temporary that is built for such a section.  F2023 8.5.7
does not make a section with a vector subscript contiguous, so
simplify the result to .false.

Assisted-by: Claude Opus 5.5

gcc/fortran/ChangeLog:

        * simplify.cc (gfc_simplify_is_contiguous): Return .false. for an
        array section with a vector subscript.

gcc/testsuite/ChangeLog:

        * gfortran.dg/is_contiguous_8.f90: New test.
>From d388722510f945d5bcde97d79da5e58472cf4dcf Mon Sep 17 00:00:00 2001
From: Jerry DeLisle <[email protected]>
Date: Wed, 18 Nov 2026 14:51:07 -0800
Subject: [PATCH] fortran: IS_CONTIGUOUS true for a section with a vector
 subscript

IS_CONTIGUOUS returned .true. for an array section with a vector
subscript, such as A([1,3]), because the run-time test inspected the
contiguous temporary that is built for such a section.  F2023 8.5.7
does not make a section with a vector subscript contiguous, so
simplify the result to .false.

Assisted-by: Claude Opus 5.5

gcc/fortran/ChangeLog:

	* simplify.cc (gfc_simplify_is_contiguous): Return .false. for an
	array section with a vector subscript.

gcc/testsuite/ChangeLog:

	* gfortran.dg/is_contiguous_8.f90: New test.
---
 gcc/fortran/simplify.cc                       |  4 ++-
 gcc/testsuite/gfortran.dg/is_contiguous_8.f90 | 28 +++++++++++++++++++
 2 files changed, 31 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gfortran.dg/is_contiguous_8.f90

diff --git a/gcc/fortran/simplify.cc b/gcc/fortran/simplify.cc
index b2f3aa6cc15..b58a6af5d06 100644
--- a/gcc/fortran/simplify.cc
+++ b/gcc/fortran/simplify.cc
@@ -7402,7 +7402,9 @@ gfc_simplify_is_contiguous (gfc_expr *array)
   if (gfc_is_simply_contiguous (array, false, true))
     return gfc_get_logical_expr (gfc_default_logical_kind, &array->where, 1);
 
-  if (gfc_is_not_contiguous (array))
+  /* F2023 8.5.7 does not make a section with a vector subscript
+     contiguous, so say it is not.  */
+  if (gfc_is_not_contiguous (array) || gfc_has_vector_subscript (array))
     return gfc_get_logical_expr (gfc_default_logical_kind, &array->where, 0);
 
   return NULL;
diff --git a/gcc/testsuite/gfortran.dg/is_contiguous_8.f90 b/gcc/testsuite/gfortran.dg/is_contiguous_8.f90
new file mode 100644
index 00000000000..8e224d4338d
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/is_contiguous_8.f90
@@ -0,0 +1,28 @@
+! { dg-do run }
+
+program p
+  implicit none
+  integer :: a(10), b(10,10), k(2) = [1,3]
+  real, allocatable :: r(:,:)
+  character(len=4), target :: c(10,10)
+  allocate (r(4,4))
+  if (is_contiguous (a([1,3]))) stop 1
+  if (is_contiguous (a(k))) stop 2
+  if (is_contiguous (b(:,[1,3]))) stop 3
+  if (is_contiguous (r(:,k))) stop 4
+  call s (b)
+  call t (c)
+contains
+  subroutine s (x)
+    integer :: x(:,:)
+    if (is_contiguous (x(:,[1,3]))) stop 5
+    if (is_contiguous (x([1,3],1))) stop 6
+    if (is_contiguous (x([1,2],:))) stop 7
+  end subroutine
+  subroutine t (y)
+    character(len=4), target :: y(:,:)
+    if (is_contiguous (y(:,[1,3]))) stop 8
+    if (is_contiguous (y([1,2],:)(1:4))) stop 9
+    if (is_contiguous (y(:,[1,2])(1:2))) stop 10
+  end subroutine
+end program
-- 
2.56.1

Reply via email to