@@ -157,81 +157,6 @@ match x {
157157See also the error E0303.
158158"## ,
159159
160- E0008 : r##"
161- Names bound in match arms retain their type in pattern guards. As such, if a
162- name is bound by move in a pattern, it should also be moved to wherever it is
163- referenced in the pattern guard code. Doing so however would prevent the name
164- from being available in the body of the match arm. Consider the following:
165-
166- ```compile_fail,E0008
167- match Some("hi".to_string()) {
168- Some(s) if s.len() == 0 => {}, // use s.
169- _ => {},
170- }
171- ```
172-
173- The variable `s` has type `String`, and its use in the guard is as a variable of
174- type `String`. The guard code effectively executes in a separate scope to the
175- body of the arm, so the value would be moved into this anonymous scope and
176- therefore becomes unavailable in the body of the arm.
177-
178- The problem above can be solved by using the `ref` keyword.
179-
180- ```
181- match Some("hi".to_string()) {
182- Some(ref s) if s.len() == 0 => {},
183- _ => {},
184- }
185- ```
186-
187- Though this example seems innocuous and easy to solve, the problem becomes clear
188- when it encounters functions which consume the value:
189-
190- ```compile_fail,E0008
191- struct A{}
192-
193- impl A {
194- fn consume(self) -> usize {
195- 0
196- }
197- }
198-
199- fn main() {
200- let a = Some(A{});
201- match a {
202- Some(y) if y.consume() > 0 => {}
203- _ => {}
204- }
205- }
206- ```
207-
208- In this situation, even the `ref` keyword cannot solve it, since borrowed
209- content cannot be moved. This problem cannot be solved generally. If the value
210- can be cloned, here is a not-so-specific solution:
211-
212- ```
213- #[derive(Clone)]
214- struct A{}
215-
216- impl A {
217- fn consume(self) -> usize {
218- 0
219- }
220- }
221-
222- fn main() {
223- let a = Some(A{});
224- match a{
225- Some(ref y) if y.clone().consume() > 0 => {}
226- _ => {}
227- }
228- }
229- ```
230-
231- If the value will be consumed in the pattern guard, using its clone will not
232- move its ownership, so the code works.
233- "## ,
234-
235160E0009 : r##"
236161In a pattern, all values that don't implement the `Copy` trait have to be bound
237162the same way. The goal here is to avoid binding simultaneously by-move and
@@ -475,13 +400,15 @@ for item in xs {
475400"## ,
476401
477402E0301 : r##"
403+ #### Note: this error code is no longer emitted by the compiler.
404+
478405Mutable borrows are not allowed in pattern guards, because matching cannot have
479406side effects. Side effects could alter the matched object or the environment
480407on which the match depends in such a way, that the match would not be
481408exhaustive. For instance, the following would not match any arm if mutable
482409borrows were allowed:
483410
484- ```compile_fail,E0301
411+ ```compile_fail,E0596
485412match Some(()) {
486413 None => { },
487414 option if option.take().is_none() => {
@@ -493,13 +420,15 @@ match Some(()) {
493420"## ,
494421
495422E0302 : r##"
423+ #### Note: this error code is no longer emitted by the compiler.
424+
496425Assignments are not allowed in pattern guards, because matching cannot have
497426side effects. Side effects could alter the matched object or the environment
498427on which the match depends in such a way, that the match would not be
499428exhaustive. For instance, the following would not match any arm if assignments
500429were allowed:
501430
502- ```compile_fail,E0302
431+ ```compile_fail,E0594
503432match Some(()) {
504433 None => { },
505434 option if { option = None; false } => { },
0 commit comments