rustc_attr_parsing/attributes/
stability.rs

1use std::num::NonZero;
2
3use rustc_errors::ErrorGuaranteed;
4use rustc_hir::{
5    DefaultBodyStability, MethodKind, PartialConstStability, Stability, StabilityLevel,
6    StableSince, Target, UnstableReason, VERSION_PLACEHOLDER,
7};
8
9use super::prelude::*;
10use super::util::parse_version;
11use crate::session_diagnostics::{self};
12
13macro_rules! reject_outside_std {
14    ($cx: ident) => {
15        // Emit errors for non-staged-api crates.
16        if !$cx.features().staged_api() {
17            $cx.emit_err(session_diagnostics::StabilityOutsideStd { span: $cx.attr_span });
18            return;
19        }
20    };
21}
22
23const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
24    Allow(Target::Fn),
25    Allow(Target::Struct),
26    Allow(Target::Enum),
27    Allow(Target::Union),
28    Allow(Target::Method(MethodKind::Inherent)),
29    Allow(Target::Method(MethodKind::Trait { body: false })),
30    Allow(Target::Method(MethodKind::Trait { body: true })),
31    Allow(Target::Method(MethodKind::TraitImpl)),
32    Allow(Target::Impl { of_trait: false }),
33    Allow(Target::Impl { of_trait: true }),
34    Allow(Target::MacroDef),
35    Allow(Target::Crate),
36    Allow(Target::Mod),
37    Allow(Target::Use), // FIXME I don't think this does anything?
38    Allow(Target::Const),
39    Allow(Target::AssocConst),
40    Allow(Target::AssocTy),
41    Allow(Target::Trait),
42    Allow(Target::TraitAlias),
43    Allow(Target::TyAlias),
44    Allow(Target::Variant),
45    Allow(Target::Field),
46    Allow(Target::Param),
47    Allow(Target::Static),
48    Allow(Target::ForeignFn),
49    Allow(Target::ForeignStatic),
50    Allow(Target::ExternCrate),
51]);
52
53#[derive(Default)]
54pub(crate) struct StabilityParser {
55    allowed_through_unstable_modules: Option<Symbol>,
56    stability: Option<(Stability, Span)>,
57}
58
59impl StabilityParser {
60    /// Checks, and emits an error when a stability (or unstability) was already set, which would be a duplicate.
61    fn check_duplicate<S: Stage>(&self, cx: &AcceptContext<'_, '_, S>) -> bool {
62        if let Some((_, _)) = self.stability {
63            cx.emit_err(session_diagnostics::MultipleStabilityLevels { span: cx.attr_span });
64            true
65        } else {
66            false
67        }
68    }
69}
70
71impl<S: Stage> AttributeParser<S> for StabilityParser {
72    const ATTRIBUTES: AcceptMapping<Self, S> = &[
73        (
74            &[sym::stable],
75            template!(List: &[r#"feature = "name", since = "version""#]),
76            |this, cx, args| {
77                reject_outside_std!(cx);
78                if !this.check_duplicate(cx)
79                    && let Some((feature, level)) = parse_stability(cx, args)
80                {
81                    this.stability = Some((Stability { level, feature }, cx.attr_span));
82                }
83            },
84        ),
85        (
86            &[sym::unstable],
87            template!(List: &[r#"feature = "name", reason = "...", issue = "N""#]),
88            |this, cx, args| {
89                reject_outside_std!(cx);
90                if !this.check_duplicate(cx)
91                    && let Some((feature, level)) = parse_unstability(cx, args)
92                {
93                    this.stability = Some((Stability { level, feature }, cx.attr_span));
94                }
95            },
96        ),
97        (
98            &[sym::rustc_allowed_through_unstable_modules],
99            template!(NameValueStr: "deprecation message"),
100            |this, cx, args| {
101                reject_outside_std!(cx);
102                let Some(nv) = args.name_value() else {
103                    cx.expected_name_value(cx.attr_span, None);
104                    return;
105                };
106                let Some(value_str) = nv.value_as_str() else {
107                    cx.expected_string_literal(nv.value_span, Some(nv.value_as_lit()));
108                    return;
109                };
110                this.allowed_through_unstable_modules = Some(value_str);
111            },
112        ),
113    ];
114    const ALLOWED_TARGETS: AllowedTargets = ALLOWED_TARGETS;
115
116    fn finalize(mut self, cx: &FinalizeContext<'_, '_, S>) -> Option<AttributeKind> {
117        if let Some(atum) = self.allowed_through_unstable_modules {
118            if let Some((
119                Stability {
120                    level: StabilityLevel::Stable { ref mut allowed_through_unstable_modules, .. },
121                    ..
122                },
123                _,
124            )) = self.stability
125            {
126                *allowed_through_unstable_modules = Some(atum);
127            } else {
128                cx.dcx().emit_err(session_diagnostics::RustcAllowedUnstablePairing {
129                    span: cx.target_span,
130                });
131            }
132        }
133
134        if let Some((Stability { level: StabilityLevel::Stable { .. }, .. }, _)) = self.stability {
135            for other_attr in cx.all_attrs {
136                if other_attr.word_is(sym::unstable_feature_bound) {
137                    cx.emit_err(session_diagnostics::UnstableFeatureBoundIncompatibleStability {
138                        span: cx.target_span,
139                    });
140                }
141            }
142        }
143
144        let (stability, span) = self.stability?;
145
146        Some(AttributeKind::Stability { stability, span })
147    }
148}
149
150// FIXME(jdonszelmann) change to Single
151#[derive(Default)]
152pub(crate) struct BodyStabilityParser {
153    stability: Option<(DefaultBodyStability, Span)>,
154}
155
156impl<S: Stage> AttributeParser<S> for BodyStabilityParser {
157    const ATTRIBUTES: AcceptMapping<Self, S> = &[(
158        &[sym::rustc_default_body_unstable],
159        template!(List: &[r#"feature = "name", reason = "...", issue = "N""#]),
160        |this, cx, args| {
161            reject_outside_std!(cx);
162            if this.stability.is_some() {
163                cx.dcx()
164                    .emit_err(session_diagnostics::MultipleStabilityLevels { span: cx.attr_span });
165            } else if let Some((feature, level)) = parse_unstability(cx, args) {
166                this.stability = Some((DefaultBodyStability { level, feature }, cx.attr_span));
167            }
168        },
169    )];
170    const ALLOWED_TARGETS: AllowedTargets = ALLOWED_TARGETS;
171
172    fn finalize(self, _cx: &FinalizeContext<'_, '_, S>) -> Option<AttributeKind> {
173        let (stability, span) = self.stability?;
174
175        Some(AttributeKind::BodyStability { stability, span })
176    }
177}
178
179pub(crate) struct ConstStabilityIndirectParser;
180impl<S: Stage> NoArgsAttributeParser<S> for ConstStabilityIndirectParser {
181    const PATH: &[Symbol] = &[sym::rustc_const_stable_indirect];
182    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Ignore;
183    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
184        Allow(Target::Fn),
185        Allow(Target::Method(MethodKind::Inherent)),
186    ]);
187    const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::ConstStabilityIndirect;
188}
189
190#[derive(Default)]
191pub(crate) struct ConstStabilityParser {
192    promotable: bool,
193    stability: Option<(PartialConstStability, Span)>,
194}
195
196impl ConstStabilityParser {
197    /// Checks, and emits an error when a stability (or unstability) was already set, which would be a duplicate.
198    fn check_duplicate<S: Stage>(&self, cx: &AcceptContext<'_, '_, S>) -> bool {
199        if let Some((_, _)) = self.stability {
200            cx.emit_err(session_diagnostics::MultipleStabilityLevels { span: cx.attr_span });
201            true
202        } else {
203            false
204        }
205    }
206}
207
208impl<S: Stage> AttributeParser<S> for ConstStabilityParser {
209    const ATTRIBUTES: AcceptMapping<Self, S> = &[
210        (
211            &[sym::rustc_const_stable],
212            template!(List: &[r#"feature = "name""#]),
213            |this, cx, args| {
214                reject_outside_std!(cx);
215
216                if !this.check_duplicate(cx)
217                    && let Some((feature, level)) = parse_stability(cx, args)
218                {
219                    this.stability = Some((
220                        PartialConstStability { level, feature, promotable: false },
221                        cx.attr_span,
222                    ));
223                }
224            },
225        ),
226        (
227            &[sym::rustc_const_unstable],
228            template!(List: &[r#"feature = "name""#]),
229            |this, cx, args| {
230                reject_outside_std!(cx);
231                if !this.check_duplicate(cx)
232                    && let Some((feature, level)) = parse_unstability(cx, args)
233                {
234                    this.stability = Some((
235                        PartialConstStability { level, feature, promotable: false },
236                        cx.attr_span,
237                    ));
238                }
239            },
240        ),
241        (&[sym::rustc_promotable], template!(Word), |this, cx, _| {
242            reject_outside_std!(cx);
243            this.promotable = true;
244        }),
245    ];
246    const ALLOWED_TARGETS: AllowedTargets = ALLOWED_TARGETS;
247
248    fn finalize(mut self, cx: &FinalizeContext<'_, '_, S>) -> Option<AttributeKind> {
249        if self.promotable {
250            if let Some((ref mut stab, _)) = self.stability {
251                stab.promotable = true;
252            } else {
253                cx.dcx()
254                    .emit_err(session_diagnostics::RustcPromotablePairing { span: cx.target_span });
255            }
256        }
257
258        let (stability, span) = self.stability?;
259
260        Some(AttributeKind::ConstStability { stability, span })
261    }
262}
263
264/// Tries to insert the value of a `key = value` meta item into an option.
265///
266/// Emits an error when either the option was already Some, or the arguments weren't of form
267/// `name = value`
268fn insert_value_into_option_or_error<S: Stage>(
269    cx: &AcceptContext<'_, '_, S>,
270    param: &MetaItemParser,
271    item: &mut Option<Symbol>,
272    name: Ident,
273) -> Option<()> {
274    if item.is_some() {
275        cx.duplicate_key(name.span, name.name);
276        None
277    } else if let Some(v) = param.args().name_value()
278        && let Some(s) = v.value_as_str()
279    {
280        *item = Some(s);
281        Some(())
282    } else {
283        cx.expected_name_value(param.span(), Some(name.name));
284        None
285    }
286}
287
288/// Read the content of a `stable`/`rustc_const_stable` attribute, and return the feature name and
289/// its stability information.
290pub(crate) fn parse_stability<S: Stage>(
291    cx: &AcceptContext<'_, '_, S>,
292    args: &ArgParser,
293) -> Option<(Symbol, StabilityLevel)> {
294    let mut feature = None;
295    let mut since = None;
296
297    let ArgParser::List(list) = args else {
298        cx.expected_list(cx.attr_span, args);
299        return None;
300    };
301
302    for param in list.mixed() {
303        let param_span = param.span();
304        let Some(param) = param.meta_item() else {
305            cx.unexpected_literal(param.span());
306            return None;
307        };
308
309        let word = param.path().word();
310        match word.map(|i| i.name) {
311            Some(sym::feature) => {
312                insert_value_into_option_or_error(cx, &param, &mut feature, word.unwrap())?
313            }
314            Some(sym::since) => {
315                insert_value_into_option_or_error(cx, &param, &mut since, word.unwrap())?
316            }
317            _ => {
318                cx.expected_specific_argument(param_span, &[sym::feature, sym::since]);
319                return None;
320            }
321        }
322    }
323
324    let feature = match feature {
325        Some(feature) if rustc_lexer::is_ident(feature.as_str()) => Ok(feature),
326        Some(_bad_feature) => {
327            Err(cx.emit_err(session_diagnostics::NonIdentFeature { span: cx.attr_span }))
328        }
329        None => Err(cx.emit_err(session_diagnostics::MissingFeature { span: cx.attr_span })),
330    };
331
332    let since = if let Some(since) = since {
333        if since.as_str() == VERSION_PLACEHOLDER {
334            StableSince::Current
335        } else if let Some(version) = parse_version(since) {
336            StableSince::Version(version)
337        } else {
338            let err = cx.emit_err(session_diagnostics::InvalidSince { span: cx.attr_span });
339            StableSince::Err(err)
340        }
341    } else {
342        let err = cx.emit_err(session_diagnostics::MissingSince { span: cx.attr_span });
343        StableSince::Err(err)
344    };
345
346    match feature {
347        Ok(feature) => {
348            let level = StabilityLevel::Stable { since, allowed_through_unstable_modules: None };
349            Some((feature, level))
350        }
351        Err(ErrorGuaranteed { .. }) => None,
352    }
353}
354
355// Read the content of a `unstable`/`rustc_const_unstable`/`rustc_default_body_unstable`
356/// attribute, and return the feature name and its stability information.
357pub(crate) fn parse_unstability<S: Stage>(
358    cx: &AcceptContext<'_, '_, S>,
359    args: &ArgParser,
360) -> Option<(Symbol, StabilityLevel)> {
361    let mut feature = None;
362    let mut reason = None;
363    let mut issue = None;
364    let mut issue_num = None;
365    let mut is_soft = false;
366    let mut implied_by = None;
367    let mut old_name = None;
368
369    let ArgParser::List(list) = args else {
370        cx.expected_list(cx.attr_span, args);
371        return None;
372    };
373
374    for param in list.mixed() {
375        let Some(param) = param.meta_item() else {
376            cx.unexpected_literal(param.span());
377            return None;
378        };
379
380        let word = param.path().word();
381        match word.map(|i| i.name) {
382            Some(sym::feature) => {
383                insert_value_into_option_or_error(cx, &param, &mut feature, word.unwrap())?
384            }
385            Some(sym::reason) => {
386                insert_value_into_option_or_error(cx, &param, &mut reason, word.unwrap())?
387            }
388            Some(sym::issue) => {
389                insert_value_into_option_or_error(cx, &param, &mut issue, word.unwrap())?;
390
391                // These unwraps are safe because `insert_value_into_option_or_error` ensures the meta item
392                // is a name/value pair string literal.
393                issue_num = match issue.unwrap().as_str() {
394                    "none" => None,
395                    issue_str => match issue_str.parse::<NonZero<u32>>() {
396                        Ok(num) => Some(num),
397                        Err(err) => {
398                            cx.emit_err(
399                                session_diagnostics::InvalidIssueString {
400                                    span: param.span(),
401                                    cause: session_diagnostics::InvalidIssueStringCause::from_int_error_kind(
402                                        param.args().name_value().unwrap().value_span,
403                                        err.kind(),
404                                    ),
405                                },
406                            );
407                            return None;
408                        }
409                    },
410                };
411            }
412            Some(sym::soft) => {
413                if let Err(span) = args.no_args() {
414                    cx.emit_err(session_diagnostics::SoftNoArgs { span });
415                }
416                is_soft = true;
417            }
418            Some(sym::implied_by) => {
419                insert_value_into_option_or_error(cx, &param, &mut implied_by, word.unwrap())?
420            }
421            Some(sym::old_name) => {
422                insert_value_into_option_or_error(cx, &param, &mut old_name, word.unwrap())?
423            }
424            _ => {
425                cx.expected_specific_argument(
426                    param.span(),
427                    &[
428                        sym::feature,
429                        sym::reason,
430                        sym::issue,
431                        sym::soft,
432                        sym::implied_by,
433                        sym::old_name,
434                    ],
435                );
436                return None;
437            }
438        }
439    }
440
441    let feature = match feature {
442        Some(feature) if rustc_lexer::is_ident(feature.as_str()) => Ok(feature),
443        Some(_bad_feature) => {
444            Err(cx.emit_err(session_diagnostics::NonIdentFeature { span: cx.attr_span }))
445        }
446        None => Err(cx.emit_err(session_diagnostics::MissingFeature { span: cx.attr_span })),
447    };
448
449    let issue =
450        issue.ok_or_else(|| cx.emit_err(session_diagnostics::MissingIssue { span: cx.attr_span }));
451
452    match (feature, issue) {
453        (Ok(feature), Ok(_)) => {
454            let level = StabilityLevel::Unstable {
455                reason: UnstableReason::from_opt_reason(reason),
456                issue: issue_num,
457                is_soft,
458                implied_by,
459                old_name,
460            };
461            Some((feature, level))
462        }
463        (Err(ErrorGuaranteed { .. }), _) | (_, Err(ErrorGuaranteed { .. })) => None,
464    }
465}