Skip to content

Commit 50b55a5

Browse files
authored
[flang][runtime] Fix AllocateAssignmentLHS for monomorphic LHS (#153073)
When the left-hand side of an assignment statement is an allocatable that has a monomorphic derived type, and the right-hand side of the assignment has a type that is an extension of that type, *don't* change the incoming type or element size of the descriptor before allocating it. Fixes #152758.
1 parent ec4e6aa commit 50b55a5

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

flang-rt/lib/runtime/assign.cpp

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,23 +88,32 @@ static inline RT_API_ATTRS bool MustDeallocateLHS(
8888
// originally deallocated or because it required reallocation
8989
static RT_API_ATTRS int AllocateAssignmentLHS(
9090
Descriptor &to, const Descriptor &from, Terminator &terminator, int flags) {
91-
to.raw().type = from.raw().type;
92-
if (!(flags & ExplicitLengthCharacterLHS)) {
93-
to.raw().elem_len = from.ElementBytes();
94-
}
95-
const typeInfo::DerivedType *derived{nullptr};
9691
DescriptorAddendum *toAddendum{to.Addendum()};
92+
const typeInfo::DerivedType *derived{nullptr};
93+
if (toAddendum) {
94+
derived = toAddendum->derivedType();
95+
}
9796
if (const DescriptorAddendum * fromAddendum{from.Addendum()}) {
98-
derived = fromAddendum->derivedType();
99-
if (toAddendum) {
100-
toAddendum->set_derivedType(derived);
101-
std::size_t lenParms{derived ? derived->LenParameters() : 0};
97+
if (!derived || (flags & PolymorphicLHS)) {
98+
derived = fromAddendum->derivedType();
99+
}
100+
if (toAddendum && derived) {
101+
std::size_t lenParms{derived->LenParameters()};
102102
for (std::size_t j{0}; j < lenParms; ++j) {
103103
toAddendum->SetLenParameterValue(j, fromAddendum->LenParameterValue(j));
104104
}
105105
}
106-
} else if (toAddendum) {
107-
toAddendum->set_derivedType(nullptr);
106+
} else {
107+
derived = nullptr;
108+
}
109+
if (toAddendum) {
110+
toAddendum->set_derivedType(derived);
111+
}
112+
to.raw().type = from.raw().type;
113+
if (derived) {
114+
to.raw().elem_len = derived->sizeInBytes();
115+
} else if (!(flags & ExplicitLengthCharacterLHS)) {
116+
to.raw().elem_len = from.ElementBytes();
108117
}
109118
// subtle: leave bounds in place when "from" is scalar (10.2.1.3(3))
110119
int rank{from.rank()};

0 commit comments

Comments
 (0)