Skip to content

[flang] Fix purity checking for internal subprograms #91759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions flang/lib/Semantics/check-purity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,16 @@ bool PurityChecker::InPureSubprogram() const {

bool PurityChecker::HasPurePrefix(
const std::list<parser::PrefixSpec> &prefixes) const {
bool result{false};
for (const parser::PrefixSpec &prefix : prefixes) {
if (std::holds_alternative<parser::PrefixSpec::Pure>(prefix.u)) {
return true;
if (std::holds_alternative<parser::PrefixSpec::Impure>(prefix.u)) {
return false;
} else if (std::holds_alternative<parser::PrefixSpec::Pure>(prefix.u) ||
std::holds_alternative<parser::PrefixSpec::Elemental>(prefix.u)) {
result = true;
}
}
return false;
return result;
}

void PurityChecker::Entered(
Expand Down
59 changes: 59 additions & 0 deletions flang/test/Semantics/pure02.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
! RUN: %python %S/test_errors.py %s %flang_fc1
pure subroutine s1
contains
!ERROR: An internal subprogram of a pure subprogram must also be pure
subroutine t1
end
pure subroutine t2 ! ok
end
elemental subroutine t3(k) ! ok
integer, intent(in) :: k
end
!ERROR: An internal subprogram of a pure subprogram must also be pure
impure elemental subroutine t4(k)
integer, intent(in) :: k
end
!ERROR: An internal subprogram of a pure subprogram must also be pure
elemental impure subroutine t5(k)
integer, intent(in) :: k
end
end

elemental subroutine s2(j)
integer, intent(in) :: j
contains
!ERROR: An internal subprogram of a pure subprogram must also be pure
subroutine t1
end
pure subroutine t2 ! ok
end
elemental subroutine t3(k) ! ok
integer, intent(in) :: k
end
!ERROR: An internal subprogram of a pure subprogram must also be pure
impure elemental subroutine t4(k)
integer, intent(in) :: k
end
!ERROR: An internal subprogram of a pure subprogram must also be pure
elemental impure subroutine t5(k)
integer, intent(in) :: k
end
end

impure elemental subroutine s3(j)
integer, intent(in) :: j
contains
subroutine t1
end
pure subroutine t2
end
elemental subroutine t3(k)
integer, intent(in) :: k
end
impure elemental subroutine t4(k)
integer, intent(in) :: k
end
elemental impure subroutine t5(k)
integer, intent(in) :: k
end
end
Loading