Skip to content

Commit 518893c

Browse files
committed
implement inline parser
1 parent bfeed70 commit 518893c

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

compiler/rustc_attr_data_structures/src/attributes.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use thin_vec::ThinVec;
88

99
use crate::{DefaultBodyStability, PartialConstStability, PrintAttribute, RustcVersion, Stability};
1010

11-
#[derive(Copy, Clone, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
11+
#[derive(Copy, Clone, PartialEq, Encodable, Decodable, Debug, HashStable_Generic, PrintAttribute)]
1212
pub enum InlineAttr {
1313
None,
1414
Hint,
@@ -189,6 +189,7 @@ pub enum AttributeKind {
189189
span: Span,
190190
comment: Symbol,
191191
},
192+
Inline(InlineAttr, Span),
192193
MacroTransparency(Transparency),
193194
Repr(ThinVec<(ReprAttr, Span)>),
194195
Stability {
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use rustc_attr_data_structures::{AttributeKind, InlineAttr};
2+
use rustc_errors::{E0534, E0535, struct_span_code_err};
3+
use rustc_span::sym;
4+
5+
use super::{AcceptContext, AttributeOrder, OnDuplicate};
6+
use crate::attributes::SingleAttributeParser;
7+
use crate::context::Stage;
8+
use crate::parser::ArgParser;
9+
10+
pub(crate) struct InlineParser;
11+
12+
impl<S: Stage> SingleAttributeParser<S> for InlineParser {
13+
const PATH: &'static [rustc_span::Symbol] = &[sym::inline];
14+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepLast;
15+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError;
16+
17+
fn convert(cx: &AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
18+
match args {
19+
ArgParser::NoArgs => Some(AttributeKind::Inline(InlineAttr::Hint, cx.attr_span)),
20+
ArgParser::List(list) => {
21+
let Some(l) = list.single() else {
22+
struct_span_code_err!(cx.dcx(), cx.attr_span, E0534, "expected one argument")
23+
.emit();
24+
return None;
25+
};
26+
27+
match l.meta_item().and_then(|i| i.word_without_args().map(|i| i.name)) {
28+
Some(sym::always) => {
29+
Some(AttributeKind::Inline(InlineAttr::Always, cx.attr_span))
30+
}
31+
Some(sym::never) => {
32+
Some(AttributeKind::Inline(InlineAttr::Never, cx.attr_span))
33+
}
34+
_ => {
35+
struct_span_code_err!(cx.dcx(), l.span(), E0535, "invalid argument")
36+
.with_help("valid inline arguments are `always` and `never`")
37+
.emit();
38+
return None;
39+
}
40+
}
41+
}
42+
ArgParser::NameValue(_) => {
43+
// silently ignored, we warn somewhere else.
44+
// FIXME(jdonszelmann): that warning *should* go here.
45+
None
46+
}
47+
}
48+
}
49+
}

compiler/rustc_attr_parsing/src/attributes/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub(crate) mod allow_unstable;
2929
pub(crate) mod cfg;
3030
pub(crate) mod confusables;
3131
pub(crate) mod deprecation;
32+
pub(crate) mod inline;
3233
pub(crate) mod repr;
3334
pub(crate) mod stability;
3435
pub(crate) mod transparency;

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span, Symbol, sym};
1919
use crate::attributes::allow_unstable::{AllowConstFnUnstableParser, AllowInternalUnstableParser};
2020
use crate::attributes::confusables::ConfusablesParser;
2121
use crate::attributes::deprecation::DeprecationParser;
22+
use crate::attributes::inline::InlineParser;
2223
use crate::attributes::repr::ReprParser;
2324
use crate::attributes::stability::{
2425
BodyStabilityParser, ConstStabilityIndirectParser, ConstStabilityParser, StabilityParser,
@@ -104,6 +105,7 @@ attribute_groups!(
104105
// tidy-alphabetical-start
105106
Single<ConstStabilityIndirectParser>,
106107
Single<DeprecationParser>,
108+
Single<InlineParser>,
107109
Single<TransparencyParser>,
108110
// tidy-alphabetical-end
109111
];

0 commit comments

Comments
 (0)