[flang] Better error recovery for missing THEN in ELSE IF
The THEN keyword in the "ELSE IF (test) THEN" statement is useless
syntactically, and to omit it is a common error (at least for me!)
that has poor error recovery. This patch changes the parser to
cough up a simple "expected 'THEN'" and still recognize the rest of
the IF construct.
Differential Revision: https://reviews.llvm.org/D110952
GitOrigin-RevId: 0061e681a3296ac9016891729e03e38ebb235bdc
diff --git a/lib/Evaluate/check-expression.cpp b/lib/Evaluate/check-expression.cpp
index cb910ca..2e0cc80 100644
--- a/lib/Evaluate/check-expression.cpp
+++ b/lib/Evaluate/check-expression.cpp
@@ -93,7 +93,7 @@
}
bool IsConstantExprHelper::operator()(const ProcedureRef &call) const {
- // LBOUND, UBOUND, and SIZE with DIM= arguments will have been reritten
+ // LBOUND, UBOUND, and SIZE with DIM= arguments will have been rewritten
// into DescriptorInquiry operations.
if (const auto *intrinsic{std::get_if<SpecificIntrinsic>(&call.proc().u)}) {
if (intrinsic->name == "kind" ||
diff --git a/lib/Parser/executable-parsers.cpp b/lib/Parser/executable-parsers.cpp
index a0b5cf2..66b217a 100644
--- a/lib/Parser/executable-parsers.cpp
+++ b/lib/Parser/executable-parsers.cpp
@@ -310,7 +310,7 @@
many(construct<IfConstruct::ElseIfBlock>(
unambiguousStatement(construct<ElseIfStmt>(
"ELSE IF" >> parenthesized(scalarLogicalExpr),
- "THEN" >> maybe(name))),
+ recovery("THEN"_tok, ok) >> maybe(name))),
block)),
maybe(construct<IfConstruct::ElseBlock>(
statement(construct<ElseStmt>("ELSE" >> maybe(name))), block)),
diff --git a/test/Parser/elseif-then.f90 b/test/Parser/elseif-then.f90
new file mode 100644
index 0000000..29ec164
--- /dev/null
+++ b/test/Parser/elseif-then.f90
@@ -0,0 +1,8 @@
+! RUN: not %flang_fc1 -fsyntax-only %s 2>&1 | FileCheck %s
+! CHECK-NOT: expected '=>'
+! CHECK: error: expected 'THEN'
+if (.false.) then
+else if (.false.)
+else
+end if
+end