]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Fortran: fix resolution of user-defined operators in named BLOCK [PR126127]
authorHarald Anlauf <anlauf@gmx.de>
Tue, 7 Jul 2026 18:06:35 +0000 (20:06 +0200)
committerHarald Anlauf <anlauf@gmx.de>
Wed, 8 Jul 2026 20:05:52 +0000 (22:05 +0200)
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
gcc/testsuite/gfortran.dg/block_18.f90 [new file with mode: 0644]

index 32152bd6777272022c9caf3ca2db61f989939586..3e4ce70eb8b8c27d2a3041e29367405ed7555c66 100644 (file)
@@ -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 (file)
index 0000000..d56b0f4
--- /dev/null
@@ -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