Skip to content

Commit 6a003ab

Browse files
committed
Auto merge of #25593 - nham:E0210_E0326, r=alexcrichton
Changes: - adds explanations for E0185, E0186, E0202, E0326 - fixes the explanation for E0053. The previous description was too narrow; there are other error cases. - changes the error message for E0202 to be specific for associated types, since it seems inherent associated constants are implemented. Part of #24407
2 parents 0ea80fa + cc9d1de commit 6a003ab

File tree

3 files changed

+82
-30
lines changed

3 files changed

+82
-30
lines changed

src/librustc_typeck/collect.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,7 @@ fn convert_item(ccx: &CrateCtxt, it: &ast::Item) {
876876
if let ast::TypeImplItem(ref ty) = impl_item.node {
877877
if opt_trait_ref.is_none() {
878878
span_err!(tcx.sess, impl_item.span, E0202,
879-
"associated items are not allowed in inherent impls");
879+
"associated types are not allowed in inherent impls");
880880
}
881881

882882
as_refsociated_type(ccx, ImplContainer(local_def(it.id)),

src/librustc_typeck/diagnostics.rs

Lines changed: 80 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -223,40 +223,27 @@ impl Foo for Bar {
223223
"##,
224224

225225
E0053: r##"
226-
For any given method of a trait, the mutabilities of the parameters must match
227-
between the trait definition and the implementation.
226+
The parameters of any trait method must match between a trait implementation
227+
and the trait definition.
228228
229-
Here's an example where the mutability of the `self` parameter is wrong:
229+
Here are a couple examples of this error:
230230
231231
```
232-
trait Foo { fn foo(&self); }
233-
234-
struct Bar;
235-
236-
impl Foo for Bar {
237-
// error, the signature should be `fn foo(&self)` instead
238-
fn foo(&mut self) { }
232+
trait Foo {
233+
fn foo(x: u16);
234+
fn bar(&self);
239235
}
240236
241-
fn main() {}
242-
```
243-
244-
Here's another example, this time for a non-`self` parameter:
245-
246-
```
247-
trait Foo { fn foo(x: &mut bool) -> bool; }
248-
249237
struct Bar;
250238
251239
impl Foo for Bar {
252-
// error, the type of `x` should be `&mut bool` instead
253-
fn foo(x: &bool) -> bool { *x }
254-
}
240+
// error, expected u16, found i16
241+
fn foo(x: i16) { }
255242
256-
fn main() {}
243+
// error, values differ in mutability
244+
fn foo(&mut self) { }
245+
}
257246
```
258-
259-
260247
"##,
261248

262249
E0054: r##"
@@ -678,6 +665,48 @@ it has been disabled for now.
678665
[iss20126]: https://github.com/rust-lang/rust/issues/20126
679666
"##,
680667

668+
E0185: r##"
669+
An associated function for a trait was defined to be static, but an
670+
implementation of the trait declared the same function to be a method (i.e. to
671+
take a `self` parameter).
672+
673+
Here's an example of this error:
674+
675+
```
676+
trait Foo {
677+
fn foo();
678+
}
679+
680+
struct Bar;
681+
682+
impl Foo for Bar {
683+
// error, method `foo` has a `&self` declaration in the impl, but not in
684+
// the trait
685+
fn foo(&self) {}
686+
}
687+
"##,
688+
689+
E0186: r##"
690+
An associated function for a trait was defined to be a method (i.e. to take a
691+
`self` parameter), but an implementation of the trait declared the same function
692+
to be static.
693+
694+
Here's an example of this error:
695+
696+
```
697+
trait Foo {
698+
fn foo(&self);
699+
}
700+
701+
struct Bar;
702+
703+
impl Foo for Bar {
704+
// error, method `foo` has a `&self` declaration in the trait, but not in
705+
// the impl
706+
fn foo() {}
707+
}
708+
"##,
709+
681710
E0197: r##"
682711
Inherent implementations (one that do not implement a trait but provide
683712
methods associated with a type) are always safe because they are not
@@ -766,6 +795,14 @@ impl Foo {
766795
```
767796
"##,
768797

798+
E0202: r##"
799+
Inherent associated types were part of [RFC 195] but are not yet implemented.
800+
See [the tracking issue][iss8995] for the status of this implementation.
801+
802+
[RFC 195]: https://github.com/rust-lang/rfcs/pull/195
803+
[iss8995]: https://github.com/rust-lang/rust/issues/8995
804+
"##,
805+
769806
E0204: r##"
770807
An attempt to implement the `Copy` trait for a struct failed because one of the
771808
fields does not implement `Copy`. To fix this, you must implement `Copy` for the
@@ -906,6 +943,25 @@ for types as needed by the compiler, and it is currently disallowed to
906943
explicitly implement it for a type.
907944
"##,
908945

946+
E0326: r##"
947+
The types of any associated constants in a trait implementation must match the
948+
types in the trait definition. This error indicates that there was a mismatch.
949+
950+
Here's an example of this error:
951+
952+
```
953+
trait Foo {
954+
const BAR: bool;
955+
}
956+
957+
struct Bar;
958+
959+
impl Foo for Bar {
960+
const BAR: u32 = 5; // error, expected bool, found u32
961+
}
962+
```
963+
"##,
964+
909965
E0368: r##"
910966
This error indicates that a binary assignment operator like `+=` or `^=` was
911967
applied to the wrong types.
@@ -1037,8 +1093,6 @@ register_diagnostics! {
10371093
E0174, // explicit use of unboxed closure methods are experimental
10381094
E0182,
10391095
E0183,
1040-
E0185,
1041-
E0186,
10421096
E0187, // can't infer the kind of the closure
10431097
E0188, // can not cast a immutable reference to a mutable pointer
10441098
E0189, // deprecated: can only cast a boxed pointer to a boxed object
@@ -1050,7 +1104,6 @@ register_diagnostics! {
10501104
E0194,
10511105
E0195, // lifetime parameters or bounds on method do not match the trait declaration
10521106
E0196, // cannot determine a type for this closure
1053-
E0202, // associated items are not allowed in inherent impls
10541107
E0203, // type parameter has more than one relaxed default bound,
10551108
// and only one is supported
10561109
E0207, // type parameter is not constrained by the impl trait, self type, or predicate
@@ -1100,7 +1153,6 @@ register_diagnostics! {
11001153
E0323, // implemented an associated const when another trait item expected
11011154
E0324, // implemented a method when another trait item expected
11021155
E0325, // implemented an associated type when another trait item expected
1103-
E0326, // associated const implemented with different type from trait
11041156
E0327, // referred to method instead of constant in match pattern
11051157
E0328, // cannot implement Unsize explicitly
11061158
E0329, // associated const depends on type parameter or Self.

src/test/compile-fail/assoc-inherent.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
struct Foo;
1414

1515
impl Foo {
16-
type Bar = isize; //~ERROR associated items are not allowed in inherent impls
16+
type Bar = isize; //~ERROR associated types are not allowed in inherent impls
1717
}
1818

1919
fn main() {}

0 commit comments

Comments
 (0)