-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Check for known but incorrect attributes #49291
Check for known but incorrect attributes #49291
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @petrochenkov (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
☔ The latest upstream changes (presumably #49308) made this pull request unmergeable. Please resolve the merge conflicts. |
@tejom so that PR was a rollup which added code for another attribute to The preferred approach is to rebase on top of master ( |
Alright cool. The commit doesn't look to crazy to deal with. Thanks! |
src/librustc/hir/check_attr.rs
Outdated
ExprCall(..) | | ||
ExprAssign(..) | | ||
ExprMethodCall(..) | | ||
ExprStruct(..) => return, |
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 don't understand what is the logic here.
How attributes on #[attr] my_fn_call()
could be handles by items or statements?
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 I got confused reading the comments on the Expr_
enum. I think only ExprAssign
overlaps with statements then.
let a = 1
seems to be both an expression and statement?
I can take this section out and everything seems to work fine still. The error that appears for
#[repr]
let _y = "123";
is attribute should not be applied to statements
with and without this match.
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.
let a = 1
is not an expression, ExprAssign
is a = b
.
If you have a statement with one expression, like
a = b;
then it's represented roughly like StmtExpr(ExprAssign)
and attributes are attached to the outer statement and not inner expression.
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.
Oh Ok. I see. Thanks for the clarification.
LGTM beside one comment (also needs rebase). |
- Change nested_visit_map so it will recusively check functions - Add visit_stmt and visit_expr for impl Visitor for CheckAttrVisitor and check for incorrect inline and repr attributes on staements and expressions - Add regression test for isssue rust-lang#43988
7bf1158
to
48825bc
Compare
src/librustc/hir/check_attr.rs
Outdated
@@ -250,7 +250,7 @@ impl<'a, 'tcx> CheckAttrVisitor<'a, 'tcx> { | |||
self.emit_repr_error( | |||
attr.span, | |||
stmt.span, | |||
&format!("attribute should not be applied to statements"), | |||
&format!("attribute should not be applied a statement"), |
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.
Missing "to".
|
||
#[inline(ABC)] | ||
foo(); | ||
//~^^ ERROR attribute should be applied to function |
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.
All the tests are for statements, could you add a test for an expression too?
Something like let x = #[repr] y;
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 added this example. I needed to add a feature in the tests to get it to actually run.
Otherwise error[E0658]: attributes on non-item statements and expressions are experimental. (see issue #15701)
stopped all of the tests early.
r=me after addressing comments |
@bors r+ |
📌 Commit 4957a40 has been approved by |
@bors p=3 |
…, r=petrochenkov Check for known but incorrect attributes fixes #43988 - Change nested_visit_map so it will recursively check functions - Add visit_stmt and visit_expr for impl Visitor for CheckAttrVisitor and check for incorrect inline and repr attributes on staements and expressions - Add regression test for issue #43988
☀️ Test successful - status-appveyor, status-travis |
Hey there, I'm investigating pest-parser/pest#218, where pest fails to build on nightly 2018-03-29; it looks like this merge is the change that did it. I'm not sure exactly why, but one suspicion is that fn main() {
#[inline] || { };
} This compiles without any warning or error on e5277c1 2018-03-28, but on ae544ee 2018-03-29: $ rustc test.rs
error[E0518]: attribute should be applied to function
--> test.rs:2:2
|
2 | #[inline] || { };
| ^^^^^^^^^ ------ not a function
error: aborting due to previous error
For more information about this error, try `rustc --explain E0518`. Is this intentional? |
Yes, |
Good to know, thank you! ❤️ |
Actually, |
The generated LLVM IR (in debug builds) for this example is different if you remove #![feature(stmt_expr_attributes)]
#![crate_type="rlib"]
pub fn main() {
let x = #[inline(always)] || {};
x();
} |
It looks like the code generated for your example with |
That's because the closure is inlined anyways in release builds. You only notice the difference in debug builds where inline hint are ignored unless they are forced. My point is that |
I'm just asking because I'm curious, would this be bug with the compiler not inlining closures when not using one of the higher optimization levels? Seems like that is what the expected behavior was? |
The inlining works fine. The bug is that your PR is too strict: |
fixes #43988
Change nested_visit_map so it will recursively check functions
Add visit_stmt and visit_expr for impl Visitor for CheckAttrVisitor and check for incorrect
inline and repr attributes on staements and expressions
Add regression test for issue Known but incorrect attributes over statements/expressions are ignored. #43988