Skip to content

Commit 0e71bda

Browse files
committed
Auto merge of #27586 - GuillaumeGomez:patch-2, r=Manishearth
Part of #24407. This PR doesn't have code example since I didn't find how to raise it. If someone finds a code which does, please say it ! cc @pnkfelix cc @eddyb r? @Manishearth
2 parents 9f227ca + 0642d99 commit 0e71bda

File tree

2 files changed

+185
-7
lines changed

2 files changed

+185
-7
lines changed

src/librustc_typeck/coherence/orphan.rs

+2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ impl<'cx, 'tcx> OrphanChecker<'cx, 'tcx> {
5050
span_err!(self.tcx.sess, span, E0390,
5151
"only a single inherent implementation marked with `#[lang = \"{}\"]` \
5252
is allowed for the `{}` primitive", lang, ty);
53+
span_help!(self.tcx.sess, span,
54+
"consider using a trait to implement these methods");
5355
}
5456
}
5557
}

src/librustc_typeck/diagnostics.rs

+183-7
Original file line numberDiff line numberDiff line change
@@ -2400,6 +2400,132 @@ for types as needed by the compiler, and it is currently disallowed to
24002400
explicitly implement it for a type.
24012401
"##,
24022402

2403+
E0323: r##"
2404+
An associated const was implemented when another trait item was expected.
2405+
Erroneous code example:
2406+
2407+
```
2408+
trait Foo {
2409+
type N;
2410+
}
2411+
2412+
struct Bar;
2413+
2414+
impl Foo for Bar {
2415+
const N : u32 = 0;
2416+
// error: item `N` is an associated const, which doesn't match its
2417+
// trait `<Bar as Foo>`
2418+
}
2419+
```
2420+
2421+
Please verify that the associated const wasn't misspelled and the correct trait
2422+
was implemented. Example:
2423+
2424+
```
2425+
struct Bar;
2426+
2427+
trait Foo {
2428+
type N;
2429+
}
2430+
2431+
impl Foo for Bar {
2432+
type N = u32; // ok!
2433+
}
2434+
2435+
// or:
2436+
trait Foo {
2437+
const N : u32;
2438+
}
2439+
2440+
impl Foo for Bar {
2441+
const N : u32 = 0; // ok!
2442+
}
2443+
```
2444+
"##,
2445+
2446+
E0324: r##"
2447+
A method was implemented when another trait item was expected. Erroneous
2448+
code example:
2449+
2450+
```
2451+
struct Bar;
2452+
2453+
trait Foo {
2454+
const N : u32;
2455+
2456+
fn M();
2457+
}
2458+
2459+
impl Foo for Bar {
2460+
fn N() {}
2461+
// error: item `N` is an associated method, which doesn't match its
2462+
// trait `<Bar as Foo>`
2463+
}
2464+
```
2465+
2466+
To fix this error, please verify that the method name wasn't misspelled and
2467+
verify that you are indeed implementing the correct trait items. Example:
2468+
2469+
```
2470+
struct Bar;
2471+
2472+
trait Foo {
2473+
const N : u32;
2474+
2475+
fn M();
2476+
}
2477+
2478+
impl Foo for Bar {
2479+
const N : u32 = 0;
2480+
2481+
fn M() {} // ok!
2482+
}
2483+
```
2484+
"##,
2485+
2486+
E0325: r##"
2487+
An associated type was implemented when another trait item was expected.
2488+
Erroneous code example:
2489+
2490+
```
2491+
struct Bar;
2492+
2493+
trait Foo {
2494+
const N : u32;
2495+
}
2496+
2497+
impl Foo for Bar {
2498+
type N = u32;
2499+
// error: item `N` is an associated type, which doesn't match its
2500+
// trait `<Bar as Foo>`
2501+
}
2502+
```
2503+
2504+
Please verify that the associated type name wasn't misspelled and your
2505+
implementation corresponds to the trait definition. Example:
2506+
2507+
```
2508+
struct Bar;
2509+
2510+
trait Foo {
2511+
type N;
2512+
}
2513+
2514+
impl Foo for Bar {
2515+
type N = u32; // ok!
2516+
}
2517+
2518+
//or:
2519+
trait Foo {
2520+
const N : u32;
2521+
}
2522+
2523+
impl Foo for Bar {
2524+
const N : u32 = 0; // ok!
2525+
}
2526+
```
2527+
"##,
2528+
24032529
E0326: r##"
24042530
The types of any associated constants in a trait implementation must match the
24052531
types in the trait definition. This error indicates that there was a mismatch.
@@ -2566,6 +2692,31 @@ to change this.
25662692
[RFC 953]: https://github.com/rust-lang/rfcs/pull/953
25672693
"##,
25682694

2695+
E0369: r##"
2696+
A binary operation was attempted on a type which doesn't support it.
2697+
Erroneous code example:
2698+
2699+
```
2700+
let x = 12f32; // error: binary operation `<<` cannot be applied to
2701+
// type `f32`
2702+
2703+
x << 2;
2704+
```
2705+
2706+
To fix this error, please check that this type implements this binary
2707+
operation. Example:
2708+
2709+
```
2710+
let x = 12u32; // the `u32` type does implement it:
2711+
// https://doc.rust-lang.org/stable/std/ops/trait.Shl.html
2712+
2713+
x << 2; // ok!
2714+
```
2715+
2716+
It is also possible to overload most operators for your own type by
2717+
implementing traits from `std::ops`.
2718+
"##,
2719+
25692720
E0371: r##"
25702721
When `Trait2` is a subtrait of `Trait1` (for example, when `Trait2` has a
25712722
definition like `trait Trait2: Trait1 { ... }`), it is not allowed to implement
@@ -2607,6 +2758,37 @@ For more information see the [opt-in builtin traits RFC](https://github.com/rust
26072758
-lang/rfcs/blob/master/text/0019-opt-in-builtin-traits.md).
26082759
"##,
26092760

2761+
E0390: r##"
2762+
You tried to implement methods for a primitive type. Erroneous code example:
2763+
2764+
```
2765+
struct Foo {
2766+
x: i32
2767+
}
2768+
2769+
impl *mut Foo {}
2770+
// error: only a single inherent implementation marked with
2771+
// `#[lang = "mut_ptr"]` is allowed for the `*mut T` primitive
2772+
```
2773+
2774+
This isn't allowed, but using a trait to implement a method is a good solution.
2775+
Example:
2776+
2777+
```
2778+
struct Foo {
2779+
x: i32
2780+
}
2781+
2782+
trait Bar {
2783+
fn bar();
2784+
}
2785+
2786+
impl Bar for *mut Foo {
2787+
fn bar() {} // ok!
2788+
}
2789+
```
2790+
"##,
2791+
26102792
E0391: r##"
26112793
This error indicates that some types or traits depend on each other
26122794
and therefore cannot be constructed.
@@ -2691,7 +2873,7 @@ register_diagnostics! {
26912873
E0085,
26922874
E0086,
26932875
E0090,
2694-
E0103,
2876+
E0103, // @GuillaumeGomez: I was unable to get this error, try your best!
26952877
E0104,
26962878
E0118,
26972879
E0122,
@@ -2750,12 +2932,8 @@ register_diagnostics! {
27502932
E0319, // trait impls for defaulted traits allowed just for structs/enums
27512933
E0320, // recursive overflow during dropck
27522934
E0321, // extended coherence rules for defaulted traits violated
2753-
E0323, // implemented an associated const when another trait item expected
2754-
E0324, // implemented a method when another trait item expected
2755-
E0325, // implemented an associated type when another trait item expected
27562935
E0328, // cannot implement Unsize explicitly
27572936
E0329, // associated const depends on type parameter or Self.
2758-
E0369, // binary operation `<op>` cannot be applied to types
27592937
E0370, // discriminant overflow
27602938
E0374, // the trait `CoerceUnsized` may only be implemented for a coercion
27612939
// between structures with one field being coerced, none found
@@ -2766,8 +2944,6 @@ register_diagnostics! {
27662944
// between structures
27672945
E0377, // the trait `CoerceUnsized` may only be implemented for a coercion
27682946
// between structures with the same definition
2769-
E0390, // only a single inherent implementation marked with
2770-
// `#[lang = \"{}\"]` is allowed for the `{}` primitive
27712947
E0393, // the type parameter `{}` must be explicitly specified in an object
27722948
// type because its default value `{}` references the type `Self`"
27732949
E0399, // trait items need to be implemented because the associated

0 commit comments

Comments
 (0)