Skip to content

Commit 4c2409c

Browse files
committed
Add error description for E0436
Part of #32777
1 parent a0c3259 commit 4c2409c

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

src/librustc_typeck/diagnostics.rs

+46-1
Original file line numberDiff line numberDiff line change
@@ -3405,6 +3405,52 @@ parameters. You can read more about it in the API documentation:
34053405
https://doc.rust-lang.org/std/marker/struct.PhantomData.html
34063406
"##,
34073407

3408+
E0436: r##"
3409+
A struct can include `..` to indicate that you want to use a copy of some other
3410+
struct for some of the values. This functional update syntax can only be used
3411+
on struct types. It is an error to use it on other types, such as enums.
3412+
3413+
Example of erroneous code:
3414+
3415+
```compile_fail
3416+
enum Foo {
3417+
A { x: u32, y: u32 }
3418+
}
3419+
3420+
fn bar() {
3421+
baz(Foo::A { x: 5, y: 10 });
3422+
}
3423+
3424+
fn baz(Foo f) -> Foo {
3425+
Foo::A { x: 6, ..f } // error: functional record update syntax requires
3426+
// a struct
3427+
}
3428+
```
3429+
3430+
To fix this, you need to manually initialize the value. For enums, you should
3431+
match on the variants and copy the values to the new enum value:
3432+
3433+
```
3434+
enum Foo {
3435+
A { x: u32, y: u32 }
3436+
}
3437+
3438+
fn bar() {
3439+
baz(Foo::A { x: 5, y: 10 });
3440+
}
3441+
3442+
fn baz(Foo f) -> Foo {
3443+
match f {
3444+
// Match each variant of the enum and bind each field to a name.
3445+
Foo::A { y: y, .. } =>
3446+
// Create a new value of the same enum type, set the field you
3447+
// want to update and the other fields that were bound.
3448+
Foo::A { x: 6, y: y },
3449+
}
3450+
}
3451+
```
3452+
"##,
3453+
34083454
E0439: r##"
34093455
The length of the platform-intrinsic function `simd_shuffle`
34103456
wasn't specified. Erroneous code example:
@@ -3759,7 +3805,6 @@ register_diagnostics! {
37593805
// type because its default value `{}` references the type `Self`"
37603806
E0399, // trait items need to be implemented because the associated
37613807
// type `{}` was overridden
3762-
E0436, // functional record update requires a struct
37633808
E0513, // no type for local variable ..
37643809
E0521 // redundant default implementations of trait
37653810
}

0 commit comments

Comments
 (0)