[flang-commits] [flang] [flang] Improve error message on bad complex literal. (PR #124331)

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Fri Jan 24 11:32:25 PST 2025


https://github.com/klausler created https://github.com/llvm/llvm-project/pull/124331

A complex literal constant can have one BOZ component, since the type and value of the literal can be determined by converting the BOZ value to the type of the other component.  But a complex literal constant with two BOZ components doesn't have a well-defined type. The error message was confusing in the case; emit a better one.

Fixes https://github.com/llvm/llvm-project/issues/124201.

>From 4cf4cd876ccaa3c05f7bc2020175b97aaa826ad7 Mon Sep 17 00:00:00 2001
From: Peter Klausler <pklausler at nvidia.com>
Date: Fri, 24 Jan 2025 11:27:27 -0800
Subject: [PATCH] [flang] Improve error message on bad complex literal.

A complex literal constant can have one BOZ component, since the
type and value of the literal can be determined by converting the
BOZ value to the type of the other component.  But a complex literal
constant with two BOZ components doesn't have a well-defined type.
The error message was confusing in the case; emit a better one.

Fixes https://github.com/llvm/llvm-project/issues/124201.
---
 flang/lib/Evaluate/tools.cpp       | 5 +++++
 flang/test/Semantics/complex01.f90 | 4 +++-
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/flang/lib/Evaluate/tools.cpp b/flang/lib/Evaluate/tools.cpp
index 2d0e1996632fc2..16b02607190973 100644
--- a/flang/lib/Evaluate/tools.cpp
+++ b/flang/lib/Evaluate/tools.cpp
@@ -212,6 +212,11 @@ ConvertRealOperandsResult ConvertRealOperands(
             return {AsSameKindExprs<TypeCategory::Real>(
                 ConvertTo(ry, std::move(bx)), std::move(ry))};
           },
+          [&](BOZLiteralConstant &&,
+              BOZLiteralConstant &&) -> ConvertRealOperandsResult {
+            messages.Say("operands cannot both be BOZ"_err_en_US);
+            return std::nullopt;
+          },
           [&](auto &&, auto &&) -> ConvertRealOperandsResult { // C718
             messages.Say(
                 "operands must be INTEGER, UNSIGNED, REAL, or BOZ"_err_en_US);
diff --git a/flang/test/Semantics/complex01.f90 b/flang/test/Semantics/complex01.f90
index c9d408ee0e111f..d268eafa685896 100644
--- a/flang/test/Semantics/complex01.f90
+++ b/flang/test/Semantics/complex01.f90
@@ -1,5 +1,5 @@
 ! RUN: %python %S/test_errors.py %s %flang_fc1
-! C718 Each named constant in a complex literal constant shall be of type 
+! C718 Each named constant in a complex literal constant shall be of type
 ! integer or real.
 subroutine s()
   integer :: ivar = 35
@@ -30,4 +30,6 @@ subroutine s()
   complex :: cvar11 = (cconst, 1.0)
   !ERROR: operands must be INTEGER, UNSIGNED, REAL, or BOZ
   complex :: cvar12 = (lconst, 1.0)
+  !ERROR: operands cannot both be BOZ
+  complex :: cvar13 = (z'3f700000', z'00000000')
 end subroutine s



More information about the flang-commits mailing list