[flang-commits] [flang] 034bab4 - [flang] Diagnose invalid initializations

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Sat Jan 28 17:00:41 PST 2023


Author: Peter Klausler
Date: 2023-01-28T17:00:28-08:00
New Revision: 034bab4cc83d86cfb334bada488b88bf761a1e71

URL: https://github.com/llvm/llvm-project/commit/034bab4cc83d86cfb334bada488b88bf761a1e71
DIFF: https://github.com/llvm/llvm-project/commit/034bab4cc83d86cfb334bada488b88bf761a1e71.diff

LOG: [flang] Diagnose invalid initializations

f18 current ignores attempts to initialize (with =expr) things
that are not objects, or allows meaningless initializations of
things that have mistakenly been promoted to be objects.
Fix by refusing to promote to objects names that have any
attributes that cannot be applied to objects, and then catch
data initializations of symbols that are not objects.

Differential Revision: https://reviews.llvm.org/D142766

Added: 
    

Modified: 
    flang/lib/Semantics/resolve-names.cpp
    flang/test/Semantics/init01.f90

Removed: 
    


################################################################################
diff  --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index eebca113fb32..dfefd9e7e3a0 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -2510,8 +2510,16 @@ bool ScopeHandler::ConvertToObjectEntity(Symbol &symbol) {
   if (symbol.has<ObjectEntityDetails>()) {
     // nothing to do
   } else if (symbol.has<UnknownDetails>()) {
+    // These are attributes that a name could have picked up from
+    // an attribute statement or type declaration statement.
+    if (symbol.attrs().HasAny({Attr::EXTERNAL, Attr::INTRINSIC})) {
+      return false;
+    }
     symbol.set_details(ObjectEntityDetails{});
   } else if (auto *details{symbol.detailsIf<EntityDetails>()}) {
+    if (symbol.attrs().HasAny({Attr::EXTERNAL, Attr::INTRINSIC})) {
+      return false;
+    }
     funcResultStack_.CompleteTypeIfFunctionResult(symbol);
     symbol.set_details(ObjectEntityDetails{std::move(*details)});
   } else if (auto *useDetails{symbol.detailsIf<UseDetails>()}) {
@@ -6970,10 +6978,6 @@ void DeclarationVisitor::Initialization(const parser::Name &name,
     return;
   }
   Symbol &ultimate{name.symbol->GetUltimate()};
-  if (IsAllocatable(ultimate)) {
-    Say(name, "Allocatable object '%s' cannot be initialized"_err_en_US);
-    return;
-  }
   // TODO: check C762 - all bounds and type parameters of component
   // are colons or constant expressions if component is initialized
   common::visit(
@@ -7074,6 +7078,10 @@ void DeclarationVisitor::NonPointerInitialization(
             "'%s' is a pointer but is not initialized like one"_err_en_US);
       } else if (auto *details{ultimate.detailsIf<ObjectEntityDetails>()}) {
         CHECK(!details->init());
+        if (IsAllocatable(ultimate)) {
+          Say(name, "Allocatable object '%s' cannot be initialized"_err_en_US);
+          return;
+        }
         Walk(expr);
         if (ultimate.owner().IsParameterizedDerivedType()) {
           // Save the expression for per-instantiation analysis.
@@ -7084,6 +7092,8 @@ void DeclarationVisitor::NonPointerInitialization(
             details->set_init(std::move(*folded));
           }
         }
+      } else {
+        Say(name, "'%s' is not an object that can be initialized"_err_en_US);
       }
     }
   }

diff  --git a/flang/test/Semantics/init01.f90 b/flang/test/Semantics/init01.f90
index fe48edeb7eea..9f75a8d55673 100644
--- a/flang/test/Semantics/init01.f90
+++ b/flang/test/Semantics/init01.f90
@@ -95,3 +95,14 @@ subroutine components(n)
     end block
   end associate
 end subroutine
+
+subroutine notObjects
+!ERROR: 'x1' is not an object that can be initialized
+  real, external :: x1 = 1.
+!ERROR: 'x2' is not a pointer but is initialized like one
+  real, external :: x2 => sin
+!ERROR: 'x3' is not an object that can be initialized
+  real, intrinsic :: x3 = 1.
+!ERROR: 'x4' is not a pointer but is initialized like one
+  real, intrinsic :: x4 => cos
+end subroutine


        


More information about the flang-commits mailing list