Dear all,

the subject says it all: the attached obvious patch fixes an ICE during
resolution of user-defined operators in named BLOCKs by also searching
the list of user-defined operators.

Regtested on x86_64-pc-linux-gnu.  OK for mainline and backports?

Thanks,
Harald

From 34e9f37a85c28731e961f3a78bb3e750c073c1a2 Mon Sep 17 00:00:00 2001
From: Harald Anlauf <[email protected]>
Date: Tue, 7 Jul 2026 20:06:35 +0200
Subject: [PATCH] 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.
---
 gcc/fortran/interface.cc               | 10 +++-
 gcc/testsuite/gfortran.dg/block_18.f90 | 66 ++++++++++++++++++++++++++
 2 files changed, 75 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gfortran.dg/block_18.f90

diff --git a/gcc/fortran/interface.cc b/gcc/fortran/interface.cc
index 32152bd6777..3e4ce70eb8b 100644
--- a/gcc/fortran/interface.cc
+++ b/gcc/fortran/interface.cc
@@ -4885,7 +4885,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;
@@ -4918,6 +4918,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 00000000000..d56b0f43ad7
--- /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
-- 
2.51.0

Reply via email to