-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Attribute rework: a parser for single attributes without arguments #142964
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
+91
−132
Merged
Changes from all commits
Commits
Show all changes
2 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
33 changes: 7 additions & 26 deletions
33
compiler/rustc_attr_parsing/src/attributes/lint_helpers.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 |
---|---|---|
@@ -1,38 +1,19 @@ | ||
use rustc_attr_data_structures::AttributeKind; | ||
use rustc_feature::{AttributeTemplate, template}; | ||
use rustc_span::{Symbol, sym}; | ||
use rustc_span::{Span, Symbol, sym}; | ||
|
||
use crate::attributes::{AttributeOrder, OnDuplicate, SingleAttributeParser}; | ||
use crate::context::{AcceptContext, Stage}; | ||
use crate::parser::ArgParser; | ||
use crate::attributes::{NoArgsAttributeParser, OnDuplicate}; | ||
use crate::context::Stage; | ||
|
||
pub(crate) struct AsPtrParser; | ||
|
||
impl<S: Stage> SingleAttributeParser<S> for AsPtrParser { | ||
impl<S: Stage> NoArgsAttributeParser<S> for AsPtrParser { | ||
const PATH: &[Symbol] = &[sym::rustc_as_ptr]; | ||
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepFirst; | ||
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error; | ||
const TEMPLATE: AttributeTemplate = template!(Word); | ||
|
||
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> { | ||
if let Err(span) = args.no_args() { | ||
cx.expected_no_args(span); | ||
} | ||
Some(AttributeKind::AsPtr(cx.attr_span)) | ||
} | ||
const CREATE: fn(Span) -> AttributeKind = AttributeKind::AsPtr; | ||
} | ||
|
||
pub(crate) struct PubTransparentParser; | ||
impl<S: Stage> SingleAttributeParser<S> for PubTransparentParser { | ||
impl<S: Stage> NoArgsAttributeParser<S> for PubTransparentParser { | ||
const PATH: &[Symbol] = &[sym::rustc_pub_transparent]; | ||
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepFirst; | ||
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error; | ||
const TEMPLATE: AttributeTemplate = template!(Word); | ||
|
||
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> { | ||
if let Err(span) = args.no_args() { | ||
cx.expected_no_args(span); | ||
} | ||
Some(AttributeKind::PubTransparent(cx.attr_span)) | ||
} | ||
const CREATE: fn(Span) -> AttributeKind = AttributeKind::PubTransparent; | ||
} |
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 |
---|---|---|
@@ -1,31 +1,19 @@ | ||
use rustc_attr_data_structures::AttributeKind; | ||
use rustc_feature::{AttributeTemplate, template}; | ||
use rustc_span::{Symbol, sym}; | ||
use rustc_span::{Span, Symbol, sym}; | ||
|
||
use crate::attributes::{AttributeOrder, OnDuplicate, SingleAttributeParser}; | ||
use crate::context::{AcceptContext, Stage}; | ||
use crate::parser::ArgParser; | ||
use crate::attributes::{NoArgsAttributeParser, OnDuplicate}; | ||
use crate::context::Stage; | ||
|
||
pub(crate) struct LoopMatchParser; | ||
impl<S: Stage> SingleAttributeParser<S> for LoopMatchParser { | ||
impl<S: Stage> NoArgsAttributeParser<S> for LoopMatchParser { | ||
const PATH: &[Symbol] = &[sym::loop_match]; | ||
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepFirst; | ||
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn; | ||
const TEMPLATE: AttributeTemplate = template!(Word); | ||
|
||
fn convert(cx: &mut AcceptContext<'_, '_, S>, _args: &ArgParser<'_>) -> Option<AttributeKind> { | ||
Some(AttributeKind::LoopMatch(cx.attr_span)) | ||
} | ||
const CREATE: fn(Span) -> AttributeKind = AttributeKind::LoopMatch; | ||
} | ||
|
||
pub(crate) struct ConstContinueParser; | ||
impl<S: Stage> SingleAttributeParser<S> for ConstContinueParser { | ||
impl<S: Stage> NoArgsAttributeParser<S> for ConstContinueParser { | ||
const PATH: &[Symbol] = &[sym::const_continue]; | ||
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepFirst; | ||
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn; | ||
const TEMPLATE: AttributeTemplate = template!(Word); | ||
|
||
fn convert(cx: &mut AcceptContext<'_, '_, S>, _args: &ArgParser<'_>) -> Option<AttributeKind> { | ||
Some(AttributeKind::ConstContinue(cx.attr_span)) | ||
} | ||
const CREATE: fn(Span) -> AttributeKind = AttributeKind::ConstContinue; | ||
} |
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 |
---|---|---|
@@ -1,22 +1,12 @@ | ||
use rustc_attr_data_structures::AttributeKind; | ||
use rustc_feature::{AttributeTemplate, template}; | ||
use rustc_span::{Symbol, sym}; | ||
use rustc_span::{Span, Symbol, sym}; | ||
|
||
use crate::attributes::{AttributeOrder, OnDuplicate, SingleAttributeParser}; | ||
use crate::context::{AcceptContext, Stage}; | ||
use crate::parser::ArgParser; | ||
use crate::attributes::{NoArgsAttributeParser, OnDuplicate}; | ||
use crate::context::Stage; | ||
|
||
pub(crate) struct MayDangleParser; | ||
impl<S: Stage> SingleAttributeParser<S> for MayDangleParser { | ||
impl<S: Stage> NoArgsAttributeParser<S> for MayDangleParser { | ||
const PATH: &[Symbol] = &[sym::may_dangle]; | ||
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepFirst; | ||
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn; | ||
const TEMPLATE: AttributeTemplate = template!(Word); | ||
|
||
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> { | ||
if let Err(span) = args.no_args() { | ||
cx.expected_no_args(span); | ||
} | ||
Some(AttributeKind::MayDangle(cx.attr_span)) | ||
} | ||
const CREATE: fn(span: Span) -> AttributeKind = AttributeKind::MayDangle; | ||
} |
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
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.
Is KeepLast acceptable here? Are there no single attributes which have KeepFirst?
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.
well there were some with keep first, but in practice it doesn't really matter
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 found it to cause fewer tests/ui/ changes than
KeepFirst
(unintuitively, it seems like the First attribute is the one closest to the item it's applied to, and not first in the source code, I think). I don't think it matters too much. We can always propagate this similarly how we do forPATH
andON_DUPLICATE
.Would be even better with default values for const members of tratis, but those aren't a thing yet.