Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix let_chains and if_let_guard feature flags #93213

Merged
merged 1 commit into from Jan 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 17 additions & 3 deletions compiler/rustc_parse/src/parser/expr.rs
Expand Up @@ -2383,16 +2383,30 @@ impl<'a> Parser<'a> {
}

pub(super) fn parse_arm(&mut self) -> PResult<'a, Arm> {
fn check_let_expr(expr: &Expr) -> (bool, bool) {
match expr.kind {
ExprKind::Binary(_, ref lhs, ref rhs) => {
let lhs_rslt = check_let_expr(lhs);
let rhs_rslt = check_let_expr(rhs);
(lhs_rslt.0 || rhs_rslt.0, false)
}
ExprKind::Let(..) => (true, true),
_ => (false, true),
}
}
let attrs = self.parse_outer_attributes()?;
self.collect_tokens_trailing_token(attrs, ForceCollect::No, |this, attrs| {
let lo = this.token.span;
let pat = this.parse_pat_allow_top_alt(None, RecoverComma::Yes, RecoverColon::Yes)?;
let guard = if this.eat_keyword(kw::If) {
let if_span = this.prev_token.span;
let cond = this.parse_expr()?;
if let ExprKind::Let(..) = cond.kind {
// Remove the last feature gating of a `let` expression since it's stable.
this.sess.gated_spans.ungate_last(sym::let_chains, cond.span);
let (has_let_expr, does_not_have_bin_op) = check_let_expr(&cond);
if has_let_expr {
if does_not_have_bin_op {
// Remove the last feature gating of a `let` expression since it's stable.
this.sess.gated_spans.ungate_last(sym::let_chains, cond.span);
}
let span = if_span.to(cond.span);
this.sess.gated_spans.gate(sym::if_let_guard, span);
}
Expand Down
13 changes: 9 additions & 4 deletions src/test/ui/rfc-2294-if-let-guard/feature-gate.rs
Expand Up @@ -14,10 +14,12 @@ fn _if_let_guard() {
//~^ ERROR `let` expressions in this position are unstable

() if true && let 0 = 1 => {}
//~^ ERROR `let` expressions in this position are unstable
//~^ ERROR `if let` guards are experimental
//~| ERROR `let` expressions in this position are unstable

() if let 0 = 1 && true => {}
//~^ ERROR `let` expressions in this position are unstable
//~^ ERROR `if let` guards are experimental
//~| ERROR `let` expressions in this position are unstable

() if (let 0 = 1) && true => {}
//~^ ERROR `let` expressions in this position are unstable
Expand All @@ -30,14 +32,17 @@ fn _if_let_guard() {
//~| ERROR `let` expressions in this position are unstable

() if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 = 5) => {}
//~^ ERROR `let` expressions in this position are unstable
//~^ ERROR `if let` guards are experimental
//~| ERROR `let` expressions in this position are unstable
//~| ERROR `let` expressions in this position are unstable
//~| ERROR `let` expressions in this position are unstable
//~| ERROR `let` expressions in this position are unstable
//~| ERROR `let` expressions in this position are unstable

() if let Range { start: _, end: _ } = (true..true) && false => {}
//~^ ERROR `let` expressions in this position are unstable
//~^ ERROR `if let` guards are experimental
//~| ERROR `let` expressions in this position are unstable

_ => {}
}
}
Expand Down
72 changes: 56 additions & 16 deletions src/test/ui/rfc-2294-if-let-guard/feature-gate.stderr
@@ -1,5 +1,5 @@
error: no rules expected the token `let`
--> $DIR/feature-gate.rs:64:15
--> $DIR/feature-gate.rs:69:15
|
LL | macro_rules! use_expr {
| --------------------- when calling this macro
Expand All @@ -18,7 +18,47 @@ LL | () if let 0 = 1 => {}
= help: you can write `if matches!(<expr>, <pattern>)` instead of `if let <pattern> = <expr>`

error[E0658]: `if let` guards are experimental
--> $DIR/feature-gate.rs:60:12
--> $DIR/feature-gate.rs:16:12
|
LL | () if true && let 0 = 1 => {}
| ^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #51114 <https://github.com/rust-lang/rust/issues/51114> for more information
= help: add `#![feature(if_let_guard)]` to the crate attributes to enable
= help: you can write `if matches!(<expr>, <pattern>)` instead of `if let <pattern> = <expr>`

error[E0658]: `if let` guards are experimental
--> $DIR/feature-gate.rs:20:12
|
LL | () if let 0 = 1 && true => {}
| ^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #51114 <https://github.com/rust-lang/rust/issues/51114> for more information
= help: add `#![feature(if_let_guard)]` to the crate attributes to enable
= help: you can write `if matches!(<expr>, <pattern>)` instead of `if let <pattern> = <expr>`

error[E0658]: `if let` guards are experimental
--> $DIR/feature-gate.rs:34:12
|
LL | () if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 = 5) => {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #51114 <https://github.com/rust-lang/rust/issues/51114> for more information
= help: add `#![feature(if_let_guard)]` to the crate attributes to enable
= help: you can write `if matches!(<expr>, <pattern>)` instead of `if let <pattern> = <expr>`

error[E0658]: `if let` guards are experimental
--> $DIR/feature-gate.rs:42:12
|
LL | () if let Range { start: _, end: _ } = (true..true) && false => {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #51114 <https://github.com/rust-lang/rust/issues/51114> for more information
= help: add `#![feature(if_let_guard)]` to the crate attributes to enable
= help: you can write `if matches!(<expr>, <pattern>)` instead of `if let <pattern> = <expr>`

error[E0658]: `if let` guards are experimental
--> $DIR/feature-gate.rs:65:12
|
LL | () if let 0 = 1 => {}
| ^^^^^^^^^^^^
Expand Down Expand Up @@ -55,7 +95,7 @@ LL | () if true && let 0 = 1 => {}
= help: add `#![feature(let_chains)]` to the crate attributes to enable

error[E0658]: `let` expressions in this position are unstable
--> $DIR/feature-gate.rs:19:15
--> $DIR/feature-gate.rs:20:15
|
LL | () if let 0 = 1 && true => {}
| ^^^^^^^^^
Expand All @@ -64,7 +104,7 @@ LL | () if let 0 = 1 && true => {}
= help: add `#![feature(let_chains)]` to the crate attributes to enable

error[E0658]: `let` expressions in this position are unstable
--> $DIR/feature-gate.rs:22:16
--> $DIR/feature-gate.rs:24:16
|
LL | () if (let 0 = 1) && true => {}
| ^^^^^^^^^
Expand All @@ -73,7 +113,7 @@ LL | () if (let 0 = 1) && true => {}
= help: add `#![feature(let_chains)]` to the crate attributes to enable

error[E0658]: `let` expressions in this position are unstable
--> $DIR/feature-gate.rs:25:24
--> $DIR/feature-gate.rs:27:24
|
LL | () if true && (let 0 = 1) => {}
| ^^^^^^^^^
Expand All @@ -82,7 +122,7 @@ LL | () if true && (let 0 = 1) => {}
= help: add `#![feature(let_chains)]` to the crate attributes to enable

error[E0658]: `let` expressions in this position are unstable
--> $DIR/feature-gate.rs:28:16
--> $DIR/feature-gate.rs:30:16
|
LL | () if (let 0 = 1) && (let 0 = 1) => {}
| ^^^^^^^^^
Expand All @@ -91,7 +131,7 @@ LL | () if (let 0 = 1) && (let 0 = 1) => {}
= help: add `#![feature(let_chains)]` to the crate attributes to enable

error[E0658]: `let` expressions in this position are unstable
--> $DIR/feature-gate.rs:28:31
--> $DIR/feature-gate.rs:30:31
|
LL | () if (let 0 = 1) && (let 0 = 1) => {}
| ^^^^^^^^^
Expand All @@ -100,7 +140,7 @@ LL | () if (let 0 = 1) && (let 0 = 1) => {}
= help: add `#![feature(let_chains)]` to the crate attributes to enable

error[E0658]: `let` expressions in this position are unstable
--> $DIR/feature-gate.rs:32:15
--> $DIR/feature-gate.rs:34:15
|
LL | () if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 = 5) => {}
| ^^^^^^^^^
Expand All @@ -109,7 +149,7 @@ LL | () if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 =
= help: add `#![feature(let_chains)]` to the crate attributes to enable

error[E0658]: `let` expressions in this position are unstable
--> $DIR/feature-gate.rs:32:28
--> $DIR/feature-gate.rs:34:28
|
LL | () if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 = 5) => {}
| ^^^^^^^^^
Expand All @@ -118,7 +158,7 @@ LL | () if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 =
= help: add `#![feature(let_chains)]` to the crate attributes to enable

error[E0658]: `let` expressions in this position are unstable
--> $DIR/feature-gate.rs:32:42
--> $DIR/feature-gate.rs:34:42
|
LL | () if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 = 5) => {}
| ^^^^^^^^^
Expand All @@ -127,7 +167,7 @@ LL | () if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 =
= help: add `#![feature(let_chains)]` to the crate attributes to enable

error[E0658]: `let` expressions in this position are unstable
--> $DIR/feature-gate.rs:32:55
--> $DIR/feature-gate.rs:34:55
|
LL | () if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 = 5) => {}
| ^^^^^^^^^
Expand All @@ -136,7 +176,7 @@ LL | () if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 =
= help: add `#![feature(let_chains)]` to the crate attributes to enable

error[E0658]: `let` expressions in this position are unstable
--> $DIR/feature-gate.rs:32:68
--> $DIR/feature-gate.rs:34:68
|
LL | () if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 = 5) => {}
| ^^^^^^^^^
Expand All @@ -145,7 +185,7 @@ LL | () if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 =
= help: add `#![feature(let_chains)]` to the crate attributes to enable

