Skip to content

Add explanation for &mut self method call when expecting -> Self #75613

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

Merged
merged 1 commit into from
Aug 18, 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
1 change: 1 addition & 0 deletions src/librustc_typeck/check/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.suggest_boxing_when_appropriate(err, expr, expected, expr_ty);
self.suggest_missing_await(err, expr, expected, expr_ty);
self.note_need_for_fn_pointer(err, expected, expr_ty);
self.note_internal_mutation_in_method(err, expr, expected, expr_ty);
}

// Requires that the two types unify, and prints an error message if
Expand Down
45 changes: 45 additions & 0 deletions src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5176,6 +5176,51 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
}

fn note_internal_mutation_in_method(
&self,
err: &mut DiagnosticBuilder<'_>,
expr: &hir::Expr<'_>,
expected: Ty<'tcx>,
found: Ty<'tcx>,
) {
if found != self.tcx.types.unit {
return;
}
if let ExprKind::MethodCall(path_segment, _, [rcvr, ..], _) = expr.kind {
if self
.typeck_results
.borrow()
.expr_ty_adjusted_opt(rcvr)
.map_or(true, |ty| expected.peel_refs() != ty.peel_refs())
{
return;
}
let mut sp = MultiSpan::from_span(path_segment.ident.span);
sp.push_span_label(
path_segment.ident.span,
format!(
"this call modifies {} in-place",
match rcvr.kind {
ExprKind::Path(QPath::Resolved(
None,
hir::Path { segments: [segment], .. },
)) => format!("`{}`", segment.ident),
_ => "its receiver".to_string(),
}
),
);
sp.push_span_label(
rcvr.span,
"you probably want to use this value after calling the method...".to_string(),
);
err.span_note(
sp,
&format!("method `{}` modifies its receiver in-place", path_segment.ident),
);
err.note(&format!("...instead of the `()` output of method `{}`", path_segment.ident));
}
}

/// When encountering an `impl Future` where `BoxFuture` is expected, suggest `Box::pin`.
fn suggest_calling_boxed_future_when_appropriate(
&self,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {}
fn foo(mut s: String) -> String {
s.push_str("asdf") //~ ERROR mismatched types
}
20 changes: 20 additions & 0 deletions src/test/ui/suggestions/chain-method-call-mutation-in-place.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
error[E0308]: mismatched types
--> $DIR/chain-method-call-mutation-in-place.rs:3:5
|
LL | fn foo(mut s: String) -> String {
| ------ expected `std::string::String` because of return type
LL | s.push_str("asdf")
| ^^^^^^^^^^^^^^^^^^ expected struct `std::string::String`, found `()`
|
note: method `push_str` modifies its receiver in-place
--> $DIR/chain-method-call-mutation-in-place.rs:3:7
|
LL | s.push_str("asdf")
| - ^^^^^^^^ this call modifies `s` in-place
| |
| you probably want to use this value after calling the method...
= note: ...instead of the `()` output of method `push_str`

error: aborting due to previous error

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