[flang-commits] [PATCH] D142766: [flang] Diagnose invalid initializations

Peter Klausler via Phabricator via flang-commits flang-commits at lists.llvm.org
Fri Jan 27 09:50:54 PST 2023


klausler created this revision.
klausler added a reviewer: PeteSteinfeld.
klausler added a project: Flang.
Herald added subscribers: sunshaoce, jdoerfert.
Herald added a project: All.
klausler requested review of this revision.

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.


https://reviews.llvm.org/D142766

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


Index: flang/test/Semantics/init01.f90
===================================================================
--- flang/test/Semantics/init01.f90
+++ flang/test/Semantics/init01.f90
@@ -95,3 +95,14 @@
     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
Index: flang/lib/Semantics/resolve-names.cpp
===================================================================
--- flang/lib/Semantics/resolve-names.cpp
+++ flang/lib/Semantics/resolve-names.cpp
@@ -2494,8 +2494,16 @@
   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>()}) {
@@ -6900,10 +6908,6 @@
     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(
@@ -7004,6 +7008,10 @@
             "'%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.
@@ -7014,6 +7022,8 @@
             details->set_init(std::move(*folded));
           }
         }
+      } else {
+        Say(name, "'%s' is not an object that can be initialized"_err_en_US);
       }
     }
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D142766.492824.patch
Type: text/x-patch
Size: 2698 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20230127/7f6fe636/attachment.bin>


More information about the flang-commits mailing list