Skip to content

Commit

Permalink
[flang] Simplify same_type_as condition
Browse files Browse the repository at this point in the history
Restore the behavior changed in D145384 and add proper
unit tests.

Unallocated unlimited poymorphic allocatable and disassociated
unlimited polymorphic pointer should return false.

Reviewed By: PeteSteinfeld

Differential Revision: https://reviews.llvm.org/D145674
  • Loading branch information
clementval committed Mar 9, 2023
1 parent 1a4d0eb commit 188c02d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
9 changes: 3 additions & 6 deletions flang/runtime/derived-api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,9 @@ bool RTNAME(SameTypeAs)(const Descriptor &a, const Descriptor &b) {
const typeInfo::DerivedType *derivedTypeA{GetDerivedType(a)};
const typeInfo::DerivedType *derivedTypeB{GetDerivedType(b)};

// One of the descriptor is an unallocated unlimited polymorphic descriptor.
// This is processor depedent according to the standard. Align the result
// with other compilers.
if ((!a.IsAllocated() && derivedTypeA == nullptr) ||
(!b.IsAllocated() && derivedTypeB == nullptr)) {
return true;
// No dynamic type in one or both descriptor.
if (derivedTypeA == nullptr || derivedTypeB == nullptr) {
return false;
}

// Exact match of derived type.
Expand Down
33 changes: 21 additions & 12 deletions flang/unittests/Runtime/Derived.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,30 @@ TEST(Derived, SameTypeAs) {
4, nullptr, 0, nullptr, CFI_attribute_pointer)};
EXPECT_FALSE(RTNAME(SameTypeAs)(*i1, *r1));

// CLASS(*), ALLOCATABLE :: p1
auto p1{Descriptor::Create(TypeCode{Fortran::common::TypeCategory::Real, 4},
// CLASS(*), ALLOCATABLE :: a1
auto a1{Descriptor::Create(TypeCode{Fortran::common::TypeCategory::Real, 4},
4, nullptr, 0, nullptr, CFI_attribute_allocatable)};
p1->raw().elem_len = 0;
p1->raw().type = CFI_type_other;
a1->raw().elem_len = 0;
a1->raw().type = CFI_type_other;

EXPECT_TRUE(RTNAME(SameTypeAs)(*i1, *p1));
EXPECT_TRUE(RTNAME(SameTypeAs)(*p1, *i1));
EXPECT_TRUE(RTNAME(SameTypeAs)(*r1, *p1));
EXPECT_FALSE(RTNAME(SameTypeAs)(*i1, *a1));
EXPECT_FALSE(RTNAME(SameTypeAs)(*a1, *i1));
EXPECT_FALSE(RTNAME(SameTypeAs)(*r1, *a1));

// CLASS(*), ALLOCATABLE :: p2
auto p2{Descriptor::Create(TypeCode{Fortran::common::TypeCategory::Real, 4},
// CLASS(*), ALLOCATABLE :: a2
auto a2{Descriptor::Create(TypeCode{Fortran::common::TypeCategory::Real, 4},
4, nullptr, 0, nullptr, CFI_attribute_allocatable)};
p2->raw().elem_len = 0;
p2->raw().type = CFI_type_other;
a2->raw().elem_len = 0;
a2->raw().type = CFI_type_other;

EXPECT_FALSE(RTNAME(SameTypeAs)(*a1, *a2));

// CLASS(*), POINTER :: p1
auto p1{Descriptor::Create(TypeCode{Fortran::common::TypeCategory::Real, 4},
4, nullptr, 0, nullptr, CFI_attribute_pointer)};
p1->raw().elem_len = 0;
p1->raw().type = CFI_type_other;

EXPECT_TRUE(RTNAME(SameTypeAs)(*p1, *p2));
EXPECT_FALSE(RTNAME(SameTypeAs)(*i1, *p1));
EXPECT_FALSE(RTNAME(SameTypeAs)(*p1, *i1));
}

0 comments on commit 188c02d

Please sign in to comment.