Skip to content

Commit 7cf9991

Browse files
committed
added review on if let guard
1 parent d1ce423 commit 7cf9991

18 files changed

+151
-246
lines changed

compiler/rustc_hir_analysis/src/check/region.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -412,11 +412,7 @@ fn resolve_expr<'tcx>(
412412

413413
hir::ExprKind::If(cond, then, Some(otherwise)) => {
414414
let expr_cx = visitor.cx;
415-
let data = if expr.span.at_least_rust_2024() {
416-
ScopeData::IfThenRescope
417-
} else {
418-
ScopeData::IfThen
419-
};
415+
let data = ScopeData::IfThenRescope;
420416
visitor.enter_scope(Scope { local_id: then.hir_id.local_id, data });
421417
visitor.cx.var_parent = visitor.cx.parent;
422418
visitor.visit_expr(cond);
@@ -427,11 +423,7 @@ fn resolve_expr<'tcx>(
427423

428424
hir::ExprKind::If(cond, then, None) => {
429425
let expr_cx = visitor.cx;
430-
let data = if expr.span.at_least_rust_2024() {
431-
ScopeData::IfThenRescope
432-
} else {
433-
ScopeData::IfThen
434-
};
426+
let data = ScopeData::IfThen;
435427
visitor.enter_scope(Scope { local_id: then.hir_id.local_id, data });
436428
visitor.cx.var_parent = visitor.cx.parent;
437429
visitor.visit_expr(cond);

compiler/rustc_lint/src/if_let_rescope.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_middle::ty::significant_drop_order::{
1111
extract_component_with_significant_dtor, ty_dtor_span,
1212
};
1313
use rustc_middle::ty::{self, Ty, TyCtxt};
14-
use rustc_session::lint::{FutureIncompatibilityReason, LintId};
14+
use rustc_session::lint::FutureIncompatibilityReason;
1515
use rustc_session::{declare_lint, impl_lint_pass};
1616
use rustc_span::edition::Edition;
1717
use rustc_span::{DUMMY_SP, Span};
@@ -270,12 +270,6 @@ impl_lint_pass!(
270270

271271
impl<'tcx> LateLintPass<'tcx> for IfLetRescope {
272272
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'tcx>) {
273-
if expr.span.edition().at_least_rust_2024()
274-
|| cx.tcx.lints_that_dont_need_to_run(()).contains(&LintId::of(IF_LET_RESCOPE))
275-
{
276-
return;
277-
}
278-
279273
if let hir::ExprKind::Loop(block, _label, hir::LoopSource::While, _span) = expr.kind
280274
&& let Some(value) = block.expr
281275
&& let hir::ExprKind::If(cond, _conseq, _alt) = value.kind

compiler/rustc_mir_build/src/thir/cx/expr.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -819,13 +819,7 @@ impl<'tcx> ThirBuildCx<'tcx> {
819819
hir::ExprKind::If(cond, then, else_opt) => ExprKind::If {
820820
if_then_scope: region::Scope {
821821
local_id: then.hir_id.local_id,
822-
data: {
823-
if expr.span.at_least_rust_2024() {
824-
region::ScopeData::IfThenRescope
825-
} else {
826-
region::ScopeData::IfThen
827-
}
828-
},
822+
data: region::ScopeData::IfThenRescope,
829823
},
830824
cond: self.mirror_expr(cond),
831825
then: self.mirror_expr(then),

src/doc/if-let-guard-review.md

+58-58
Large diffs are not rendered by default.

tests/ui/rfcs/rfc-2294-if-let-guard/drop-order.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
// For normal guards all temporaries are dropped before the body of the arm.
33
// For let guards temporaries live until the end of the arm.
44

5-
//@ run-pass
5+
//@ revisions: edition2021 edition2024
6+
//@ [edition2024] run-pass
7+
//@ [edition2021] run-pass
68

79
#![feature(if_let_guard)]
810
#![allow(irrefutable_let_patterns)]
@@ -49,11 +51,7 @@ fn main() {
4951
if_guard(2);
5052
if_let_guard(1);
5153
if_let_guard(2);
52-
let expected = [
53-
1, 2, !2, 3, !3, !1,
54-
1, 2, !2, 3, !3, !1,
55-
1, 2, 3, !3, !2, !1,
56-
1, 2, 3, !3, !2, !1,
57-
];
54+
let expected =
55+
[1, 2, !2, 3, !3, !1, 1, 2, !2, 3, !3, !1, 1, 2, 3, !3, !2, !1, 1, 2, 3, !3, !2, !1];
5856
assert_eq!(*A.lock().unwrap(), expected);
5957
}

tests/ui/rfcs/rfc-2294-if-let-guard/drop-scope.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// Ensure that temporaries in if-let guards live for the arm
22
// regression test for #118593
33

4-
//@ check-pass
4+
//@ revisions: edition2021 edition2024
5+
//@ [edition2024] check-pass
6+
//@ [edition2021] check-pass
57

68
#![feature(if_let_guard)]
79
#![feature(let_chains)]

tests/ui/rfcs/rfc-2294-if-let-guard/guard-lifetime-2.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// References to by-mutable-ref bindings in an if-let guard *can* be used after the guard.
22

3-
//@ check-pass
3+
//@ revisions: edition2021 edition2024
4+
//@ [edition2024] check-pass
5+
//@ [edition2021] check-pass
46

57
#![feature(if_let_guard)]
68

tests/ui/rfcs/rfc-2294-if-let-guard/loop-mutability.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
//@ check-pass
1+
//@ revisions: edition2021 edition2024
2+
//@ [edition2024] check-pass
3+
//@ [edition2021] check-pass
24

35
#![feature(if_let_guard)]
46

tests/ui/rfcs/rfc-2294-if-let-guard/move-guard-if-let-chain.rs

+34-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//@ edition: 2024
2+
//@ edition: 2018
23
#![feature(if_let_guard)]
34
#![allow(irrefutable_let_patterns)]
45

@@ -8,7 +9,12 @@ fn same_pattern(c: bool) {
89
let v = (1, 2);
910

1011
match v {
11-
(1, 2) if let y = x && c => (),
12+
(1, 2)
13+
if let y = x
14+
&& c =>
15+
{
16+
()
17+
}
1218
(1, 2) if let z = x => (), //~ ERROR use of moved value: `x`
1319
_ => (),
1420
}
@@ -32,7 +38,12 @@ fn different_patterns(c: bool) {
3238
let v = (1, 2);
3339

3440
match v {
35-
(1, _) if let y = x && c => (),
41+
(1, _)
42+
if let y = x
43+
&& c =>
44+
{
45+
()
46+
}
3647
(_, 2) if let z = x => (), //~ ERROR use of moved value: `x`
3748
_ => (),
3849
}
@@ -56,7 +67,12 @@ fn or_pattern(c: bool) {
5667
let v = (1, 2);
5768

5869
match v {
59-
(1, _) | (_, 2) if let y = x && c => (), //~ ERROR use of moved value: `x`
70+
(1, _) | (_, 2)
71+
if let y = x
72+
&& c =>
73+
{
74+
()
75+
} //~ ERROR use of moved value: `x`
6076
_ => (),
6177
}
6278
}
@@ -78,8 +94,13 @@ fn use_in_arm(c: bool) {
7894
let v = (1, 2);
7995

8096
match v {
81-
(1, 2) if let y = x && c => false,
82-
_ => { *x == 1 }, //~ ERROR use of moved value: `x`
97+
(1, 2)
98+
if let y = x
99+
&& c =>
100+
{
101+
false
102+
}
103+
_ => *x == 1, //~ ERROR use of moved value: `x`
83104
};
84105
}
85106

@@ -90,7 +111,7 @@ fn use_in_arm_ok(c: bool) {
90111

91112
match v {
92113
(1, 2) if c && let y = x => false,
93-
_ => { *x == 1 },
114+
_ => *x == 1,
94115
};
95116
}
96117

@@ -100,7 +121,13 @@ fn use_in_same_chain(c: bool) {
100121
let v = (1, 2);
101122

102123
match v {
103-
(1, 2) if let y = x && c && let z = x => false, //~ ERROR use of moved value: `x`
124+
(1, 2)
125+
if let y = x
126+
&& c
127+
&& let z = x =>
128+
{
129+
false
130+
} //~ ERROR use of moved value: `x`
104131
_ => true,
105132
};
106133
}
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,2 @@
1-
error[E0382]: use of moved value: `x`
2-
--> $DIR/move-guard-if-let-chain.rs:12:23
3-
|
4-
LL | let x: Box<_> = Box::new(1);
5-
| - move occurs because `x` has type `Box<i32>`, which does not implement the `Copy` trait
6-
...
7-
LL | (1, 2) if let y = x && c => (),
8-
| - value moved here
9-
LL | (1, 2) if let z = x => (),
10-
| ^ value used here after move
11-
|
12-
help: borrow this binding in the pattern to avoid moving the value
13-
|
14-
LL | (1, 2) if let ref y = x && c => (),
15-
| +++
1+
error: Option 'edition' given more than once
162

17-
error[E0382]: use of moved value: `x`
18-
--> $DIR/move-guard-if-let-chain.rs:36:23
19-
|
20-
LL | let x: Box<_> = Box::new(1);
21-
| - move occurs because `x` has type `Box<i32>`, which does not implement the `Copy` trait
22-
...
23-
LL | (1, _) if let y = x && c => (),
24-
| - value moved here
25-
LL | (_, 2) if let z = x => (),
26-
| ^ value used here after move
27-
|
28-
help: borrow this binding in the pattern to avoid moving the value
29-
|
30-
LL | (1, _) if let ref y = x && c => (),
31-
| +++
32-
33-
error[E0382]: use of moved value: `x`
34-
--> $DIR/move-guard-if-let-chain.rs:59:32
35-
|
36-
LL | let x: Box<_> = Box::new(1);
37-
| - move occurs because `x` has type `Box<i32>`, which does not implement the `Copy` trait
38-
...
39-
LL | (1, _) | (_, 2) if let y = x && c => (),
40-
| ^ value used here after move
41-
|
42-
help: borrow this binding in the pattern to avoid moving the value
43-
|
44-
LL | (1, _) | (_, 2) if let ref y = x && c => (),
45-
| +++
46-
47-
error[E0382]: use of moved value: `x`
48-
--> $DIR/move-guard-if-let-chain.rs:82:16
49-
|
50-
LL | let x: Box<_> = Box::new(1);
51-
| - move occurs because `x` has type `Box<i32>`, which does not implement the `Copy` trait
52-
...
53-
LL | (1, 2) if let y = x && c => false,
54-
| - value moved here
55-
LL | _ => { *x == 1 },
56-
| ^^ value used here after move
57-
|
58-
help: borrow this binding in the pattern to avoid moving the value
59-
|
60-
LL | (1, 2) if let ref y = x && c => false,
61-
| +++
62-
63-
error[E0382]: use of moved value: `x`
64-
--> $DIR/move-guard-if-let-chain.rs:103:41
65-
|
66-
LL | let x: Box<_> = Box::new(1);
67-
| - move occurs because `x` has type `Box<i32>`, which does not implement the `Copy` trait
68-
...
69-
LL | (1, 2) if let y = x && c && let z = x => false,
70-
| - ^ value used here after move
71-
| |
72-
| value moved here
73-
|
74-
help: borrow this binding in the pattern to avoid moving the value
75-
|
76-
LL | (1, 2) if let ref y = x && c && let z = x => false,
77-
| +++
78-
79-
error: aborting due to 5 previous errors
80-
81-
For more information about this error, try `rustc --explain E0382`.

tests/ui/rfcs/rfc-2294-if-let-guard/move-guard-if-let.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// Check that borrowck knows that moves in the pattern for if-let guards
22
// only happen when the pattern is matched.
33

4-
//@ build-pass
4+
//@ revisions: edition2021 edition2024
5+
//@ [edition2024] build-pass
6+
//@ [edition2021] build-pass
57

68
#![feature(if_let_guard)]
79
#![allow(irrefutable_let_patterns)]
@@ -36,6 +38,6 @@ fn main() {
3638

3739
match v {
3840
(1, 2) if let y = x => false,
39-
_ => { *x == 1 },
41+
_ => *x == 1,
4042
};
4143
}

tests/ui/rfcs/rfc-2294-if-let-guard/parens.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//@ edition: 2024
2+
//@ edition: 2021
23
// Parenthesised let "expressions" are not allowed in guards
34

45
#![feature(if_let_guard)]
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,2 @@
1-
error: expected expression, found `let` statement
2-
--> $DIR/parens.rs:10:16
3-
|
4-
LL | () if (let 0 = 1) => {}
5-
| ^^^^^^^^^
6-
|
7-
= note: only supported directly in conditions of `if` and `while` expressions
8-
note: `let`s wrapped in parentheses are not supported in a context with let chains
9-
--> $DIR/parens.rs:10:16
10-
|
11-
LL | () if (let 0 = 1) => {}
12-
| ^^^^^^^^^
13-
14-
error: expected expression, found `let` statement
15-
--> $DIR/parens.rs:12:18
16-
|
17-
LL | () if (((let 0 = 1))) => {}
18-
| ^^^^^^^^^
19-
|
20-
= note: only supported directly in conditions of `if` and `while` expressions
21-
note: `let`s wrapped in parentheses are not supported in a context with let chains
22-
--> $DIR/parens.rs:12:18
23-
|
24-
LL | () if (((let 0 = 1))) => {}
25-
| ^^^^^^^^^
26-
27-
error: expected expression, found `let` statement
28-
--> $DIR/parens.rs:20:16
29-
|
30-
LL | () if (let 0 = 1) => {}
31-
| ^^^^^^^^^
32-
|
33-
= note: only supported directly in conditions of `if` and `while` expressions
34-
note: `let`s wrapped in parentheses are not supported in a context with let chains
35-
--> $DIR/parens.rs:20:16
36-
|
37-
LL | () if (let 0 = 1) => {}
38-
| ^^^^^^^^^
39-
40-
error: expected expression, found `let` statement
41-
--> $DIR/parens.rs:22:18
42-
|
43-
LL | () if (((let 0 = 1))) => {}
44-
| ^^^^^^^^^
45-
|
46-
= note: only supported directly in conditions of `if` and `while` expressions
47-
note: `let`s wrapped in parentheses are not supported in a context with let chains
48-
--> $DIR/parens.rs:22:18
49-
|
50-
LL | () if (((let 0 = 1))) => {}
51-
| ^^^^^^^^^
52-
53-
error: aborting due to 4 previous errors
1+
error: Option 'edition' given more than once
542

0 commit comments

Comments
 (0)