Skip to content

Commit 0684db3

Browse files
authored
[flang] A nested STRUCTURE must declare entities (#99379)
When a DEC legacy STRUCTURE definition appears within another, its STRUCTURE statement must also declare some components of the enclosing structure. Fixes #99288.
1 parent 043aca3 commit 0684db3

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

flang/lib/Parser/Fortran-parsers.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,10 +1304,16 @@ TYPE_PARSER(extension<LanguageFeature::CUDA>(construct<CUDAAttributesStmt>(
13041304
defaulted(
13051305
maybe("::"_tok) >> nonemptyList("expected names"_err_en_US, name)))))
13061306

1307-
// Subtle: the name includes the surrounding slashes, which avoids
1307+
// Subtle: A structure's name includes the surrounding slashes, which avoids
13081308
// clashes with other uses of the name in the same scope.
1309-
TYPE_PARSER(construct<StructureStmt>(
1310-
"STRUCTURE" >> maybe(sourced("/" >> name / "/")), optionalList(entityDecl)))
1309+
constexpr auto structureName{maybe(sourced("/" >> name / "/"))};
1310+
1311+
// Note that Parser<StructureStmt>{} has a mandatory list of entity-decls
1312+
// and is used only by NestedStructureStmt{}.Parse() in user-state.cpp.
1313+
TYPE_PARSER(construct<StructureStmt>("STRUCTURE" >> structureName,
1314+
localRecovery(
1315+
"entity declarations are required on a nested structure"_err_en_US,
1316+
nonemptyList(entityDecl), ok)))
13111317

13121318
constexpr auto nestedStructureDef{
13131319
CONTEXT_PARSER("nested STRUCTURE definition"_en_US,
@@ -1323,7 +1329,9 @@ TYPE_PARSER(construct<StructureField>(statement(StructureComponents{})) ||
13231329
TYPE_CONTEXT_PARSER("STRUCTURE definition"_en_US,
13241330
extension<LanguageFeature::DECStructures>(
13251331
"nonstandard usage: STRUCTURE"_port_en_US,
1326-
construct<StructureDef>(statement(Parser<StructureStmt>{}),
1332+
construct<StructureDef>(
1333+
statement(construct<StructureStmt>(
1334+
"STRUCTURE" >> structureName, optionalList(entityDecl))),
13271335
many(Parser<StructureField>{}),
13281336
statement(construct<StructureDef::EndStructureStmt>(
13291337
"END STRUCTURE"_tok)))))

flang/test/Semantics/struct03.f90

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
! RUN: %python %S/test_errors.py %s %flang_fc1
2+
structure /s/
3+
!ERROR: entity declarations are required on a nested structure
4+
structure /nested/
5+
end structure
6+
end structure
7+
end

0 commit comments

Comments
 (0)