Skip to content

Commit bca1317

Browse files
committed
[flang] Accept structure constructor value for polymorphic component
Semantic analysis was emitting a bogus error message when a structure constructor contains a monomorphic value for a (limited) polymorphic component of a derived type. The type compatibility test was too strict; this patch relaxes it a little to allow values that could be assigned or passed to a variable or dummy argument with that type. Also add some quotes to an error message that was sometimes confusing without them, and remove a repeated space character from another. Differential Revision: https://reviews.llvm.org/D119744
1 parent 793924d commit bca1317

File tree

5 files changed

+17
-8
lines changed

5 files changed

+17
-8
lines changed

flang/lib/Evaluate/tools.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,9 @@ std::optional<Expr<SomeType>> ConvertToType(
653653
break;
654654
case TypeCategory::Derived:
655655
if (auto fromType{x.GetType()}) {
656-
if (type == *fromType) {
656+
if (type.IsTkCompatibleWith(*fromType)) {
657+
// "x" could be assigned or passed to "type", or appear in a
658+
// structure constructor as a value for a component with "type"
657659
return std::move(x);
658660
}
659661
}

flang/lib/Semantics/expression.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1754,16 +1754,16 @@ MaybeExpr ExpressionAnalyzer::Analyze(
17541754
} else if (valueType) {
17551755
AttachDeclaration(
17561756
Say(expr.source,
1757-
"Value in structure constructor of type %s is "
1758-
"incompatible with component '%s' of type %s"_err_en_US,
1757+
"Value in structure constructor of type '%s' is "
1758+
"incompatible with component '%s' of type '%s'"_err_en_US,
17591759
valueType->AsFortran(), symbol->name(),
17601760
symType->AsFortran()),
17611761
*symbol);
17621762
} else {
17631763
AttachDeclaration(
17641764
Say(expr.source,
17651765
"Value in structure constructor is incompatible with "
1766-
" component '%s' of type %s"_err_en_US,
1766+
"component '%s' of type %s"_err_en_US,
17671767
symbol->name(), symType->AsFortran()),
17681768
*symbol);
17691769
}

flang/test/Semantics/data02.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ subroutine s1
66
character(1) :: c
77
end type
88
type(t) :: x
9-
!ERROR: Value in structure constructor of type INTEGER(4) is incompatible with component 'c' of type CHARACTER(KIND=1,LEN=1_8)
9+
!ERROR: Value in structure constructor of type 'INTEGER(4)' is incompatible with component 'c' of type 'CHARACTER(KIND=1,LEN=1_8)'
1010
data x /t(1)/
1111
end
1212

flang/test/Semantics/structconst01.f90

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,18 @@ subroutine polycomponent
7373
class(*), allocatable :: p
7474
end type poly
7575
type(poly) :: x
76+
type :: poly2
77+
class(type1(1)), allocatable :: p1
78+
type(type1(1)), allocatable :: p2
79+
end type poly2
80+
type(type1(1)) :: t1val
81+
type(poly2) :: x2
7682
! These cases are not errors
7783
x = poly(1)
7884
x = poly('hello')
7985
x = poly(type1(1)(123))
80-
!ERROR: Value in structure constructor is incompatible with component 'p' of type CLASS(*)
86+
x2 = poly2(t1val, t1val)
87+
!ERROR: Value in structure constructor is incompatible with component 'p' of type CLASS(*)
8188
x = poly(z'feedface')
8289
end subroutine
8390
end module module1

flang/test/Semantics/structconst02.f90

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ subroutine errors
3232
! call scalararg(scalar(4)(5.,6,(7._8,8._2),4_'b',.true._4))
3333
call scalararg(scalar(4)(ix=5.,rx=6,zx=(7._8,8._2),cx=4_'b',lx=.true.))
3434
call scalararg(scalar(4)(5.,6,(7._8,8._2),4_'b',.true.))
35-
!ERROR: Value in structure constructor of type CHARACTER(1) is incompatible with component 'ix' of type INTEGER(4)
35+
!ERROR: Value in structure constructor of type 'CHARACTER(1)' is incompatible with component 'ix' of type 'INTEGER(4)'
3636
call scalararg(scalar(4)(ix='a'))
37-
!ERROR: Value in structure constructor of type LOGICAL(4) is incompatible with component 'ix' of type INTEGER(4)
37+
!ERROR: Value in structure constructor of type 'LOGICAL(4)' is incompatible with component 'ix' of type 'INTEGER(4)'
3838
call scalararg(scalar(4)(ix=.false.))
3939
!ERROR: Rank-1 array value is not compatible with scalar component 'ix'
4040
call scalararg(scalar(4)(ix=[1]))

0 commit comments

Comments
 (0)