[flang] Fix crash in error recovery (#140768)

When a TYPE(*) dummy argument is erroneously used as a component value
in a structure constructor, semantics crashes if the structure
constructor had been initially parsed as a potential function reference.
Clean out stale typed expressions when reanalyzing the reconstructed
parse subtree to ensure that errors are caught the next time around.
This commit is contained in:
Peter Klausler 2025-05-28 13:59:48 -07:00 committed by GitHub
parent 4c6b60a639
commit ff8288442d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 1 deletions

View File

@ -3376,6 +3376,10 @@ MaybeExpr ExpressionAnalyzer::Analyze(const parser::FunctionReference &funcRef,
auto &mutableRef{const_cast<parser::FunctionReference &>(funcRef)};
*structureConstructor =
mutableRef.ConvertToStructureConstructor(type.derivedTypeSpec());
// Don't use saved typed expressions left over from argument
// analysis; they might not be valid structure components
// (e.g., a TYPE(*) argument)
auto restorer{DoNotUseSavedTypedExprs()};
return Analyze(structureConstructor->value());
}
}
@ -4058,7 +4062,7 @@ MaybeExpr ExpressionAnalyzer::ExprOrVariable(
// first to be sure.
std::optional<parser::StructureConstructor> ctor;
result = Analyze(funcRef->value(), &ctor);
if (result && ctor) {
if (ctor) {
// A misparsed function reference is really a structure
// constructor. Repair the parse tree in situ.
const_cast<PARSED &>(x).u = std::move(*ctor);

View File

@ -0,0 +1,10 @@
! RUN: %python %S/test_errors.py %s %flang_fc1
! Regression test for crash
subroutine sub(xx)
type(*) :: xx
type ty
end type
type(ty) obj
!ERROR: TYPE(*) dummy argument may only be used as an actual argument
obj = ty(xx)
end