Skip to content

Commit b2afe6c

Browse files
committed
---
yaml --- r: 278143 b: refs/heads/auto c: 9f302b6 h: refs/heads/master i: 278141: e91660a 278139: a028333 278135: 1c8f3b9 278127: 799717c 278111: 41eb738 278079: 8a78adb 278015: f12855c
1 parent 654c437 commit b2afe6c

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503
88
refs/tags/release-0.3.1: 495bae036dfe5ec6ceafd3312b4dca48741e845b
99
refs/tags/release-0.4: e828ea2080499553b97dfe33b3f4d472b4562ad7
1010
refs/tags/release-0.5: 7e3bcfbf21278251ee936ad53e92e9b719702d73
11-
refs/heads/auto: 5f9e30431021f1c8eae7f4cfa6db9b1d39a8e216
11+
refs/heads/auto: 9f302b6bcdc7b3d35a074ff565e17350f12038b7
1212
refs/tags/release-0.6: b4ebcfa1812664df5e142f0134a5faea3918544c
1313
refs/tags/0.1: b19db808c2793fe2976759b85a355c3ad8c8b336
1414
refs/tags/0.2: 1754d02027f2924bed83b0160ee340c7f41d5ea1

branches/auto/src/librustc_resolve/diagnostics.rs

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,69 @@ let Foo = 12i32; // ok!
623623
The goal here is to avoid a conflict of names.
624624
"##,
625625

626+
E0414: r##"
627+
A variable binding in an irrefutable pattern is shadowing the name of a
628+
constant. Example of erroneous code:
629+
630+
```compile_fail
631+
const FOO: u8 = 7;
632+
633+
let FOO = 5; // error: variable bindings cannot shadow constants
634+
635+
// or
636+
637+
fn bar(FOO: u8) { // error: variable bindings cannot shadow constants
638+
639+
}
640+
641+
// or
642+
643+
for FOO in bar {
644+
645+
}
646+
```
647+
648+
Introducing a new variable in Rust is done through a pattern. Thus you can have
649+
`let` bindings like `let (a, b) = ...`. However, patterns also allow constants
650+
in them, e.g. if you want to match over a constant:
651+
652+
```ignore
653+
const FOO: u8 = 1;
654+
655+
match (x,y) {
656+
(3, 4) => { .. }, // it is (3,4)
657+
(FOO, 1) => { .. }, // it is (1,1)
658+
(foo, 1) => { .. }, // it is (anything, 1)
659+
// call the value in the first slot "foo"
660+
_ => { .. } // it is anything
661+
}
662+
```
663+
664+
Here, the second arm matches the value of `x` against the constant `FOO`,
665+
whereas the third arm will accept any value of `x` and call it `foo`.
666+
667+
This works for `match`, however in cases where an irrefutable pattern is
668+
required, constants can't be used. An irrefutable pattern is one which always
669+
matches, whose purpose is only to bind variable names to values. These are
670+
required by let, for, and function argument patterns.
671+
672+
Refutable patterns in such a situation do not make sense, for example:
673+
674+
```ignore
675+
let Some(x) = foo; // what if foo is None, instead?
676+
677+
let (1, x) = foo; // what if foo.0 is not 1?
678+
679+
let (SOME_CONST, x) = foo; // what if foo.0 is not SOME_CONST?
680+
681+
let SOME_CONST = foo; // what if foo is not SOME_CONST?
682+
```
683+
684+
Thus, an irrefutable variable binding can't contain a constant.
685+
686+
To fix this error, just give the marked variable a different name.
687+
"##,
688+
626689
E0415: r##"
627690
More than one function parameter have the same name. Example of erroneous code:
628691
@@ -1086,7 +1149,6 @@ register_diagnostics! {
10861149
E0409, // variable is bound with different mode in pattern # than in
10871150
// pattern #1
10881151
E0410, // variable from pattern is not bound in pattern 1
1089-
E0414, // only irrefutable patterns allowed here
10901152
E0418, // is not an enum variant, struct or const
10911153
E0420, // is not an associated const
10921154
E0421, // unresolved associated const

0 commit comments

Comments
 (0)