Skip to content

Commit

Permalink
[flang] Catch structure constructor in its own type definition (llvm#…
Browse files Browse the repository at this point in the history
…102241)

The check for a structure constructor to a forward-referenced derived
type wasn't tripping for constructors in the type definition itself. Set
the forward reference flag unconditionally at the beginning of name
resolution for the type definition.
  • Loading branch information
klausler authored Aug 8, 2024
1 parent 25822dc commit 245eb0a
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 8 deletions.
3 changes: 1 addition & 2 deletions flang/lib/Semantics/expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3026,8 +3026,7 @@ const Symbol *AssumedTypeDummy<parser::PointerObject>(
bool ExpressionAnalyzer::CheckIsValidForwardReference(
const semantics::DerivedTypeSpec &dtSpec) {
if (dtSpec.IsForwardReferenced()) {
Say("Cannot construct value for derived type '%s' "
"before it is defined"_err_en_US,
Say("Cannot construct value for derived type '%s' before it is defined"_err_en_US,
dtSpec.name());
return false;
}
Expand Down
7 changes: 2 additions & 5 deletions flang/lib/Semantics/resolve-names.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5507,11 +5507,8 @@ void DeclarationVisitor::Post(const parser::DerivedTypeStmt &x) {
std::optional<DerivedTypeSpec> extendsType{
ResolveExtendsType(name, extendsName)};
DerivedTypeDetails derivedTypeDetails;
if (Symbol * typeSymbol{FindInScope(currScope(), name)}; typeSymbol &&
typeSymbol->has<DerivedTypeDetails>() &&
typeSymbol->get<DerivedTypeDetails>().isForwardReferenced()) {
derivedTypeDetails.set_isForwardReferenced(true);
}
// Catch any premature structure constructors within the definition
derivedTypeDetails.set_isForwardReferenced(true);
auto &symbol{MakeSymbol(name, GetAttrs(), std::move(derivedTypeDetails))};
symbol.ReplaceName(name.source);
derivedTypeInfo_.type = &symbol;
Expand Down
3 changes: 2 additions & 1 deletion flang/test/Semantics/bad-forward-type.f90
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ subroutine s8
!ERROR: Cannot construct value for derived type 't2' before it is defined
parameter(y=t2(12.3))
type t2
real :: c
!ERROR: Cannot construct value for derived type 't2' before it is defined
real :: c = transfer(t2(),0.)
end type
end subroutine

Expand Down

0 comments on commit 245eb0a

Please sign in to comment.