Skip to content

Commit 5d3bd98

Browse files
committed
implement inline parser
1 parent 8d7ff0f commit 5d3bd98

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

compiler/rustc_attr_data_structures/src/attributes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ pub enum AttributeKind {
189189
span: Span,
190190
comment: Symbol,
191191
},
192-
Inline(InlineAttr),
192+
Inline(InlineAttr, Span),
193193
MacroTransparency(Transparency),
194194
Repr(ThinVec<(ReprAttr, Span)>),
195195
Stability {
Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,49 @@
1+
use rustc_attr_data_structures::{AttributeKind, InlineAttr};
2+
use rustc_errors::{E0534, E0535, struct_span_code_err};
13
use rustc_span::sym;
24

5+
use super::{AcceptContext, AttributeOrder, OnDuplicate};
36
use crate::attributes::SingleAttributeParser;
7+
use crate::context::Stage;
8+
use crate::parser::ArgParser;
49

510
pub(crate) struct InlineParser;
611

7-
impl SingleAttributeParser for InlineParser {
12+
impl<S: Stage> SingleAttributeParser<S> for InlineParser {
813
const PATH: &'static [rustc_span::Symbol] = &[sym::inline];
14+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepLast;
15+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError;
916

10-
fn on_duplicate(cx: &super::AcceptContext<'_>, first_span: rustc_span::Span) {
11-
todo!()
12-
}
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+
};
1326

14-
fn convert(cx: &super::AcceptContext<'_>, args: &crate::parser::ArgParser<'_>) -> Option<rustc_attr_data_structures::AttributeKind> {
15-
todo!()
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+
}
1648
}
1749
}

0 commit comments

Comments
 (0)