Skip to content

Commit ace6bb9

Browse files
committed
Fix unwrap error in overflowing int literal
1 parent 43ca9d1 commit ace6bb9

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
@@ -245,12 +245,11 @@ fn lint_int_literal<'tcx>(
245245
lit: &hir::Lit,
246246
t: ty::IntTy,
247247
v: u128,
248-
negated: bool,
249248
) {
250249
let int_type = t.normalize(cx.sess().target.pointer_width);
251250
let (min, max) = int_ty_range(int_type);
252251
let max = max as u128;
253-
let negative = negated ^ (type_limits.negated_expr_id == Some(hir_id));
252+
let negative = type_limits.negated_expr_id == Some(hir_id);
254253

255254
// Detect literal value out of range [min, max] inclusive
256255
// avoiding use of -min to prevent overflow/panic
@@ -366,7 +365,7 @@ pub(crate) fn lint_literal<'tcx>(
366365
ty::Int(t) => {
367366
match lit.node {
368367
ast::LitKind::Int(v, ast::LitIntType::Signed(_) | ast::LitIntType::Unsuffixed) => {
369-
lint_int_literal(cx, type_limits, hir_id, span, lit, t, v.get(), negated)
368+
lint_int_literal(cx, type_limits, hir_id, span, lit, t, v.get())
370369
}
371370
_ => bug!(),
372371
};
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)