Skip to content

Commit f6ac728

Browse files
committed
convert strings to symbols in attr diagnostics
1 parent f5703d5 commit f6ac728

File tree

10 files changed

+36
-31
lines changed

10 files changed

+36
-31
lines changed

compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl<S: Stage> SingleAttributeParser<S> for OptimizeParser {
3535
Some(sym::speed) => OptimizeAttr::Speed,
3636
Some(sym::none) => OptimizeAttr::DoNotOptimize,
3737
_ => {
38-
cx.expected_specific_argument(single.span(), vec!["size", "speed", "none"]);
38+
cx.expected_specific_argument(single.span(), &[sym::size, sym::speed, sym::none]);
3939
OptimizeAttr::Default
4040
}
4141
};
@@ -82,7 +82,7 @@ impl<S: Stage> SingleAttributeParser<S> for CoverageParser {
8282

8383
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
8484
let Some(args) = args.list() else {
85-
cx.expected_specific_argument_and_list(cx.attr_span, vec!["on", "off"]);
85+
cx.expected_specific_argument_and_list(cx.attr_span, &[sym::on, sym::off]);
8686
return None;
8787
};
8888

@@ -91,7 +91,8 @@ impl<S: Stage> SingleAttributeParser<S> for CoverageParser {
9191
return None;
9292
};
9393

94-
let fail_incorrect_argument = |span| cx.expected_specific_argument(span, vec!["on", "off"]);
94+
let fail_incorrect_argument =
95+
|span| cx.expected_specific_argument(span, &[sym::on, sym::off]);
9596

9697
let Some(arg) = arg.meta_item() else {
9798
fail_incorrect_argument(args.span);
@@ -343,7 +344,7 @@ impl<S: Stage> AttributeParser<S> for UsedParser {
343344
UsedBy::Linker
344345
}
345346
_ => {
346-
cx.expected_specific_argument(l.span(), vec!["compiler", "linker"]);
347+
cx.expected_specific_argument(l.span(), &[sym::compiler, sym::linker]);
347348
return;
348349
}
349350
}

compiler/rustc_attr_parsing/src/attributes/inline.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl<S: Stage> SingleAttributeParser<S> for InlineParser {
4949
Some(AttributeKind::Inline(InlineAttr::Never, cx.attr_span))
5050
}
5151
_ => {
52-
cx.expected_specific_argument(l.span(), vec!["always", "never"]);
52+
cx.expected_specific_argument(l.span(), &[sym::always, sym::never]);
5353
return None;
5454
}
5555
}

