See attached patch. Resolution drops a substring of an array section
only when its range is a constant covering the whole string. For
c(:)(1:n), dc(:)(:len(dc)) or a substring of an assumed-length dummy,
the reference stays, gfc_is_not_contiguous treats any reference after
the section as proof, and so IS_CONTIGUOUS folds to .false. and a
CONTIGUOUS pointer assignment is rejected. The run-time path cannot
answer either, because gfc_conv_expr_descriptor builds a temporary for
a substring section. With the patch, a trailing substring is proof
only when substring_is_partial shows it starts after 1 or ends before
the length; otherwise IS_CONTIGUOUS tests the array without the
substring at run time and ANDs that with start == 1 and end == the
string length.
gfc_is_not_contiguous also decides copy-in for a CONTIGUOUS, TARGET
dummy in trans-expr.cc; passing c(:)(1:n) to one still creates a
temporary and copies back correctly.
Regression tested on x86_64.
OK for mainline?
Regards,
Jerry
---
fortran: [PR128935] IS_CONTIGUOUS false for a full-length substring
A substring of a contiguous array that covers the whole string is
contiguous (F2023 8.5.7), but unless its range was constant,
IS_CONTIGUOUS returned .false. and pointer assignment to a CONTIGUOUS
pointer was rejected, because gfc_is_not_contiguous took any
reference after a section as proof. Take a trailing substring as
proof only when it is known to be partial, and at run time test the
array without the substring and check that the substring starts at 1
and ends at the string length.
Assisted-by: Claude Opus 5.5
PR fortran/128935
gcc/fortran/ChangeLog:
* expr.cc (substring_is_partial): New.
(gfc_is_not_contiguous): Do not treat a trailing substring as
proof unless substring_is_partial.
* trans-intrinsic.cc (gfc_conv_is_contiguous_expr): For a substring
of an array, test the array and check that the substring is the
whole string.
gcc/testsuite/ChangeLog:
* gfortran.dg/is_contiguous_9.f90: New test.
>From 757bf889e56064c5e1a1c9a4700c1173f40709cb Mon Sep 17 00:00:00 2001
From: Jerry DeLisle <[email protected]>
Date: Sat, 21 Nov 2026 17:56:57 -0800
Subject: [PATCH] fortran: [PR128935] IS_CONTIGUOUS false for a full-length
substring
A substring of a contiguous array that covers the whole string is
contiguous (F2023 8.5.7), but unless its range was constant,
IS_CONTIGUOUS returned .false. and pointer assignment to a CONTIGUOUS
pointer was rejected, because gfc_is_not_contiguous took any
reference after a section as proof. Take a trailing substring as
proof only when it is known to be partial, and at run time test the
array without the substring and check that the substring starts at 1
and ends at the string length.
Assisted-by: Claude Opus 5.5
PR fortran/128935
gcc/fortran/ChangeLog:
* expr.cc (substring_is_partial): New.
(gfc_is_not_contiguous): Do not treat a trailing substring as
proof unless substring_is_partial.
* trans-intrinsic.cc (gfc_conv_is_contiguous_expr): For a substring
of an array, test the array and check that the substring is the
whole string.
gcc/testsuite/ChangeLog:
* gfortran.dg/is_contiguous_9.f90: New test.
---
gcc/fortran/expr.cc | 23 ++++++++-
gcc/fortran/trans-intrinsic.cc | 50 +++++++++++++++++++
gcc/testsuite/gfortran.dg/is_contiguous_9.f90 | 32 ++++++++++++
3 files changed, 103 insertions(+), 2 deletions(-)
create mode 100644 gcc/testsuite/gfortran.dg/is_contiguous_9.f90
diff --git a/gcc/fortran/expr.cc b/gcc/fortran/expr.cc
index 40143b4158a..4fb3cd7ce3c 100644
--- a/gcc/fortran/expr.cc
+++ b/gcc/fortran/expr.cc
@@ -6605,6 +6605,23 @@ gfc_is_simply_contiguous (gfc_expr *expr, bool strict, bool permit_element)
return true;
}
+/* Return true if substring REF is known to be shorter than its parent
+ string. */
+
+static bool
+substring_is_partial (gfc_ref *ref)
+{
+ gfc_expr *start = ref->u.ss.start;
+ gfc_expr *end = ref->u.ss.end;
+ gfc_expr *len = ref->u.ss.length ? ref->u.ss.length->length : NULL;
+
+ if (start && start->expr_type == EXPR_CONSTANT
+ && mpz_cmp_si (start->value.integer, 1) > 0)
+ return true;
+
+ return end && len && gfc_dep_compare_expr (end, len) == -1;
+}
+
/* Return true if the expression is guaranteed to be non-contiguous,
false if we cannot prove anything. It is probably best to call
this after gfc_is_simply_contiguous. If neither of them returns
@@ -6620,9 +6637,11 @@ gfc_is_not_contiguous (gfc_expr *array)
for (ref = array->ref; ref; ref = ref->next)
{
- /* Array-ref shall be last ref. */
+ /* Array-ref shall be last ref, but a substring that may be the
+ whole string leaves the section to decide. */
- if (ar && ar->type != AR_ELEMENT)
+ if (ar && ar->type != AR_ELEMENT
+ && (ref->type != REF_SUBSTRING || substring_is_partial (ref)))
return true;
if (ref->type == REF_ARRAY)
diff --git a/gcc/fortran/trans-intrinsic.cc b/gcc/fortran/trans-intrinsic.cc
index 7a6b1bbe999..1141ae035d0 100644
--- a/gcc/fortran/trans-intrinsic.cc
+++ b/gcc/fortran/trans-intrinsic.cc
@@ -2427,10 +2427,28 @@ gfc_conv_is_contiguous_expr (gfc_se *se, gfc_expr *arg)
tree fncall0;
gfc_array_spec *as;
gfc_symbol *sym = NULL;
+ gfc_ref *ss_ref = NULL;
if (arg->ts.type == BT_CLASS)
gfc_add_class_array_ref (arg);
+ /* A substring of an array is contiguous if the array is and the
+ substring is the whole string; test the array without it. */
+ if (arg->expr_type == EXPR_VARIABLE && arg->rank > 0)
+ {
+ gfc_ref **last;
+ for (last = &arg->ref; *last && (*last)->next; last = &(*last)->next)
+ ;
+ if (*last && (*last)->type == REF_SUBSTRING)
+ {
+ ss_ref = *last;
+ *last = NULL;
+ arg = gfc_copy_expr (arg);
+ arg->ts.u.cl = ss_ref->u.ss.length;
+ *last = ss_ref;
+ }
+ }
+
if (arg->expr_type == EXPR_VARIABLE)
sym = arg->symtree->n.sym;
@@ -2505,6 +2523,38 @@ gfc_conv_is_contiguous_expr (gfc_se *se, gfc_expr *arg)
convert (boolean_type_node, se->expr));
}
+ if (ss_ref)
+ {
+ gfc_se sse;
+ tree one = build_int_cst (gfc_charlen_type_node, 1);
+
+ gcc_assert (argse.string_length);
+ cond = boolean_true_node;
+ if (ss_ref->u.ss.start)
+ {
+ gfc_init_se (&sse, se);
+ gfc_conv_expr_type (&sse, ss_ref->u.ss.start, gfc_charlen_type_node);
+ gfc_add_block_to_block (&se->pre, &sse.pre);
+ cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
+ sse.expr, one);
+ }
+ if (ss_ref->u.ss.end)
+ {
+ gfc_init_se (&sse, se);
+ gfc_conv_expr_type (&sse, ss_ref->u.ss.end, gfc_charlen_type_node);
+ gfc_add_block_to_block (&se->pre, &sse.pre);
+ tmp = fold_convert (gfc_charlen_type_node, argse.string_length);
+ tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
+ sse.expr, tmp);
+ cond = fold_build2_loc (input_location, TRUTH_AND_EXPR,
+ boolean_type_node, cond, tmp);
+ }
+ se->expr = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
+ boolean_type_node, cond,
+ convert (boolean_type_node, se->expr));
+ gfc_free_expr (arg);
+ }
+
if (as && as->type == AS_ASSUMED_RANK)
{
tree rank = gfc_conv_descriptor_rank_get (desc);
diff --git a/gcc/testsuite/gfortran.dg/is_contiguous_9.f90 b/gcc/testsuite/gfortran.dg/is_contiguous_9.f90
new file mode 100644
index 00000000000..2f982db8b3b
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/is_contiguous_9.f90
@@ -0,0 +1,32 @@
+! { dg-do run }
+!
+! PR 128935
+
+program p
+ implicit none
+ character(len=4), target :: c(10)
+ character(len=:), allocatable :: dc(:)
+ character(len=:), pointer, contiguous :: cp(:)
+ integer :: n
+ allocate (character(len=3) :: dc(10))
+ do n = 2, 4
+ if (is_contiguous (c(:)(1:n)) .neqv. (n == 4)) stop 1
+ if (is_contiguous (c(2:5)(:n)) .neqv. (n == 4)) stop 2
+ end do
+ n = 4
+ if (is_contiguous (c(::2)(1:n))) stop 3
+ if (.not. is_contiguous (dc(2:5)(1:3))) stop 4
+ if (.not. is_contiguous (dc(:)(:len(dc)))) stop 5
+ if (is_contiguous (dc(:)(2:))) stop 6
+ cp => c(:)(1:n)
+ if (.not. is_contiguous (cp)) stop 7
+ call sub (c, 4)
+ call sub (c(::2), 4)
+contains
+ subroutine sub (ac, m)
+ character(len=*) :: ac(:)
+ integer :: m
+ if (is_contiguous (ac(:)(1:m)) .neqv. is_contiguous (ac)) stop 8
+ if (is_contiguous (ac(:)(1:m-1))) stop 9
+ end subroutine
+end program
--
2.56.1