Skip to content

Refactor Adjust and CastKind #59987

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 20, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 24 additions & 25 deletions src/librustc_mir/hair/cx/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,26 @@ impl<'tcx> Mirror<'tcx> for &'tcx hir::Expr {
}
}

/// Adjust the span from the block, to the last expression of the
/// block. This is a better span when returning a mutable reference
/// with too short a lifetime. The error message will use the span
/// from the assignment to the return place, which should only point
/// at the returned value, not the entire function body.
///
/// fn return_short_lived<'a>(x: &'a mut i32) -> &'static mut i32 {
/// x
/// // ^ error message points at this expression.
/// }
fn adjust_span<'tcx>(expr: &mut Expr<'tcx>) -> Span {
if let ExprKind::Block { body } = expr.kind {
if let Some(ref last_expr) = body.expr {
expr.span = last_expr.span;
}
}

expr.span
}

fn apply_adjustment<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
hir_expr: &'tcx hir::Expr,
mut expr: Expr<'tcx>,
Expand All @@ -76,12 +96,7 @@ fn apply_adjustment<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
let Expr { temp_lifetime, mut span, .. } = expr;
let kind = match adjustment.kind {
Adjust::Pointer(PointerCast::Unsize) => {
if let ExprKind::Block { body } = expr.kind {
if let Some(ref last_expr) = body.expr {
span = last_expr.span;
expr.span = span;
}
}
span = adjust_span(&mut expr);
ExprKind::Pointer { cast: PointerCast::Unsize, source: expr.to_ref() }
}
Adjust::Pointer(cast) => {
Expand All @@ -91,28 +106,12 @@ fn apply_adjustment<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
ExprKind::NeverToAny { source: expr.to_ref() }
}
Adjust::Deref(None) => {
// Adjust the span from the block, to the last expression of the
// block. This is a better span when returning a mutable reference
// with too short a lifetime. The error message will use the span
// from the assignment to the return place, which should only point
// at the returned value, not the entire function body.
//
// fn return_short_lived<'a>(x: &'a mut i32) -> &'static mut i32 {
// x
// // ^ error message points at this expression.
// }
//
// We don't need to do this adjustment in the next match arm since
// deref coercions always start with a built-in deref.
if let ExprKind::Block { body } = expr.kind {
if let Some(ref last_expr) = body.expr {
span = last_expr.span;
expr.span = span;
}
}
span = adjust_span(&mut expr);
ExprKind::Deref { arg: expr.to_ref() }
}
Adjust::Deref(Some(deref)) => {
// We don't need to do call adjust_span here since
// deref coercions always start with a built-in deref.
let call = deref.method_call(cx.tcx(), expr.ty);

expr = Expr {
Expand Down