compiler/rustc_attr_parsing/src/attributes/link_attrs.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -206,16 +206,16 @@ impl<S: Stage> SingleAttributeParser<S> for LinkageParser {
206206
_ => {
207207
cx.expected_specific_argument(
208208
name_value.value_span,
209-
vec![
210-
"available_externally",
211-
"common",
212-
"extern_weak",
213-
"external",
214-
"internal",
215-
"linkonce",
216-
"linkonce_odr",
217-
"weak",
218-
"weak_odr",
209+
&[
210+
sym::available_externally,
211+
sym::common,
212+
sym::extern_weak,
213+
sym::external,
214+
sym::internal,
215+
sym::linkonce,
216+
sym::linkonce_odr,
217+
sym::weak,
218+
sym::weak_odr,
219219
],
220220
);
221221
return None;

compiler/rustc_attr_parsing/src/attributes/proc_macro_attrs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ fn parse_derive_like<S: Stage>(
100100
return None;
101101
};
102102
if !attr_list.path().word_is(sym::attributes) {
103-
cx.expected_specific_argument(attrs.span(), vec!["attributes"]);
103+
cx.expected_specific_argument(attrs.span(), &[sym::attributes]);
104104
return None;
105105
}
106106
let Some(attr_list) = attr_list.args().list() else {

compiler/rustc_attr_parsing/src/attributes/prototype.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ fn parse_dialect<S: Stage>(
109109
sym::runtime => MirDialect::Runtime,
110110

111111
_ => {
112-
cx.expected_specific_argument(span, vec!["analysis", "built", "runtime"]);
112+
cx.expected_specific_argument(span, &[sym::analysis, sym::built, sym::runtime]);
113113
*failed = true;
114114
return None;
115115
}
@@ -131,7 +131,7 @@ fn parse_phase<S: Stage>(
131131
sym::optimized => MirPhase::Optimized,
132132

133133
_ => {
134-
cx.expected_specific_argument(span, vec!["initial", "post-cleanup", "optimized"]);
134+
cx.expected_specific_argument(span, &[sym::initial, sym::post_cleanup, sym::optimized]);
135135
*failed = true;
136136
return None;
137137
}

compiler/rustc_attr_parsing/src/attributes/test_attrs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ impl<S: Stage> SingleAttributeParser<S> for ShouldPanicParser {
8181
return None;
8282
};
8383
if !single.path().word_is(sym::expected) {
84-
cx.expected_specific_argument_strings(list.span, vec!["expected"]);
84+
cx.expected_specific_argument_strings(list.span, &[sym::expected]);
8585
return None;
8686
}
8787
let Some(nv) = single.args().name_value() else {

compiler/rustc_attr_parsing/src/attributes/traits.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl<S: Stage> SingleAttributeParser<S> for SkipDuringMethodDispatchParser {
4242
Some(key @ sym::array) => (key, &mut array),
4343
Some(key @ sym::boxed_slice) => (key, &mut boxed_slice),
4444
_ => {
45-
cx.expected_specific_argument(path.span(), vec!["array", "boxed_slice"]);
45+
cx.expected_specific_argument(path.span(), &[sym::array, sym::boxed_slice]);
4646
continue;
4747
}
4848
};

compiler/rustc_attr_parsing/src/attributes/transparency.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl<S: Stage> SingleAttributeParser<S> for TransparencyParser {
2929
Some(_) => {
3030
cx.expected_specific_argument_strings(
3131
nv.value_span,
32-
vec!["transparent", "semitransparent", "opaque"],
32+
&[sym::transparent, sym::semitransparent, sym::opaque],
3333
);
3434
None
3535
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -502,10 +502,11 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
502502
})
503503
}
504504

505+
/// produces an error along the lines of `expected one of [foo, meow]`
505506
pub(crate) fn expected_specific_argument(
506507
&self,
507508
span: Span,
508-
possibilities: Vec<&'static str>,
509+
possibilities: &[Symbol],
509510
) -> ErrorGuaranteed {
510511
self.emit_err(AttributeParseError {
511512
span,
@@ -521,10 +522,12 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
521522
})
522523
}
523524

525+
/// produces an error along the lines of `expected one of [foo, meow] as an argument`.
526+
/// i.e. slightly different wording to [`expected_specific_argument`](Self::expected_specific_argument).
524527
pub(crate) fn expected_specific_argument_and_list(
525528
&self,
526529
span: Span,
527-
possibilities: Vec<&'static str>,
530+
possibilities: &[Symbol],
528531
) -> ErrorGuaranteed {
529532
self.emit_err(AttributeParseError {
530533
span,
@@ -540,10 +543,11 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
540543
})
541544
}
542545

546+
/// produces an error along the lines of `expected one of ["foo", "meow"]`
543547
pub(crate) fn expected_specific_argument_strings(
544548
&self,
545549
span: Span,
546-
possibilities: Vec<&'static str>,
550+
possibilities: &[Symbol],
547551
) -> ErrorGuaranteed {
548552
self.emit_err(AttributeParseError {
549553
span,

compiler/rustc_attr_parsing/src/session_diagnostics.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ pub(crate) struct LinkOrdinalOutOfRange {
558558
pub ordinal: u128,
559559
}
560560

561-
pub(crate) enum AttributeParseErrorReason {
561+
pub(crate) enum AttributeParseErrorReason<'a> {
562562
ExpectedNoArgs,
563563
ExpectedStringLiteral {
564564
byte_string: Option<Span>,
@@ -571,24 +571,24 @@ pub(crate) enum AttributeParseErrorReason {
571571
ExpectedNameValue(Option<Symbol>),
572572
DuplicateKey(Symbol),
573573
ExpectedSpecificArgument {
574-
possibilities: Vec<&'static str>,
574+
possibilities: &'a [Symbol],
575575
strings: bool,
576576
/// Should we tell the user to write a list when they didn't?
577577
list: bool,
578578
},
579579
ExpectedIdentifier,
580580
}
581581

582-
pub(crate) struct AttributeParseError {
582+
pub(crate) struct AttributeParseError<'a> {
583583
pub(crate) span: Span,
584584
pub(crate) attr_span: Span,
585585
pub(crate) attr_style: AttrStyle,
586586
pub(crate) template: AttributeTemplate,
587587
pub(crate) attribute: AttrPath,
588-
pub(crate) reason: AttributeParseErrorReason,
588+
pub(crate) reason: AttributeParseErrorReason<'a>,
589589
}
590590

591-
impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for AttributeParseError {
591+
impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for AttributeParseError<'_> {
592592
fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, G> {
593593
let name = self.attribute.to_string();
594594

@@ -657,7 +657,7 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for AttributeParseError {
657657
list: false,
658658
} => {
659659
let quote = if strings { '"' } else { '`' };
660-
match possibilities.as_slice() {
660+
match possibilities {
661661
&[] => {}
662662
&[x] => {
663663
diag.span_label(
@@ -687,7 +687,7 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for AttributeParseError {
687687
list: true,
688688
} => {
689689
let quote = if strings { '"' } else { '`' };
690-
match possibilities.as_slice() {
690+
match possibilities {
691691
&[] => {}
692692
&[x] => {
693693
diag.span_label(

0 commit comments

Comments
 (0)