about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2025-02-09 19:44:53 +0100
committerGitHub <noreply@github.com>2025-02-09 19:44:53 +0100
commitb5b460ddf1d366bcb068f5acf25aff60ce6afd66 (patch)
treeadacb36788cca9e5ca8a203e5d93c6727ff3d2e4
parent4b319bcadaba37a0a3fc08cb88c87eeb6eac6f57 (diff)
parentace6bb9869747b50d7d4bfaacc65d0592ec94aef (diff)
downloadrust-b5b460ddf1d366bcb068f5acf25aff60ce6afd66.tar.gz
rust-b5b460ddf1d366bcb068f5acf25aff60ce6afd66.zip
Rollup merge of #136760 - chenyukang:fix-overflowing-int-lint-crash, r=oli-obk
Fix unwrap error in overflowing int literal

Fixes #136675

it's maybe `negative` only from [check_lit](https://github.com/chenyukang/rust/blob/526e3288feb68eac55013746e03fb54d6a0b9a1e/compiler/rustc_lint/src/types.rs#L546), in this scenario the fields in `TypeLimits` is none.

r? ``@oli-obk``
-rw-r--r--compiler/rustc_lint/src/types.rs6
-rw-r--r--compiler/rustc_lint/src/types/literal.rs5
-rw-r--r--tests/ui/lint/lint-overflowing-int-136675.rs4
-rw-r--r--tests/ui/lint/lint-overflowing-int-136675.stderr21
4 files changed, 32 insertions, 4 deletions
diff --git a/compiler/rustc_lint/src/types.rs b/compiler/rustc_lint/src/types.rs
index a9fffcf348b..601d2fbfb67 100644
--- a/compiler/rustc_lint/src/types.rs
+++ b/compiler/rustc_lint/src/types.rs
@@ -543,7 +543,11 @@ impl<'tcx> LateLintPass<'tcx> for TypeLimits {
         lit: &'tcx hir::Lit,
         negated: bool,
     ) {
-        lint_literal(cx, self, hir_id, lit.span, lit, negated)
+        if negated {
+            self.negated_expr_id = Some(hir_id);
+            self.negated_expr_span = Some(lit.span);
+        }
+        lint_literal(cx, self, hir_id, lit.span, lit, negated);
     }
 
     fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx hir::Expr<'tcx>) {
diff --git a/compiler/rustc_lint/src/types/literal.rs b/compiler/rustc_lint/src/types/literal.rs
index da98b6461f1..3e4532a6dbe 100644
--- a/compiler/rustc_lint/src/types/literal.rs
+++ b/compiler/rustc_lint/src/types/literal.rs
@@ -250,12 +250,11 @@ fn lint_int_literal<'tcx>(
     lit: &hir::Lit,
     t: ty::IntTy,
     v: u128,
-    negated: bool,
 ) {
     let int_type = t.normalize(cx.sess().target.pointer_width);
     let (min, max) = int_ty_range(int_type);
     let max = max as u128;
-    let negative = negated ^ (type_limits.negated_expr_id == Some(hir_id));
+    let negative = type_limits.negated_expr_id == Some(hir_id);
 
     // Detect literal value out of range [min, max] inclusive
     // avoiding use of -min to prevent overflow/panic
@@ -374,7 +373,7 @@ pub(crate) fn lint_literal<'tcx>(
         ty::Int(t) => {
             match lit.node {
                 ast::LitKind::Int(v, ast::LitIntType::Signed(_) | ast::LitIntType::Unsuffixed) => {
-                    lint_int_literal(cx, type_limits, hir_id, span, lit, t, v.get(), negated)
+                    lint_int_literal(cx, type_limits, hir_id, span, lit, t, v.get())
                 }
                 _ => bug!(),
             };
diff --git a/tests/ui/lint/lint-overflowing-int-136675.rs b/tests/ui/lint/lint-overflowing-int-136675.rs
new file mode 100644
index 00000000000..616531519a6
--- /dev/null
+++ b/tests/ui/lint/lint-overflowing-int-136675.rs
@@ -0,0 +1,4 @@
+fn main() {
+    if let -129 = 0i8 {} //~ ERROR literal out of range for `i8`
+    let x: i8 = -129; //~ ERROR literal out of range for `i8`
+}
diff --git a/tests/ui/lint/lint-overflowing-int-136675.stderr b/tests/ui/lint/lint-overflowing-int-136675.stderr
new file mode 100644
index 00000000000..3b67c663ac2
--- /dev/null
+++ b/tests/ui/lint/lint-overflowing-int-136675.stderr
@@ -0,0 +1,21 @@
+error: literal out of range for `i8`
+  --> $DIR/lint-overflowing-int-136675.rs:2:12
+   |
+LL |     if let -129 = 0i8 {}
+   |            ^^^^
+   |
+   = note: the literal `-129` does not fit into the type `i8` whose range is `-128..=127`
+   = help: consider using the type `i16` instead
+   = note: `#[deny(overflowing_literals)]` on by default
+
+error: literal out of range for `i8`
+  --> $DIR/lint-overflowing-int-136675.rs:3:17
+   |
+LL |     let x: i8 = -129;
+   |                 ^^^^
+   |
+   = note: the literal `-129` does not fit into the type `i8` whose range is `-128..=127`
+   = help: consider using the type `i16` instead
+
+error: aborting due to 2 previous errors
+