[gcc r16-9251] Fortran: fix resolution of user-defined operators in named BLOCK [PR126127]

Harald Anlauf via Gcc-cvs <[email protected]>
Newsgroups gmane.comp.gcc.cvs
Message-ID <[email protected]>
https://gcc.gnu.org/g:f8da72c33adf5190566f9aa30b2ed239589e3592

commit r16-9251-gf8da72c33adf5190566f9aa30b2ed239589e3592
Author: Harald Anlauf <[email protected]>
Date:   Tue Jul 7 20:06:35 2026 +0200

    Fortran: fix resolution of user-defined operators in named BLOCK [PR126127]
    
            PR fortran/126127
    
    gcc/fortran/ChangeLog:
    
            * interface.cc (find_symtree0): Check for NULL pointer.
            (gfc_find_sym_in_symtree): Also search user-defined operator list
            when the symbol refers to a function.
    
    gcc/testsuite/ChangeLog:
    
            * gfortran.dg/block_18.f90: New test.
    
    (cherry picked from commit 2fad7b2d4d4ae51bb65a23d45e883e07beae624c)

Diff:
---
 gcc/fortran/interface.cc               | 10 +++++-
 gcc/testsuite/gfortran.dg/block_18.f90 | 66 ++++++++++++++++++++++++++++++++++
 2 files changed, 75 insertions(+), 1 deletion(-)

diff --git a/gcc/fortran/interface.cc b/gcc/fortran/interface.cc
index 79e9357466c5..c93f2948731c 100644
--- a/gcc/fortran/interface.cc
+++ b/gcc/fortran/interface.cc
@@ -4844,7 +4844,7 @@ find_symtree0 (gfc_symtree *root, gfc_symbol *sym)
 {
   gfc_symtree * st;
 
-  if (root->n.sym == sym)
+  if (root == NULL || root->n.sym == sym)
     return root;
 
   st = NULL;
@@ -4877,6 +4877,14 @@ gfc_find_sym_in_symtree (gfc_symbol *sym)
       st = find_symtree0 (ns->sym_root, sym);
       if (st)
 	return st;
+
+      /* Search user-defined operators.  */
+      if (ns->uop_root && sym->attr.function)
+	{
+	  st = find_symtree0 (ns->uop_root, sym);
+	  if (st)
+	    return st;
+	}
     }
   gfc_internal_error ("Unable to find symbol %qs", sym->name);
   /* Not reached.  */
diff --git a/gcc/testsuite/gfortran.dg/block_18.f90 b/gcc/testsuite/gfortran.dg/block_18.f90
new file mode 100644
index 000000000000..d56b0f43ad7d
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/block_18.f90
@@ -0,0 +1,66 @@
+! { dg-do compile }
+!
+! PR fortran/126127 - resolution of user-defined operators in named BLOCK
+
+module geom2d
+  use, intrinsic :: iso_fortran_env, only: wp => real64
+  implicit none
+  private
+  public :: vec2_t, operator(.dot.)
+
+  type :: vec2_t
+     real(wp) :: x = 0, y = 0
+  end type vec2_t
+
+  interface operator(.dot.)
+     module procedure vec_dot
+  end interface operator(.dot.)
+contains
+  pure function vec_dot(a, b) result(r)
+    type(vec2_t), intent(in) :: a, b
+    real(wp)                 :: r
+    r = a%x*b%x + a%y*b%y
+  end function vec_dot
+end module
+
+module consumer
+  use, intrinsic :: iso_fortran_env, only: r8 => real64
+  use geom2d, only: vec2_t, operator(.dot.)
+  implicit none
+contains
+  subroutine f(id)
+    integer, intent(out) :: id
+    type(vec2_t) :: d
+    real(r8)     :: dd
+    id = 0
+    blk: block
+      dd = d .dot. d
+    end block blk
+  end subroutine f
+
+  subroutine f2(id)
+    integer, intent(out) :: id
+    type(vec2_t) :: d
+    real(r8)     :: dd
+    id = 0
+    block
+      dd = d .dot. d
+    end block
+  end subroutine f2
+
+  subroutine f3(id)
+    integer, intent(out) :: id
+    type(vec2_t) :: d
+    real(r8)     :: dd
+    block
+      blk1: block
+        id = 0
+        block
+          blk2: block
+            dd = d .dot. d
+          end block blk2
+        end block
+      end block blk1
+    end block
+  end subroutine f3
+end module
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.