|
1 |
| -use rustc_feature::AttributeType; |
| 1 | +use std::num::IntErrorKind; |
| 2 | + |
| 3 | +use crate::session_diagnostics::LimitInvalid; |
2 | 4 |
|
3 | 5 | use super::prelude::*;
|
4 | 6 |
|
| 7 | +impl<S: Stage> AcceptContext<'_, '_, S> { |
| 8 | + fn parse_limit_int(&self, nv: &NameValueParser) -> Option<usize> { |
| 9 | + let Some(limit) = nv.value_as_str() else { |
| 10 | + self.expected_string_literal(nv.value_span, Some(nv.value_as_lit())); |
| 11 | + return None; |
| 12 | + }; |
| 13 | + |
| 14 | + let error_str = match limit.as_str().parse() { |
| 15 | + Ok(i) => return Some(i), |
| 16 | + Err(e) => match e.kind() { |
| 17 | + IntErrorKind::PosOverflow => "`limit` is too large", |
| 18 | + IntErrorKind::Empty => "`limit` must be a non-negative integer", |
| 19 | + IntErrorKind::InvalidDigit => "not a valid integer", |
| 20 | + IntErrorKind::NegOverflow => { |
| 21 | + panic!( |
| 22 | + "`limit` should never negatively overflow since we're parsing into a usize and we'd get Empty instead" |
| 23 | + ) |
| 24 | + } |
| 25 | + IntErrorKind::Zero => { |
| 26 | + panic!("zero is a valid `limit` so should have returned Ok() when parsing") |
| 27 | + } |
| 28 | + kind => panic!("unimplemented IntErrorKind variant: {:?}", kind), |
| 29 | + }, |
| 30 | + }; |
| 31 | + |
| 32 | + self.emit_err(LimitInvalid { span: self.attr_span, value_span: nv.value_span, error_str }); |
| 33 | + |
| 34 | + None |
| 35 | + } |
| 36 | +} |
| 37 | + |
5 | 38 | pub(crate) struct CrateNameParser;
|
6 | 39 |
|
7 | 40 | impl<S: Stage> SingleAttributeParser<S> for CrateNameParser {
|
@@ -34,3 +67,30 @@ impl<S: Stage> SingleAttributeParser<S> for CrateNameParser {
|
34 | 67 | })
|
35 | 68 | }
|
36 | 69 | }
|
| 70 | + |
| 71 | +pub(crate) struct RecursionLimitParser; |
| 72 | + |
| 73 | +impl<S: Stage> SingleAttributeParser<S> for RecursionLimitParser { |
| 74 | + const PATH: &[Symbol] = &[sym::recursion_limit]; |
| 75 | + const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost; |
| 76 | + const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError; |
| 77 | + const TEMPLATE: AttributeTemplate = template!(NameValueStr: "N", "https://doc.rust-lang.org/reference/attributes/limits.html#the-recursion_limit-attribute"); |
| 78 | + const TYPE: AttributeType = AttributeType::CrateLevel; |
| 79 | + |
| 80 | + // FIXME: recursion limit is allowed on all targets and ignored, |
| 81 | + // even though it should only be valid on crates of course |
| 82 | + const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS); |
| 83 | + |
| 84 | + fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> { |
| 85 | + let ArgParser::NameValue(nv) = args else { |
| 86 | + cx.expected_name_value(cx.attr_span, None); |
| 87 | + return None; |
| 88 | + }; |
| 89 | + |
| 90 | + Some(AttributeKind::RecursionLimit { |
| 91 | + limit: cx.parse_limit_int(nv)?, |
| 92 | + attr_span: cx.attr_span, |
| 93 | + limit_span: nv.value_span, |
| 94 | + }) |
| 95 | + } |
| 96 | +} |
0 commit comments