|
1 | 1 | use rustc_attr_data_structures::{AttributeKind, OptimizeAttr};
|
2 | 2 | use rustc_feature::{AttributeTemplate, template};
|
3 |
| -use rustc_span::sym; |
| 3 | +use rustc_session::parse::feature_err; |
| 4 | +use rustc_span::{Span, sym}; |
4 | 5 |
|
5 |
| -use super::{AttributeOrder, OnDuplicate, SingleAttributeParser}; |
6 |
| -use crate::context::{AcceptContext, Stage}; |
| 6 | +use super::{AcceptMapping, AttributeOrder, AttributeParser, OnDuplicate, SingleAttributeParser}; |
| 7 | +use crate::context::{AcceptContext, FinalizeContext, Stage}; |
7 | 8 | use crate::parser::ArgParser;
|
| 9 | +use crate::session_diagnostics::NakedFunctionIncompatibleAttribute; |
8 | 10 |
|
9 | 11 | pub(crate) struct OptimizeParser;
|
10 | 12 |
|
@@ -51,12 +53,119 @@ impl<S: Stage> SingleAttributeParser<S> for ColdParser {
|
51 | 53 | if !args.no_args() {
|
52 | 54 | cx.expected_no_args(args.span().unwrap_or(cx.attr_span));
|
53 | 55 | return None;
|
54 |
| - }; |
| 56 | + } |
55 | 57 |
|
56 | 58 | Some(AttributeKind::Cold(cx.attr_span))
|
57 | 59 | }
|
58 | 60 | }
|
59 | 61 |
|
| 62 | +#[derive(Default)] |
| 63 | +pub(crate) struct NakedParser { |
| 64 | + span: Option<Span>, |
| 65 | +} |
| 66 | + |
| 67 | +impl<S: Stage> AttributeParser<S> for NakedParser { |
| 68 | + const ATTRIBUTES: AcceptMapping<Self, S> = |
| 69 | + &[(&[sym::naked], template!(Word), |this, cx, args| { |
| 70 | + if !args.no_args() { |
| 71 | + cx.expected_no_args(args.span().unwrap_or(cx.attr_span)); |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + if let Some(earlier) = this.span { |
| 76 | + let span = cx.attr_span; |
| 77 | + cx.warn_unused_duplicate(earlier, span); |
| 78 | + } else { |
| 79 | + this.span = Some(cx.attr_span); |
| 80 | + } |
| 81 | + })]; |
| 82 | + |
| 83 | + fn finalize(self, cx: &FinalizeContext<'_, '_, S>) -> Option<AttributeKind> { |
| 84 | + // FIXME(jdonszelmann): upgrade this list to *parsed* attributes |
| 85 | + // once all of these have parsed forms. That'd make the check much nicer... |
| 86 | + // |
| 87 | + // many attributes don't make sense in combination with #[naked]. |
| 88 | + // Notable attributes that are incompatible with `#[naked]` are: |
| 89 | + // |
| 90 | + // * `#[inline]` |
| 91 | + // * `#[track_caller]` |
| 92 | + // * `#[test]`, `#[ignore]`, `#[should_panic]` |
| 93 | + // |
| 94 | + // NOTE: when making changes to this list, check that `error_codes/E0736.md` remains |
| 95 | + // accurate. |
| 96 | + const ALLOW_LIST: &[rustc_span::Symbol] = &[ |
| 97 | + // conditional compilation |
| 98 | + sym::cfg_trace, |
| 99 | + sym::cfg_attr_trace, |
| 100 | + // testing (allowed here so better errors can be generated in `rustc_builtin_macros::test`) |
| 101 | + sym::test, |
| 102 | + sym::ignore, |
| 103 | + sym::should_panic, |
| 104 | + sym::bench, |
| 105 | + // diagnostics |
| 106 | + sym::allow, |
| 107 | + sym::warn, |
| 108 | + sym::deny, |
| 109 | + sym::forbid, |
| 110 | + sym::deprecated, |
| 111 | + sym::must_use, |
| 112 | + // abi, linking and FFI |
| 113 | + sym::cold, |
| 114 | + sym::export_name, |
| 115 | + sym::link_section, |
| 116 | + sym::linkage, |
| 117 | + sym::no_mangle, |
| 118 | + sym::instruction_set, |
| 119 | + sym::repr, |
| 120 | + sym::rustc_std_internal_symbol, |
| 121 | + sym::align, |
| 122 | + // obviously compatible with self |
| 123 | + sym::naked, |
| 124 | + // documentation |
| 125 | + sym::doc, |
| 126 | + ]; |
| 127 | + |
| 128 | + let span = self.span?; |
| 129 | + |
| 130 | + // only if we found a naked attribute do we do the somewhat expensive check |
| 131 | + 'outer: for other_attr in cx.all_attrs { |
| 132 | + for allowed_attr in ALLOW_LIST { |
| 133 | + if other_attr.segments().next().is_some_and(|i| cx.tools.contains(&i.name)) { |
| 134 | + // effectively skips the error message being emitted below |
| 135 | + // if it's a tool attribute |
| 136 | + continue 'outer; |
| 137 | + } |
| 138 | + if other_attr.word_is(*allowed_attr) { |
| 139 | + // effectively skips the error message being emitted below |
| 140 | + // if its an allowed attribute |
| 141 | + continue 'outer; |
| 142 | + } |
| 143 | + |
| 144 | + if other_attr.word_is(sym::target_feature) { |
| 145 | + if !cx.features().naked_functions_target_feature() { |
| 146 | + feature_err( |
| 147 | + &cx.sess(), |
| 148 | + sym::naked_functions_target_feature, |
| 149 | + other_attr.span(), |
| 150 | + "`#[target_feature(/* ... */)]` is currently unstable on `#[naked]` functions", |
| 151 | + ).emit(); |
| 152 | + } |
| 153 | + |
| 154 | + continue 'outer; |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + cx.emit_err(NakedFunctionIncompatibleAttribute { |
| 159 | + span: other_attr.span(), |
| 160 | + naked_span: span, |
| 161 | + attr: other_attr.get_attribute_path().to_string(), |
| 162 | + }); |
| 163 | + } |
| 164 | + |
| 165 | + Some(AttributeKind::Naked(span)) |
| 166 | + } |
| 167 | +} |
| 168 | + |
60 | 169 | pub(crate) struct NoMangleParser;
|
61 | 170 |
|
62 | 171 | impl<S: Stage> SingleAttributeParser<S> for NoMangleParser {
|
|
0 commit comments