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

Check for known but incorrect attributes #49291

Merged
merged 3 commits into from
Mar 29, 2018

Conversation

tejom
Copy link
Contributor

@tejom tejom commented Mar 23, 2018

fixes #43988

@rust-highfive
Copy link
Collaborator

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.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 23, 2018
@tejom
Copy link
Contributor Author

tejom commented Mar 23, 2018

r? @petrochenkov

@abonander

@bors
Copy link
Contributor

bors commented Mar 23, 2018

☔ The latest upstream changes (presumably #49308) made this pull request unmergeable. Please resolve the merge conflicts.

@abonander
Copy link
Contributor

@tejom so that PR was a rollup which added code for another attribute to CheckAttrVisitor: https://github.com/rust-lang/rust/pull/49308/files#diff-a7a4db27e720b5572001d15719153b54

The preferred approach is to rebase on top of master (git rebase upstream/master where upstream is rust-lang/rust) and then resolve the merge conflict that comes up.

@tejom
Copy link
Contributor Author

tejom commented Mar 24, 2018

Alright cool. The commit doesn't look to crazy to deal with. Thanks!

ExprCall(..) |
ExprAssign(..) |
ExprMethodCall(..) |
ExprStruct(..) => return,
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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.

@petrochenkov
Copy link
Contributor

LGTM beside one comment (also needs rebase).

@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 26, 2018
- 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
@tejom tejom force-pushed the check-for-known-but-incorrect-attributes branch from 7bf1158 to 48825bc Compare March 27, 2018 02:49
@@ -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"),
Copy link
Contributor

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
Copy link
Contributor

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;

Copy link
Contributor Author

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.

@petrochenkov
Copy link
Contributor

r=me after addressing comments

@petrochenkov
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Mar 27, 2018

📌 Commit 4957a40 has been approved by petrochenkov

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Mar 27, 2018
@kennytm
Copy link
Member

kennytm commented Mar 28, 2018

@bors p=3

@bors
Copy link
Contributor

bors commented Mar 29, 2018

⌛ Testing commit 4957a40 with merge 3615093...

bors added a commit that referenced this pull request Mar 29, 2018
…, 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
@bors
Copy link
Contributor

bors commented Mar 29, 2018

☀️ Test successful - status-appveyor, status-travis
Approved by: petrochenkov
Pushing 3615093 to master...

@kivikakk
Copy link

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 #[inline] as applied to a lambda is now being disallowed? Take this example:

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?

@petrochenkov
Copy link
Contributor

petrochenkov commented Mar 31, 2018

Is this intentional?

Yes, #[inline] attribute never had any effect on lambdas.
(IIRC, operator () on lambdas is automatically marked as inline by the compiler.)

@kivikakk
Copy link

Good to know, thank you! ❤️

@Amanieu
Copy link
Member

Amanieu commented Apr 3, 2018

Actually, #[inline(always)] does have an effect on closures. This doesn't work any more and I have some code that relies on inline for optimizations.

@Amanieu
Copy link
Member

Amanieu commented Apr 3, 2018

The generated LLVM IR (in debug builds) for this example is different if you remove #[inline(always)]:

 #![feature(stmt_expr_attributes)] 
 #![crate_type="rlib"]

pub fn main() {
    let x = #[inline(always)] || {};
    x();
}

@tejom
Copy link
Contributor Author

tejom commented Apr 3, 2018

It looks like the code generated for your example with -O option is the same for both? That's what I saw with the stable build.

@Amanieu
Copy link
Member

Amanieu commented Apr 3, 2018

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 #[inline(always)] does have an effect on code generation when applied to closures, and this change is a regression since it now disallows this (though technically not a break change since stmt_expr_attributes is still unstable anyways).

@tejom
Copy link
Contributor Author

tejom commented Apr 4, 2018

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?

@Amanieu
Copy link
Member

Amanieu commented Apr 4, 2018

The inlining works fine. The bug is that your PR is too strict: #[inline] should be allowed on closures as well as functions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Known but incorrect attributes over statements/expressions are ignored.
8 participants