Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Account for type params on method without parens
  • Loading branch information
estebank committed Feb 9, 2020
1 parent a19edd6 commit 97d47a5
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/librustc_typeck/check/expr.rs
Expand Up @@ -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();
Expand All @@ -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");
Expand Down
18 changes: 13 additions & 5 deletions src/librustc_typeck/check/method/mod.rs
Expand Up @@ -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(
Expand All @@ -144,21 +144,29 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
method_name,
IsSuggestion(false),
self_ty,
call_expr_id,
call_expr.hir_id,
ProbeScope::TraitsInScope,
)
.and_then(|pick| {
let sig = self.tcx.fn_sig(pick.item.def_id);
Ok(sig.inputs().skip_binder().len() > 1)
});

// Account for `foo.bar<T>`;
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
Expand Down
5 changes: 5 additions & 0 deletions src/test/ui/suggestions/method-missing-parentheses.rs
@@ -0,0 +1,5 @@
fn main() {
let _ = vec![].into_iter().collect::<usize>;
//~^ ERROR attempted to take value of method `collect` on type `std::vec::IntoIter<_>`
//~| ERROR field expressions may not have generic arguments
}
17 changes: 17 additions & 0 deletions 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::<usize>;
| ^^^^^^^

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::<usize>;
| ^^^^^^^---------
| |
| help: use parentheses to call the method: `collect::<usize>()`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0615`.

0 comments on commit 97d47a5

Please sign in to comment.