Skip to content

Commit 3cd445a

Browse files
committed
Fallback to string formatting if source is not available
1 parent c0e3298 commit 3cd445a

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

Diff for: compiler/rustc_lint/src/types.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,11 @@ fn lint_int_literal<'tcx>(
462462
}
463463

464464
let span = if negative { type_limits.negated_expr_span.unwrap() } else { e.span };
465-
let lit =
466-
cx.sess().source_map().span_to_snippet(span).expect("must get snippet from literal");
465+
let lit = cx
466+
.sess()
467+
.source_map()
468+
.span_to_snippet(span)
469+
.unwrap_or_else(|_| if negative { format!("-{v}") } else { v.to_string() });
467470
let help = get_type_suggestion(cx.typeck_results().node_type(e.hir_id), v, negative)
468471
.map(|suggestion_ty| OverflowingIntHelp { suggestion_ty });
469472

@@ -489,6 +492,7 @@ fn lint_uint_literal<'tcx>(
489492
ast::LitKind::Int(v, _) => v.get(),
490493
_ => bug!(),
491494
};
495+
492496
if lit_val < min || lit_val > max {
493497
if let Node::Expr(par_e) = cx.tcx.parent_hir_node(e.hir_id) {
494498
match par_e.kind {
@@ -530,7 +534,7 @@ fn lint_uint_literal<'tcx>(
530534
.sess()
531535
.source_map()
532536
.span_to_snippet(lit.span)
533-
.expect("must get snippet from literal"),
537+
.unwrap_or_else(|_| lit_val.to_string()),
534538
min,
535539
max,
536540
},
@@ -555,14 +559,14 @@ fn lint_literal<'tcx>(
555559
}
556560
ty::Uint(t) => lint_uint_literal(cx, e, lit, t),
557561
ty::Float(t) => {
558-
let is_infinite = match lit.node {
562+
let (is_infinite, sym) = match lit.node {
559563
ast::LitKind::Float(v, _) => match t {
560564
// FIXME(f16_f128): add this check once `is_infinite` is reliable (ABI
561565
// issues resolved).
562-
ty::FloatTy::F16 => Ok(false),
563-
ty::FloatTy::F32 => v.as_str().parse().map(f32::is_infinite),
564-
ty::FloatTy::F64 => v.as_str().parse().map(f64::is_infinite),
565-
ty::FloatTy::F128 => Ok(false),
566+
ty::FloatTy::F16 => (Ok(false), v),
567+
ty::FloatTy::F32 => (v.as_str().parse().map(f32::is_infinite), v),
568+
ty::FloatTy::F64 => (v.as_str().parse().map(f64::is_infinite), v),
569+
ty::FloatTy::F128 => (Ok(false), v),
566570
},
567571
_ => bug!(),
568572
};
@@ -576,7 +580,7 @@ fn lint_literal<'tcx>(
576580
.sess()
577581
.source_map()
578582
.span_to_snippet(lit.span)
579-
.expect("must get snippet from literal"),
583+
.unwrap_or_else(|_| sym.to_string()),
580584
},
581585
);
582586
}

0 commit comments

Comments
 (0)