Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flang] Detect more misparsed statement functions (same name as funct… #73852

Merged
merged 1 commit into from
Nov 30, 2023

Conversation

klausler
Copy link
Contributor

…ion result)

A function can't return a statement function, so an apparent attempt to define a statement function with the same name as the function's result must be a misparsed assignment statement.

@llvmbot llvmbot added flang Flang issues not falling into any other category flang:semantics labels Nov 29, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Nov 29, 2023

@llvm/pr-subscribers-flang-semantics

Author: Peter Klausler (klausler)

Changes

…ion result)

A function can't return a statement function, so an apparent attempt to define a statement function with the same name as the function's result must be a misparsed assignment statement.


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

2 Files Affected:

  • (modified) flang/lib/Semantics/resolve-names.cpp (+2-1)
  • (modified) flang/test/Semantics/stmt-func02.f90 (+19)
diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index 8f15f2f51da7c89..946ecfc5d60ee8a 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -3508,7 +3508,8 @@ bool SubprogramVisitor::HandleStmtFunction(const parser::StmtFunctionStmt &x) {
     Symbol &ultimate{symbol->GetUltimate()};
     if (ultimate.has<ObjectEntityDetails>() ||
         ultimate.has<AssocEntityDetails>() ||
-        CouldBeDataPointerValuedFunction(&ultimate)) {
+        CouldBeDataPointerValuedFunction(&ultimate) ||
+        (&symbol->owner() == &currScope() && IsFunctionResult(*symbol))) {
       misparsedStmtFuncFound_ = true;
       return false;
     }
diff --git a/flang/test/Semantics/stmt-func02.f90 b/flang/test/Semantics/stmt-func02.f90
index 90a89e93530c24b..0f4e8c034f659a1 100644
--- a/flang/test/Semantics/stmt-func02.f90
+++ b/flang/test/Semantics/stmt-func02.f90
@@ -25,4 +25,23 @@ subroutine test3
     !ERROR: 'sf' has not been declared as an array or pointer-valued function
     sf(x) = 4.
   end
+  function f()
+    !ERROR: Recursive call to 'f' requires a distinct RESULT in its declaration
+    !ERROR: Left-hand side of assignment is not definable
+    !BECAUSE: 'f()' is not a variable or pointer
+    f() = 1. ! statement function of same name as function
+  end
+  function g() result(r)
+    !WARNING: Name 'g' from host scope should have a type declaration before its local statement function definition
+    !ERROR: 'g' is already declared in this scoping unit
+    g() = 1. ! statement function of same name as function
+  end
+  function h1() result(r)
+    !ERROR: 'r' is not a callable procedure
+    r() = 1. ! statement function of same name as function result
+  end
+  function h2() result(r)
+    procedure(real), pointer :: r
+    r() = 1. ! not a statement function
+  end
 end

…ion result)

A function can't return a statement function, so an apparent attempt
to define a statement function with the same name as the function's
result must be a misparsed assignment statement.
Copy link
Contributor

@jeanPerier jeanPerier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@klausler klausler merged commit a4745ff into llvm:main Nov 30, 2023
3 checks passed
@klausler klausler deleted the bug1442 branch November 30, 2023 21:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:semantics flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants