@@ -68,6 +68,26 @@ 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+
7191fn apply_adjustment < ' a , ' gcx , ' tcx > ( cx : & mut Cx < ' a , ' gcx , ' tcx > ,
7292 hir_expr : & ' tcx hir:: Expr ,
7393 mut expr : Expr < ' tcx > ,
@@ -76,12 +96,7 @@ fn apply_adjustment<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
7696 let Expr { temp_lifetime, mut span, .. } = expr;
7797 let kind = match adjustment. kind {
7898 Adjust :: Pointer ( PointerCast :: Unsize ) => {
79- if let ExprKind :: Block { body } = expr. kind {
80- if let Some ( ref last_expr) = body. expr {
81- span = last_expr. span ;
82- expr. span = span;
83- }
84- }
99+ span = adjust_span ( & mut expr) ;
85100 ExprKind :: Pointer { cast : PointerCast :: Unsize , source : expr. to_ref ( ) }
86101 }
87102 Adjust :: Pointer ( cast) => {
@@ -91,28 +106,12 @@ fn apply_adjustment<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
91106 ExprKind :: NeverToAny { source : expr. to_ref ( ) }
92107 }
93108 Adjust :: Deref ( None ) => {
94- // Adjust the span from the block, to the last expression of the
95- // block. This is a better span when returning a mutable reference
96- // with too short a lifetime. The error message will use the span
97- // from the assignment to the return place, which should only point
98- // at the returned value, not the entire function body.
99- //
100- // fn return_short_lived<'a>(x: &'a mut i32) -> &'static mut i32 {
101- // x
102- // // ^ error message points at this expression.
103- // }
104- //
105- // We don't need to do this adjustment in the next match arm since
106- // deref coercions always start with a built-in deref.
107- if let ExprKind :: Block { body } = expr. kind {
108- if let Some ( ref last_expr) = body. expr {
109- span = last_expr. span ;
110- expr. span = span;
111- }
112- }
109+ span = adjust_span ( & mut expr) ;
113110 ExprKind :: Deref { arg : expr. to_ref ( ) }
114111 }
115112 Adjust :: Deref ( Some ( deref) ) => {
113+ // We don't need to do call adjust_span here since
114+ // deref coercions always start with a built-in deref.
116115 let call = deref. method_call ( cx. tcx ( ) , expr. ty ) ;
117116
118117 expr = Expr {
0 commit comments