Skip to content

Commit 13d74eb

Browse files
committed
[flang] Refine "same type" testing for intrinsic arguments
Some errors aren't being caught, such as the case in the linked bug where the PAD= argument to RESHAPE() didn't have the same declared type as the ARRAY=; this led to a crash in lowering. Refine the "same type" testing logic for intrinsic procedures, and add a better test. Fixes #124976.
1 parent cbf4abf commit 13d74eb

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

flang/lib/Evaluate/intrinsics.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2032,16 +2032,21 @@ std::optional<SpecificCall> IntrinsicInterface::Match(
20322032
dimArg = j;
20332033
argOk = true;
20342034
break;
2035-
case KindCode::same:
2035+
case KindCode::same: {
20362036
if (!sameArg) {
20372037
sameArg = arg;
2038-
argOk = true;
2038+
}
2039+
auto sameType{sameArg->GetType().value()};
2040+
if (name == "move_alloc"s) {
2041+
// second argument can be more general
2042+
argOk = type->IsTkLenCompatibleWith(sameType);
2043+
} else if (name == "merge"s) {
2044+
argOk = type->IsTkLenCompatibleWith(sameType) &&
2045+
sameType.IsTkLenCompatibleWith(*type);
20392046
} else {
2040-
auto sameType{sameArg->GetType().value()};
2041-
argOk = sameType.IsTkLenCompatibleWith(*type) ||
2042-
(name == "move_alloc"s && type->IsTkLenCompatibleWith(sameType));
2047+
argOk = sameType.IsTkLenCompatibleWith(*type);
20432048
}
2044-
break;
2049+
} break;
20452050
case KindCode::sameKind:
20462051
if (!sameArg) {
20472052
sameArg = arg;

flang/test/Semantics/bug124976.f90

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
!RUN: %python %S/test_errors.py %s %flang_fc1
2+
program main
3+
type base
4+
integer :: x = 1
5+
end type
6+
type, extends(base) :: child
7+
integer :: y = 2
8+
end type
9+
class(child), allocatable :: c1(:), c2(:,:)
10+
class(base), allocatable :: b1(:), b2(:,:)
11+
logical var(1)
12+
common /blk/ var
13+
allocate(c1(2), c2(2,2), b1(2), b2(2,2))
14+
!ERROR: Actual argument for 'pad=' has bad type or kind 'CLASS(base)'
15+
c2 = reshape(c1, shape(c2), pad=b1)
16+
b2 = reshape(b1, shape(b2), pad=c1) ! ok
17+
!ERROR: Actual argument for 'to=' has bad type or kind 'CLASS(child)'
18+
call move_alloc(b1, c1)
19+
call move_alloc(c1, b1) ! ok
20+
!ERROR: Actual argument for 'boundary=' has bad type or kind 'CLASS(base)'
21+
c1 = eoshift(c1, 1, b1(1))
22+
c1 = eoshift(c1, 1, c2(1,1)) ! ok
23+
b1 = eoshift(b1, 1, c1(1)) ! ok
24+
!ERROR: Actual argument for 'fsource=' has bad type or kind 'CLASS(child)'
25+
b1 = merge(b1, c1, var(1))
26+
!ERROR: Actual argument for 'fsource=' has bad type or kind 'CLASS(base)'
27+
b1 = merge(c1, b1, var(1))
28+
b1 = merge(b1, b1, var(1)) ! ok
29+
!ERROR: Actual argument for 'vector=' has bad type or kind 'CLASS(base)'
30+
c1 = pack(c1, var, b1)
31+
c1 = pack(c1, var, c1) ! ok
32+
b1 = pack(b1, var, c1) ! ok
33+
end

0 commit comments

Comments
 (0)