-
-
Notifications
You must be signed in to change notification settings - Fork 16.9k
port lint attributes #162811
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
Open
mejrs
wants to merge
4
commits into
rust-lang:main
Choose a base branch
from
mejrs:port_lint_check
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
port lint attributes #162811
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| use rustc_macros::{Decodable, Encodable, PrintAttribute, StableHash}; | ||
| use rustc_span::{Span, Symbol, sym}; | ||
|
|
||
| use crate::{HashIgnoredAttrId, PrintAttribute}; | ||
| #[derive(Clone, Copy, Debug, StableHash, Encodable, Decodable, PrintAttribute)] | ||
| pub enum LintCheckKind { | ||
| Allow, | ||
| Warn, | ||
| Deny, | ||
| Forbid, | ||
| Expect, | ||
| } | ||
|
|
||
| impl LintCheckKind { | ||
| pub fn sym(self) -> Symbol { | ||
| match self { | ||
| LintCheckKind::Allow => sym::allow, | ||
| LintCheckKind::Warn => sym::warn, | ||
| LintCheckKind::Deny => sym::deny, | ||
| LintCheckKind::Forbid => sym::forbid, | ||
| LintCheckKind::Expect => sym::expect, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// A lint check attribute. | ||
| /// | ||
| /// For example `#[deny(clippy::blah, reason = "reason")]` is lowered into this. | ||
| /// | ||
| /// These are smooshed and flattened together; | ||
| /// ```rust | ||
| /// #[allow(dead_code)] | ||
| /// #[deny(unused, unsafe_code)] | ||
| /// # const _: () = (); | ||
| /// ``` | ||
| /// is lowered into | ||
| /// ```text | ||
| /// #[attr = LintCheck([ | ||
| /// LintCheck { lint_name: "dead_code", kind: Allow }, | ||
| /// LintCheck { lint_name: "unused", kind: Deny }, | ||
| /// LintCheck { lint_name: "unsafe_code", kind: Deny }, | ||
| /// ])] | ||
| /// ``` | ||
| #[derive(Clone, Debug, StableHash, Encodable, Decodable, PrintAttribute)] | ||
| pub struct LintCheck { | ||
| /// The lint's tool name, if present. | ||
| pub tool_name: Option<Symbol>, | ||
| /// The lint's name. | ||
| /// | ||
| /// With e.g. `clippy:blah` this will be `blah`. | ||
| /// Any extra segments are stored in `rest`. | ||
| pub lint_name: Symbol, | ||
| /// The span of the lint name. | ||
| /// | ||
| /// For example `#[deny(foo, bar, reason = "reason")]` | ||
| /// produces multiple `LintCheck`s, one with a span pointing to `foo` | ||
| /// and another pointing to `bar`. | ||
| pub lint_span: Span, | ||
| pub kind: LintCheckKind, | ||
| pub reason: Option<Symbol>, | ||
| /// Needed by `LintExpectationId` to track fulfilled expectations | ||
| pub attr_id: HashIgnoredAttrId, | ||
| /// The span of the attribute this lintcheck came from. | ||
| /// | ||
| /// Like mentioned above, multiple lint attributes and multiple lints | ||
| /// inside one attribute are all smooshed and flattened together, so | ||
| /// multiple `LintCheck`s can have the same `attr_span`. | ||
| pub attr_span: Span, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add a doc comment clarifying the difference between |
||
| /// Any extra segments of the lint name, this should be rare and indicates misuse of | ||
| /// the attribute as nothing supports 3+ segment lints like `#[allow(tool::two::three)]`. | ||
| pub rest: Option<Box<[Symbol]>>, | ||
| } | ||
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,205 @@ | ||
| use rustc_attr_ir::AttributeKind; | ||
| use rustc_attr_ir::lint::{LintCheck, LintCheckKind}; | ||
| use rustc_attr_ir::target::{AssocCtxt, MethodKind, Target}; | ||
| use rustc_lint_defs::builtin::UNUSED_ATTRIBUTES; | ||
| use rustc_span::{Span, Symbol, sym}; | ||
| use thin_vec::ThinVec; | ||
|
|
||
| use crate::attributes::{AcceptMapping, AttributeParser, AttributeStability}; | ||
| use crate::context::{AcceptContext, ExpectStringLiteral, FinalizeContext}; | ||
| use crate::parser::ArgParser; | ||
| use crate::target_checking::AllowedTargets; | ||
| use crate::target_checking::Policy::{Allow, Warn}; | ||
| use crate::{AttributeTemplate, diagnostics, template}; | ||
|
|
||
| const LINT_TEMPLATE: AttributeTemplate = template!( | ||
| List: &["lint1", "lint1, lint2, ...", r#"lint1, lint2, lint3, reason = "...""#], | ||
| "https://doc.rust-lang.org/reference/attributes/diagnostics.html#lint-check-attributes" | ||
| ); | ||
|
|
||
| #[derive(Default, Debug)] | ||
| pub(crate) struct LintParser { | ||
| lints: ThinVec<LintCheck>, | ||
| } | ||
|
|
||
| impl LintParser { | ||
| fn parse(&mut self, kind: LintCheckKind, cx: &mut AcceptContext<'_, '_>, args: &ArgParser) { | ||
| let attr_span = cx.attr_span; | ||
| let attr_id = cx.attr_id.expect("no `AttrId` for lint attribute"); | ||
| let mut lints: Vec<(Option<Symbol>, Symbol, Option<Box<[Symbol]>>, Span)> = Vec::new(); | ||
|
|
||
| if let Some(list) = cx.expect_list(args, cx.attr_span) { | ||
| let mut parsers = list.sub_parsers(); | ||
| // Optionally, the last (and only the last) | ||
| // element can be `reason = "reason"` | ||
| let reason = try { | ||
| let p = parsers.last()?.meta_item()?; | ||
| let nv = p.args().as_name_value()?; | ||
| if !p.path().word_is(sym::reason) { | ||
| cx.emit_err(diagnostics::MalformedAttribute { | ||
| span: p.span(), | ||
| sub: diagnostics::MalformedAttributeSub::BadAttributeArgument(p.span()), | ||
| }); | ||
| } else { | ||
| parsers = &parsers[..(parsers.len() - 1)]; | ||
| } | ||
|
|
||
| let reason = nv.expect_string_literal(cx)?; | ||
| reason | ||
| }; | ||
|
|
||
| for item in parsers { | ||
| if let Some(p) = item.meta_item() { | ||
| match p.args() { | ||
| ArgParser::NoArgs => { | ||
| let (tool_name, lint_name, rest) = match &*p.path().0.segments { | ||
| [] => unreachable!(), | ||
| [lint_name] => (None, lint_name.ident.name, None), | ||
| [tool_name, lint_name] => { | ||
| (Some(tool_name.ident.name), lint_name.ident.name, None) | ||
| } | ||
| [tool_name, lint_name, rest @ ..] => { | ||
| let rest = rest | ||
| .iter() | ||
| .map(|s| s.ident.name) | ||
| .collect::<Vec<_>>() | ||
| .into(); | ||
| (Some(tool_name.ident.name), lint_name.ident.name, Some(rest)) | ||
| } | ||
| }; | ||
|
|
||
| lints.push((tool_name, lint_name, rest, p.span())) | ||
| } | ||
| // We're found a `reason = "reason"` but we're not the last element. | ||
| ArgParser::NameValue(nv) if p.path().word_is(sym::reason) => { | ||
| cx.emit_err(diagnostics::MalformedAttribute { | ||
| span: p.span(), | ||
| sub: diagnostics::MalformedAttributeSub::ReasonMustComeLast( | ||
| item.span(), | ||
| ), | ||
| }); | ||
| nv.expect_string_literal(cx); | ||
| } | ||
| ArgParser::NameValue(_) | ArgParser::List(_) => { | ||
| cx.emit_err(diagnostics::MalformedAttribute { | ||
| span: p.span(), | ||
| sub: diagnostics::MalformedAttributeSub::BadAttributeArgument( | ||
| item.span(), | ||
| ), | ||
| }); | ||
| } | ||
| } | ||
| } else { | ||
| cx.emit_err(diagnostics::MalformedAttribute { | ||
| span: item.span(), | ||
| sub: diagnostics::MalformedAttributeSub::BadAttributeArgument(item.span()), | ||
| }); | ||
| } | ||
| } | ||
| if parsers.is_empty() { | ||
| cx.emit_lint( | ||
| UNUSED_ATTRIBUTES, | ||
| diagnostics::Unused { | ||
| attr_span, | ||
| note: if list.is_empty() { | ||
| diagnostics::UnusedNote::EmptyList { name: kind.sym() } | ||
| } else { | ||
| diagnostics::UnusedNote::NoLints { name: kind.sym() } | ||
| }, | ||
| }, | ||
| attr_span, | ||
| ); | ||
| } | ||
|
|
||
| for (tool_name, lint_name, rest, lint_span) in lints.into_iter() { | ||
| self.lints.push(LintCheck { | ||
| tool_name, | ||
| lint_name, | ||
| lint_span, | ||
| kind, | ||
| attr_id, | ||
| reason, | ||
| attr_span, | ||
| rest, | ||
| }) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl AttributeParser for LintParser { | ||
| const ATTRIBUTES: AcceptMapping<Self> = &[ | ||
| (&[sym::allow], LINT_TEMPLATE, AttributeStability::Stable, |this, cx, args| { | ||
| this.parse(LintCheckKind::Allow, cx, args) | ||
| }), | ||
| (&[sym::warn], LINT_TEMPLATE, AttributeStability::Stable, |this, cx, args| { | ||
| this.parse(LintCheckKind::Warn, cx, args) | ||
| }), | ||
| (&[sym::deny], LINT_TEMPLATE, AttributeStability::Stable, |this, cx, args| { | ||
| this.parse(LintCheckKind::Deny, cx, args) | ||
| }), | ||
| (&[sym::forbid], LINT_TEMPLATE, AttributeStability::Stable, |this, cx, args| { | ||
| this.parse(LintCheckKind::Forbid, cx, args) | ||
| }), | ||
| (&[sym::expect], LINT_TEMPLATE, AttributeStability::Stable, |this, cx, args| { | ||
| this.parse(LintCheckKind::Expect, cx, args) | ||
| }), | ||
| ]; | ||
| const ALLOWED_TARGETS: AllowedTargets<'_> = { | ||
| AllowedTargets::AllowList(&[ | ||
| Allow(Target::ExternCrate), | ||
| Allow(Target::Use), | ||
| Allow(Target::Static), | ||
| Allow(Target::Const), | ||
| Allow(Target::Fn), | ||
| Allow(Target::Closure), | ||
| Allow(Target::Mod), | ||
| Allow(Target::ForeignMod), | ||
| Allow(Target::GlobalAsm), | ||
| Allow(Target::TyAlias), | ||
| Allow(Target::Enum), | ||
| Allow(Target::Variant), | ||
| Allow(Target::Struct), | ||
| Allow(Target::Field), | ||
| Allow(Target::Union), | ||
| Allow(Target::Trait), | ||
| Allow(Target::TraitAlias), | ||
| Allow(Target::Impl { of_trait: false }), | ||
| Allow(Target::Impl { of_trait: true }), | ||
| Allow(Target::Expression), | ||
| Allow(Target::Statement), | ||
| Allow(Target::Arm), | ||
| Allow(Target::AssocConst(AssocCtxt::Impl { of_trait: false })), | ||
| Allow(Target::AssocConst(AssocCtxt::Trait)), | ||
| Allow(Target::AssocConst(AssocCtxt::Impl { of_trait: true })), | ||
| Allow(Target::Method(MethodKind::Inherent)), | ||
| Allow(Target::Method(MethodKind::Trait { body: false })), | ||
| Allow(Target::Method(MethodKind::Trait { body: true })), | ||
| Allow(Target::Method(MethodKind::TraitImpl)), | ||
| Allow(Target::AssocTy(AssocCtxt::Impl { of_trait: false })), | ||
| Allow(Target::AssocTy(AssocCtxt::Trait)), | ||
| Allow(Target::AssocTy(AssocCtxt::Impl { of_trait: true })), | ||
| Allow(Target::ForeignFn), | ||
| Allow(Target::ForeignStatic), | ||
| Allow(Target::ForeignTy), | ||
| Allow(Target::MacroDef), | ||
| Allow(Target::Param), | ||
| Allow(Target::PatField), | ||
| Allow(Target::ExprField), | ||
| Allow(Target::Crate), | ||
| Allow(Target::Delegation { mac: false }), | ||
| Allow(Target::Delegation { mac: true }), | ||
| Allow(Target::ConstParam), | ||
| Allow(Target::LifetimeParam), | ||
| Allow(Target::TypeParam), | ||
| Allow(Target::Loop), | ||
| Allow(Target::ForLoop), | ||
| Allow(Target::While), | ||
| Allow(Target::Break), | ||
| Warn(Target::MacroCall), | ||
| ]) | ||
| }; | ||
| fn finalize(self, _cx: &FinalizeContext<'_, '_>) -> Option<AttributeKind> { | ||
| if self.lints.is_empty() { None } else { Some(AttributeKind::LintCheck(self.lints)) } | ||
| } | ||
| } |
Oops, something went wrong.
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.
I'd like to see the perf regression solved, or at least explained.
The previous attempt is a lot more green: #155691 (comment)
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 did make a change where the lint name is stored as
{tool_name: Option<Symbol>, lint_name: Symbol}which did a little better in #162811 (comment) But I think i'd like a vibecheck before I start dropping segments on (unsupported?) things likelint::blah::fooThere 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.
that did get rid of a lot of allocations, though