Skip to content

Commit a2f8269

Browse files
author
Saleem Jaffer
committed
making adjust_span a closure
1 parent 999ee01 commit a2f8269

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

src/librustc_mir/hair/cx/expr.rs

+22-22
Original file line numberDiff line numberDiff line change
@@ -68,35 +68,35 @@ impl<'tcx> Mirror<'tcx> for &'tcx hir::Expr {
6868
}
6969
}
7070

71-
/// Adjust the span from the block, to the last expression of the
72-
/// block. This is a better span when returning a mutable reference
73-
/// with too short a lifetime. The error message will use the span
74-
/// from the assignment to the return place, which should only point
75-
/// at the returned value, not the entire function body.
76-
///
77-
/// fn return_short_lived<'a>(x: &'a mut i32) -> &'static mut i32 {
78-
/// x
79-
/// // ^ error message points at this expression.
80-
/// }
81-
fn adjust_span<'tcx>(expr: &mut Expr<'tcx>) -> Span {
82-
if let ExprKind::Block { body } = expr.kind {
83-
if let Some(ref last_expr) = body.expr {
84-
expr.span = last_expr.span;
85-
}
86-
}
87-
88-
expr.span
89-
}
90-
9171
fn apply_adjustment<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
9272
hir_expr: &'tcx hir::Expr,
9373
mut expr: Expr<'tcx>,
9474
adjustment: &Adjustment<'tcx>)
9575
-> Expr<'tcx> {
9676
let Expr { temp_lifetime, mut span, .. } = expr;
77+
78+
// Adjust the span from the block, to the last expression of the
79+
// block. This is a better span when returning a mutable reference
80+
// with too short a lifetime. The error message will use the span
81+
// from the assignment to the return place, which should only point
82+
// at the returned value, not the entire function body.
83+
//
84+
// fn return_short_lived<'a>(x: &'a mut i32) -> &'static mut i32 {
85+
// x
86+
// // ^ error message points at this expression.
87+
// }
88+
let mut adjust_span = |expr: &mut Expr<'tcx>| {
89+
if let ExprKind::Block { body } = expr.kind {
90+
if let Some(ref last_expr) = body.expr {
91+
span = last_expr.span;
92+
expr.span = span;
93+
}
94+
}
95+
};
96+
9797
let kind = match adjustment.kind {
9898
Adjust::Pointer(PointerCast::Unsize) => {
99-
span = adjust_span(&mut expr);
99+
adjust_span(&mut expr);
100100
ExprKind::Pointer { cast: PointerCast::Unsize, source: expr.to_ref() }
101101
}
102102
Adjust::Pointer(cast) => {
@@ -106,7 +106,7 @@ fn apply_adjustment<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
106106
ExprKind::NeverToAny { source: expr.to_ref() }
107107
}
108108
Adjust::Deref(None) => {
109-
span = adjust_span(&mut expr);
109+
adjust_span(&mut expr);
110110
ExprKind::Deref { arg: expr.to_ref() }
111111
}
112112
Adjust::Deref(Some(deref)) => {

0 commit comments

Comments
 (0)