-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Expand Attributes on Statements and Expressions #49124
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
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
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
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
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
38 changes: 38 additions & 0 deletions
38
src/test/compile-fail-fulldeps/proc-macro/attr-invalid-exprs.rs
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,38 @@ | ||
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
// aux-build:attr-stmt-expr.rs | ||
// ignore-stage1 | ||
|
||
//! Attributes producing expressions in invalid locations | ||
|
||
#![feature(proc_macro, stmt_expr_attributes)] | ||
|
||
extern crate attr_stmt_expr; | ||
use attr_stmt_expr::{duplicate, no_output}; | ||
|
||
fn main() { | ||
let _ = #[no_output] "Hello, world!"; | ||
//~^ ERROR expected expression, found `<eof>` | ||
|
||
let _ = #[duplicate] "Hello, world!"; | ||
//~^ ERROR macro expansion ignores token `,` and any following | ||
|
||
let _ = { | ||
#[no_output] | ||
"Hello, world!" | ||
}; | ||
|
||
let _ = { | ||
#[duplicate] | ||
//~^ ERROR macro expansion ignores token `,` and any following | ||
"Hello, world!" | ||
}; | ||
} |
38 changes: 38 additions & 0 deletions
38
src/test/compile-fail-fulldeps/proc-macro/attr-stmt-expr.rs
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,38 @@ | ||
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
// aux-build:attr-stmt-expr.rs | ||
// ignore-stage1 | ||
|
||
#![feature(proc_macro)] | ||
|
||
extern crate attr_stmt_expr; | ||
use attr_stmt_expr::{expect_let, expect_print_stmt, expect_expr, expect_print_expr}; | ||
|
||
fn print_str(string: &'static str) { | ||
// macros are handled a bit differently | ||
#[expect_print_expr] | ||
//~^ ERROR attributes on expressions are experimental | ||
//~| HELP add #![feature(stmt_expr_attributes)] to the crate attributes to enable | ||
println!("{}", string) | ||
} | ||
|
||
fn main() { | ||
#[expect_let] | ||
let string = "Hello, world!"; | ||
|
||
#[expect_print_stmt] | ||
println!("{}", string); | ||
|
||
#[expect_expr] | ||
//~^ ERROR attributes on expressions are experimental | ||
//~| HELP add #![feature(stmt_expr_attributes)] to the crate attributes to enable | ||
print_str("string") | ||
} |
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.
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.
So, you can cfg-out a trailing expression and implicit
()
will be used instead? Fun.At least this is consistent with existing behavior of
#[cfg]
.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 that's just the behavior of an empty block, no?
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.
https://play.rust-lang.org/?gist=46ec7b69960efe58ea7e55a8c05159cd&version=stable
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.
It's behavior of an empty block, yes, but it's not obvious that
{ #[cfg_like] trailing }
should expand into an empty block - it depends on interpretation.If the attribute is applied to the statement, then
1 statement -> 0 statements
transformation is okay, but if the attribute is applied to the expression inside of that statement, then it supposedly should be an error, because you can't shrink an expression in that position into nothing (it's not a part of expression list).As I understand the code in
expand.rs
, attributes on expression statements are treated as expression attributes and not statement attributes, but the behavior follows the statement attribute interpretation.Ok, now I'm actually not sure whether
#[no_output]
is applied at all here.We need to add a test making sure that this doesn't expand into
let _ = { "Hello, world!" };
ignoring the attribute.