|
| 1 | +use std::num::IntErrorKind; |
| 2 | + |
1 | 3 | use rustc_ast::LitKind;
|
2 | 4 | use rustc_ast::attr::AttributeExt;
|
3 | 5 | use rustc_feature::is_builtin_attr_name;
|
4 | 6 | use rustc_hir::RustcVersion;
|
| 7 | +use rustc_hir::limit::Limit; |
5 | 8 | use rustc_span::{Symbol, sym};
|
6 | 9 |
|
7 | 10 | use crate::context::{AcceptContext, Stage};
|
8 |
| -use crate::parser::ArgParser; |
| 11 | +use crate::parser::{ArgParser, NameValueParser}; |
| 12 | +use crate::session_diagnostics::LimitInvalid; |
9 | 13 |
|
10 | 14 | /// Parse a rustc version number written inside string literal in an attribute,
|
11 | 15 | /// like appears in `since = "1.0.0"`. Suffixes like "-dev" and "-nightly" are
|
@@ -85,3 +89,34 @@ pub(crate) fn parse_single_integer<S: Stage>(
|
85 | 89 | };
|
86 | 90 | Some(num.0)
|
87 | 91 | }
|
| 92 | + |
| 93 | +impl<S: Stage> AcceptContext<'_, '_, S> { |
| 94 | + pub(crate) fn parse_limit_int(&self, nv: &NameValueParser) -> Option<Limit> { |
| 95 | + let Some(limit) = nv.value_as_str() else { |
| 96 | + self.expected_string_literal(nv.value_span, Some(nv.value_as_lit())); |
| 97 | + return None; |
| 98 | + }; |
| 99 | + |
| 100 | + let error_str = match limit.as_str().parse() { |
| 101 | + Ok(i) => return Some(Limit::new(i)), |
| 102 | + Err(e) => match e.kind() { |
| 103 | + IntErrorKind::PosOverflow => "`limit` is too large", |
| 104 | + IntErrorKind::Empty => "`limit` must be a non-negative integer", |
| 105 | + IntErrorKind::InvalidDigit => "not a valid integer", |
| 106 | + IntErrorKind::NegOverflow => { |
| 107 | + panic!( |
| 108 | + "`limit` should never negatively overflow since we're parsing into a usize and we'd get Empty instead" |
| 109 | + ) |
| 110 | + } |
| 111 | + IntErrorKind::Zero => { |
| 112 | + panic!("zero is a valid `limit` so should have returned Ok() when parsing") |
| 113 | + } |
| 114 | + kind => panic!("unimplemented IntErrorKind variant: {:?}", kind), |
| 115 | + }, |
| 116 | + }; |
| 117 | + |
| 118 | + self.emit_err(LimitInvalid { span: self.attr_span, value_span: nv.value_span, error_str }); |
| 119 | + |
| 120 | + None |
| 121 | + } |
| 122 | +} |
0 commit comments