Skip to content

Commit f8300f1

Browse files
authored
[flang] Refine "same type" testing for intrinsic arguments (llvm#125133)
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 llvm#124976.
1 parent 56c4684 commit f8300f1

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

flang/lib/Evaluate/intrinsics.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2036,11 +2036,16 @@ std::optional<SpecificCall> IntrinsicInterface::Match(
20362036
if (!sameArg) {
20372037
sameArg = arg;
20382038
}
2039-
// Check both ways so that a CLASS(*) actuals to
2040-
// MOVE_ALLOC and EOSHIFT both work.
20412039
auto sameType{sameArg->GetType().value()};
2042-
argOk = sameType.IsTkLenCompatibleWith(*type) ||
2043-
type->IsTkLenCompatibleWith(sameType);
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);
2046+
} else {
2047+
argOk = sameType.IsTkLenCompatibleWith(*type);
2048+
}
20442049
} break;
20452050
case KindCode::sameKind:
20462051
if (!sameArg) {

flang/lib/Semantics/resolve-names.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9746,7 +9746,7 @@ void ResolveNamesVisitor::ResolveSpecificationParts(ProgramTree &node) {
97469746
},
97479747
node.stmt());
97489748
Walk(node.spec());
9749-
bool inDeviceSubprogram = false;
9749+
bool inDeviceSubprogram{false};
97509750
// If this is a function, convert result to an object. This is to prevent the
97519751
// result from being converted later to a function symbol if it is called
97529752
// inside the function.

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)