Skip to content

Conversation

klausler
Copy link
Contributor

When a DEC legacy STRUCTURE definition appears within another, its STRUCTURE statement must also declare some components of the enclosing structure.

Fixes #99288.

When a DEC legacy STRUCTURE definition appears within another,
its STRUCTURE statement must also declare some components of
the enclosing structure.

Fixes llvm#99288.
@llvmbot
Copy link
Member

llvmbot commented Jul 17, 2024

@llvm/pr-subscribers-flang-semantics

@llvm/pr-subscribers-flang-parser

Author: Peter Klausler (klausler)

Changes

When a DEC legacy STRUCTURE definition appears within another, its STRUCTURE statement must also declare some components of the enclosing structure.

Fixes #99288.


Full diff: https://github.com/llvm/llvm-project/pull/99379.diff

2 Files Affected:

  • (modified) flang/lib/Parser/Fortran-parsers.cpp (+12-4)
  • (added) flang/test/Semantics/struct03.f90 (+7)
diff --git a/flang/lib/Parser/Fortran-parsers.cpp b/flang/lib/Parser/Fortran-parsers.cpp
index 746d04ad649d1..0bdc4c4e033c7 100644
--- a/flang/lib/Parser/Fortran-parsers.cpp
+++ b/flang/lib/Parser/Fortran-parsers.cpp
@@ -1304,10 +1304,16 @@ TYPE_PARSER(extension<LanguageFeature::CUDA>(construct<CUDAAttributesStmt>(
     defaulted(
         maybe("::"_tok) >> nonemptyList("expected names"_err_en_US, name)))))
 
-// Subtle: the name includes the surrounding slashes, which avoids
+// Subtle: A structure's name includes the surrounding slashes, which avoids
 // clashes with other uses of the name in the same scope.
-TYPE_PARSER(construct<StructureStmt>(
-    "STRUCTURE" >> maybe(sourced("/" >> name / "/")), optionalList(entityDecl)))
+constexpr auto structureName{maybe(sourced("/" >> name / "/"))};
+
+// Note that Parser<StructureStmt>{} has a mandatory list of entity-decls
+// and is used only by NestedStructureStmt{}.Parse() in user-state.cpp.
+TYPE_PARSER(construct<StructureStmt>("STRUCTURE" >> structureName,
+    localRecovery(
+        "entity declarations are required on a nested structure"_err_en_US,
+        nonemptyList(entityDecl), ok)))
 
 constexpr auto nestedStructureDef{
     CONTEXT_PARSER("nested STRUCTURE definition"_en_US,
@@ -1323,7 +1329,9 @@ TYPE_PARSER(construct<StructureField>(statement(StructureComponents{})) ||
 TYPE_CONTEXT_PARSER("STRUCTURE definition"_en_US,
     extension<LanguageFeature::DECStructures>(
         "nonstandard usage: STRUCTURE"_port_en_US,
-        construct<StructureDef>(statement(Parser<StructureStmt>{}),
+        construct<StructureDef>(
+            statement(construct<StructureStmt>(
+                "STRUCTURE" >> structureName, optionalList(entityDecl))),
             many(Parser<StructureField>{}),
             statement(construct<StructureDef::EndStructureStmt>(
                 "END STRUCTURE"_tok)))))
diff --git a/flang/test/Semantics/struct03.f90 b/flang/test/Semantics/struct03.f90
new file mode 100644
index 0000000000000..a334cca8945fc
--- /dev/null
+++ b/flang/test/Semantics/struct03.f90
@@ -0,0 +1,7 @@
+! RUN: %python %S/test_errors.py %s %flang_fc1
+  structure /s/
+    !ERROR: entity declarations are required on a nested structure
+    structure /nested/
+    end structure
+  end structure
+end

Copy link
Contributor

@psteinfeld psteinfeld left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All builds and tests correctly and looks good.

@klausler klausler merged commit 0684db3 into llvm:main Jul 18, 2024
@klausler klausler deleted the bug99288 branch July 18, 2024 23:32
yuxuanchen1997 pushed a commit that referenced this pull request Jul 25, 2024
Summary:
When a DEC legacy STRUCTURE definition appears within another, its
STRUCTURE statement must also declare some components of the enclosing
structure.

Fixes #99288.

Test Plan: 

Reviewers: 

Subscribers: 

Tasks: 

Tags: 


Differential Revision: https://phabricator.intern.facebook.com/D60251397
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:parser flang:semantics flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Flang] No error checking when nested structure statement has no field name
3 participants