From 5e019def0d8c277ef7b82d9348350ed1fc8747db Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Tue, 13 Aug 2019 23:59:22 +0200 Subject: [PATCH] typeck: extract ban_take_value_of_method --- src/librustc_typeck/check/expr.rs | 48 +++++++++++++++++++------------ 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/src/librustc_typeck/check/expr.rs b/src/librustc_typeck/check/expr.rs index b4154c15d673..c25a60d065d6 100644 --- a/src/librustc_typeck/check/expr.rs +++ b/src/librustc_typeck/check/expr.rs @@ -1342,23 +1342,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } else if field.name == kw::Invalid { self.tcx().types.err } else if self.method_exists(field, expr_t, expr.hir_id, true) { - let mut err = type_error_struct!(self.tcx().sess, field.span, expr_t, E0615, - "attempted to take value of method `{}` on type `{}`", - field, expr_t); - - if !self.expr_in_place(expr.hir_id) { - self.suggest_method_call( - &mut err, - "use parentheses to call the method", - field, - expr_t, - expr.hir_id - ); - } else { - err.help("methods are immutable and cannot be assigned to"); - } - - err.emit(); + self.ban_take_value_of_method(expr, expr_t, field); self.tcx().types.err } else { if !expr_t.is_primitive_ty() { @@ -1436,9 +1420,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { expr: &'tcx hir::Expr, expr_t: Ty<'tcx>, field: ast::Ident, - def_id: DefId, + base_did: DefId, ) { - let struct_path = self.tcx().def_path_str(def_id); + let struct_path = self.tcx().def_path_str(base_did); let mut err = struct_span_err!( self.tcx().sess, expr.span, @@ -1462,6 +1446,32 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { err.emit(); } + fn ban_take_value_of_method(&self, expr: &'tcx hir::Expr, expr_t: Ty<'tcx>, field: ast::Ident) { + let mut err = type_error_struct!( + self.tcx().sess, + field.span, + expr_t, + E0615, + "attempted to take value of method `{}` on type `{}`", + field, + expr_t + ); + + if !self.expr_in_place(expr.hir_id) { + self.suggest_method_call( + &mut err, + "use parentheses to call the method", + field, + expr_t, + expr.hir_id + ); + } else { + err.help("methods are immutable and cannot be assigned to"); + } + + err.emit(); + } + fn no_such_field_err(&self, span: Span, field: T, expr_t: &ty::TyS<'_>) -> DiagnosticBuilder<'_> { type_error_struct!(self.tcx().sess, span, expr_t, E0609,