Skip to content

Suggest fix when encountering different mutability from impl to trait #52702

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 2 commits into from
Jul 28, 2018
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
11 changes: 11 additions & 0 deletions src/librustc_typeck/check/compare_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,17 @@ fn compare_predicate_entailment<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
E0053,
"method `{}` has an incompatible type for trait",
trait_m.ident);
if let TypeError::Mutability = terr {
if let Some(trait_err_span) = trait_err_span {
if let Ok(trait_err_str) = tcx.sess.codemap().span_to_snippet(trait_err_span) {
diag.span_suggestion(
impl_err_span,
"consider change the type to match the mutability in trait",
format!("{}", trait_err_str),
);
}
}
}

infcx.note_type_err(&mut diag,
&cause,
Expand Down
4 changes: 4 additions & 0 deletions src/test/ui/issue-13033.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ LL | fn bar(&mut self, other: &Foo) {}
|
= note: expected type `fn(&mut Baz, &mut dyn Foo)`
found type `fn(&mut Baz, &dyn Foo)`
help: consider change the type to match the mutability in trait
|
LL | fn bar(&mut self, other: &mut Foo) {}
| ^^^^^^^^

error: aborting due to previous error

Expand Down
4 changes: 4 additions & 0 deletions src/test/ui/mismatched_types/E0053.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ LL | fn bar(&mut self) { }
|
= note: expected type `fn(&Bar)`
found type `fn(&mut Bar)`
help: consider change the type to match the mutability in trait
|
LL | fn bar(&self) { }
| ^^^^^

error: aborting due to 2 previous errors

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ LL | fn bar(&mut self, bar: &Bar) { } //~ ERROR incompatible type
|
= note: expected type `fn(&mut Bar, &mut Bar)`
found type `fn(&mut Bar, &Bar)`
help: consider change the type to match the mutability in trait
|
LL | fn bar(&mut self, bar: &mut Bar) { } //~ ERROR incompatible type
| ^^^^^^^^

error: aborting due to 2 previous errors

Expand Down