Skip to content

[flang] Improve error messages about overflowed integer conversions #110031

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
Sep 30, 2024

Conversation

klausler
Copy link
Contributor

When an INTEGER conversion to a smaller kind overflows in constant folding, report the truncated value so that it makes more sense later if it shows up in other messages.

When an INTEGER conversion to a smaller kind overflows in constant
folding, report the truncated value so that it makes more sense
later if it shows up in other messages.
@llvmbot
Copy link
Member

llvmbot commented Sep 25, 2024

@llvm/pr-subscribers-flang-semantics

Author: Peter Klausler (klausler)

Changes

When an INTEGER conversion to a smaller kind overflows in constant folding, report the truncated value so that it makes more sense later if it shows up in other messages.


Full diff: https://github.com/llvm/llvm-project/pull/110031.diff

2 Files Affected:

  • (modified) flang/lib/Evaluate/fold-implementation.h (+3-2)
  • (modified) flang/test/Evaluate/errors01.f90 (+2)
diff --git a/flang/lib/Evaluate/fold-implementation.h b/flang/lib/Evaluate/fold-implementation.h
index 1b14a305b87f4f..89477dfb364351 100644
--- a/flang/lib/Evaluate/fold-implementation.h
+++ b/flang/lib/Evaluate/fold-implementation.h
@@ -1736,8 +1736,9 @@ Expr<TO> FoldOperation(
                   msvcWorkaround.context.languageFeatures().ShouldWarn(
                       common::UsageWarning::FoldingException)) {
                 ctx.messages().Say(
-                    "INTEGER(%d) to INTEGER(%d) conversion overflowed"_warn_en_US,
-                    Operand::kind, TO::kind);
+                    "conversion of %s_%d to INTEGER(%d) overflowed; result is %s"_warn_en_US,
+                    value->SignedDecimal(), Operand::kind, TO::kind,
+                    converted.value.SignedDecimal());
               }
               return ScalarConstantToExpr(std::move(converted.value));
             } else if constexpr (FromCat == TypeCategory::Real) {
diff --git a/flang/test/Evaluate/errors01.f90 b/flang/test/Evaluate/errors01.f90
index bad73f2e8b160f..684f3fc34e6872 100644
--- a/flang/test/Evaluate/errors01.f90
+++ b/flang/test/Evaluate/errors01.f90
@@ -192,6 +192,8 @@ subroutine warnings
     real, parameter :: bad10 = product([huge(1.),huge(1.)])
     !CHECK: warning: PRODUCT() of COMPLEX(4) data overflowed
     complex, parameter :: bad11 = product([(huge(1.),0.),(huge(1.),0.)])
+    !CHECK: warning: conversion of 111111111111111111111_16 to INTEGER(8) overflowed; result is 430646668853801415
+    integer(8), parameter :: bad12 = int(111111111111111111111, 8)
     !CHECK: warning: overflow on REAL(8) to REAL(4) conversion
     x = 1.D40
     !CHECK-NOT: warning: invalid argument

Copy link
Contributor

@tarunprabhu tarunprabhu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks!

@klausler klausler merged commit 1759f3b into llvm:main Sep 30, 2024
11 checks passed
@klausler klausler deleted the bug109983 branch September 30, 2024 19:38
Sterling-Augustine pushed a commit to Sterling-Augustine/llvm-project that referenced this pull request Oct 3, 2024
…lvm#110031)

When an INTEGER conversion to a smaller kind overflows in constant
folding, report the truncated value so that it makes more sense later if
it shows up in other messages.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:semantics flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants