Skip to content

Conversation

@ahatanak
Copy link
Collaborator

@ahatanak ahatanak commented Jun 23, 2025

This commit addresses a fallout introduced by #126846.

Previously, TryGetExprRange would return an IntRange that has an active range exceeding the maximum representable range for the expression's underlying type. This led to clang erroneously issuing warnings about implicit conversions losing integer precision.

This commit fixes the bug by capping IntRange::Width to MaxWidth.

rdar://149444029

This commit addresses a fallout introduced by llvm#126846.

Previously, TryGetExprRange would return an IntRange that has an active
range exceeding the maximum representable range for the expression's
underlying type. This led to clang erroneously issuing warnings about
implicit conversions losing integer precision.

This commit fixes the bug by capping IntRange::Width to the size of the
expression's type.

rdar://149444029
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jun 23, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 23, 2025

@llvm/pr-subscribers-clang

Author: Akira Hatanaka (ahatanak)

Changes

This commit addresses a fallout introduced by #126846.

Previously, TryGetExprRange would return an IntRange that has an active range exceeding the maximum representable range for the expression's underlying type. This led to clang erroneously issuing warnings about implicit conversions losing integer precision.

This commit fixes the bug by capping IntRange::Width to the size of the expression's type.

rdar://149444029


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

2 Files Affected:

  • (modified) clang/lib/Sema/SemaChecking.cpp (+4-2)
  • (modified) clang/test/Sema/implicit-int-conversion-on-int.c (+8)
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 69276ce418fa6..57cb810adceaa 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -10808,7 +10808,7 @@ static std::optional<IntRange> TryGetExprRange(ASTContext &C, const Expr *E,
       // sign bit. If the range was not non-negative, we need an extra bit
       // because the negation of the most-negative value is one bit wider than
       // that value.
-      return IntRange(SubRange->Width + 1, false);
+      return IntRange(std::min(SubRange->Width + 1, MaxWidth), false);
     }
 
     case UO_Not: {
@@ -10825,7 +10825,9 @@ static std::optional<IntRange> TryGetExprRange(ASTContext &C, const Expr *E,
 
       // The width increments by 1 if the sub-expression cannot be negative
       // since it now can be.
-      return IntRange(SubRange->Width + (int)SubRange->NonNegative, false);
+      return IntRange(
+          std::min(SubRange->Width + (int)SubRange->NonNegative, MaxWidth),
+          false);
     }
 
     default:
diff --git a/clang/test/Sema/implicit-int-conversion-on-int.c b/clang/test/Sema/implicit-int-conversion-on-int.c
index f555893d9e17a..3b1d81cd6a9ab 100644
--- a/clang/test/Sema/implicit-int-conversion-on-int.c
+++ b/clang/test/Sema/implicit-int-conversion-on-int.c
@@ -48,3 +48,11 @@ unsigned long long test_ull(unsigned long long x) {
 unsigned _BitInt(16) test_unsigned_bit_int(unsigned _BitInt(16) x) {
   return -x;
 }
+
+unsigned test_shift_minus(int i) {
+  return -(1 << i);
+}
+
+unsigned test_shift_not(int i) {
+  return ~(1 << i);
+}

@ahatanak
Copy link
Collaborator Author

Note that the range exceeds the underlying type's size in this case because 1 << i is treated as logically positive (see #126846 (comment)).

@ahatanak ahatanak changed the title Cap IntRange::Width to the expression's type size Cap IntRange::Width to MaxWidth Jun 24, 2025
@shafik shafik requested a review from erichkeane June 25, 2025 21:44
@ahatanak ahatanak merged commit 5bbe153 into llvm:main Jun 26, 2025
7 checks passed
@ahatanak ahatanak deleted the fix-sign-compare branch June 26, 2025 15:03
anthonyhatran pushed a commit to anthonyhatran/llvm-project that referenced this pull request Jun 26, 2025
This commit addresses a fallout introduced by llvm#126846.

Previously, TryGetExprRange would return an IntRange that has an active
range exceeding the maximum representable range for the expression's
underlying type. This led to clang erroneously issuing warnings about
implicit conversions losing integer precision.

This commit fixes the bug by capping IntRange::Width to MaxWidth.

rdar://149444029
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants