Skip to content

Commit 06aef33

Browse files
author
Nick Hamann
committed
Add error explanations for E0053, E0251, E0252, E0255, E0256, E0368.
1 parent a4444aa commit 06aef33

File tree

2 files changed

+164
-8
lines changed

2 files changed

+164
-8
lines changed

src/librustc_resolve/diagnostics.rs

Lines changed: 84 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,88 @@ about what constitutes an Item declaration and what does not:
4949
http://doc.rust-lang.org/reference.html#statements
5050
"##,
5151

52+
E0251: r##"
53+
Two items of the same name cannot be imported without rebinding one of the
54+
items under a new local name.
55+
56+
An example of this error:
57+
58+
```
59+
use foo::baz;
60+
use bar::*; // error, do `use foo::baz as quux` instead on the previous line
61+
62+
fn main() {}
63+
64+
mod foo {
65+
pub struct baz;
66+
}
67+
68+
mod bar {
69+
pub mod baz {}
70+
}
71+
```
72+
"##,
73+
74+
E0252: r##"
75+
Two items of the same name cannot be imported without rebinding one of the
76+
items under a new local name.
77+
78+
An example of this error:
79+
80+
```
81+
use foo::baz;
82+
use bar::baz; // error, do `use bar::baz as quux` instead
83+
84+
fn main() {}
85+
86+
mod foo {
87+
pub struct baz;
88+
}
89+
90+
mod bar {
91+
pub mod baz {}
92+
}
93+
```
94+
"##,
95+
96+
E0255: r##"
97+
You can't import a value whose name is the same as another value defined in the
98+
module.
99+
100+
An example of this error:
101+
102+
```
103+
use foo::FOO; // error, do `use foo::FOO as BAR` instead
104+
105+
fn FOO() {}
106+
107+
mod foo {
108+
pub const FOO: bool = true;
109+
}
110+
111+
fn main() {}
112+
```
113+
"##,
114+
115+
E0256: r##"
116+
You can't import a type or module when the name of the item being imported is
117+
the same as another type or submodule defined in the module.
118+
119+
An example of this error:
120+
121+
```
122+
use foo::Bar; // error
123+
124+
struct Bar;
125+
126+
mod foo {
127+
pub mod Bar { }
128+
}
129+
130+
fn main() {}
131+
```
132+
"##,
133+
52134
E0259: r##"
53135
The name chosen for an external crate conflicts with another external crate that
54136
has been imported into the current module.
@@ -122,14 +204,10 @@ http://doc.rust-lang.org/reference.html#types
122204
register_diagnostics! {
123205
E0157,
124206
E0153,
125-
E0251, // a named type or value has already been imported in this module
126-
E0252, // a named type or value has already been imported in this module
127207
E0253, // not directly importable
128208
E0254, // import conflicts with imported crate in this module
129-
E0255, // import conflicts with value in this module
130-
E0256, // import conflicts with type in this module
131-
E0257, // inherent implementations are only allowed on types defined in the current module
132-
E0258, // import conflicts with existing submodule
209+
E0257,
210+
E0258,
133211
E0364, // item is private
134212
E0365 // item is private
135213
}

src/librustc_typeck/diagnostics.rs

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,44 @@ impl Foo for Bar {
6464
```
6565
"##,
6666

67+
E0053: r##"
68+
In a trait method implementation, the function signature must match exactly.
69+
This error indicates a mutability mismatch between a trait method signature
70+
and the signature of the implementation.
71+
72+
Here's an example where the mutability of the `self` parameter is wrong:
73+
74+
```
75+
trait Foo { fn foo(&self); }
76+
77+
struct Bar;
78+
79+
impl Foo for Bar {
80+
// error, the signature should be `fn foo(&self)` instead
81+
fn foo(&mut self) { }
82+
}
83+
84+
fn main() {}
85+
```
86+
87+
Here's another example, this time for a non-`self` parameter:
88+
89+
```
90+
trait Foo { fn foo(x: &mut bool) -> bool; }
91+
92+
struct Bar;
93+
94+
impl Foo for Bar {
95+
// error, the type of `x` should be `&mut bool` instead
96+
fn foo(x: &bool) -> bool { *x }
97+
}
98+
99+
fn main() {}
100+
```
101+
102+
103+
"##,
104+
67105
E0054: r##"
68106
It is not allowed to cast to a bool. If you are trying to cast a numeric type
69107
to a bool, you can compare it with zero instead:
@@ -483,6 +521,48 @@ The `Sized` trait is a special trait built-in to the compiler for types with a
483521
constant size known at compile-time. This trait is automatically implemented
484522
for types as needed by the compiler, and it is currently disallowed to
485523
explicitly implement it for a type.
524+
"##,
525+
526+
E0368: r##"
527+
This error indicates that a binary assignment operator like `+=` or `^=` was
528+
applied to the wrong types.
529+
530+
A couple examples of this are as follows:
531+
532+
```
533+
let mut x: u16 = 5;
534+
x ^= true; // error, `^=` cannot be applied to types `u16` and `bool`
535+
x += (); // error, `+=` cannot be applied to types `u16` and `()`
536+
```
537+
538+
Another problem you might be facing is this: suppose you've overloaded the `+`
539+
operator for some type `Foo` by implementing the `std::ops::Add` trait for
540+
`Foo`, but you find that using `+=` does not work, as in this example:
541+
542+
```
543+
use std::ops::Add;
544+
545+
struct Foo(u32);
546+
547+
impl Add for Foo {
548+
type Output = Foo;
549+
550+
fn add(self, rhs: Foo) -> Foo {
551+
Foo(self.0 + rhs.0)
552+
}
553+
}
554+
555+
fn main() {
556+
let mut x: Foo = Foo(5);
557+
x += Foo(7); // error, `+= cannot be applied to types `Foo` and `Foo`
558+
}
559+
```
560+
561+
This is because the binary assignment operators currently do not work off of
562+
traits, so it is not possible to overload them. See [RFC 953] for a proposal
563+
to change this.
564+
565+
[RFC 953]: https://github.com/rust-lang/rfcs/pull/953
486566
"##
487567

488568
}
@@ -503,7 +583,6 @@ register_diagnostics! {
503583
E0040, // explicit use of destructor method
504584
E0044, // foreign items may not have type parameters
505585
E0045, // variadic function must have C calling convention
506-
E0053,
507586
E0055, // method has an incompatible type for trait
508587
E0057, // method has an incompatible type for trait
509588
E0059,
@@ -629,7 +708,6 @@ register_diagnostics! {
629708
E0328, // cannot implement Unsize explicitly
630709
E0366, // dropck forbid specialization to concrete type or region
631710
E0367, // dropck forbid specialization to predicate not in struct/enum
632-
E0368, // binary operation `<op>=` cannot be applied to types
633711
E0369, // binary operation `<op>` cannot be applied to types
634712
E0371, // impl Trait for Trait is illegal
635713
E0372, // impl Trait for Trait where Trait is not object safe

0 commit comments

Comments
 (0)