@@ -78,7 +78,7 @@ fn apply_adjustment<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
78
78
mut expr : Expr < ' tcx > ,
79
79
adjustment : & Adjustment < ' tcx > )
80
80
-> Expr < ' tcx > {
81
- let Expr { temp_lifetime, span, .. } = expr;
81
+ let Expr { temp_lifetime, mut span, .. } = expr;
82
82
let kind = match adjustment. kind {
83
83
Adjust :: ReifyFnPointer => {
84
84
ExprKind :: ReifyFnPointer { source : expr. to_ref ( ) }
@@ -96,6 +96,25 @@ fn apply_adjustment<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
96
96
ExprKind :: Cast { source : expr. to_ref ( ) }
97
97
}
98
98
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
+ }
99
118
ExprKind :: Deref { arg : expr. to_ref ( ) }
100
119
}
101
120
Adjust :: Deref ( Some ( deref) ) => {
0 commit comments