Skip to content

Commit 88638df

Browse files
committed
Auto merge of #161827 - GuillaumeGomez:trait-mut-mismatch, r=mejrs
Fix suggestion for mutability mismatch between trait def and trait impl in 2015 edition This code: ```rust mod bar { pub struct X; } pub trait A { fn f(x: &mut bar::X); } mod b { pub struct X; impl crate::A for X { fn f(_x: &crate::bar::X) {} } } ``` emits: ```console error[E0053]: method `f` has an incompatible type for trait --> foo.rs:13:18 | 13 | fn f(_x: &crate::bar::X) {} | ^^^^^^^^^^^^^^ types differ in mutability | note: type in trait --> foo.rs:6:13 | 6 | fn f(x: &mut bar::X); | ^^^^^^^^^^^ = note: expected signature `fn(&mut X)` found signature `fn(&X)` help: change the parameter type to match the trait | 13 - fn f(_x: &crate::bar::X) {} 13 + fn f(_x: &mut bar::X) {} | ``` Which is wrong. This PR fixes it. However the `Ty` doesn't hold a context (or at least not a correct one) allowing to have a correct path to the `X` type. So instead, in case of a mutability mismatch, we (try to) retrieve the code snippet, and remove/add the `mut` keyword. If we fail to, we revert to the current behaviour. r? @mejrs
2 parents 1303417 + 7542191 commit 88638df

37 files changed

Lines changed: 382 additions & 183 deletions

‎compiler/rustc_hir_analysis/src/check/compare_impl_item.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,12 +1066,12 @@ fn report_trait_method_mismatch<'tcx>(
10661066
};
10671067
};
10681068
} else if let Some(trait_ty) = trait_sig.inputs().get(*i) {
1069-
diag.span_suggestion_verbose(
1069+
rustc_middle::ty::print::with_crate_prefix!(diag.span_suggestion_verbose(
10701070
impl_err_span,
10711071
"change the parameter type to match the trait",
10721072
trait_ty,
10731073
Applicability::MachineApplicable,
1074-
);
1074+
));
10751075
}
10761076
}
10771077
_ => {}

