[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
This commit is contained in:
Peter Klausler 2022-02-08 11:56:22 -08:00
parent 793924dd5f
commit bca13174bc
5 changed files with 17 additions and 8 deletions

View file

@ -653,7 +653,9 @@ std::optional<Expr<SomeType>> ConvertToType(
break;
case TypeCategory::Derived:
if (auto fromType{x.GetType()}) {
if (type == *fromType) {
if (type.IsTkCompatibleWith(*fromType)) {
// "x" could be assigned or passed to "type", or appear in a
// structure constructor as a value for a component with "type"
return std::move(x);
}
}

View file

@ -1754,8 +1754,8 @@ MaybeExpr ExpressionAnalyzer::Analyze(
} else if (valueType) {
AttachDeclaration(
Say(expr.source,
"Value in structure constructor of type %s is "
"incompatible with component '%s' of type %s"_err_en_US,
"Value in structure constructor of type '%s' is "
"incompatible with component '%s' of type '%s'"_err_en_US,
valueType->AsFortran(), symbol->name(),
symType->AsFortran()),
*symbol);
@ -1763,7 +1763,7 @@ MaybeExpr ExpressionAnalyzer::Analyze(
AttachDeclaration(
Say(expr.source,
"Value in structure constructor is incompatible with "
" component '%s' of type %s"_err_en_US,
"component '%s' of type %s"_err_en_US,
symbol->name(), symType->AsFortran()),
*symbol);
}

View file

@ -6,7 +6,7 @@ subroutine s1
character(1) :: c
end type
type(t) :: x
!ERROR: Value in structure constructor of type INTEGER(4) is incompatible with component 'c' of type CHARACTER(KIND=1,LEN=1_8)
!ERROR: Value in structure constructor of type 'INTEGER(4)' is incompatible with component 'c' of type 'CHARACTER(KIND=1,LEN=1_8)'
data x /t(1)/
end

View file

@ -73,11 +73,18 @@ module module1
class(*), allocatable :: p
end type poly
type(poly) :: x
type :: poly2
class(type1(1)), allocatable :: p1
type(type1(1)), allocatable :: p2
end type poly2
type(type1(1)) :: t1val
type(poly2) :: x2
! These cases are not errors
x = poly(1)
x = poly('hello')
x = poly(type1(1)(123))
!ERROR: Value in structure constructor is incompatible with component 'p' of type CLASS(*)
x2 = poly2(t1val, t1val)
!ERROR: Value in structure constructor is incompatible with component 'p' of type CLASS(*)
x = poly(z'feedface')
end subroutine
end module module1

View file

@ -32,9 +32,9 @@ module module1
! call scalararg(scalar(4)(5.,6,(7._8,8._2),4_'b',.true._4))
call scalararg(scalar(4)(ix=5.,rx=6,zx=(7._8,8._2),cx=4_'b',lx=.true.))
call scalararg(scalar(4)(5.,6,(7._8,8._2),4_'b',.true.))
!ERROR: Value in structure constructor of type CHARACTER(1) is incompatible with component 'ix' of type INTEGER(4)
!ERROR: Value in structure constructor of type 'CHARACTER(1)' is incompatible with component 'ix' of type 'INTEGER(4)'
call scalararg(scalar(4)(ix='a'))
!ERROR: Value in structure constructor of type LOGICAL(4) is incompatible with component 'ix' of type INTEGER(4)
!ERROR: Value in structure constructor of type 'LOGICAL(4)' is incompatible with component 'ix' of type 'INTEGER(4)'
call scalararg(scalar(4)(ix=.false.))
!ERROR: Rank-1 array value is not compatible with scalar component 'ix'
call scalararg(scalar(4)(ix=[1]))