Skip to content

Commit

Permalink
[flang] Catch obscure structure constructor error
Browse files Browse the repository at this point in the history
A scalar value in a structure constructor may correspond to an
array component in the derived type only when that component has
a shape to which the scalar value may be expanded.

Differential Revision: https://reviews.llvm.org/D143822
  • Loading branch information
klausler committed Feb 13, 2023
1 parent ee88f11 commit b57bc15
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
5 changes: 3 additions & 2 deletions flang/lib/Semantics/expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2024,8 +2024,9 @@ MaybeExpr ExpressionAnalyzer::Analyze(
"component", "value")};
if (checked && *checked && GetRank(*componentShape) > 0 &&
GetRank(*valueShape) == 0 &&
!IsExpandableScalar(*converted, GetFoldingContext(),
*componentShape, true /*admit PURE call*/)) {
(IsDeferredShape(*symbol) ||
!IsExpandableScalar(*converted, GetFoldingContext(),
*componentShape, true /*admit PURE call*/))) {
AttachDeclaration(
Say(expr.source,
"Scalar value cannot be expanded to shape of array component '%s'"_err_en_US,
Expand Down
2 changes: 1 addition & 1 deletion flang/test/Semantics/data01.f90
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module m1
integer :: myAge = 2
type(person) associated
type hasAlloc
integer, allocatable :: a(:)
integer, allocatable :: a
end type
end

Expand Down
9 changes: 9 additions & 0 deletions flang/test/Semantics/structconst06.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
! RUN: %python %S/test_errors.py %s %flang_fc1
! Don't expand scalars for allocatable components.
module m
type t
real, allocatable :: a(:)
end type
!ERROR: Scalar value cannot be expanded to shape of array component 'a'
type(t) :: x = t(0.)
end module

0 comments on commit b57bc15

Please sign in to comment.