Skip to content

[flang][Semantics] set scope even for module subroutines outside modules #109009

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 18, 2024

Conversation

tblah
Copy link
Contributor

@tblah tblah commented Sep 17, 2024

The missing scope information led to a crash in OpenMP semantic checks run before printing the error that was already discovered in the code.

The following block has to be skipped for this invalid code so that we don't emit a second spurious error.

Fixes #82913

The missing scope information led to a crash in OpenMP semantic checks
run before printing the error that was already discovered in the code.

The following block has to be skipped for this invalid code so that we
don't emit a second spurious error.

Fixes llvm#82913
@llvmbot
Copy link
Member

llvmbot commented Sep 17, 2024

@llvm/pr-subscribers-flang-semantics

@llvm/pr-subscribers-flang-openmp

Author: Tom Eccles (tblah)

Changes

The missing scope information led to a crash in OpenMP semantic checks run before printing the error that was already discovered in the code.

The following block has to be skipped for this invalid code so that we don't emit a second spurious error.

Fixes #82913


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

2 Files Affected:

  • (modified) flang/lib/Semantics/resolve-names.cpp (+5-2)
  • (added) flang/test/Semantics/OpenMP/bad_module_subroutine.f90 (+6)
diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index d8f601212d8d01..eb57ae7a64ec16 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -4344,15 +4344,18 @@ bool SubprogramVisitor::BeginSubprogram(const parser::Name &name,
     Symbol::Flag subpFlag, bool hasModulePrefix,
     const parser::LanguageBindingSpec *bindingSpec,
     const ProgramTree::EntryStmtList *entryStmts) {
+  bool isValid{true};
   if (hasModulePrefix && !currScope().IsModule() &&
       !currScope().IsSubmodule()) { // C1547
     Say(name,
         "'%s' is a MODULE procedure which must be declared within a "
         "MODULE or SUBMODULE"_err_en_US);
-    return false;
+    // Don't return here because it can be useful to have the scope set for
+    // other semantic checks run before we print the errors
+    isValid = false;
   }
   Symbol *moduleInterface{nullptr};
-  if (hasModulePrefix && !inInterfaceBlock()) {
+  if (isValid && hasModulePrefix && !inInterfaceBlock()) {
     moduleInterface = FindSeparateModuleProcedureInterface(name);
     if (moduleInterface && &moduleInterface->owner() == &currScope()) {
       // Subprogram is MODULE FUNCTION or MODULE SUBROUTINE with an interface
diff --git a/flang/test/Semantics/OpenMP/bad_module_subroutine.f90 b/flang/test/Semantics/OpenMP/bad_module_subroutine.f90
new file mode 100644
index 00000000000000..339d6bf27e7dfd
--- /dev/null
+++ b/flang/test/Semantics/OpenMP/bad_module_subroutine.f90
@@ -0,0 +1,6 @@
+! RUN: %python %S/../test_errors.py %s %flang_fc1 -fopenmp
+! Test that we don't crash on this code inside of openmp semantics checks
+
+!ERROR: 'e' is a MODULE procedure which must be declared within a MODULE or SUBMODULE
+impure elemental module subroutine e()
+end subroutine

Copy link
Member

@Thirumalai-Shaktivel Thirumalai-Shaktivel left a comment

Choose a reason for hiding this comment

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

LGTM!

@tblah tblah merged commit 41d5fed into llvm:main Sep 18, 2024
12 checks passed
tmsri pushed a commit to tmsri/llvm-project that referenced this pull request Sep 19, 2024
…les (llvm#109009)

The missing scope information led to a crash in OpenMP semantic checks
run before printing the error that was already discovered in the code.

The following block has to be skipped for this invalid code so that we
don't emit a second spurious error.

Fixes llvm#82913
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:openmp 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][OpenMP] fatal internal error: SemanticsContext::FindScope(): invalid source location for 'end subroutine'
4 participants