Skip to content

Commit 97d47a5

Browse files
committed
Account for type params on method without parens
1 parent a19edd6 commit 97d47a5

File tree

4 files changed

+37
-7
lines changed

4 files changed

+37
-7
lines changed

src/librustc_typeck/check/expr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1586,7 +1586,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
15861586
&format!("a method `{}` also exists, call it with parentheses", field),
15871587
field,
15881588
expr_t,
1589-
expr.hir_id,
1589+
expr,
15901590
);
15911591
}
15921592
err.emit();
@@ -1609,7 +1609,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
16091609
"use parentheses to call the method",
16101610
field,
16111611
expr_t,
1612-
expr.hir_id,
1612+
expr,
16131613
);
16141614
} else {
16151615
err.help("methods are immutable and cannot be assigned to");

src/librustc_typeck/check/method/mod.rs

+13-5
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
135135
msg: &str,
136136
method_name: ast::Ident,
137137
self_ty: Ty<'tcx>,
138-
call_expr_id: hir::HirId,
138+
call_expr: &hir::Expr<'_>,
139139
) {
140140
let has_params = self
141141
.probe_for_name(
@@ -144,21 +144,29 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
144144
method_name,
145145
IsSuggestion(false),
146146
self_ty,
147-
call_expr_id,
147+
call_expr.hir_id,
148148
ProbeScope::TraitsInScope,
149149
)
150150
.and_then(|pick| {
151151
let sig = self.tcx.fn_sig(pick.item.def_id);
152152
Ok(sig.inputs().skip_binder().len() > 1)
153153
});
154154

155+
// Account for `foo.bar<T>`;
156+
let sugg_span = method_name.span.with_hi(call_expr.span.hi());
157+
let snippet = self
158+
.tcx
159+
.sess
160+
.source_map()
161+
.span_to_snippet(sugg_span)
162+
.unwrap_or_else(|_| method_name.to_string());
155163
let (suggestion, applicability) = if has_params.unwrap_or_default() {
156-
(format!("{}(...)", method_name), Applicability::HasPlaceholders)
164+
(format!("{}(...)", snippet), Applicability::HasPlaceholders)
157165
} else {
158-
(format!("{}()", method_name), Applicability::MaybeIncorrect)
166+
(format!("{}()", snippet), Applicability::MaybeIncorrect)
159167
};
160168

161-
err.span_suggestion(method_name.span, msg, suggestion, applicability);
169+
err.span_suggestion(sugg_span, msg, suggestion, applicability);
162170
}
163171

164172
/// Performs method lookup. If lookup is successful, it will return the callee
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn main() {
2+
let _ = vec![].into_iter().collect::<usize>;
3+
//~^ ERROR attempted to take value of method `collect` on type `std::vec::IntoIter<_>`
4+
//~| ERROR field expressions may not have generic arguments
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error: field expressions may not have generic arguments
2+
--> $DIR/method-missing-parentheses.rs:2:41
3+
|
4+
LL | let _ = vec![].into_iter().collect::<usize>;
5+
| ^^^^^^^
6+
7+
error[E0615]: attempted to take value of method `collect` on type `std::vec::IntoIter<_>`
8+
--> $DIR/method-missing-parentheses.rs:2:32
9+
|
10+
LL | let _ = vec![].into_iter().collect::<usize>;
11+
| ^^^^^^^---------
12+
| |
13+
| help: use parentheses to call the method: `collect::<usize>()`
14+
15+
error: aborting due to 2 previous errors
16+
17+
For more information about this error, try `rustc --explain E0615`.

0 commit comments

Comments
 (0)