-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Suggest adding missing braces in const
block pattern
#78173
Changes from all commits
5294992
f19fc49
4dbe2ac
24fa10f
4315b85
854f207
d6d30cc
a72f39f
d9dfa89
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// run-pass | ||
|
||
#![allow(incomplete_features)] | ||
#![feature(inline_const)] | ||
|
||
fn if_let_1() -> i32 { | ||
let x = 2; | ||
const Y: i32 = 3; | ||
|
||
if let const { (Y + 1) / 2 } = x { | ||
x | ||
} else { | ||
0 | ||
} | ||
} | ||
|
||
fn if_let_2() -> i32 { | ||
let x = 2; | ||
|
||
if let const { 1 + 2 } = x { | ||
const { 1 + 2 } | ||
} else { | ||
0 | ||
} | ||
} | ||
|
||
fn main() { | ||
assert_eq!(if_let_1(), 2); | ||
assert_eq!(if_let_2(), 0); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// run-rustfix | ||
|
||
// See issue #78168. | ||
|
||
#![allow(incomplete_features)] | ||
#![feature(inline_const)] | ||
|
||
// FIXME(#78171): the lint has to be allowed because of a bug | ||
#[allow(dead_code)] | ||
const fn one() -> i32 { | ||
1 | ||
} | ||
|
||
fn foo() -> i32 { | ||
let x = 2; | ||
|
||
match x { | ||
const { 2 } => {} | ||
//~^ ERROR expected `{`, found `2` | ||
//~| HELP try placing this code inside a block | ||
_ => {} | ||
} | ||
|
||
match x { | ||
const { 1 + 2 * 3 / 4 } => {} | ||
//~^ ERROR expected `{`, found `1` | ||
//~| HELP try placing this code inside a block | ||
_ => {} | ||
} | ||
|
||
match x { | ||
const { one() } => {} | ||
//~^ ERROR expected `{`, found `one` | ||
//~| HELP try placing this code inside a block | ||
_ => {} | ||
} | ||
|
||
x | ||
} | ||
|
||
fn bar() -> i32 { | ||
let x = const { 2 }; | ||
//~^ ERROR expected `{`, found `2` | ||
//~| HELP try placing this code inside a block | ||
|
||
x | ||
} | ||
|
||
fn baz() -> i32 { | ||
let y = const { 1 + 2 * 3 / 4 }; | ||
//~^ ERROR expected `{`, found `1` | ||
//~| HELP try placing this code inside a block | ||
|
||
y | ||
} | ||
|
||
fn main() { | ||
foo(); | ||
bar(); | ||
baz(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// run-rustfix | ||
|
||
// See issue #78168. | ||
|
||
#![allow(incomplete_features)] | ||
#![feature(inline_const)] | ||
|
||
// FIXME(#78171): the lint has to be allowed because of a bug | ||
#[allow(dead_code)] | ||
const fn one() -> i32 { | ||
1 | ||
} | ||
|
||
fn foo() -> i32 { | ||
let x = 2; | ||
|
||
match x { | ||
const 2 => {} | ||
//~^ ERROR expected `{`, found `2` | ||
//~| HELP try placing this code inside a block | ||
_ => {} | ||
} | ||
|
||
match x { | ||
const 1 + 2 * 3 / 4 => {} | ||
//~^ ERROR expected `{`, found `1` | ||
//~| HELP try placing this code inside a block | ||
_ => {} | ||
} | ||
|
||
match x { | ||
const one() => {} | ||
//~^ ERROR expected `{`, found `one` | ||
//~| HELP try placing this code inside a block | ||
_ => {} | ||
} | ||
|
||
x | ||
} | ||
|
||
fn bar() -> i32 { | ||
let x = const 2; | ||
//~^ ERROR expected `{`, found `2` | ||
//~| HELP try placing this code inside a block | ||
|
||
x | ||
} | ||
|
||
fn baz() -> i32 { | ||
let y = const 1 + 2 * 3 / 4; | ||
//~^ ERROR expected `{`, found `1` | ||
//~| HELP try placing this code inside a block | ||
|
||
y | ||
} | ||
|
||
fn main() { | ||
foo(); | ||
bar(); | ||
baz(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
error: expected `{`, found `2` | ||
--> $DIR/inline-const-without-block.rs:18:15 | ||
| | ||
LL | const 2 => {} | ||
| ^ | ||
| | | ||
| expected `{` | ||
| help: try placing this code inside a block: `{ 2 }` | ||
|
||
error: expected `{`, found `1` | ||
--> $DIR/inline-const-without-block.rs:25:15 | ||
| | ||
LL | const 1 + 2 * 3 / 4 => {} | ||
| ^------------ | ||
| | | ||
| expected `{` | ||
| help: try placing this code inside a block: `{ 1 + 2 * 3 / 4 }` | ||
|
||
error: expected `{`, found `one` | ||
--> $DIR/inline-const-without-block.rs:32:15 | ||
| | ||
LL | const one() => {} | ||
| ^^^-- | ||
| | | ||
| expected `{` | ||
| help: try placing this code inside a block: `{ one() }` | ||
|
||
error: expected `{`, found `2` | ||
--> $DIR/inline-const-without-block.rs:42:19 | ||
| | ||
LL | let x = const 2; | ||
| ^ | ||
| | | ||
| expected `{` | ||
| help: try placing this code inside a block: `{ 2 }` | ||
|
||
error: expected `{`, found `1` | ||
--> $DIR/inline-const-without-block.rs:50:19 | ||
| | ||
LL | let y = const 1 + 2 * 3 / 4; | ||
| ^------------ | ||
| | | ||
| expected `{` | ||
| help: try placing this code inside a block: `{ 1 + 2 * 3 / 4 }` | ||
|
||
error: aborting due to 5 previous errors | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// This file was auto-generated using 'src/etc/generate-keyword-tests.py const' | ||
|
||
fn main() { | ||
let const = "foo"; //~ error: expected identifier, found keyword `const` | ||
let const = "foo"; | ||
//~^ ERROR expected `{`, found `=` | ||
//~| ERROR inline-const is experimental [E0658] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,18 @@ | ||
error: expected identifier, found keyword `const` | ||
--> $DIR/keyword-const-as-identifier.rs:4:9 | ||
error: expected `{`, found `=` | ||
--> $DIR/keyword-const-as-identifier.rs:2:15 | ||
| | ||
LL | let const = "foo"; | ||
| ^^^^^ expected identifier, found keyword | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This has regressed a bit now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The thing is that this: let const { "foo" } = "foo"; is a totally valid statement (although it ICEs; see #78174). Perhaps we could give the old warning if it's just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, but, what do you expect that code to do?. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's valid syntactically, not semantically. (And either way it shouldn't ICE.) And yes, the error you suggested is correct; it's the one that occurs for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. exactly, 👍 |
||
| ^ expected `{` | ||
|
||
error[E0658]: inline-const is experimental | ||
--> $DIR/keyword-const-as-identifier.rs:2:9 | ||
| | ||
help: you can escape reserved keywords to use them as identifiers | ||
LL | let const = "foo"; | ||
| ^^^^^ | ||
| | ||
LL | let r#const = "foo"; | ||
| ^^^^^^^ | ||
= note: see issue #76001 <https://github.com/rust-lang/rust/issues/76001> for more information | ||
= help: add `#![feature(inline_const)]` to the crate attributes to enable | ||
|
||
error: aborting due to previous error | ||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0658`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think @petrochenkov and others wanted to avoid eating the keyword before checking if it's really an inline const. My first reaction was like ... but we're eating
Unsafe
right above. I'm not sure if there's another way to solve this problem and we could still rely on checking or if we just need to eat the keyword.Anyway, would leave this to @petrochenkov
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The difference is that
unsafe
just modifies the block expression, whereasconst
needs to create theExprKind::Const
- but maybe it doesn't have to be too complicated shrug.