[flang-commits] [flang] [flang] Improve error message for polymorphic implicit interface (PR #177225)

via flang-commits flang-commits at lists.llvm.org
Wed Jan 21 11:48:50 PST 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-semantics

Author: Peter Klausler (klausler)

<details>
<summary>Changes</summary>

PROCEDURE(CLASS(...)) specifies a procedure with an implicit interface whose result is polymorphic.  That turns out to never be a valid declaration, since a CLASS(...) function result is required to also be POINTER or ALLOCATABLE, both of which require the presence of an explicit interface.  We caught this error already, but the message should be improved for procedures.

Fixes https://github.com/llvm/llvm-project/issues/176861.

---
Full diff: https://github.com/llvm/llvm-project/pull/177225.diff


2 Files Affected:

- (modified) flang/lib/Semantics/check-declarations.cpp (+9-3) 
- (modified) flang/test/Semantics/declarations06.f90 (+4-4) 


``````````diff
diff --git a/flang/lib/Semantics/check-declarations.cpp b/flang/lib/Semantics/check-declarations.cpp
index 684c1dcc98fa3..89fa488cced29 100644
--- a/flang/lib/Semantics/check-declarations.cpp
+++ b/flang/lib/Semantics/check-declarations.cpp
@@ -3862,9 +3862,15 @@ void CheckHelper::CheckSymbolType(const Symbol &symbol) {
   } else if (auto dyType{evaluate::DynamicType::From(relevant)}) {
     if (dyType->IsPolymorphic() && !dyType->IsAssumedType() &&
         !(IsDummy(symbol) && !IsProcedure(relevant))) { // C708
-      messages_.Say(
-          "CLASS entity '%s' must be a dummy argument, allocatable, or object pointer"_err_en_US,
-          symbol.name());
+      if (IsProcedure(symbol)) {
+        messages_.Say(
+            "Polymorphic function%s '%s' must have an explicit interface whose result is ALLOCATABLE or POINTER"_err_en_US,
+            IsPointer(symbol) ? " pointer" : "", symbol.name());
+      } else {
+        messages_.Say(
+            "CLASS entity '%s' must be a dummy argument, allocatable, or object pointer"_err_en_US,
+            symbol.name());
+      }
     }
     if (dyType->HasDeferredTypeParameter()) { // C702
       messages_.Say(
diff --git a/flang/test/Semantics/declarations06.f90 b/flang/test/Semantics/declarations06.f90
index ae9ef6acd7542..a490b8c3394fd 100644
--- a/flang/test/Semantics/declarations06.f90
+++ b/flang/test/Semantics/declarations06.f90
@@ -8,11 +8,11 @@ module m
   class(t) v1
   class(t), allocatable :: v2 ! ok
   class(t), pointer :: v3 ! ok
-  !ERROR: CLASS entity 'p1' must be a dummy argument, allocatable, or object pointer
+  !ERROR: Polymorphic function 'p1' must have an explicit interface whose result is ALLOCATABLE or POINTER
   procedure(cf1) :: p1
   procedure(cf2) :: p2
   procedure(cf3) :: p3
-  !ERROR: CLASS entity 'pp1' must be a dummy argument, allocatable, or object pointer
+  !ERROR: Polymorphic function pointer 'pp1' must have an explicit interface whose result is ALLOCATABLE or POINTER
   procedure(cf1), pointer :: pp1
   procedure(cf2), pointer :: pp2
   procedure(cf3), pointer :: pp3
@@ -29,9 +29,9 @@ class(t) function cf3()
   end
   subroutine test(d1,d2,d3)
     class(t) d1 ! ok
-    !ERROR: CLASS entity 'd2' must be a dummy argument, allocatable, or object pointer
+    !ERROR: Polymorphic function 'd2' must have an explicit interface whose result is ALLOCATABLE or POINTER
     class(t), external :: d2
-    !ERROR: CLASS entity 'd3' must be a dummy argument, allocatable, or object pointer
+    !ERROR: Polymorphic function pointer 'd3' must have an explicit interface whose result is ALLOCATABLE or POINTER
     class(t), external, pointer :: d3
   end
   function cf4()

``````````

</details>


https://github.com/llvm/llvm-project/pull/177225


More information about the flang-commits mailing list