From 97d47a5e7c41274eacbec55a4c08112407c78ff5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sat, 8 Feb 2020 20:02:54 -0800 Subject: [PATCH] Account for type params on method without parens --- src/librustc_typeck/check/expr.rs | 4 ++-- src/librustc_typeck/check/method/mod.rs | 18 +++++++++++++----- .../suggestions/method-missing-parentheses.rs | 5 +++++ .../method-missing-parentheses.stderr | 17 +++++++++++++++++ 4 files changed, 37 insertions(+), 7 deletions(-) create mode 100644 src/test/ui/suggestions/method-missing-parentheses.rs create mode 100644 src/test/ui/suggestions/method-missing-parentheses.stderr diff --git a/src/librustc_typeck/check/expr.rs b/src/librustc_typeck/check/expr.rs index 9ce89bd636304..90b7b300da9d5 100644 --- a/src/librustc_typeck/check/expr.rs +++ b/src/librustc_typeck/check/expr.rs @@ -1586,7 +1586,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { &format!("a method `{}` also exists, call it with parentheses", field), field, expr_t, - expr.hir_id, + expr, ); } err.emit(); @@ -1609,7 +1609,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { "use parentheses to call the method", field, expr_t, - expr.hir_id, + expr, ); } else { err.help("methods are immutable and cannot be assigned to"); diff --git a/src/librustc_typeck/check/method/mod.rs b/src/librustc_typeck/check/method/mod.rs index e90c2ef5e4361..a75154b160a5c 100644 --- a/src/librustc_typeck/check/method/mod.rs +++ b/src/librustc_typeck/check/method/mod.rs @@ -135,7 +135,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { msg: &str, method_name: ast::Ident, self_ty: Ty<'tcx>, - call_expr_id: hir::HirId, + call_expr: &hir::Expr<'_>, ) { let has_params = self .probe_for_name( @@ -144,7 +144,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { method_name, IsSuggestion(false), self_ty, - call_expr_id, + call_expr.hir_id, ProbeScope::TraitsInScope, ) .and_then(|pick| { @@ -152,13 +152,21 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { Ok(sig.inputs().skip_binder().len() > 1) }); + // Account for `foo.bar`; + let sugg_span = method_name.span.with_hi(call_expr.span.hi()); + let snippet = self + .tcx + .sess + .source_map() + .span_to_snippet(sugg_span) + .unwrap_or_else(|_| method_name.to_string()); let (suggestion, applicability) = if has_params.unwrap_or_default() { - (format!("{}(...)", method_name), Applicability::HasPlaceholders) + (format!("{}(...)", snippet), Applicability::HasPlaceholders) } else { - (format!("{}()", method_name), Applicability::MaybeIncorrect) + (format!("{}()", snippet), Applicability::MaybeIncorrect) }; - err.span_suggestion(method_name.span, msg, suggestion, applicability); + err.span_suggestion(sugg_span, msg, suggestion, applicability); } /// Performs method lookup. If lookup is successful, it will return the callee diff --git a/src/test/ui/suggestions/method-missing-parentheses.rs b/src/test/ui/suggestions/method-missing-parentheses.rs new file mode 100644 index 0000000000000..e1a85271ad0a3 --- /dev/null +++ b/src/test/ui/suggestions/method-missing-parentheses.rs @@ -0,0 +1,5 @@ +fn main() { + let _ = vec![].into_iter().collect::; + //~^ ERROR attempted to take value of method `collect` on type `std::vec::IntoIter<_>` + //~| ERROR field expressions may not have generic arguments +} diff --git a/src/test/ui/suggestions/method-missing-parentheses.stderr b/src/test/ui/suggestions/method-missing-parentheses.stderr new file mode 100644 index 0000000000000..8569eb6f4ad1e --- /dev/null +++ b/src/test/ui/suggestions/method-missing-parentheses.stderr @@ -0,0 +1,17 @@ +error: field expressions may not have generic arguments + --> $DIR/method-missing-parentheses.rs:2:41 + | +LL | let _ = vec![].into_iter().collect::; + | ^^^^^^^ + +error[E0615]: attempted to take value of method `collect` on type `std::vec::IntoIter<_>` + --> $DIR/method-missing-parentheses.rs:2:32 + | +LL | let _ = vec![].into_iter().collect::; + | ^^^^^^^--------- + | | + | help: use parentheses to call the method: `collect::()` + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0615`.