Skip to content

Commit 211cdac

Browse files
committed
convert entire codebase to parsed inline attrs
1 parent 3a3a21a commit 211cdac

File tree

7 files changed

+82
-132
lines changed

7 files changed

+82
-132
lines changed

compiler/rustc_attr_parsing/src/attributes/inline.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -64,25 +64,22 @@ impl<S: Stage> SingleAttributeParser<S> for RustcForceInlineParser {
6464
let reason = match args {
6565
ArgParser::NoArgs => None,
6666
ArgParser::List(list) => {
67-
cx.emit_err(IncorrectMetaItem {
68-
span: list.span,
69-
suggestion: None,
70-
});
71-
return None
67+
cx.emit_err(IncorrectMetaItem { span: list.span, suggestion: None });
68+
return None;
7269
}
7370
ArgParser::NameValue(v) => {
7471
let Some(str) = v.value_as_str() else {
75-
cx.emit_err(IncorrectMetaItem {
76-
span: v.value_span,
77-
suggestion: None,
78-
});
72+
cx.emit_err(IncorrectMetaItem { span: v.value_span, suggestion: None });
7973
return None;
8074
};
8175

8276
Some(str)
8377
}
8478
};
8579

86-
Some(AttributeKind::Inline(InlineAttr::Force { attr_span: cx.attr_span, reason }, cx.attr_span))
80+
Some(AttributeKind::Inline(
81+
InlineAttr::Force { attr_span: cx.attr_span, reason },
82+
cx.attr_span,
83+
))
8784
}
8885
}

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

+3-46
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_ast::expand::autodiff_attrs::{
66
};
77
use rustc_ast::{MetaItem, MetaItemInner, attr};
88
use rustc_attr_parsing::ReprAttr::ReprAlign;
9-
use rustc_attr_parsing::{AttributeKind, InlineAttr, InstructionSetAttr, OptimizeAttr};
9+
use rustc_attr_parsing::{AttributeKind, InlineAttr, InstructionSetAttr, OptimizeAttr, find_attr};
1010
use rustc_data_structures::fx::FxHashMap;
1111
use rustc_errors::codes::*;
1212
use rustc_errors::{DiagMessage, SubdiagMessage, struct_span_code_err};
@@ -25,7 +25,6 @@ use rustc_session::parse::feature_err;
2525
use rustc_session::{Session, lint};
2626
use rustc_span::{Ident, Span, sym};
2727
use rustc_target::spec::SanitizerSet;
28-
use tracing::debug;
2928

3029
use crate::errors;
3130
use crate::target_features::{check_target_feature_trait_unsafe, from_target_feature_attr};
@@ -519,50 +518,8 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
519518

520519
mixed_export_name_no_mangle_lint_state.lint_if_mixed(tcx);
521520

522-
codegen_fn_attrs.inline = attrs.iter().fold(InlineAttr::None, |ia, attr| {
523-
if !attr.has_name(sym::inline) {
524-
return ia;
525-
}
526-
527-
if attr.is_word() {
528-
return InlineAttr::Hint;
529-
}
530-
let Some(ref items) = attr.meta_item_list() else {
531-
return ia;
532-
};
533-
inline_span = Some(attr.span());
534-
535-
let [item] = &items[..] else {
536-
struct_span_code_err!(tcx.dcx(), attr.span(), E0534, "expected one argument").emit();
537-
return InlineAttr::None;
538-
};
539-
540-
if item.has_name(sym::always) {
541-
InlineAttr::Always
542-
} else if item.has_name(sym::never) {
543-
InlineAttr::Never
544-
} else {
545-
struct_span_code_err!(tcx.dcx(), items[0].span(), E0535, "invalid argument")
546-
.with_help("valid inline arguments are `always` and `never`")
547-
.emit();
548-
549-
InlineAttr::None
550-
}
551-
});
552-
codegen_fn_attrs.inline = attrs.iter().fold(codegen_fn_attrs.inline, |ia, attr| {
553-
if !attr.has_name(sym::rustc_force_inline) || !tcx.features().rustc_attrs() {
554-
return ia;
555-
}
556-
557-
if attr.is_word() {
558-
InlineAttr::Force { attr_span: attr.span(), reason: None }
559-
} else if let Some(val) = attr.value_str() {
560-
InlineAttr::Force { attr_span: attr.span(), reason: Some(val) }
561-
} else {
562-
debug!("`rustc_force_inline` not checked by attribute validation");
563-
ia
564-
}
565-
});
521+
codegen_fn_attrs.inline =
522+
find_attr!(attrs, AttributeKind::Inline(i, _) => *i).unwrap_or(InlineAttr::None);
566523

567524
// naked function MUST NOT be inlined! This attribute is required for the rust compiler itself,
568525
// but not for the code generation backend because at that point the naked function will just be