error[E0658]: `let` expressions in this position are unstable
--> $DIR/feature-gate.rs:39:15
--> $DIR/feature-gate.rs:42:15
|
LL | () if let Range { start: _, end: _ } = (true..true) && false => {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -154,7 +194,7 @@ LL | () if let Range { start: _, end: _ } = (true..true) && false => {}
= help: add `#![feature(let_chains)]` to the crate attributes to enable

error[E0658]: `let` expressions in this position are unstable
--> $DIR/feature-gate.rs:54:16
--> $DIR/feature-gate.rs:59:16
|
LL | use_expr!((let 0 = 1 && 0 == 0));
| ^^^^^^^^^
Expand All @@ -163,14 +203,14 @@ LL | use_expr!((let 0 = 1 && 0 == 0));
= help: add `#![feature(let_chains)]` to the crate attributes to enable

error[E0658]: `let` expressions in this position are unstable
--> $DIR/feature-gate.rs:56:16
--> $DIR/feature-gate.rs:61:16
|
LL | use_expr!((let 0 = 1));
| ^^^^^^^^^
|
= note: see issue #53667 <https://github.com/rust-lang/rust/issues/53667> for more information
= help: add `#![feature(let_chains)]` to the crate attributes to enable

error: aborting due to 19 previous errors
error: aborting due to 23 previous errors

For more information about this error, try `rustc --explain E0658`.
8 changes: 8 additions & 0 deletions src/test/ui/rfc-2497-if-let-chains/issue-93150.rs
@@ -0,0 +1,8 @@
fn main() {
match true {
_ if let true = true && true => {}
//~^ ERROR `if let` guards are
//~| ERROR `let` expressions in this
_ => {}
}
}
22 changes: 22 additions & 0 deletions src/test/ui/rfc-2497-if-let-chains/issue-93150.stderr
@@ -0,0 +1,22 @@
error[E0658]: `if let` guards are experimental
--> $DIR/issue-93150.rs:3:11
|
LL | _ if let true = true && true => {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #51114 <https://github.com/rust-lang/rust/issues/51114> for more information
= help: add `#![feature(if_let_guard)]` to the crate attributes to enable
= help: you can write `if matches!(<expr>, <pattern>)` instead of `if let <pattern> = <expr>`

error[E0658]: `let` expressions in this position are unstable
--> $DIR/issue-93150.rs:3:14
|
LL | _ if let true = true && true => {}
| ^^^^^^^^^^^^^^^
|
= note: see issue #53667 <https://github.com/rust-lang/rust/issues/53667> for more information
= help: add `#![feature(let_chains)]` to the crate attributes to enable

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0658`.