‎compiler/rustc_middle/src/ty/print/pretty.rs‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2395,12 +2395,9 @@ impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> {
23952395
fn print_crate_name(&mut self, cnum: CrateNum) -> Result<(), PrintError> {
23962396
self.empty_path = true;
23972397
if cnum == LOCAL_CRATE && !with_resolve_crate_name() {
2398-
if self.tcx.sess.at_least_rust_2018() {
2399-
// We add the `crate::` keyword on Rust 2018, only when desired.
2400-
if with_crate_prefix() {
2401-
write!(self, "{}", kw::Crate)?;
2402-
self.empty_path = false;
2403-
}
2398+
if with_crate_prefix() {
2399+
write!(self, "{}", kw::Crate)?;
2400+
self.empty_path = false;
24042401
}
24052402
} else {
24062403
write!(self, "{}", self.tcx.crate_name(cnum))?;

‎tests/ui/coherence/coherence_inherent.stderr‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | s.the_fn();
77
= help: items from traits can only be used if the trait is in scope
88
help: trait `TheTrait` which provides `the_fn` is implemented but not in scope; perhaps you want to import it
99
|
10-
LL + use Lib::TheTrait;
10+
LL + use crate::Lib::TheTrait;
1111
|
1212

1313
error: aborting due to 1 previous error

‎tests/ui/coherence/coherence_inherent_cc.stderr‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | s.the_fn();
77
= help: items from traits can only be used if the trait is in scope
88
help: trait `TheTrait` which provides `the_fn` is implemented but not in scope; perhaps you want to import it
99
|
10-
LL + use coherence_inherent_cc_lib::TheTrait;
10+
LL + use crate::coherence_inherent_cc_lib::TheTrait;
1111
|
1212

1313
error: aborting due to 1 previous error

‎tests/ui/hygiene/trait_items.stderr‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ LL | pub macro m() { ().f() }
1313
= help: items from traits can only be used if the trait is in scope
1414
help: trait `T` which provides `f` is implemented but not in scope; perhaps you want to import it
1515
|
16-
LL + use foo::T;
16+
LL + use crate::foo::T;
1717
|
1818

1919
error: aborting due to 1 previous error

‎tests/ui/impl-restriction/impl-restriction-check.e2015.stderr‎

Lines changed: 0 additions & 98 deletions
This file was deleted.

‎tests/ui/impl-restriction/impl-restriction-check.rs‎

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
//@ aux-build: external-impl-restriction.rs
2-
//@ revisions: e2015 e2018
3-
//@ [e2015] edition: 2015
4-
//@ [e2018] edition: 2018..
2+
53
#![feature(impl_restriction)]
64

75
extern crate external_impl_restriction as external;
@@ -26,23 +24,23 @@ pub mod foo {
2624
impl FooBar for i16 {} // OK
2725
}
2826

29-
impl bar::Foo for i8 {} //[e2015]~ ERROR trait cannot be implemented outside `foo::bar`
30-
//[e2018]~^ ERROR trait cannot be implemented outside `crate::foo::bar`
27+
impl bar::Foo for i8 {}
28+
//~^ ERROR trait cannot be implemented outside `crate::foo::bar`
3129
impl bar::Bar for i8 {} // OK
3230
impl bar::Baz for i8 {} // OK
33-
impl bar::Qux for i8 {} //[e2015]~ ERROR trait cannot be implemented outside `foo::bar`
34-
//[e2018]~^ ERROR trait cannot be implemented outside `crate::foo::bar`
31+
impl bar::Qux for i8 {}
32+
//~^ ERROR trait cannot be implemented outside `crate::foo::bar`
3533
impl bar::FooBar for i8 {} // OK
3634
}
3735

38-
impl foo::bar::Foo for u8 {} //[e2015]~ ERROR trait cannot be implemented outside `foo::bar`
39-
//[e2018]~^ ERROR trait cannot be implemented outside `crate::foo::bar`
40-
impl foo::bar::Bar for u8 {} //[e2015]~ ERROR trait cannot be implemented outside `foo`
41-
//[e2018]~^ ERROR trait cannot be implemented outside `crate::foo`
36+
impl foo::bar::Foo for u8 {}
37+
//~^ ERROR trait cannot be implemented outside `crate::foo::bar`
38+
impl foo::bar::Bar for u8 {}
39+
//~^ ERROR trait cannot be implemented outside `crate::foo`
4240
impl foo::bar::Baz for u8 {} // OK
43-
impl foo::bar::Qux for u8 {} //[e2015]~ ERROR trait cannot be implemented outside `foo::bar`
44-
//[e2018]~^ ERROR trait cannot be implemented outside `crate::foo::bar`
45-
impl foo::bar::FooBar for u8 {} //[e2015]~ ERROR trait cannot be implemented outside `foo`
46-
//[e2018]~^ ERROR trait cannot be implemented outside `crate::foo`
41+
impl foo::bar::Qux for u8 {}
42+
//~^ ERROR trait cannot be implemented outside `crate::foo::bar`
43+
impl foo::bar::FooBar for u8 {}
44+
//~^ ERROR trait cannot be implemented outside `crate::foo`
4745

4846
fn main() {}

tests/ui/impl-restriction/impl-restriction-check.e2018.stderr renamed to tests/ui/impl-restriction/impl-restriction-check.stderr

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: trait cannot be implemented outside `external`
2-
--> $DIR/impl-restriction-check.rs:11:1
2+
--> $DIR/impl-restriction-check.rs:9:1
33
|
44
LL | impl external::TopLevel for LocalType {}
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -11,7 +11,7 @@ LL | pub impl(crate) trait TopLevel {}
1111
| ^^^^^^^^^^^
1212

1313
error: trait cannot be implemented outside `external`
14-
--> $DIR/impl-restriction-check.rs:12:1
14+
--> $DIR/impl-restriction-check.rs:10:1
1515
|
1616
LL | impl external::inner::Inner for LocalType {}
1717
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -23,73 +23,73 @@ LL | pub impl(self) trait Inner {}
2323
| ^^^^^^^^^^
2424

2525
error: trait cannot be implemented outside `crate::foo::bar`
26-
--> $DIR/impl-restriction-check.rs:29:5
26+
--> $DIR/impl-restriction-check.rs:27:5
2727
|
2828
LL | impl bar::Foo for i8 {}
2929
| ^^^^^^^^^^^^^^^^^^^^
3030
|
3131
note: trait restricted here
32-
--> $DIR/impl-restriction-check.rs:16:20
32+
--> $DIR/impl-restriction-check.rs:14:20
3333
|
3434
LL | pub(crate) impl(self) trait Foo {}
3535
| ^^^^^^^^^^
3636

3737
error: trait cannot be implemented outside `crate::foo::bar`
38-
--> $DIR/impl-restriction-check.rs:38:1
38+
--> $DIR/impl-restriction-check.rs:36:1
3939
|
4040
LL | impl foo::bar::Foo for u8 {}
4141
| ^^^^^^^^^^^^^^^^^^^^^^^^^
4242
|
4343
note: trait restricted here
44-
--> $DIR/impl-restriction-check.rs:16:20
44+
--> $DIR/impl-restriction-check.rs:14:20
4545
|
4646
LL | pub(crate) impl(self) trait Foo {}
4747
| ^^^^^^^^^^
4848

4949
error: trait cannot be implemented outside `crate::foo`
50-
--> $DIR/impl-restriction-check.rs:40:1
50+
--> $DIR/impl-restriction-check.rs:38:1
5151
|
5252
LL | impl foo::bar::Bar for u8 {}
5353
| ^^^^^^^^^^^^^^^^^^^^^^^^^
5454
|
5555
note: trait restricted here
56-
--> $DIR/impl-restriction-check.rs:17:20
56+
--> $DIR/impl-restriction-check.rs:15:20
5757
|
5858
LL | pub(crate) impl(super) trait Bar {}
5959
| ^^^^^^^^^^^
6060

6161
error: trait cannot be implemented outside `crate::foo::bar`
62-
--> $DIR/impl-restriction-check.rs:33:5
62+
--> $DIR/impl-restriction-check.rs:31:5
6363
|
6464
LL | impl bar::Qux for i8 {}
6565
| ^^^^^^^^^^^^^^^^^^^^
6666
|
6767
note: trait restricted here
68-
--> $DIR/impl-restriction-check.rs:19:20
68+
--> $DIR/impl-restriction-check.rs:17:20
6969
|
7070
LL | pub(crate) impl(in crate::foo::bar) trait Qux {}
7171
| ^^^^^^^^^^^^^^^^^^^^^^^^
7272

7373
error: trait cannot be implemented outside `crate::foo::bar`
74-
--> $DIR/impl-restriction-check.rs:43:1
74+
--> $DIR/impl-restriction-check.rs:41:1
7575
|
7676
LL | impl foo::bar::Qux for u8 {}
7777
| ^^^^^^^^^^^^^^^^^^^^^^^^^
7878
|
7979
note: trait restricted here
80-
--> $DIR/impl-restriction-check.rs:19:20
80+
--> $DIR/impl-restriction-check.rs:17:20
8181
|
8282
LL | pub(crate) impl(in crate::foo::bar) trait Qux {}
8383
| ^^^^^^^^^^^^^^^^^^^^^^^^
8484

8585
error: trait cannot be implemented outside `crate::foo`
86-
--> $DIR/impl-restriction-check.rs:45:1
86+
--> $DIR/impl-restriction-check.rs:43:1
8787
|
8888
LL | impl foo::bar::FooBar for u8 {}
8989
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9090
|
9191
note: trait restricted here
92-
--> $DIR/impl-restriction-check.rs:20:20
92+
--> $DIR/impl-restriction-check.rs:18:20
9393
|
9494
LL | pub(crate) impl(in crate::foo) trait FooBar {}
9595
| ^^^^^^^^^^^^^^^^^^^

‎tests/ui/impl-trait/call_method_on_inherent_impl_on_rigid_type.next.stderr‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | x.my_debug();
77
= help: items from traits can only be used if the trait is implemented and in scope
88
help: trait `MyDebug` which provides `my_debug` is implemented but not in scope; perhaps you want to import it
99
|
10-
LL + use MyDebug;
10+
LL + use crate::MyDebug;
1111
|
1212

1313
error: aborting due to 1 previous error

‎tests/ui/impl-trait/method/would-constrain-opaque.next.stderr‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | x.method();
77
= help: items from traits can only be used if the trait is implemented and in scope
88
help: trait `Trait` which provides `method` is implemented but not in scope; perhaps you want to import it
99
|
10-
LL + use Trait;
10+
LL + use crate::Trait;
1111
|
1212

1313
error[E0599]: no method named `method` found for reference `&_` in the current scope
@@ -19,7 +19,7 @@ LL | x.method();
1919
= help: items from traits can only be used if the trait is implemented and in scope
2020
help: trait `Trait` which provides `method` is implemented but not in scope; perhaps you want to import it
2121
|
22-
LL + use Trait;
22+
LL + use crate::Trait;
2323
|
2424

2525
error: aborting due to 2 previous errors

0 commit comments

Comments
 (0)