Dear All,

here's a small, obvious fix for an issue where a warning from -Wextra
was actually "extra" helpful for debugging gfortran. ;-)
The contiguous attribute of an associate target was not copied to the
associate variable.

Regtested on x86_64-pc-linux-gnu.  OK for mainline / 15-branch?

Thanks,
Harald

From 67481ff742f4eb8488df14261f64630d78b1f934 Mon Sep 17 00:00:00 2001
From: Harald Anlauf <[email protected]>
Date: Mon, 17 Nov 2025 21:20:08 +0100
Subject: [PATCH] Fortran: contiguous pointer assignment to select type target
 [PR122709]

	PR fortran/122709

gcc/fortran/ChangeLog:

	* resolve.cc (resolve_assoc_var): If the associate target is a
	contiguous pointer, so is the associate variable.

gcc/testsuite/ChangeLog:

	* gfortran.dg/select_contiguous.f90: New test.
---
 gcc/fortran/resolve.cc                        |  4 ++
 .../gfortran.dg/select_contiguous.f90         | 51 +++++++++++++++++++
 2 files changed, 55 insertions(+)
 create mode 100644 gcc/testsuite/gfortran.dg/select_contiguous.f90

diff --git a/gcc/fortran/resolve.cc b/gcc/fortran/resolve.cc
index b1d2ff220bf..2390858424e 100644
--- a/gcc/fortran/resolve.cc
+++ b/gcc/fortran/resolve.cc
@@ -10790,6 +10790,10 @@ resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
   /* If the target is a good class object, so is the associate variable.  */
   if (sym->ts.type == BT_CLASS && gfc_expr_attr (target).class_ok)
     sym->attr.class_ok = 1;
+
+  /* If the target is a contiguous pointer, so is the associate variable.  */
+  if (gfc_expr_attr (target).pointer && gfc_expr_attr (target).contiguous)
+    sym->attr.contiguous = 1;
 }
 
 
diff --git a/gcc/testsuite/gfortran.dg/select_contiguous.f90 b/gcc/testsuite/gfortran.dg/select_contiguous.f90
new file mode 100644
index 00000000000..b947006ba1e
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/select_contiguous.f90
@@ -0,0 +1,51 @@
+! { dg-do compile }
+! { dg-options "-O2 -Wextra -fdump-tree-optimized" }
+!
+! PR fortran/122709 - bogus warning for contiguous pointer assignment
+!                     to select type target
+!
+! Contributed by <mscfd at gmx dot net>
+
+module sc_mod
+  implicit none
+  public
+
+  type :: t
+     integer :: i = 0
+  end type t
+
+  type :: s
+     class(t), dimension(:), contiguous, pointer :: p => null()
+  end type s
+
+contains
+
+  subroutine foo(x)
+    class(s), intent(in) :: x
+    type(t), dimension(:), contiguous, pointer :: q
+    select type (p_ => x%p)
+    type is (t)
+       q => p_
+       if (.not. is_contiguous(x%p)) stop 1
+       if (.not. is_contiguous(p_))  stop 2     ! Should get optimized out
+       if (.not. is_contiguous(q))   stop 3
+       write(*,*) 'is contiguous: ', is_contiguous(x%p), &
+            is_contiguous(p_), is_contiguous(q)
+    end select
+  end subroutine foo
+
+end module sc_mod
+
+program select_contiguous
+  use sc_mod
+  implicit none
+
+  type(s) :: x
+
+  allocate(t :: x%p(1:10))
+  call foo(x)
+  deallocate(x%p)
+
+end program select_contiguous
+
+! { dg-final { scan-tree-dump-not "_gfortran_stop_numeric" "optimized" } }
-- 
2.51.0

Reply via email to