From 2985d5623c88f760d5c594d19558cd5c57ff3f45 Mon Sep 17 00:00:00 2001 From: Peter Klausler Date: Tue, 11 Jan 2022 11:01:12 -0800 Subject: [PATCH] [flang] Improve error message (initialized variable in pure subprogram) When variable with the SAVE attribute appears in a pure subprogram, emit a more specialized error message if the SAVE attribute was acquired from static initialization. Differential Revision: https://reviews.llvm.org/D117699 --- flang/lib/Semantics/check-declarations.cpp | 9 +++++++-- flang/test/Semantics/call10.f90 | 6 +++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/flang/lib/Semantics/check-declarations.cpp b/flang/lib/Semantics/check-declarations.cpp index d5c677af3bc6..dcb8cd383403 100644 --- a/flang/lib/Semantics/check-declarations.cpp +++ b/flang/lib/Semantics/check-declarations.cpp @@ -237,8 +237,13 @@ void CheckHelper::Check(const Symbol &symbol) { } if (InPure()) { if (IsSaved(symbol)) { - messages_.Say( - "A pure subprogram may not have a variable with the SAVE attribute"_err_en_US); + if (IsInitialized(symbol)) { + messages_.Say( + "A pure subprogram may not initialize a variable"_err_en_US); + } else { + messages_.Say( + "A pure subprogram may not have a variable with the SAVE attribute"_err_en_US); + } } if (symbol.attrs().test(Attr::VOLATILE)) { messages_.Say( diff --git a/flang/test/Semantics/call10.f90 b/flang/test/Semantics/call10.f90 index fabadad32f1a..007466875308 100644 --- a/flang/test/Semantics/call10.f90 +++ b/flang/test/Semantics/call10.f90 @@ -85,9 +85,9 @@ module m pure subroutine s05 ! C1589 !ERROR: A pure subprogram may not have a variable with the SAVE attribute real, save :: v1 - !ERROR: A pure subprogram may not have a variable with the SAVE attribute + !ERROR: A pure subprogram may not initialize a variable real :: v2 = 0. - !ERROR: A pure subprogram may not have a variable with the SAVE attribute + !ERROR: A pure subprogram may not initialize a variable real :: v3 data v3/0./ !ERROR: A pure subprogram may not have a variable with the SAVE attribute @@ -97,7 +97,7 @@ module m block !ERROR: A pure subprogram may not have a variable with the SAVE attribute real, save :: v5 - !ERROR: A pure subprogram may not have a variable with the SAVE attribute + !ERROR: A pure subprogram may not initialize a variable real :: v6 = 0. end block end subroutine