Skip to content

Commit 325d1d0

Browse files
authored
[flang] Fix purity checking for internal subprograms (llvm#91759)
ELEMENTAL internal subprograms are pure unless explicitly IMPURE.
1 parent 667d12f commit 325d1d0

File tree

2 files changed

+66
-3
lines changed

2 files changed

+66
-3
lines changed

flang/lib/Semantics/check-purity.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,16 @@ bool PurityChecker::InPureSubprogram() const {
3939

4040
bool PurityChecker::HasPurePrefix(
4141
const std::list<parser::PrefixSpec> &prefixes) const {
42+
bool result{false};
4243
for (const parser::PrefixSpec &prefix : prefixes) {
43-
if (std::holds_alternative<parser::PrefixSpec::Pure>(prefix.u)) {
44-
return true;
44+
if (std::holds_alternative<parser::PrefixSpec::Impure>(prefix.u)) {
45+
return false;
46+
} else if (std::holds_alternative<parser::PrefixSpec::Pure>(prefix.u) ||
47+
std::holds_alternative<parser::PrefixSpec::Elemental>(prefix.u)) {
48+
result = true;
4549
}
4650
}
47-
return false;
51+
return result;
4852
}
4953

5054
void PurityChecker::Entered(

flang/test/Semantics/pure02.f90

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
! RUN: %python %S/test_errors.py %s %flang_fc1
2+
pure subroutine s1
3+
contains
4+
!ERROR: An internal subprogram of a pure subprogram must also be pure
5+
subroutine t1
6+
end
7+
pure subroutine t2 ! ok
8+
end
9+
elemental subroutine t3(k) ! ok
10+
integer, intent(in) :: k
11+
end
12+
!ERROR: An internal subprogram of a pure subprogram must also be pure
13+
impure elemental subroutine t4(k)
14+
integer, intent(in) :: k
15+
end
16+
!ERROR: An internal subprogram of a pure subprogram must also be pure
17+
elemental impure subroutine t5(k)
18+
integer, intent(in) :: k
19+
end
20+
end
21+
22+
elemental subroutine s2(j)
23+
integer, intent(in) :: j
24+
contains
25+
!ERROR: An internal subprogram of a pure subprogram must also be pure
26+
subroutine t1
27+
end
28+
pure subroutine t2 ! ok
29+
end
30+
elemental subroutine t3(k) ! ok
31+
integer, intent(in) :: k
32+
end
33+
!ERROR: An internal subprogram of a pure subprogram must also be pure
34+
impure elemental subroutine t4(k)
35+
integer, intent(in) :: k
36+
end
37+
!ERROR: An internal subprogram of a pure subprogram must also be pure
38+
elemental impure subroutine t5(k)
39+
integer, intent(in) :: k
40+
end
41+
end
42+
43+
impure elemental subroutine s3(j)
44+
integer, intent(in) :: j
45+
contains
46+
subroutine t1
47+
end
48+
pure subroutine t2
49+
end
50+
elemental subroutine t3(k)
51+
integer, intent(in) :: k
52+
end
53+
impure elemental subroutine t4(k)
54+
integer, intent(in) :: k
55+
end
56+
elemental impure subroutine t5(k)
57+
integer, intent(in) :: k
58+
end
59+
end

0 commit comments

Comments
 (0)