Skip to content

Commit d764c2d

Browse files
committed
address comments
1 parent 25fdea0 commit d764c2d

File tree

2 files changed

+50
-45
lines changed

2 files changed

+50
-45
lines changed

compiler/rustc_macros/src/diagnostics/utils.rs

+49-44
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,51 @@ pub(super) enum AllowMultipleAlternatives {
420420
Yes,
421421
}
422422

423+
fn parse_suggestion_values(
424+
nested: ParseNestedMeta<'_>,
425+
allow_multiple: AllowMultipleAlternatives,
426+
) -> syn::Result<Vec<LitStr>> {
427+
let values = if let Ok(val) = nested.value() {
428+
vec![val.parse()?]
429+
} else {
430+
let content;
431+
parenthesized!(content in nested.input);
432+
433+
if let AllowMultipleAlternatives::No = allow_multiple {
434+
span_err(
435+
nested.input.span().unwrap(),
436+
"expected exactly one string literal for `code = ...`",
437+
)
438+
.emit();
439+
vec![]
440+
} else {
441+
let literals = Punctuated::<LitStr, Token![,]>::parse_terminated(&content);
442+
443+
match literals {
444+
Ok(p) if p.is_empty() => {
445+
span_err(
446+
content.span().unwrap(),
447+
"expected at least one string literal for `code(...)`",
448+
)
449+
.emit();
450+
vec![]
451+
}
452+
Ok(p) => p.into_iter().collect(),
453+
Err(_) => {
454+
span_err(
455+
content.span().unwrap(),
456+
"`code(...)` must contain only string literals",
457+
)
458+
.emit();
459+
vec![]
460+
}
461+
}
462+
}
463+
};
464+
465+
Ok(values)
466+
}
467+
423468
/// Constructs the `format!()` invocation(s) necessary for a `#[suggestion*(code = "foo")]` or
424469
/// `#[suggestion*(code("foo", "bar"))]` attribute field
425470
pub(super) fn build_suggestion_code(
@@ -428,47 +473,7 @@ pub(super) fn build_suggestion_code(
428473
fields: &impl HasFieldMap,
429474
allow_multiple: AllowMultipleAlternatives,
430475
) -> TokenStream {
431-
let values = match (|| {
432-
let values: Vec<LitStr> = if let Ok(val) = nested.value() {
433-
vec![val.parse()?]
434-
} else {
435-
let content;
436-
parenthesized!(content in nested.input);
437-
438-
if let AllowMultipleAlternatives::No = allow_multiple {
439-
span_err(
440-
nested.input.span().unwrap(),
441-
"expected exactly one string literal for `code = ...`",
442-
)
443-
.emit();
444-
vec![]
445-
} else {
446-
let literals = Punctuated::<LitStr, Token![,]>::parse_terminated(&content);
447-
448-
match literals {
449-
Ok(p) if p.is_empty() => {
450-
span_err(
451-
content.span().unwrap(),
452-
"expected at least one string literal for `code(...)`",
453-
)
454-
.emit();
455-
vec![]
456-
}
457-
Ok(p) => p.into_iter().collect(),
458-
Err(_) => {
459-
span_err(
460-
content.span().unwrap(),
461-
"`code(...)` must contain only string literals",
462-
)
463-
.emit();
464-
vec![]
465-
}
466-
}
467-
}
468-
};
469-
470-
Ok(values)
471-
})() {
476+
let values = match parse_suggestion_values(nested, allow_multiple) {
472477
Ok(x) => x,
473478
Err(e) => return e.into_compile_error(),
474479
};
@@ -706,14 +711,14 @@ impl SubdiagnosticKind {
706711
let path_span = nested.path.span().unwrap();
707712
let val_span = nested.input.span().unwrap();
708713

709-
macro get_string() {
710-
{
714+
macro_rules! get_string {
715+
() => {{
711716
let Ok(value) = nested.value().and_then(|x| x.parse::<LitStr>()) else {
712717
span_err(val_span, "expected `= \"xxx\"`").emit();
713718
return Ok(());
714719
};
715720
value
716-
}
721+
}};
717722
}
718723

719724
let mut has_errors = false;

compiler/rustc_macros/src/newtype.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl Parse for Newtype {
4747
false
4848
}
4949
"debug_format" => {
50-
let Meta::NameValue(MetaNameValue { value: Expr::Lit(lit), .. } ) = &attr.meta else {
50+
let Meta::NameValue(MetaNameValue { value: Expr::Lit(lit), .. }) = &attr.meta else {
5151
panic!("#[debug_format = FMT] attribute requires a format");
5252
};
5353

0 commit comments

Comments
 (0)