Hi all,
here is my shot on fixing this PR. The issue is, that when checking if the tree
to associate to is a pointer, gfortran does not respect void* aka c_ptr
correctly. On the tree level this can be done by checking the compatibility of
the data pointed to. If not, then just add an address op.
I also check F2018 standard and could not find any mention that a c_ptr is
disallowed or needs to be treated specially in an associate.
Regtests ok on x86_64-pc-linux-gnu / F41. Ok for mainline?
Regards,
Andre
--
Andre Vehreschild * Email: vehre ad gmx dot de
From 292a1d9e67f44124474d8e4198723baa5dea5b4d Mon Sep 17 00:00:00 2001
From: Andre Vehreschild <[email protected]>
Date: Tue, 25 Feb 2025 17:15:47 +0100
Subject: [PATCH] Fortran: Fix ICE on associate of pointer [PR118789]
Fix ICE when associating a pointer to void (c_ptr) by looking at the
compatibility of the type hierarchy.
PR fortran/118789
gcc/fortran/ChangeLog:
* trans-stmt.cc (trans_associate_var): Compare pointed to types when
expr to associate is already a pointer.
gcc/testsuite/ChangeLog:
* gfortran.dg/associate_73.f90: New test.
---
gcc/fortran/trans-stmt.cc | 7 ++++++-
gcc/testsuite/gfortran.dg/associate_73.f90 | 21 +++++++++++++++++++++
2 files changed, 27 insertions(+), 1 deletion(-)
create mode 100644 gcc/testsuite/gfortran.dg/associate_73.f90
diff --git a/gcc/fortran/trans-stmt.cc b/gcc/fortran/trans-stmt.cc
index e7da8fea3b2..f16e1e3b46e 100644
--- a/gcc/fortran/trans-stmt.cc
+++ b/gcc/fortran/trans-stmt.cc
@@ -2287,7 +2287,12 @@ trans_associate_var (gfc_symbol *sym, gfc_wrapped_block *block)
tmp = se.expr;
}
}
- if (!POINTER_TYPE_P (TREE_TYPE (se.expr)))
+ /* For non-pointer types in se.expr, the first condition holds.
+ For pointer or reference types in se.expr, a double TREE_TYPE ()
+ is possible and an associate variable always is a pointer. */
+ if (!POINTER_TYPE_P (TREE_TYPE (se.expr))
+ || TREE_TYPE (TREE_TYPE (se.expr))
+ != TREE_TYPE (TREE_TYPE (sym->backend_decl)))
tmp = gfc_build_addr_expr (tmp, se.expr);
}
diff --git a/gcc/testsuite/gfortran.dg/associate_73.f90 b/gcc/testsuite/gfortran.dg/associate_73.f90
new file mode 100644
index 00000000000..a5c3ca79b9c
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/associate_73.f90
@@ -0,0 +1,21 @@
+!{ dg-do compile }
+
+! Check associate to a "void *" does not ICE.
+! Contributed by Matthias Klose <[email protected]>
+! and Steve Kargl <[email protected]>
+
+module pr118789
+
+ implicit none
+
+ CONTAINS
+
+ subroutine fckit_c_nodelete(cptr) bind(c)
+ use, intrinsic :: iso_c_binding
+ type(c_ptr), value :: cptr
+ associate( unused_ => cptr )
+ end associate
+ end subroutine
+
+end module
+
--
2.48.1