@@ -78,7 +78,7 @@ fn apply_adjustment<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
7878 mut expr : Expr < ' tcx > ,
7979 adjustment : & Adjustment < ' tcx > )
8080 -> Expr < ' tcx > {
81- let Expr { temp_lifetime, span, .. } = expr;
81+ let Expr { temp_lifetime, mut span, .. } = expr;
8282 let kind = match adjustment. kind {
8383 Adjust :: ReifyFnPointer => {
8484 ExprKind :: ReifyFnPointer { source : expr. to_ref ( ) }
@@ -96,6 +96,25 @@ fn apply_adjustment<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
9696 ExprKind :: Cast { source : expr. to_ref ( ) }
9797 }
9898 Adjust :: Deref ( None ) => {
99+ // Adjust the span from the block, to the last expression of the
100+ // block. This is a better span when returning a mutable reference
101+ // with too short a lifetime. The error message will use the span
102+ // from the assignment to the return place, which should only point
103+ // at the returned value, not the entire function body.
104+ //
105+ // fn return_short_lived<'a>(x: &'a mut i32) -> &'static mut i32 {
106+ // x
107+ // // ^ error message points at this expression.
108+ // }
109+ //
110+ // We don't need to do this adjustment in the next match arm since
111+ // deref coercions always start with a built-in deref.
112+ if let ExprKind :: Block { body } = expr. kind {
113+ if let Some ( ref last_expr) = body. expr {
114+ span = last_expr. span ;
115+ expr. span = span;
116+ }
117+ }
99118 ExprKind :: Deref { arg : expr. to_ref ( ) }
100119 }
101120 Adjust :: Deref ( Some ( deref) ) => {
@@ -180,6 +199,13 @@ fn apply_adjustment<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
180199 ExprKind :: Use { source : cast_expr. to_ref ( ) }
181200 }
182201 Adjust :: Unsize => {
202+ // See the above comment for Adjust::Deref
203+ if let ExprKind :: Block { body } = expr. kind {
204+ if let Some ( ref last_expr) = body. expr {
205+ span = last_expr. span ;
206+ expr. span = span;
207+ }
208+ }
183209 ExprKind :: Unsize { source : expr. to_ref ( ) }
184210 }
185211 } ;
0 commit comments