[flang-commits] [flang] [flang] Fix crash in semantics on error	case (PR #91482)
    Peter Klausler via flang-commits 
    flang-commits at lists.llvm.org
       
    Wed May  8 07:55:31 PDT 2024
    
    
  
https://github.com/klausler created https://github.com/llvm/llvm-project/pull/91482
An erroneous statement function declaration exposed an unhandled situation in a utility routine in semantics.  Patch that hole and add a test.
Fixes https://github.com/llvm/llvm-project/issues/91429.
>From 431d44e91f7d2e32c43d65d6de69dc8608b86d6a Mon Sep 17 00:00:00 2001
From: Peter Klausler <pklausler at nvidia.com>
Date: Wed, 8 May 2024 07:47:56 -0700
Subject: [PATCH] [flang] Fix crash in semantics on error case
An erroneous statement function declaration exposed an unhandled
situation in a utility routine in semantics.  Patch that hole
and add a test.
Fixes https://github.com/llvm/llvm-project/issues/91429.
---
 flang/lib/Semantics/tools.cpp        | 14 ++++++++------
 flang/test/Semantics/stmt-func01.f90 |  8 ++++++++
 2 files changed, 16 insertions(+), 6 deletions(-)
diff --git a/flang/lib/Semantics/tools.cpp b/flang/lib/Semantics/tools.cpp
index 2d0caff82eb2b..99381918fc638 100644
--- a/flang/lib/Semantics/tools.cpp
+++ b/flang/lib/Semantics/tools.cpp
@@ -256,15 +256,17 @@ static const Symbol &FollowHostAssoc(const Symbol &symbol) {
 }
 
 bool IsHostAssociated(const Symbol &symbol, const Scope &scope) {
-  return DoesScopeContain(
-      &GetProgramUnitOrBlockConstructContaining(FollowHostAssoc(symbol)),
-      GetProgramUnitOrBlockConstructContaining(scope));
+  const Symbol &base{FollowHostAssoc(symbol)};
+  return base.owner().IsTopLevel() ||
+      DoesScopeContain(&GetProgramUnitOrBlockConstructContaining(base),
+          GetProgramUnitOrBlockConstructContaining(scope));
 }
 
 bool IsHostAssociatedIntoSubprogram(const Symbol &symbol, const Scope &scope) {
-  return DoesScopeContain(
-      &GetProgramUnitOrBlockConstructContaining(FollowHostAssoc(symbol)),
-      GetProgramUnitContaining(scope));
+  const Symbol &base{FollowHostAssoc(symbol)};
+  return base.owner().IsTopLevel() ||
+      DoesScopeContain(&GetProgramUnitOrBlockConstructContaining(base),
+          GetProgramUnitContaining(scope));
 }
 
 bool IsInStmtFunction(const Symbol &symbol) {
diff --git a/flang/test/Semantics/stmt-func01.f90 b/flang/test/Semantics/stmt-func01.f90
index 733a7a56dfdb2..3c9ffa565900a 100644
--- a/flang/test/Semantics/stmt-func01.f90
+++ b/flang/test/Semantics/stmt-func01.f90
@@ -83,3 +83,11 @@ subroutine s4
   !ERROR: VOLATILE attribute may apply only to a variable
   sf(x) = 1.
 end
+
+subroutine s5
+  !ERROR: Invalid specification expression: reference to impure function 'k'
+  real x(k())
+  !WARNING: Name 'k' from host scope should have a type declaration before its local statement function definition
+  !ERROR: 'k' is already declared in this scoping unit
+  k() = 0.0
+end
    
    
More information about the flang-commits
mailing list