diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp index eb61a76f10874..87b6130880808 100644 --- a/flang/lib/Semantics/resolve-names.cpp +++ b/flang/lib/Semantics/resolve-names.cpp @@ -3711,13 +3711,17 @@ bool SubprogramVisitor::Pre(const parser::Suffix &suffix) { bool SubprogramVisitor::Pre(const parser::PrefixSpec &x) { // Save this to process after UseStmt and ImplicitPart if (const auto *parsedType{std::get_if(&x.u)}) { - FuncResultStack::FuncInfo &info{DEREF(funcResultStack().Top())}; - if (info.parsedType) { // C1543 - Say(currStmtSource().value(), - "FUNCTION prefix cannot specify the type more than once"_err_en_US); + if (FuncResultStack::FuncInfo * info{funcResultStack().Top()}) { + if (info->parsedType) { // C1543 + Say(currStmtSource().value(), + "FUNCTION prefix cannot specify the type more than once"_err_en_US); + } else { + info->parsedType = parsedType; + info->source = currStmtSource(); + } } else { - info.parsedType = parsedType; - info.source = currStmtSource(); + Say(currStmtSource().value(), + "SUBROUTINE prefix cannot specify a type"_err_en_US); } return false; } else { diff --git a/flang/test/Semantics/typed-subr.f90 b/flang/test/Semantics/typed-subr.f90 new file mode 100644 index 0000000000000..c6637c95bdfd7 --- /dev/null +++ b/flang/test/Semantics/typed-subr.f90 @@ -0,0 +1,4 @@ +! RUN: %python %S/test_errors.py %s %flang_fc1 +! ERROR: SUBROUTINE prefix cannot specify a type +integer subroutine foo +end