Skip to content
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

Account for type params on method without parentheses #68981

Merged
merged 2 commits into from
Feb 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion src/librustc_parse/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,7 @@ impl<'a> Parser<'a> {
if let Some(args) = segment.args {
self.struct_span_err(
args.span(),
"field expressions may not have generic arguments",
"field expressions cannot have generic arguments",
)
.emit();
estebank marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_typeck/check/expr.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
6 changes: 3 additions & 3 deletions src/test/ui/parser/type-parameters-in-field-exprs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ fn main() {
y: 2,
};
f.x::<isize>;
//~^ ERROR field expressions may not have generic arguments
//~^ ERROR field expressions cannot have generic arguments
f.x::<>;
//~^ ERROR field expressions may not have generic arguments
//~^ ERROR field expressions cannot have generic arguments
f.x::();
//~^ ERROR field expressions may not have generic arguments
//~^ ERROR field expressions cannot have generic arguments
}
6 changes: 3 additions & 3 deletions src/test/ui/parser/type-parameters-in-field-exprs.stderr
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
error: field expressions may not have generic arguments
error: field expressions cannot have generic arguments
--> $DIR/type-parameters-in-field-exprs.rs:11:10
|
LL | f.x::<isize>;
| ^^^^^^^

error: field expressions may not have generic arguments
error: field expressions cannot have generic arguments
--> $DIR/type-parameters-in-field-exprs.rs:13:10
|
LL | f.x::<>;
| ^^

error: field expressions may not have generic arguments
error: field expressions cannot have generic arguments
--> $DIR/type-parameters-in-field-exprs.rs:15:7
|
LL | f.x::();
Expand Down
5 changes: 5 additions & 0 deletions src/test/ui/suggestions/method-missing-parentheses.rs
Original file line number Diff line number Diff line change
@@ -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 cannot have generic arguments
}
17 changes: 17 additions & 0 deletions src/test/ui/suggestions/method-missing-parentheses.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error: field expressions cannot 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`.