Skip to content

Commit bd8ed64

Browse files
committed
[flang] Fix crash in error recovery
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. Fixes #140794.
1 parent b7e13ab commit bd8ed64

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

flang/lib/Semantics/expression.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3376,6 +3376,10 @@ MaybeExpr ExpressionAnalyzer::Analyze(const parser::FunctionReference &funcRef,
33763376
auto &mutableRef{const_cast<parser::FunctionReference &>(funcRef)};
33773377
*structureConstructor =
33783378
mutableRef.ConvertToStructureConstructor(type.derivedTypeSpec());
3379+
// Don't use saved typed expressions left over from argument
3380+
// analysis; they might not be valid structure components
3381+
// (e.g., a TYPE(*) argument)
3382+
auto restorer{DoNotUseSavedTypedExprs()};
33793383
return Analyze(structureConstructor->value());
33803384
}
33813385
}
@@ -4058,7 +4062,7 @@ MaybeExpr ExpressionAnalyzer::ExprOrVariable(
40584062
// first to be sure.
40594063
std::optional<parser::StructureConstructor> ctor;
40604064
result = Analyze(funcRef->value(), &ctor);
4061-
if (result && ctor) {
4065+
if (ctor) {
40624066
// A misparsed function reference is really a structure
40634067
// constructor. Repair the parse tree in situ.
40644068
const_cast<PARSED &>(x).u = std::move(*ctor);

flang/test/Semantics/bug869.f90

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
! RUN: %python %S/test_errors.py %s %flang_fc1
2+
! Regression test for crash
3+
subroutine sub(xx)
4+
type(*) :: xx
5+
type ty
6+
end type
7+
type(ty) obj
8+
!ERROR: TYPE(*) dummy argument may only be used as an actual argument
9+
obj = ty(xx)
10+
end

0 commit comments

Comments
 (0)