Skip to content

Commit

Permalink
[flang] Always check procedure characterizability
Browse files Browse the repository at this point in the history
When a procedure is defined with a subprogram but never referenced
in a compilation unit, it may not be characterized until lowering,
and any errors in characterization then may crash the compiler.
So always ensure that procedure definitions are characterizable
in declaration checking.

Fixes llvm#91845.
  • Loading branch information
klausler committed May 13, 2024
1 parent 023cdfc commit 6b0a6a8
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 20 deletions.
9 changes: 9 additions & 0 deletions flang/lib/Semantics/check-declarations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1357,6 +1357,15 @@ bool CheckHelper::IsResultOkToDiffer(const FunctionResult &result) {

void CheckHelper::CheckSubprogram(
const Symbol &symbol, const SubprogramDetails &details) {
// Evaluate a procedure definition's characteristics to flush out
// any errors that analysis might expose, in case this subprogram hasn't
// had any calls in this compilation unit that would have validated them.
if (!context_.HasError(symbol) && !details.isDummy() &&
!details.isInterface() && !details.stmtFunction()) {
if (!Procedure::Characterize(symbol, foldingContext_)) {
context_.SetError(symbol);
}
}
if (const Symbol *iface{FindSeparateModuleSubprogramInterface(&symbol)}) {
SubprogramMatchHelper{*this}.Check(symbol, *iface);
}
Expand Down
3 changes: 1 addition & 2 deletions flang/lib/Semantics/resolve-names.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5013,8 +5013,7 @@ bool DeclarationVisitor::HasCycle(
if (procsInCycle.count(*interface) > 0) {
for (const auto &procInCycle : procsInCycle) {
Say(procInCycle->name(),
"The interface for procedure '%s' is recursively "
"defined"_err_en_US,
"The interface for procedure '%s' is recursively defined"_err_en_US,
procInCycle->name());
context().SetError(*procInCycle);
}
Expand Down
2 changes: 2 additions & 0 deletions flang/test/Semantics/entry01.f90
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ function ifunc()
!ERROR: 'ibad1' is already declared in this scoping unit
entry ibad1() result(ibad1res) ! C1570
!ERROR: 'ibad2' is already declared in this scoping unit
!ERROR: Procedure 'ibad2' is referenced before being sufficiently defined in a context where it must be so
entry ibad2()
!ERROR: ENTRY in a function may not have an alternate return dummy argument
entry ibadalt(*) ! C1573
Expand All @@ -91,6 +92,7 @@ function ifunc()
entry iok()
!ERROR: Explicit RESULT('iok') of function 'isameres2' cannot have the same name as a distinct ENTRY into the same scope
entry isameres2() result(iok) ! C1574
!ERROR: Procedure 'iok2' is referenced before being sufficiently defined in a context where it must be so
!ERROR: Explicit RESULT('iok2') of function 'isameres3' cannot have the same name as a distinct ENTRY into the same scope
entry isameres3() result(iok2) ! C1574
!ERROR: 'iok2' is already declared in this scoping unit
Expand Down
23 changes: 5 additions & 18 deletions flang/test/Semantics/resolve102.f90
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@
!ERROR: Procedure 'sub' is recursively defined. Procedures in the cycle: 'sub', 'p2'
subroutine sub(p2)
PROCEDURE(sub) :: p2

call sub()
end subroutine

subroutine circular
!ERROR: Procedure 'p' is recursively defined. Procedures in the cycle: 'p', 'sub', 'p2'
procedure(sub) :: p

call p(sub)

contains
!ERROR: Procedure 'sub' is recursively defined. Procedures in the cycle: 'p', 'sub', 'p2'
subroutine sub(p2)
procedure(p) :: p2
end subroutine
Expand All @@ -41,11 +36,10 @@ subroutine sub(p2)

subroutine mutual
Procedure(sub1) :: p

Call p(sub)

contains
!ERROR: Procedure 'sub1' is recursively defined. Procedures in the cycle: 'p', 'sub1', 'arg'
!ERROR: Procedure 'sub1' is recursively defined. Procedures in the cycle: 'sub1', 'arg', 'sub', 'p2'
!ERROR: Procedure 'sub1' is recursively defined. Procedures in the cycle: 'sub1', 'arg'
Subroutine sub1(arg)
procedure(sub1) :: arg
End Subroutine
Expand All @@ -57,15 +51,14 @@ Subroutine sub(p2)

subroutine mutual1
Procedure(sub1) :: p

Call p(sub)

contains
!ERROR: Procedure 'sub1' is recursively defined. Procedures in the cycle: 'p', 'sub1', 'arg', 'sub', 'p2'
!ERROR: Procedure 'sub1' is recursively defined. Procedures in the cycle: 'sub1', 'arg', 'sub', 'p2'
Subroutine sub1(arg)
procedure(sub) :: arg
End Subroutine

!ERROR: Procedure 'sub' is recursively defined. Procedures in the cycle: 'sub1', 'arg', 'sub', 'p2'
Subroutine sub(p2)
Procedure(sub1) :: p2
End Subroutine
Expand All @@ -76,8 +69,6 @@ subroutine twoCycle
!ERROR: The interface for procedure 'p2' is recursively defined
procedure(p1) p2
procedure(p2) p1
call p1
call p2
end subroutine

subroutine threeCycle
Expand All @@ -87,9 +78,6 @@ subroutine threeCycle
!ERROR: The interface for procedure 'p3' is recursively defined
procedure(p2) p3
procedure(p3) p1
call p1
call p2
call p3
end subroutine

module mutualSpecExprs
Expand Down Expand Up @@ -118,4 +106,3 @@ function ifunc(x)
ifunc = x
end
end

0 comments on commit 6b0a6a8

Please sign in to comment.