-
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,25 @@ | ||
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)) | ||
fn create(span: Span) -> AttributeKind { | ||
AttributeKind::AsPtr(span) | ||
} | ||
} | ||
|
||
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)) | ||
fn create(span: Span) -> AttributeKind { | ||
AttributeKind::PubTransparent(span) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,25 @@ | ||
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)) | ||
fn create(span: Span) -> AttributeKind { | ||
AttributeKind::LoopMatch(span) | ||
} | ||
} | ||
|
||
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)) | ||
fn create(span: Span) -> AttributeKind { | ||
AttributeKind::ConstContinue(span) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,7 @@ | |
use std::marker::PhantomData; | ||
|
||
use rustc_attr_data_structures::AttributeKind; | ||
use rustc_feature::AttributeTemplate; | ||
use rustc_feature::{AttributeTemplate, template}; | ||
use rustc_span::{Span, Symbol}; | ||
use thin_vec::ThinVec; | ||
|
||
|
@@ -228,6 +228,41 @@ pub(crate) enum AttributeOrder { | |
KeepLast, | ||
} | ||
|
||
/// An even simpler version of [`SingleAttributeParser`]: | ||
/// now automatically check that there are no arguments provided to the attribute. | ||
/// | ||
/// [`WithoutArgs<T> where T: NoArgsAttributeParser`](WithoutArgs) implements [`SingleAttributeParser`]. | ||
// | ||
pub(crate) trait NoArgsAttributeParser<S: Stage>: 'static { | ||
const PATH: &[Symbol]; | ||
const ON_DUPLICATE: OnDuplicate<S>; | ||
|
||
/// Create the [`AttributeKind`] given attribute's [`Span`]. | ||
fn create(span: Span) -> AttributeKind; | ||
} | ||
|
||
pub(crate) struct WithoutArgs<T: NoArgsAttributeParser<S>, S: Stage>(PhantomData<(S, T)>); | ||
|
||
impl<T: NoArgsAttributeParser<S>, S: Stage> Default for WithoutArgs<T, S> { | ||
fn default() -> Self { | ||
Self(Default::default()) | ||
} | ||
} | ||
|
||
impl<T: NoArgsAttributeParser<S>, S: Stage> SingleAttributeParser<S> for WithoutArgs<T, S> { | ||
const PATH: &[Symbol] = T::PATH; | ||
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepLast; | ||
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. 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more.
I found it to cause fewer tests/ui/ changes than Would be even better with default values for const members of tratis, but those aren't a thing yet. |
||
const ON_DUPLICATE: OnDuplicate<S> = T::ON_DUPLICATE; | ||
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(T::create(cx.attr_span)) | ||
} | ||
} | ||
|
||
type ConvertFn<E> = fn(ThinVec<E>) -> AttributeKind; | ||
|
||
/// Alternative to [`AttributeParser`] that automatically handles state management. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,15 @@ | ||
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)) | ||
fn create(span: Span) -> AttributeKind { | ||
AttributeKind::MayDangle(span) | ||
} | ||
} |
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 we have reached the point where you can just make this an assoc const of the
fn(Span) -> AttributeKind
sort, as no usage sites actually do anything but invoke a tuple variant constructor