[flang-commits] [flang] 1b92478 - [flang] Catch statement function typing error

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Wed Feb 1 11:19:31 PST 2023


Author: Peter Klausler
Date: 2023-02-01T11:19:19-08:00
New Revision: 1b924783003fdf676c6b9dd6be53450e8a0acc3f

URL: https://github.com/llvm/llvm-project/commit/1b924783003fdf676c6b9dd6be53450e8a0acc3f
DIFF: https://github.com/llvm/llvm-project/commit/1b924783003fdf676c6b9dd6be53450e8a0acc3f.diff

LOG: [flang] Catch statement function typing error

Emit an error message when the right-hand side expression of a statement function
definition cannot be converted to the type of the statement function.

Differential Revision: https://reviews.llvm.org/D142745

Added: 
    

Modified: 
    flang/lib/Semantics/resolve-names.cpp
    flang/test/Semantics/stmt-func01.f90

Removed: 
    


################################################################################
diff  --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index 594a718c7bf6c..476554bf5f7f1 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -7508,7 +7508,8 @@ void ResolveNamesVisitor::FinishSpecificationPart(
 // expressions, so it's safe to defer processing their definitions.)
 void ResolveNamesVisitor::AnalyzeStmtFunctionStmt(
     const parser::StmtFunctionStmt &stmtFunc) {
-  Symbol *symbol{std::get<parser::Name>(stmtFunc.t).symbol};
+  const auto &name{std::get<parser::Name>(stmtFunc.t)};
+  Symbol *symbol{name.symbol};
   auto *details{symbol ? symbol->detailsIf<SubprogramDetails>() : nullptr};
   if (!details || !symbol->scope()) {
     return;
@@ -7520,8 +7521,12 @@ void ResolveNamesVisitor::AnalyzeStmtFunctionStmt(
   PopScope();
   if (auto expr{AnalyzeExpr(context(), stmtFunc)}) {
     if (auto type{evaluate::DynamicType::From(*symbol)}) {
-      if (auto converted{ConvertToType(*type, std::move(*expr))}) {
+      if (auto converted{evaluate::ConvertToType(*type, std::move(*expr))}) {
         details->set_stmtFunction(std::move(*converted));
+      } else {
+        Say(name.source,
+            "Defining expression of statement function '%s' cannot be converted to its result type %s"_err_en_US,
+            name.source, type->AsFortran());
       }
     } else {
       details->set_stmtFunction(std::move(*expr));

diff  --git a/flang/test/Semantics/stmt-func01.f90 b/flang/test/Semantics/stmt-func01.f90
index ad1fda7209ac4..5682cb97078b5 100644
--- a/flang/test/Semantics/stmt-func01.f90
+++ b/flang/test/Semantics/stmt-func01.f90
@@ -31,6 +31,9 @@ pure integer function ifunc()
   !PORTABILITY: Statement function 'sf8' should not pass an array argument that is not a whole array
   sf8(n) = sum(a(1:2))
   sf8a(n) = sum(a) ! ok
+  integer :: sf9
+  !ERROR: Defining expression of statement function 'sf9' cannot be converted to its result type INTEGER(4)
+  sf9(n) = "bad"
  contains
   real function explicit(x,y)
     integer, intent(in) :: x


        


More information about the flang-commits mailing list