-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Known but incorrect attributes over statements/expressions are ignored. #43988
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
Comments
Possibly related to #41475 |
Turns out this is not related to #41475. This is because This should be a pretty straightforward fix, actually, and wouldn't be a bad introduction to some concepts used in the compiler frontend. I'm willing to mentor this bug @sfackler @Mark-Simulacrum if you want to add |
Done! |
Mentoring InstructionsThis bug is caused by the fact that Attributes can be applied to a lot of things and the parser will accept all of them. However, most attributes don't make sense in a certain context:
The The discrepancy in |
Hey, I should have time to look at this. From the instructions and what I read so far there seems to be a bit of new code that needs to be written to do this? Something similar to |
Yeah, all you should have to do is add I made a mistake in my original assessment and assumed that this code used types from For
And then the final step would be to write a regression test under fn main() {
#[repr(bogus_statement)]
//~^ ERROR the `#[repr]` attribute cannot be applied to statements or expressions
let x = 0;
// ... make sure to test expressions as well!
} The file name for regression tests associated with a certain issue is just |
For bonus points, you could add a help message that suggests using |
Hey, Maybe I misunderstood something but the two extra functions in that trait don't seem to be used. These are the two extra functions I added in the impl Visitor here here fn visit_stmt(&mut self, stmt: &'tcx hir::Stmt) {
debug!("visting statement {:#?}", stmt );
self.check_stmt_attributes(stmt);
intravisit::walk_stmt(self, stmt);
}
fn visit_expr(&mut self, ex: &'tcx hir::Expr) {
debug!("visting an expr {:#?}", ex);
intravisit::walk_expr(self,ex);
}
My test program is fn main() {
#[inline]
let _a = 4;
#[inline(UUUU)]
let _b = 4;
#[repr(nothing)]
let _x = 0;
#[repr(something_not_real)]
{
1
};
} |
Ah, you have to change the return value of fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
// only visit the bodies of functions, not their definitions
NestedVisitorMap::OnlyBodies(&this.hir)
} |
Cool. I'm moving forward again heh. I guess that makes sense then. I was seeing |
- Change nested_visit_map so it will recusively check function - Add visit_stmt and visit_expr for impl Visitor and check for incorrect inline and repr attributes on staements and expressions - Add regression test for isssue rust-lang#43988
Hey so I pushed a commit on my own fork. I wasn't sure if its ready for a pull request but feedback on the code i think could be helpful just in case I did anything incredibly wrong heh. I wasn't sure what the best way to share a commit would be. If there's a better or more preferred way let me know. |
I've left a couple of comments but you can probably go ahead and open a pull request, it'll start running tests on it. It also makes it easier to review. I don't have review permissions so add |
Cool. Thanks a lot. I'll address your comments and open a pull request |
- 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
…, 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
I tried to look for this, but I couldn't find an existing issue. Or is this expected behaviour?
Putting attributes that exist (though the arguments can be bogus) seem to be ignored when one would expect an error, or at least a warning. E.g like this:
or
This seems to be the case on stable, beta and nightly.
More examples here: https://play.rust-lang.org/?gist=b48bfc076d29c770d8d2c81f2f99f0bb
The text was updated successfully, but these errors were encountered: