@@ -68,6 +68,26 @@ impl<'tcx> Mirror<'tcx> for &'tcx hir::Expr {
68
68
}
69
69
}
70
70
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
+
71
91
fn apply_adjustment < ' a , ' gcx , ' tcx > ( cx : & mut Cx < ' a , ' gcx , ' tcx > ,
72
92
hir_expr : & ' tcx hir:: Expr ,
73
93
mut expr : Expr < ' tcx > ,
@@ -76,12 +96,7 @@ fn apply_adjustment<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
76
96
let Expr { temp_lifetime, mut span, .. } = expr;
77
97
let kind = match adjustment. kind {
78
98
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) ;
85
100
ExprKind :: Pointer { cast : PointerCast :: Unsize , source : expr. to_ref ( ) }
86
101
}
87
102
Adjust :: Pointer ( cast) => {
@@ -91,28 +106,12 @@ fn apply_adjustment<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
91
106
ExprKind :: NeverToAny { source : expr. to_ref ( ) }
92
107
}
93
108
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) ;
113
110
ExprKind :: Deref { arg : expr. to_ref ( ) }
114
111
}
115
112
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.
116
115
let call = deref. method_call ( cx. tcx ( ) , expr. ty ) ;
117
116
118
117
expr = Expr {
0 commit comments