-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Permit attributes on 'if' expressions #69201
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f63b88c
Permit attributes on 'if' expressions
Aaron1011 e912d9d
Test #[allow(unused)] on `if` expression
Aaron1011 e9ec47b
Test that stmt_expr_attrs properly gates if-attrs
Aaron1011 9a299e4
Test trying to cfg-remove an `if` expression
Aaron1011 b00f674
Remove recovery test
Aaron1011 e11cdfd
Add run-pass test suggested by @joshtriplett
Aaron1011 7f19358
Move if-attr tests to their own directory
Aaron1011 1b681d6
Test that cfg-gated if-exprs are not type-checked
Aaron1011 37c2c38
Extent pretty-print test
Aaron1011 66b152c
Fix tabs
Aaron1011 e50fd5a
Update stderr
Aaron1011 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// pp-exact | ||
|
||
#[cfg(FALSE)] | ||
fn simple_attr() { | ||
|
||
#[attr] | ||
if true { } | ||
|
||
#[allow_warnings] | ||
if true { } | ||
} | ||
|
||
#[cfg(FALSE)] | ||
fn if_else_chain() { | ||
|
||
#[first_attr] | ||
if true { } else if false { } else { } | ||
} | ||
|
||
#[cfg(FALSE)] | ||
fn if_let() { | ||
|
||
#[attr] | ||
if let Some(_) = Some(true) { } | ||
} | ||
|
||
#[cfg(FALSE)] | ||
fn let_attr_if() { | ||
let _ = #[attr] if let _ = 0 { }; | ||
let _ = #[attr] if true { }; | ||
|
||
let _ = #[attr] if let _ = 0 { } else { }; | ||
let _ = #[attr] if true { } else { }; | ||
} | ||
|
||
|
||
fn main() { } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#![feature(stmt_expr_attributes)] | ||
|
||
fn main() { | ||
let _ = #[cfg(FALSE)] if true {}; //~ ERROR removing an expression | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
error: removing an expression is not supported in this position | ||
--> $DIR/bad-cfg.rs:4:13 | ||
| | ||
LL | let _ = #[cfg(FALSE)] if true {}; | ||
| ^^^^^^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// check-pass | ||
|
||
fn main() { | ||
#[allow(unused_variables)] | ||
if true { | ||
let a = 1; | ||
} else if false { | ||
let b = 1; | ||
} else { | ||
let c = 1; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// check-pass | ||
|
||
#[cfg(FALSE)] | ||
fn simple_attr() { | ||
#[attr] if true {} | ||
#[allow_warnings] if true {} | ||
} | ||
|
||
#[cfg(FALSE)] | ||
fn if_else_chain() { | ||
#[first_attr] if true { | ||
} else if false { | ||
} else { | ||
} | ||
} | ||
|
||
#[cfg(FALSE)] | ||
fn if_let() { | ||
#[attr] if let Some(_) = Some(true) {} | ||
} | ||
|
||
fn bar() { | ||
#[cfg(FALSE)] | ||
if true { | ||
let x: () = true; // Should not error due to the #[cfg(FALSE)] | ||
} | ||
|
||
#[cfg_attr(not(unset_attr), cfg(FALSE))] | ||
if true { | ||
let a: () = true; // Should not error due to the applied #[cfg(FALSE)] | ||
} | ||
} | ||
|
||
macro_rules! custom_macro { | ||
($expr:expr) => {} | ||
} | ||
|
||
custom_macro! { | ||
#[attr] if true {} | ||
} | ||
|
||
|
||
fn main() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#[cfg(FALSE)] | ||
fn if_else_parse_error() { | ||
if true { | ||
} #[attr] else if false { //~ ERROR expected | ||
} | ||
} | ||
|
||
#[cfg(FALSE)] | ||
fn else_attr_ifparse_error() { | ||
if true { | ||
} else #[attr] if false { //~ ERROR expected | ||
} else { | ||
} | ||
} | ||
|
||
#[cfg(FALSE)] | ||
fn else_parse_error() { | ||
if true { | ||
} else if false { | ||
} #[attr] else { //~ ERROR expected | ||
} | ||
} | ||
|
||
fn main() { | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
error: expected expression, found keyword `else` | ||
--> $DIR/else-attrs.rs:4:15 | ||
| | ||
LL | } #[attr] else if false { | ||
| ^^^^ expected expression | ||
|
||
error: expected `{`, found `#` | ||
--> $DIR/else-attrs.rs:11:12 | ||
| | ||
LL | } else #[attr] if false { | ||
| ^ expected `{` | ||
| | ||
help: try placing this code inside a block | ||
| | ||
LL | } else #[attr] { if false { | ||
LL | } else { | ||
LL | } } | ||
| | ||
|
||
error: expected expression, found keyword `else` | ||
--> $DIR/else-attrs.rs:20:15 | ||
| | ||
LL | } #[attr] else { | ||
| ^^^^ expected expression | ||
|
||
error: aborting due to 3 previous errors | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// run-pass | ||
|
||
fn main() { | ||
let x = 1; | ||
|
||
#[cfg(FALSE)] | ||
if false { | ||
x = 2; | ||
} else if true { | ||
x = 3; | ||
} else { | ||
x = 4; | ||
} | ||
assert_eq!(x, 1); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// check-pass | ||
|
||
#![feature(let_chains)] //~ WARN the feature `let_chains` is incomplete | ||
|
||
#[cfg(FALSE)] | ||
fn foo() { | ||
#[attr] | ||
if let Some(_) = Some(true) && let Ok(_) = Ok(1) { | ||
} else if let Some(false) = Some(true) { | ||
} | ||
} | ||
|
||
fn main() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
warning: the feature `let_chains` is incomplete and may cause the compiler to crash | ||
--> $DIR/let-chains-attr.rs:3:12 | ||
| | ||
LL | #![feature(let_chains)] | ||
| ^^^^^^^^^^ | ||
| | ||
= note: `#[warn(incomplete_features)]` on by default | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
fn main() { | ||
let _ = #[deny(warnings)] if true { //~ ERROR attributes on expressions | ||
} else if false { | ||
} else { | ||
}; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
error[E0658]: attributes on expressions are experimental | ||
--> $DIR/stmt-expr-gated.rs:2:13 | ||
| | ||
LL | let _ = #[deny(warnings)] if true { | ||
| ^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: see issue #15701 <https://github.com/rust-lang/rust/issues/15701> for more information | ||
= help: add `#![feature(stmt_expr_attributes)]` to the crate attributes to enable | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0658`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.