Skip to content

Commit a4745ff

Browse files
authored
[flang] Detect more misparsed statement functions (same name as funct… (#73852)
…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.
1 parent d3e5c20 commit a4745ff

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

flang/lib/Semantics/check-declarations.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,6 +1357,9 @@ void CheckHelper::CheckSubprogram(
13571357
if (auto msg{evaluate::CheckStatementFunction(
13581358
symbol, *stmtFunction, context_.foldingContext())}) {
13591359
SayWithDeclaration(symbol, std::move(*msg));
1360+
} else if (IsPointer(symbol)) {
1361+
SayWithDeclaration(symbol,
1362+
"A statement function must not have the POINTER attribute"_err_en_US);
13601363
} else if (details.result().flags().test(Symbol::Flag::Implicit)) {
13611364
// 15.6.4 p2 weird requirement
13621365
if (const Symbol *

flang/lib/Semantics/resolve-names.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3531,7 +3531,8 @@ bool SubprogramVisitor::HandleStmtFunction(const parser::StmtFunctionStmt &x) {
35313531
Symbol &ultimate{symbol->GetUltimate()};
35323532
if (ultimate.has<ObjectEntityDetails>() ||
35333533
ultimate.has<AssocEntityDetails>() ||
3534-
CouldBeDataPointerValuedFunction(&ultimate)) {
3534+
CouldBeDataPointerValuedFunction(&ultimate) ||
3535+
(&symbol->owner() == &currScope() && IsFunctionResult(*symbol))) {
35353536
misparsedStmtFuncFound_ = true;
35363537
return false;
35373538
}

flang/test/Semantics/stmt-func01.f90

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,33 @@ subroutine foo
5353
sf13(x) = 2.*x
5454
end subroutine
5555
end
56+
57+
subroutine s0
58+
allocatable :: sf
59+
!ERROR: 'sf' is not a callable procedure
60+
sf(x) = 1.
61+
end
62+
63+
subroutine s1
64+
asynchronous :: sf
65+
!ERROR: An entity may not have the ASYNCHRONOUS attribute unless it is a variable
66+
sf(x) = 1.
67+
end
68+
69+
subroutine s2
70+
pointer :: sf
71+
!ERROR: A statement function must not have the POINTER attribute
72+
sf(x) = 1.
73+
end
74+
75+
subroutine s3
76+
save :: sf
77+
!ERROR: The entity 'sf' with an explicit SAVE attribute must be a variable, procedure pointer, or COMMON block
78+
sf(x) = 1.
79+
end
80+
81+
subroutine s4
82+
volatile :: sf
83+
!ERROR: VOLATILE attribute may apply only to a variable
84+
sf(x) = 1.
85+
end

flang/test/Semantics/stmt-func02.f90

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,23 @@ subroutine test3
2525
!ERROR: 'sf' has not been declared as an array or pointer-valued function
2626
sf(x) = 4.
2727
end
28+
function f()
29+
!ERROR: Recursive call to 'f' requires a distinct RESULT in its declaration
30+
!ERROR: Left-hand side of assignment is not definable
31+
!BECAUSE: 'f()' is not a variable or pointer
32+
f() = 1. ! statement function of same name as function
33+
end
34+
function g() result(r)
35+
!WARNING: Name 'g' from host scope should have a type declaration before its local statement function definition
36+
!ERROR: 'g' is already declared in this scoping unit
37+
g() = 1. ! statement function of same name as function
38+
end
39+
function h1() result(r)
40+
!ERROR: 'r' is not a callable procedure
41+
r() = 1. ! statement function of same name as function result
42+
end
43+
function h2() result(r)
44+
procedure(real), pointer :: r
45+
r() = 1. ! not a statement function
46+
end
2847
end

0 commit comments

Comments
 (0)