Skip to content

Commit 3ea932a

Browse files
committed
Refer to "self type" instead of "receiver type"
1 parent dfd43f0 commit 3ea932a

25 files changed

+155
-75
lines changed

src/librustc/infer/error_reporting/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1627,7 +1627,7 @@ impl<'tcx> ObligationCause<'tcx> {
16271627
MainFunctionType => Error0580("main function has wrong type"),
16281628
StartFunctionType => Error0308("start function has wrong type"),
16291629
IntrinsicType => Error0308("intrinsic has wrong type"),
1630-
MethodReceiver => Error0308("mismatched method receiver"),
1630+
MethodReceiver => Error0308("mismatched `self` parameter type"),
16311631

16321632
// In the case where we have no more specific thing to
16331633
// say, also take a look at the error code, maybe we can

src/librustc/traits/object_safety.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,15 @@ impl ObjectSafetyViolation {
4747
"the trait cannot use `Self` as a type parameter \
4848
in the supertraits or where-clauses".into(),
4949
ObjectSafetyViolation::Method(name, MethodViolationCode::StaticMethod) =>
50-
format!("method `{}` has no receiver", name).into(),
51-
ObjectSafetyViolation::Method(name, MethodViolationCode::ReferencesSelf) =>
52-
format!("method `{}` references the `Self` type \
53-
in its arguments or return type", name).into(),
54-
ObjectSafetyViolation::Method(name,
55-
MethodViolationCode::WhereClauseReferencesSelf(_)) =>
56-
format!("method `{}` references the `Self` type in where clauses", name).into(),
50+
format!("associated function `{}` has no `self` parameter", name).into(),
51+
ObjectSafetyViolation::Method(name, MethodViolationCode::ReferencesSelf) => format!(
52+
"method `{}` references the `Self` type in its arguments or return type",
53+
name,
54+
).into(),
55+
ObjectSafetyViolation::Method(
56+
name,
57+
MethodViolationCode::WhereClauseReferencesSelf(_),
58+
) => format!("method `{}` references the `Self` type in where clauses", name).into(),
5759
ObjectSafetyViolation::Method(name, MethodViolationCode::Generic) =>
5860
format!("method `{}` has generic type parameters", name).into(),
5961
ObjectSafetyViolation::Method(name, MethodViolationCode::UndispatchableReceiver) =>

src/librustc_typeck/check/wfcheck.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -762,19 +762,19 @@ fn check_opaque_types<'fcx, 'tcx>(
762762
substituted_predicates
763763
}
764764

765+
const HELP_FOR_SELF_TYPE: &str =
766+
"consider changing to `self`, `&self`, `&mut self`, `self: Box<Self>`, \
767+
`self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one \
768+
of the previous types except `Self`)";
769+
765770
fn check_method_receiver<'fcx, 'tcx>(
766771
fcx: &FnCtxt<'fcx, 'tcx>,
767772
method_sig: &hir::MethodSig,
768773
method: &ty::AssocItem,
769774
self_ty: Ty<'tcx>,
770775
) {
771-
const HELP_FOR_SELF_TYPE: &str =
772-
"consider changing to `self`, `&self`, `&mut self`, `self: Box<Self>`, \
773-
`self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one \
774-
of the previous types except `Self`)";
775776
// Check that the method has a valid receiver type, given the type `Self`.
776-
debug!("check_method_receiver({:?}, self_ty={:?})",
777-
method, self_ty);
777+
debug!("check_method_receiver({:?}, self_ty={:?})", method, self_ty);
778778

779779
if !method.method_has_self_argument {
780780
return;
@@ -805,12 +805,7 @@ fn check_method_receiver<'fcx, 'tcx>(
805805
if fcx.tcx.features().arbitrary_self_types {
806806
if !receiver_is_valid(fcx, span, receiver_ty, self_ty, true) {
807807
// Report error; `arbitrary_self_types` was enabled.
808-
fcx.tcx.sess.diagnostic().mut_span_err(
809-
span, &format!("invalid method receiver type: {:?}", receiver_ty)
810-
).note("type of `self` must be `Self` or a type that dereferences to it")
811-
.help(HELP_FOR_SELF_TYPE)
812-
.code(DiagnosticId::Error("E0307".into()))
813-
.emit();
808+
e0307(fcx, span, receiver_ty);
814809
}
815810
} else {
816811
if !receiver_is_valid(fcx, span, receiver_ty, self_ty, false) {
@@ -830,17 +825,22 @@ fn check_method_receiver<'fcx, 'tcx>(
830825
.emit();
831826
} else {
832827
// Report error; would not have worked with `arbitrary_self_types`.
833-
fcx.tcx.sess.diagnostic().mut_span_err(
834-
span, &format!("invalid method receiver type: {:?}", receiver_ty)
835-
).note("type must be `Self` or a type that dereferences to it")
836-
.help(HELP_FOR_SELF_TYPE)
837-
.code(DiagnosticId::Error("E0307".into()))
838-
.emit();
828+
e0307(fcx, span, receiver_ty);
839829
}
840830
}
841831
}
842832
}
843833

834+
fn e0307(fcx: &FnCtxt<'fcx, 'tcx>, span: Span, receiver_ty: Ty<'_>) {
835+
fcx.tcx.sess.diagnostic().mut_span_err(
836+
span,
837+
&format!("invalid `self` parameter type: {:?}", receiver_ty)
838+
).note("type of `self` must be `Self` or a type that dereferences to it")
839+
.help(HELP_FOR_SELF_TYPE)
840+
.code(DiagnosticId::Error("E0307".into()))
841+
.emit();
842+
}
843+
844844
/// Returns whether `receiver_ty` would be considered a valid receiver type for `self_ty`. If
845845
/// `arbitrary_self_types` is enabled, `receiver_ty` must transitively deref to `self_ty`, possibly
846846
/// through a `*const/mut T` raw pointer. If the feature is not enabled, the requirements are more

src/librustc_typeck/error_codes.rs

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2425,6 +2425,83 @@ struct Bar<S, T> { x: Foo<S, T> }
24252425
```
24262426
"##,
24272427

2428+
E0307: r##"
2429+
This error indicates that the `self` parameter in a method has an invalid
2430+
"reciever type".
2431+
2432+
Methods take a special first parameter, of which there are three variants:
2433+
`self`, `&self`, and `&mut self`. The type `Self` acts as an alias to the
2434+
type of the current trait implementor, or "receiver type". Besides the
2435+
already mentioned `Self`, `&Self` and `&mut Self` valid receiver types, the
2436+
following are also valid, if less common: `self: Box<Self>`,
2437+
`self: Rc<Self>`, `self: Arc<Self>`, and `self: Pin<P>` (where P is one of
2438+
the previous types except `Self`).
2439+
2440+
```
2441+
# struct Foo;
2442+
trait Trait {
2443+
fn foo(&self);
2444+
// ^^^^^ this let's you refer to the type that implements this trait
2445+
}
2446+
impl Trait for Foo {
2447+
// ^^^ this is the "receiver type"
2448+
fn foo(&self) {}
2449+
// ^^^^^ this is of type `Foo`
2450+
}
2451+
```
2452+
2453+
The above is equivalent to:
2454+
2455+
```
2456+
# struct Foo;
2457+
# trait Trait {
2458+
# fn foo(&self);
2459+
# }
2460+
impl Trait for Foo {
2461+
fn foo(&self: &Foo) {}
2462+
}
2463+
```
2464+
2465+
When using an invalid reciver type, like in the following example,
2466+
2467+
```compile_fail,E0307
2468+
# struct Foo;
2469+
# struct Bar;
2470+
# trait Trait {
2471+
# fn foo(&self);
2472+
# }
2473+
impl Trait for Struct {
2474+
fn foo(&self: &Bar) {}
2475+
}
2476+
```
2477+
2478+
The nightly feature [Arbintrary self types][AST] extends the accepted
2479+
receiver type to also include any type that can dereference to `Self`:
2480+
2481+
```
2482+
#![feature(arbitrary_self_types)]
2483+
2484+
struct Foo;
2485+
struct Bar;
2486+
2487+
// Because you can dereference `Bar` into `Foo`...
2488+
impl std::ops::Deref for Bar {
2489+
type Target = Foo;
2490+
2491+
fn deref(&self) -> &Foo {
2492+
&Foo
2493+
}
2494+
}
2495+
2496+
impl Foo {
2497+
fn foo(self: Bar) {}
2498+
// ^^^^^^^^^ ...it can be used as the receiver type
2499+
}
2500+
```
2501+
2502+
[AST]: https://doc.rust-lang.org/unstable-book/language-features/arbitrary-self-types.html
2503+
"##,
2504+
24282505
E0321: r##"
24292506
A cross-crate opt-out trait was implemented on something which wasn't a struct
24302507
or enum type. Erroneous code example:
@@ -4851,7 +4928,6 @@ register_diagnostics! {
48514928
// E0247,
48524929
// E0248, // value used as a type, now reported earlier during resolution as E0412
48534930
// E0249,
4854-
E0307, // invalid method `self` type
48554931
// E0319, // trait impls for defaulted traits allowed just for structs/enums
48564932
// E0372, // coherence not object safe
48574933
E0377, // the trait `CoerceUnsized` may only be implemented for a coercion

src/test/ui/did_you_mean/issue-40006.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ error[E0038]: the trait `X` cannot be made into an object
6262
LL | impl dyn X {
6363
| ^^^^^ the trait `X` cannot be made into an object
6464
|
65-
= note: method `xxx` has no receiver
65+
= note: associated function `xxx` has no `self` parameter
6666

6767
error: aborting due to 9 previous errors
6868

src/test/ui/error-codes/E0033-teach.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fn main() {
88
let trait_obj: &dyn SomeTrait = SomeTrait;
99
//~^ ERROR expected value, found trait `SomeTrait`
1010
//~| ERROR E0038
11-
//~| method `foo` has no receiver
11+
//~| associated function `foo` has no `self` parameter
1212

1313
let &invalid = trait_obj;
1414
//~^ ERROR E0033

src/test/ui/error-codes/E0033-teach.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ error[E0038]: the trait `SomeTrait` cannot be made into an object
1010
LL | let trait_obj: &dyn SomeTrait = SomeTrait;
1111
| ^^^^^^^^^^^^^^ the trait `SomeTrait` cannot be made into an object
1212
|
13-
= note: method `foo` has no receiver
13+
= note: associated function `foo` has no `self` parameter
1414

1515
error[E0033]: type `&dyn SomeTrait` cannot be dereferenced
1616
--> $DIR/E0033-teach.rs:13:9

src/test/ui/error-codes/E0033.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ fn main() {
66
let trait_obj: &dyn SomeTrait = SomeTrait;
77
//~^ ERROR expected value, found trait `SomeTrait`
88
//~| ERROR E0038
9-
//~| method `foo` has no receiver
9+
//~| associated function `foo` has no `self` parameter
1010

1111
let &invalid = trait_obj;
1212
//~^ ERROR E0033

src/test/ui/error-codes/E0033.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ error[E0038]: the trait `SomeTrait` cannot be made into an object
1010
LL | let trait_obj: &dyn SomeTrait = SomeTrait;
1111
| ^^^^^^^^^^^^^^ the trait `SomeTrait` cannot be made into an object
1212
|
13-
= note: method `foo` has no receiver
13+
= note: associated function `foo` has no `self` parameter
1414

1515
error[E0033]: type `&dyn SomeTrait` cannot be dereferenced
1616
--> $DIR/E0033.rs:11:9

src/test/ui/explicit/explicit-self-lifetime-mismatch.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ struct Foo<'a,'b> {
66
impl<'a,'b> Foo<'a,'b> {
77
fn bar(self:
88
Foo<'b,'a>
9-
//~^ ERROR mismatched method receiver
9+
//~^ ERROR mismatched `self` parameter type
1010
//~| expected type `Foo<'a, 'b>`
1111
//~| found type `Foo<'b, 'a>`
1212
//~| lifetime mismatch
13-
//~| ERROR mismatched method receiver
13+
//~| ERROR mismatched `self` parameter type
1414
//~| expected type `Foo<'a, 'b>`
1515
//~| found type `Foo<'b, 'a>`
1616
//~| lifetime mismatch

src/test/ui/explicit/explicit-self-lifetime-mismatch.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0308]: mismatched method receiver
1+
error[E0308]: mismatched `self` parameter type
22
--> $DIR/explicit-self-lifetime-mismatch.rs:8:12
33
|
44
LL | Foo<'b,'a>
@@ -17,7 +17,7 @@ note: ...does not necessarily outlive the lifetime 'a as defined on the impl at
1717
LL | impl<'a,'b> Foo<'a,'b> {
1818
| ^^
1919

20-
error[E0308]: mismatched method receiver
20+
error[E0308]: mismatched `self` parameter type
2121
--> $DIR/explicit-self-lifetime-mismatch.rs:8:12
2222
|
2323
LL | Foo<'b,'a>

src/test/ui/issues/issue-17740.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ struct Foo<'a> {
44

55
impl <'a> Foo<'a>{
66
fn bar(self: &mut Foo) {
7-
//~^ mismatched method receiver
7+
//~^ mismatched `self` parameter type
88
//~| expected type `Foo<'a>`
99
//~| found type `Foo<'_>`
1010
//~| lifetime mismatch
11-
//~| mismatched method receiver
11+
//~| mismatched `self` parameter type
1212
//~| expected type `Foo<'a>`
1313
//~| found type `Foo<'_>`
1414
//~| lifetime mismatch

src/test/ui/issues/issue-17740.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0308]: mismatched method receiver
1+
error[E0308]: mismatched `self` parameter type
22
--> $DIR/issue-17740.rs:6:18
33
|
44
LL | fn bar(self: &mut Foo) {
@@ -23,7 +23,7 @@ note: ...does not necessarily outlive the lifetime 'a as defined on the impl at
2323
LL | impl <'a> Foo<'a>{
2424
| ^^
2525

26-
error[E0308]: mismatched method receiver
26+
error[E0308]: mismatched `self` parameter type
2727
--> $DIR/issue-17740.rs:6:18
2828
|
2929
LL | fn bar(self: &mut Foo) {

src/test/ui/issues/issue-17905-2.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ impl Pair<
66
isize
77
> {
88
fn say(self: &Pair<&str, isize>) {
9-
//~^ ERROR mismatched method receiver
10-
//~| ERROR mismatched method receiver
9+
//~^ ERROR mismatched `self` parameter type
10+
//~| ERROR mismatched `self` parameter type
1111
println!("{:?}", self);
1212
}
1313
}

src/test/ui/issues/issue-17905-2.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0308]: mismatched method receiver
1+
error[E0308]: mismatched `self` parameter type
22
--> $DIR/issue-17905-2.rs:8:18
33
|
44
LL | fn say(self: &Pair<&str, isize>) {
@@ -21,7 +21,7 @@ note: ...does not necessarily outlive the lifetime '_ as defined on the impl at
2121
LL | &str,
2222
| ^
2323

24-
error[E0308]: mismatched method receiver
24+
error[E0308]: mismatched `self` parameter type
2525
--> $DIR/issue-17905-2.rs:8:18
2626
|
2727
LL | fn say(self: &Pair<&str, isize>) {

src/test/ui/issues/issue-19380.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0038]: the trait `Qiz` cannot be made into an object
44
LL | foos: &'static [&'static (dyn Qiz + 'static)]
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Qiz` cannot be made into an object
66
|
7-
= note: method `qiz` has no receiver
7+
= note: associated function `qiz` has no `self` parameter
88

99
error: aborting due to previous error
1010

src/test/ui/issues/issue-56806.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
pub trait Trait {
22
fn dyn_instead_of_self(self: Box<dyn Trait>);
3-
//~^ ERROR invalid method receiver type: std::boxed::Box<(dyn Trait + 'static)>
3+
//~^ ERROR invalid `self` parameter type
44
}
55

6-
pub fn main() {
7-
}
6+
pub fn main() {}

src/test/ui/issues/issue-56806.stderr

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
error[E0307]: invalid method receiver type: std::boxed::Box<(dyn Trait + 'static)>
1+
error[E0307]: invalid `self` parameter type: std::boxed::Box<(dyn Trait + 'static)>
22
--> $DIR/issue-56806.rs:2:34
33
|
44
LL | fn dyn_instead_of_self(self: Box<dyn Trait>);
55
| ^^^^^^^^^^^^^^
66
|
7-
= note: type must be `Self` or a type that dereferences to it
7+
= note: type of `self` must be `Self` or a type that dereferences to it
88
= help: consider changing to `self`, `&self`, `&mut self`, `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one of the previous types except `Self`)
99

1010
error: aborting due to previous error
1111

12+
For more information about this error, try `rustc --explain E0307`.

src/test/ui/object-safety/object-safety-no-static.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0038]: the trait `Foo` cannot be made into an object
44
LL | fn foo_implicit<T:Foo+'static>(b: Box<T>) -> Box<dyn Foo + 'static> {
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Foo` cannot be made into an object
66
|
7-
= note: method `foo` has no receiver
7+
= note: associated function `foo` has no `self` parameter
88

99
error: aborting due to previous error
1010

src/test/ui/resolve/issue-3907-2.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0038]: the trait `issue_3907::Foo` cannot be made into an object
44
LL | fn bar(_x: Foo) {}
55
| ^^^^^^^^^^^^^^^ the trait `issue_3907::Foo` cannot be made into an object
66
|
7-
= note: method `bar` has no receiver
7+
= note: associated function `bar` has no `self` parameter
88

99
error: aborting due to previous error
1010

src/test/ui/span/issue-27522.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
struct SomeType {}
44

55
trait Foo {
6-
fn handler(self: &SomeType); //~ ERROR invalid method receiver type
6+
fn handler(self: &SomeType); //~ ERROR invalid `self` parameter type
77
}
88

99
fn main() {}

src/test/ui/span/issue-27522.stderr

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
error[E0307]: invalid method receiver type: &SomeType
1+
error[E0307]: invalid `self` parameter type: &SomeType
22
--> $DIR/issue-27522.rs:6:22
33
|
44
LL | fn handler(self: &SomeType);
55
| ^^^^^^^^^
66
|
7-
= note: type must be `Self` or a type that dereferences to it
7+
= note: type of `self` must be `Self` or a type that dereferences to it
88
= help: consider changing to `self`, `&self`, `&mut self`, `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one of the previous types except `Self`)
99

1010
error: aborting due to previous error
1111

12+
For more information about this error, try `rustc --explain E0307`.

0 commit comments

Comments
 (0)