Skip to content

Commit cf8a1b0

Browse files
committed
Add E0408/E0409
1 parent cae42a4 commit cf8a1b0

File tree

1 file changed

+87
-4
lines changed

1 file changed

+87
-4
lines changed

src/librustc_resolve/diagnostics.rs

+87-4
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,92 @@ impl Bar {
497497
```
498498
"##,
499499

500+
E0408: r##"
501+
An "or" pattern was used where the variable bindings are not consistently bound
502+
across patterns.
503+
504+
Example of erroneous code:
505+
506+
```compile_fail
507+
match x {
508+
Some(y) | None => { /* use y */ } // error: variable `y` from pattern #1 is
509+
// not bound in pattern #2
510+
_ => ()
511+
}
512+
```
513+
514+
515+
Here, `y` is bound to the contents of the `Some` and can be used within the
516+
block corresponding to the match arm. However, in case `x` is `None`, we have
517+
not specified what `y` is, and the block will use a nonexistent variable.
518+
519+
To fix this error, either split into multiple match arms:
520+
521+
```
522+
let x = Some(1);
523+
match x {
524+
Some(y) => { /* use y */ }
525+
None => { /* ... */ }
526+
}
527+
```
528+
529+
or, bind the variable to a field of the same type in all sub-patterns of the
530+
or pattern:
531+
532+
```
533+
let x = (0,2);
534+
match x {
535+
(0, y) | (y, 0) => { /* use y */}
536+
}
537+
```
538+
539+
In this example, if `x` matches the pattern `(0, _)`, the second field is set
540+
to `y`, and if it matches `(_, 0)`, the first field is set to `y`, so in all
541+
cases `y` is set to some value.
542+
"##,
543+
544+
E0409: r##"
545+
An "or" pattern was used where the variable bindings are not consistently bound
546+
across patterns.
547+
548+
Example of erroneous code:
549+
550+
```compile_fail
551+
let x = (0,2);
552+
match x {
553+
(0, ref y) | (y, 0) => { /* use y */} // error: variable `y` is bound with
554+
// different mode in pattern #2 than
555+
// in pattern #1
556+
_ => ()
557+
}
558+
```
559+
560+
561+
Here, `y` is bound by-value in one case and by-reference in the other.
562+
563+
To fix this error, just use the same mode in both cases.
564+
Generally using `ref` or `ref mut` where not already used will fix this.
565+
566+
```
567+
let x = (0,2);
568+
match x {
569+
(0, ref y) | (ref y, 0) => { /* use y */}
570+
_ => ()
571+
}
572+
```
573+
574+
Alternatively, split the pattern
575+
576+
```compile_fail
577+
let x = (0,2);
578+
match x {
579+
(0, ref y) => { /* use y */}
580+
(y, 0) => { /* use y */ }
581+
_ => ()
582+
}
583+
```
584+
"##,
585+
500586
E0411: r##"
501587
The `Self` keyword was used outside an impl or a trait. Erroneous code example:
502588
@@ -1145,10 +1231,7 @@ register_diagnostics! {
11451231
// E0258,
11461232
E0402, // cannot use an outer type parameter in this context
11471233
E0406, // undeclared associated type
1148-
E0408, // variable from pattern #1 is not bound in pattern #
1149-
E0409, // variable is bound with different mode in pattern # than in
1150-
// pattern #1
1151-
E0410, // variable from pattern is not bound in pattern 1
1234+
// E0410, merged into 408
11521235
E0418, // is not an enum variant, struct or const
11531236
E0420, // is not an associated const
11541237
E0421, // unresolved associated const

0 commit comments

Comments
 (0)