https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122977

--- Comment #7 from Steve Kargl <sgk at troutmask dot apl.washington.edu> ---
On Wed, Dec 03, 2025 at 09:20:35PM +0000, anlauf at gcc dot gnu.org wrote:
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122977
> 
> --- Comment #6 from anlauf at gcc dot gnu.org ---
> (In reply to Steve Kargl from comment #5)
> > On Wed, Dec 03, 2025 at 08:44:39PM +0000, anlauf at gcc dot gnu.org wrote:
> > > 
> > > --- Comment #4 from anlauf at gcc dot gnu.org ---
> > > (In reply to Steve Kargl from comment #3)
> > > > 
> > > > I haven't looked at the issue with gfc_is_simply_contiguous() in
> > > > comment #2, but if it passes regression testing, and you have a
> > > > testcase (or two) feel free to commit.  The comment in the patch
> > > > explains the rationale.
> > > 
> > > The change in comment#2 regresses on gfortran.dg/associate_11.f90
> > > because it will pack the associate variable before passing to a 
> > > subroutine.
> > > I don't have a simple answer yet.
> > 
> > I see.  In looking at the symbol_attribute struct in gfortran.h,
> > does the packing lead to attr.temporary or attr.artificial being
> > set?  These are the only ones that look like they may convey
> > some needed info; otherwise, we could add a new packed_arg attribute.
> 
> Unfortunately I do not fully understand what you mean.

Just thinking out loud.


  program foo
    integer, dimension(:), pointer, contiguous :: a
    allocate (a(4))
    associate (c => a(1::2))
      if (is_contiguous(c)) stop 3              ! runtime check
    end associate
  end program foo


'a' is contiguous, but 'c' is associated with an array section.
Do we agree that in this case 'c' is not contiguous?  

(gdb) b simplify.cc:7365  <-- 1st line in gfc_simplify_is_contiguous()
...
(gdb) p array->symtree->name
$12 = 0x204bfc228 "c"
(gdb) p array->symtree->n.sym->attr.dimension
$13 = 1
(gdb) p array->symtree->n.sym->attr.associate_var 
$14 = 1
(gdb) p array->symtree->n.sym->attr.contiguous
$15 = 0

So, can we look at the target of the associate variable
in gfc_simplify_is_contiguous().  gfc_is_simply_contiguous()
is called by gfc_simplify_is_contiguous() and returns true
(not sure why, yet; likely associate variables weren't considered
when it was written).

(gdb) p array->symtree->n.sym->assoc->target->expr_type
$47 = EXPR_VARIABLE
(gdb) call debug(array->symtree->n.sym->assoc->target)
foo:a(1::2_8) (INTEGER 4)


This seems to allow your testcase to pass.  I haven't regression
tested yet.


diff --git a/gcc/fortran/simplify.cc b/gcc/fortran/simplify.cc
index b25cd2c2388..65e135b9908 100644
--- a/gcc/fortran/simplify.cc
+++ b/gcc/fortran/simplify.cc
@@ -7362,6 +7362,13 @@ do_xor (gfc_expr *result, gfc_expr *e)
 gfc_expr *
 gfc_simplify_is_contiguous (gfc_expr *array)
 {
+  if (array->expr_type == EXPR_VARIABLE
+      && array->symtree->n.sym->attr.associate_var == 1
+      && array->symtree->n.sym->attr.dimension > 0
+      && array->symtree->n.sym->assoc
+      && array->symtree->n.sym->assoc->target)
+    return gfc_simplify_is_contiguous (array->symtree->n.sym->assoc->target);
+
   if (gfc_is_simply_contiguous (array, false, true))
     return gfc_get_logical_expr (gfc_default_logical_kind, &array->where, 1);

Reply via email to