Skip to content

Commit b5b460d

Browse files
authored
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``
2 parents 4b319bc + ace6bb9 commit b5b460d

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

compiler/rustc_lint/src/types.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,11 @@ impl<'tcx> LateLintPass<'tcx> for TypeLimits {
543543
lit: &'tcx hir::Lit,
544544
negated: bool,
545545
) {
546-
lint_literal(cx, self, hir_id, lit.span, lit, negated)
546+
if negated {
547+
self.negated_expr_id = Some(hir_id);
548+
self.negated_expr_span = Some(lit.span);
549+
}
550+
lint_literal(cx, self, hir_id, lit.span, lit, negated);
547551
}
548552

549553
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx hir::Expr<'tcx>) {

compiler/rustc_lint/src/types/literal.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -250,12 +250,11 @@ fn lint_int_literal<'tcx>(
250250
lit: &hir::Lit,
251251
t: ty::IntTy,
252252
v: u128,
253-
negated: bool,
254253
) {
255254
let int_type = t.normalize(cx.sess().target.pointer_width);
256255
let (min, max) = int_ty_range(int_type);
257256
let max = max as u128;
258-
let negative = negated ^ (type_limits.negated_expr_id == Some(hir_id));
257+
let negative = type_limits.negated_expr_id == Some(hir_id);
259258

260259
// Detect literal value out of range [min, max] inclusive
261260
// avoiding use of -min to prevent overflow/panic
@@ -374,7 +373,7 @@ pub(crate) fn lint_literal<'tcx>(
374373
ty::Int(t) => {
375374
match lit.node {
376375
ast::LitKind::Int(v, ast::LitIntType::Signed(_) | ast::LitIntType::Unsuffixed) => {
377-
lint_int_literal(cx, type_limits, hir_id, span, lit, t, v.get(), negated)
376+
lint_int_literal(cx, type_limits, hir_id, span, lit, t, v.get())
378377
}
379378
_ => bug!(),
380379
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fn main() {
2+
if let -129 = 0i8 {} //~ ERROR literal out of range for `i8`
3+
let x: i8 = -129; //~ ERROR literal out of range for `i8`
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error: literal out of range for `i8`
2+
--> $DIR/lint-overflowing-int-136675.rs:2:12
3+
|
4+
LL | if let -129 = 0i8 {}
5+
| ^^^^
6+
|
7+
= note: the literal `-129` does not fit into the type `i8` whose range is `-128..=127`
8+
= help: consider using the type `i16` instead
9+
= note: `#[deny(overflowing_literals)]` on by default
10+
11+
error: literal out of range for `i8`
12+
--> $DIR/lint-overflowing-int-136675.rs:3:17
13+
|
14+
LL | let x: i8 = -129;
15+
| ^^^^
16+
|
17+
= note: the literal `-129` does not fit into the type `i8` whose range is `-128..=127`
18+
= help: consider using the type `i16` instead
19+
20+
error: aborting due to 2 previous errors
21+

0 commit comments

Comments
 (0)