diff options
| author | Deadbeef <ent3rm4n@gmail.com> | 2023-04-06 04:52:03 +0000 |
|---|---|---|
| committer | Deadbeef <ent3rm4n@gmail.com> | 2023-04-06 04:55:58 +0000 |
| commit | d764c2da99fb20032e4dc41c1c23bb90aabd39ad (patch) | |
| tree | a2b1c84196df00ae477cdec8261996c0b08eef7c /compiler/rustc_macros/src/diagnostics/utils.rs | |
| parent | 25fdea009ef0db01134d17cf97cd09b507beca99 (diff) | |
| download | rust-d764c2da99fb20032e4dc41c1c23bb90aabd39ad.tar.gz rust-d764c2da99fb20032e4dc41c1c23bb90aabd39ad.zip | |
address comments
Diffstat (limited to 'compiler/rustc_macros/src/diagnostics/utils.rs')
| -rw-r--r-- | compiler/rustc_macros/src/diagnostics/utils.rs | 93 |
1 files changed, 49 insertions, 44 deletions
diff --git a/compiler/rustc_macros/src/diagnostics/utils.rs b/compiler/rustc_macros/src/diagnostics/utils.rs index 838347ecb61..b9b09c66230 100644 --- a/compiler/rustc_macros/src/diagnostics/utils.rs +++ b/compiler/rustc_macros/src/diagnostics/utils.rs @@ -420,6 +420,51 @@ pub(super) enum AllowMultipleAlternatives { Yes, } +fn parse_suggestion_values( + nested: ParseNestedMeta<'_>, + allow_multiple: AllowMultipleAlternatives, +) -> syn::Result<Vec<LitStr>> { + let values = if let Ok(val) = nested.value() { + vec![val.parse()?] + } else { + let content; + parenthesized!(content in nested.input); + + if let AllowMultipleAlternatives::No = allow_multiple { + span_err( + nested.input.span().unwrap(), + "expected exactly one string literal for `code = ...`", + ) + .emit(); + vec![] + } else { + let literals = Punctuated::<LitStr, Token![,]>::parse_terminated(&content); + + match literals { + Ok(p) if p.is_empty() => { + span_err( + content.span().unwrap(), + "expected at least one string literal for `code(...)`", + ) + .emit(); + vec![] + } + Ok(p) => p.into_iter().collect(), + Err(_) => { + span_err( + content.span().unwrap(), + "`code(...)` must contain only string literals", + ) + .emit(); + vec![] + } + } + } + }; + + Ok(values) +} + /// Constructs the `format!()` invocation(s) necessary for a `#[suggestion*(code = "foo")]` or /// `#[suggestion*(code("foo", "bar"))]` attribute field pub(super) fn build_suggestion_code( @@ -428,47 +473,7 @@ pub(super) fn build_suggestion_code( fields: &impl HasFieldMap, allow_multiple: AllowMultipleAlternatives, ) -> TokenStream { - let values = match (|| { - let values: Vec<LitStr> = if let Ok(val) = nested.value() { - vec![val.parse()?] - } else { - let content; - parenthesized!(content in nested.input); - - if let AllowMultipleAlternatives::No = allow_multiple { - span_err( - nested.input.span().unwrap(), - "expected exactly one string literal for `code = ...`", - ) - .emit(); - vec![] - } else { - let literals = Punctuated::<LitStr, Token![,]>::parse_terminated(&content); - - match literals { - Ok(p) if p.is_empty() => { - span_err( - content.span().unwrap(), - "expected at least one string literal for `code(...)`", - ) - .emit(); - vec![] - } - Ok(p) => p.into_iter().collect(), - Err(_) => { - span_err( - content.span().unwrap(), - "`code(...)` must contain only string literals", - ) - .emit(); - vec![] - } - } - } - }; - - Ok(values) - })() { + let values = match parse_suggestion_values(nested, allow_multiple) { Ok(x) => x, Err(e) => return e.into_compile_error(), }; @@ -706,14 +711,14 @@ impl SubdiagnosticKind { let path_span = nested.path.span().unwrap(); let val_span = nested.input.span().unwrap(); - macro get_string() { - { + macro_rules! get_string { + () => {{ let Ok(value) = nested.value().and_then(|x| x.parse::<LitStr>()) else { span_err(val_span, "expected `= \"xxx\"`").emit(); return Ok(()); }; value - } + }}; } let mut has_errors = false; |