compiler/rustc_passes/src/check_attr.rs

+44-42
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::collections::hash_map::Entry;
1010

1111
use rustc_abi::{Align, ExternAbi, Size};
1212
use rustc_ast::{AttrStyle, LitKind, MetaItemInner, MetaItemKind, MetaItemLit, ast};
13-
use rustc_attr_parsing::{AttributeKind, ReprAttr, find_attr};
13+
use rustc_attr_parsing::{AttributeKind, InlineAttr, ReprAttr, find_attr};
1414
use rustc_data_structures::fx::FxHashMap;
1515
use rustc_errors::{Applicability, DiagCtxtHandle, IntoDiagArg, MultiSpan, StashKey};
1616
use rustc_feature::{AttributeDuplicates, AttributeType, BUILTIN_ATTRIBUTE_MAP, BuiltinAttribute};
@@ -124,6 +124,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
124124
AttributeKind::Stability { span, .. }
125125
| AttributeKind::ConstStability { span, .. },
126126
) => self.check_stability_promotable(*span, target),
127+
Attribute::Parsed(AttributeKind::Inline(_, attr_span)) => {
128+
self.check_inline(hir_id, *attr_span, span, target)
129+
}
127130
Attribute::Parsed(AttributeKind::AllowInternalUnstable(syms)) => self
128131
.check_allow_internal_unstable(
129132
hir_id,
@@ -140,7 +143,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
140143
[sym::diagnostic, sym::on_unimplemented, ..] => {
141144
self.check_diagnostic_on_unimplemented(attr.span(), hir_id, target)
142145
}
143-
[sym::inline, ..] => self.check_inline(hir_id, attr, span, target),
144146
[sym::coverage, ..] => self.check_coverage(attr, span, target),
145147
[sym::optimize, ..] => self.check_optimize(hir_id, attr, span, target),
146148
[sym::no_sanitize, ..] => {
@@ -351,11 +353,11 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
351353
self.check_rustc_force_inline(hir_id, attrs, span, target);
352354
}
353355

354-
fn inline_attr_str_error_with_macro_def(&self, hir_id: HirId, attr: &Attribute, sym: &str) {
356+
fn inline_attr_str_error_with_macro_def(&self, hir_id: HirId, attr_span: Span, sym: &str) {
355357
self.tcx.emit_node_span_lint(
356358
UNUSED_ATTRIBUTES,
357359
hir_id,
358-
attr.span(),
360+
attr_span,
359361
errors::IgnoredAttrWithMacro { sym },
360362
);
361363
}
@@ -415,7 +417,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
415417
}
416418

417419
/// Checks if an `#[inline]` is applied to a function or a closure.
418-
fn check_inline(&self, hir_id: HirId, attr: &Attribute, span: Span, target: Target) {
420+
fn check_inline(&self, hir_id: HirId, attr_span: Span, defn_span: Span, target: Target) {
419421
match target {
420422
Target::Fn
421423
| Target::Closure
@@ -424,7 +426,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
424426
self.tcx.emit_node_span_lint(
425427
UNUSED_ATTRIBUTES,
426428
hir_id,
427-
attr.span(),
429+
attr_span,
428430
errors::IgnoredInlineAttrFnProto,
429431
)
430432
}
@@ -435,18 +437,15 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
435437
Target::AssocConst => self.tcx.emit_node_span_lint(
436438
UNUSED_ATTRIBUTES,
437439
hir_id,
438-
attr.span(),
440+
attr_span,
439441
errors::IgnoredInlineAttrConstants,
440442
),
441443
// FIXME(#80564): Same for fields, arms, and macro defs
442444
Target::Field | Target::Arm | Target::MacroDef => {
443-
self.inline_attr_str_error_with_macro_def(hir_id, attr, "inline")
445+
self.inline_attr_str_error_with_macro_def(hir_id, attr_span, "inline")
444446
}
445447
_ => {
446-
self.dcx().emit_err(errors::InlineNotFnOrClosure {
447-
attr_span: attr.span(),
448-
defn_span: span,
449-
});
448+
self.dcx().emit_err(errors::InlineNotFnOrClosure { attr_span, defn_span });
450449
}
451450
}
452451
}
@@ -640,7 +639,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
640639
// erroneously allowed it and some crates used it accidentally, to be compatible
641640
// with crates depending on them, we can't throw an error here.
642641
Target::Field | Target::Arm | Target::MacroDef => {
643-
self.inline_attr_str_error_with_macro_def(hir_id, attr, "naked")
642+
self.inline_attr_str_error_with_macro_def(hir_id, attr.span(), "naked")
644643
}
645644
_ => {
646645
self.dcx().emit_err(errors::AttrShouldBeAppliedToFn {
@@ -718,7 +717,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
718717
// with crates depending on them, we can't throw an error here.
719718
Target::Field | Target::Arm | Target::MacroDef => {
720719
for attr in attrs {
721-
self.inline_attr_str_error_with_macro_def(hir_id, attr, "track_caller");
720+
self.inline_attr_str_error_with_macro_def(hir_id, attr.span(), "track_caller");
722721
}
723722
}
724723
_ => {
@@ -761,7 +760,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
761760
// erroneously allowed it and some crates used it accidentally, to be compatible
762761
// with crates depending on them, we can't throw an error here.
763762
Target::Field | Target::Arm | Target::MacroDef => {
764-
self.inline_attr_str_error_with_macro_def(hir_id, attr, "non_exhaustive");
763+
self.inline_attr_str_error_with_macro_def(hir_id, attr.span(), "non_exhaustive");
765764
}
766765
_ => {
767766
self.dcx().emit_err(errors::NonExhaustiveWrongLocation {
@@ -781,7 +780,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
781780
// erroneously allowed it and some crates used it accidentally, to be compatible
782781
// with crates depending on them, we can't throw an error here.
783782
Target::Field | Target::Arm | Target::MacroDef => {
784-
self.inline_attr_str_error_with_macro_def(hir_id, attr, "marker");
783+
self.inline_attr_str_error_with_macro_def(hir_id, attr.span(), "marker");
785784
}
786785
_ => {
787786
self.dcx().emit_err(errors::AttrShouldBeAppliedToTrait {
@@ -835,7 +834,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
835834
// erroneously allowed it and some crates used it accidentally, to be compatible
836835
// with crates depending on them, we can't throw an error here.
837836
Target::Field | Target::Arm | Target::MacroDef => {
838-
self.inline_attr_str_error_with_macro_def(hir_id, attr, "target_feature");
837+
self.inline_attr_str_error_with_macro_def(hir_id, attr.span(), "target_feature");
839838
}
840839
_ => {
841840
self.dcx().emit_err(errors::AttrShouldBeAppliedToFn {
@@ -1546,7 +1545,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
15461545
// erroneously allowed it and some crates used it accidentally, to be compatible
15471546
// with crates depending on them, we can't throw an error here.
15481547
Target::Field | Target::Arm | Target::MacroDef => {
1549-
self.inline_attr_str_error_with_macro_def(hir_id, attr, "cold");
1548+
self.inline_attr_str_error_with_macro_def(hir_id, attr.span(), "cold");
15501549
}
15511550
_ => {
15521551
// FIXME: #[cold] was previously allowed on non-functions and some crates used
@@ -1588,7 +1587,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
15881587
// erroneously allowed it and some crates used it accidentally, to be compatible
15891588
// with crates depending on them, we can't throw an error here.
15901589
Target::Field | Target::Arm | Target::MacroDef => {
1591-
self.inline_attr_str_error_with_macro_def(hir_id, attr, "link_name");
1590+
self.inline_attr_str_error_with_macro_def(hir_id, attr.span(), "link_name");
15921591
}
15931592
_ => {
15941593
// FIXME: #[cold] was previously allowed on non-functions/statics and some crates
@@ -1622,7 +1621,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
16221621
// erroneously allowed it and some crates used it accidentally, to be compatible
16231622
// with crates depending on them, we can't throw an error here.
16241623
Target::Field | Target::Arm | Target::MacroDef => {
1625-
self.inline_attr_str_error_with_macro_def(hir_id, attr, "no_link");
1624+
self.inline_attr_str_error_with_macro_def(hir_id, attr.span(), "no_link");
16261625
}
16271626
_ => {
16281627
self.dcx().emit_err(errors::NoLink { attr_span: attr.span(), span });
@@ -1644,7 +1643,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
16441643
// erroneously allowed it and some crates used it accidentally, to be compatible
16451644
// with crates depending on them, we can't throw an error here.
16461645
Target::Field | Target::Arm | Target::MacroDef => {
1647-
self.inline_attr_str_error_with_macro_def(hir_id, attr, "export_name");
1646+
self.inline_attr_str_error_with_macro_def(hir_id, attr.span(), "export_name");
16481647
}
16491648
_ => {
16501649
self.dcx().emit_err(errors::ExportName { attr_span: attr.span(), span });
@@ -1818,7 +1817,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
18181817
// erroneously allowed it and some crates used it accidentally, to be compatible
18191818
// with crates depending on them, we can't throw an error here.
18201819
Target::Field | Target::Arm | Target::MacroDef => {
1821-
self.inline_attr_str_error_with_macro_def(hir_id, attr, "link_section");
1820+
self.inline_attr_str_error_with_macro_def(hir_id, attr.span(), "link_section");
18221821
}
18231822
_ => {
18241823
// FIXME: #[link_section] was previously allowed on non-functions/statics and some
@@ -1843,7 +1842,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
18431842
// erroneously allowed it and some crates used it accidentally, to be compatible
18441843
// with crates depending on them, we can't throw an error here.
18451844
Target::Field | Target::Arm | Target::MacroDef => {
1846-
self.inline_attr_str_error_with_macro_def(hir_id, attr, "no_mangle");
1845+
self.inline_attr_str_error_with_macro_def(hir_id, attr.span(), "no_mangle");
18471846
}
18481847
// FIXME: #[no_mangle] was previously allowed on non-functions/statics, this should be an error
18491848
// The error should specify that the item that is wrong is specifically a *foreign* fn/static
@@ -2190,9 +2189,12 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
21902189
// `#[allow_internal_unstable]` attribute with just a lint, because we previously
21912190
// erroneously allowed it and some crates used it accidentally, to be compatible
21922191
// with crates depending on them, we can't throw an error here.
2193-
Target::Field | Target::Arm | Target::MacroDef => {
2194-
self.inline_attr_str_error_with_macro_def(hir_id, attr, "allow_internal_unstable")
2195-
}
2192+
Target::Field | Target::Arm | Target::MacroDef => self
2193+
.inline_attr_str_error_with_macro_def(
2194+
hir_id,
2195+
attr.span(),
2196+
"allow_internal_unstable",
2197+
),
21962198
_ => {
21972199
self.tcx
21982200
.dcx()
@@ -2568,8 +2570,10 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
25682570
span: Span,
25692571
target: Target,
25702572
) {
2571-
let force_inline_attr = attrs.iter().find(|attr| attr.has_name(sym::rustc_force_inline));
2572-
match (target, force_inline_attr) {
2573+
match (
2574+
target,
2575+
find_attr!(attrs, AttributeKind::Inline(InlineAttr::Force { attr_span, .. }, _) => *attr_span),
2576+
) {
25732577
(Target::Closure, None) => {
25742578
let is_coro = matches!(
25752579
self.tcx.hir().expect_expr(hir_id).kind,
@@ -2581,20 +2585,19 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
25812585
);
25822586
let parent_did = self.tcx.hir_get_parent_item(hir_id).to_def_id();
25832587
let parent_span = self.tcx.def_span(parent_did);
2584-
let parent_force_inline_attr =
2585-
self.tcx.get_attr(parent_did, sym::rustc_force_inline);
2586-
if let Some(attr) = parent_force_inline_attr
2587-
&& is_coro
2588+
2589+
if let Some(attr_span) = find_attr!(
2590+
self.tcx.get_all_attrs(parent_did),
2591+
AttributeKind::Inline(InlineAttr::Force { attr_span, .. }, _) => *attr_span
2592+
) && is_coro
25882593
{
2589-
self.dcx().emit_err(errors::RustcForceInlineCoro {
2590-
attr_span: attr.span(),
2591-
span: parent_span,
2592-
});
2594+
self.dcx()
2595+
.emit_err(errors::RustcForceInlineCoro { attr_span, span: parent_span });
25932596
}
25942597
}
25952598
(Target::Fn, _) => (),
2596-
(_, Some(attr)) => {
2597-
self.dcx().emit_err(errors::RustcForceInline { attr_span: attr.span(), span });
2599+
(_, Some(attr_span)) => {
2600+
self.dcx().emit_err(errors::RustcForceInline { attr_span, span });
25982601
}
25992602
(_, None) => (),
26002603
}
@@ -2816,10 +2819,8 @@ fn check_invalid_crate_level_attr(tcx: TyCtxt<'_>, attrs: &[Attribute]) {
28162819
fn check_non_exported_macro_for_invalid_attrs(tcx: TyCtxt<'_>, item: &Item<'_>) {
28172820
let attrs = tcx.hir().attrs(item.hir_id());
28182821

2819-
for attr in attrs {
2820-
if attr.has_name(sym::inline) {
2821-
tcx.dcx().emit_err(errors::NonExportedMacroInvalidAttrs { attr_span: attr.span() });
2822-
}
2822+
if let Some(attr_span) = find_attr!(attrs, AttributeKind::Inline(_, span) => *span) {
2823+
tcx.dcx().emit_err(errors::NonExportedMacroInvalidAttrs { attr_span });
28232824
}
28242825
}
28252826

@@ -2839,6 +2840,7 @@ pub(crate) fn provide(providers: &mut Providers) {
28392840
*providers = Providers { check_mod_attrs, ..*providers };
28402841
}
28412842

2843+
// FIXME(jdonszelmann): remove, check during parsing
28422844
fn check_duplicates(
28432845
tcx: TyCtxt<'_>,
28442846
attr: &Attribute,

0 commit comments

Comments
 (0)