Skip to content

Commit 80c798b

Browse files
authored
Rollup merge of rust-lang#52702 - csmoe:mut_diff, r=estebank
Suggest fix when encountering different mutability from impl to trait Closes rust-lang#52412 r? @estebank
2 parents 4f1e235 + d5347ff commit 80c798b

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed

src/librustc_typeck/check/compare_method.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,17 @@ fn compare_predicate_entailment<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
319319
E0053,
320320
"method `{}` has an incompatible type for trait",
321321
trait_m.ident);
322+
if let TypeError::Mutability = terr {
323+
if let Some(trait_err_span) = trait_err_span {
324+
if let Ok(trait_err_str) = tcx.sess.codemap().span_to_snippet(trait_err_span) {
325+
diag.span_suggestion(
326+
impl_err_span,
327+
"consider change the type to match the mutability in trait",
328+
format!("{}", trait_err_str),
329+
);
330+
}
331+
}
332+
}
322333

323334
infcx.note_type_err(&mut diag,
324335
&cause,

src/test/ui/issue-13033.stderr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ LL | fn bar(&mut self, other: &Foo) {}
99
|
1010
= note: expected type `fn(&mut Baz, &mut dyn Foo)`
1111
found type `fn(&mut Baz, &dyn Foo)`
12+
help: consider change the type to match the mutability in trait
13+
|
14+
LL | fn bar(&mut self, other: &mut Foo) {}
15+
| ^^^^^^^^
1216

1317
error: aborting due to previous error
1418

src/test/ui/mismatched_types/E0053.stderr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ LL | fn bar(&mut self) { }
2121
|
2222
= note: expected type `fn(&Bar)`
2323
found type `fn(&mut Bar)`
24+
help: consider change the type to match the mutability in trait
25+
|
26+
LL | fn bar(&self) { }
27+
| ^^^^^
2428

2529
error: aborting due to 2 previous errors
2630

src/test/ui/mismatched_types/trait-impl-fn-incompatibility.stderr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ LL | fn bar(&mut self, bar: &Bar) { } //~ ERROR incompatible type
2121
|
2222
= note: expected type `fn(&mut Bar, &mut Bar)`
2323
found type `fn(&mut Bar, &Bar)`
24+
help: consider change the type to match the mutability in trait
25+
|
26+
LL | fn bar(&mut self, bar: &mut Bar) { } //~ ERROR incompatible type
27+
| ^^^^^^^^
2428

2529
error: aborting due to 2 previous errors
2630

0 commit comments

Comments
 (0)