From f2360e1156d086ac5ac58d7f29449adf9e75d716 Mon Sep 17 00:00:00 2001 From: peter klausler Date: Tue, 19 Oct 2021 13:49:21 -0700 Subject: [PATCH] [flang] Enforce rest of semantic constraint C919 A reference to an allocatable or pointer component must be applied to a scalar base object. (This is the second part of constraint C919; the first part is already checked.) Differential Revision: https://reviews.llvm.org/D112241 --- flang/include/flang/Semantics/expression.h | 2 ++ flang/lib/Semantics/expression.cpp | 9 ++++++--- flang/test/Semantics/deallocate01.f90 | 17 +++++++++-------- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/flang/include/flang/Semantics/expression.h b/flang/include/flang/Semantics/expression.h index cf200ac..74e1b96 100644 --- a/flang/include/flang/Semantics/expression.h +++ b/flang/include/flang/Semantics/expression.h @@ -317,6 +317,8 @@ private: const parser::SectionSubscript &); std::vector AnalyzeSectionSubscripts( const std::list &); + std::optional CreateComponent( + DataRef &&, const Symbol &, const semantics::Scope &); MaybeExpr Designate(DataRef &&); MaybeExpr CompleteSubscripts(ArrayRef &&); MaybeExpr ApplySubscripts(DataRef &&, std::vector &&); diff --git a/flang/lib/Semantics/expression.cpp b/flang/lib/Semantics/expression.cpp index 00b34c3..0f8eef3 100644 --- a/flang/lib/Semantics/expression.cpp +++ b/flang/lib/Semantics/expression.cpp @@ -235,7 +235,7 @@ MaybeExpr ExpressionAnalyzer::CompleteSubscripts(ArrayRef &&ref) { for (const auto &expr : ref.subscript()) { subscriptRank += expr.Rank(); } - if (subscriptRank > 0) { + if (subscriptRank > 0) { // C919a Say("Subscripts of component '%s' of rank-%d derived type " "array have rank %d but must all be scalar"_err_en_US, symbol.name(), baseRank, subscriptRank); @@ -292,7 +292,7 @@ MaybeExpr ExpressionAnalyzer::TopLevelChecks(DataRef &&dataRef) { int componentRank{symbol.Rank()}; if (componentRank > 0) { int baseRank{component->base().Rank()}; - if (baseRank > 0) { + if (baseRank > 0) { // C919a Say("Reference to whole rank-%d component '%%%s' of " "rank-%d array of derived type is not allowed"_err_en_US, componentRank, symbol.name(), baseRank); @@ -972,8 +972,11 @@ static NamedEntity IgnoreAnySubscripts(Designator &&designator) { } // Components of parent derived types are explicitly represented as such. -static std::optional CreateComponent( +std::optional ExpressionAnalyzer::CreateComponent( DataRef &&base, const Symbol &component, const semantics::Scope &scope) { + if (IsAllocatableOrPointer(component) && base.Rank() > 0) { // C919b + Say("An allocatable or pointer component reference must be applied to a scalar base"_err_en_US); + } if (&component.owner() == &scope) { return Component{std::move(base), component}; } diff --git a/flang/test/Semantics/deallocate01.f90 b/flang/test/Semantics/deallocate01.f90 index 88c29d0..b224f30 100644 --- a/flang/test/Semantics/deallocate01.f90 +++ b/flang/test/Semantics/deallocate01.f90 @@ -29,20 +29,21 @@ Deallocate(pi) Deallocate(z%p) +!ERROR: An allocatable or pointer component reference must be applied to a scalar base Deallocate(x%p, stat=s, errmsg=e) -Deallocate(x%p, errmsg=e) -Deallocate(x%p, stat=s) +Deallocate(x, errmsg=e) +Deallocate(x, stat=s) -Deallocate(y%p, stat=s, errmsg=e) -Deallocate(y%p, errmsg=e) -Deallocate(y%p, stat=s) +Deallocate(y, stat=s, errmsg=e) +Deallocate(y, errmsg=e) +Deallocate(y, stat=s) Deallocate(z, stat=s, errmsg=e) Deallocate(z, errmsg=e) Deallocate(z, stat=s) -Deallocate(z, y%p, stat=s, errmsg=e) -Deallocate(z, y%p, errmsg=e) -Deallocate(z, y%p, stat=s) +Deallocate(z, y, stat=s, errmsg=e) +Deallocate(z, y, errmsg=e) +Deallocate(z, y, stat=s) End Program -- 2.7.4