Skip to content

Commit 77e5234

Browse files
committed
extract "expected string literal" special case to its own subfunction
1 parent fe4e53e commit 77e5234

4 files changed

Lines changed: 44 additions & 20 deletions

File tree

compiler/rustc_attr_parsing/src/session_diagnostics.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,23 @@ pub(crate) struct AttributeParseError<'a> {
595595
}
596596

597597
impl<'a> AttributeParseError<'a> {
598+
fn render_expected_string_literal<G>(&self, diag: &mut Diag<'_, G>, byte_string: Option<Span>)
599+
where
600+
G: EmissionGuarantee,
601+
{
602+
if let Some(start_point_span) = byte_string {
603+
diag.span_suggestion(
604+
start_point_span,
605+
"consider removing the prefix",
606+
"",
607+
Applicability::MaybeIncorrect,
608+
);
609+
diag.note("expected a normal string literal, not a byte string literal");
610+
} else {
611+
diag.span_label(self.span, "expected a string literal here");
612+
}
613+
}
614+
598615
fn render_expected_specific_argument<G>(
599616
&self,
600617
diag: &mut Diag<'_, G>,
@@ -685,19 +702,7 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for AttributeParseError<'_> {
685702
diag.code(E0539);
686703
match &self.reason {
687704
AttributeParseErrorReason::ExpectedStringLiteral { byte_string } => {
688-
if let Some(start_point_span) = byte_string {
689-
diag.span_suggestion(
690-
start_point_span,
691-
"consider removing the prefix",
692-
"",
693-
Applicability::MaybeIncorrect,
694-
);
695-
diag.note("expected a normal string literal, not a byte string literal");
696-
697-
return diag;
698-
} else {
699-
diag.span_label(self.span, "expected a string literal here");
700-
}
705+
self.render_expected_string_literal(&mut diag, *byte_string);
701706
}
702707
AttributeParseErrorReason::ExpectedFilenameLiteral => {
703708
diag.span_label(self.span, "expected a filename string literal here");

tests/ui/conditional-compilation/cfg-attr-syntax-validation.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ struct S8;
4444

4545
#[cfg(a = b"hi")] //~ ERROR malformed `cfg` attribute input
4646
//~^ NOTE expected a normal string literal, not a byte string literal
47+
//~| NOTE for more information, visit
4748
struct S9;
4849

4950
macro_rules! generate_s10 {

tests/ui/conditional-compilation/cfg-attr-syntax-validation.stderr

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,23 @@ error[E0539]: malformed `cfg` attribute input
8585
--> $DIR/cfg-attr-syntax-validation.rs:45:1
8686
|
8787
LL | #[cfg(a = b"hi")]
88-
| ^^^^^^^^^^-^^^^^^
89-
| |
90-
| help: consider removing the prefix
88+
| ^^^^^^^^^^^^^^^^^
9189
|
9290
= note: expected a normal string literal, not a byte string literal
91+
= note: for more information, visit <https://doc.rust-lang.org/reference/conditional-compilation.html#the-cfg-attribute>
92+
help: consider removing the prefix
93+
|
94+
LL - #[cfg(a = b"hi")]
95+
LL + #[cfg(a = "hi")]
96+
|
97+
help: must be of the form
98+
|
99+
LL - #[cfg(a = b"hi")]
100+
LL + #[cfg(predicate)]
101+
|
93102

94103
error: expected a literal (`1u8`, `1.0f32`, `"string"`, etc.) here, found `expr` metavariable
95-
--> $DIR/cfg-attr-syntax-validation.rs:51:25
104+
--> $DIR/cfg-attr-syntax-validation.rs:52:25
96105
|
97106
LL | #[cfg(feature = $expr)]
98107
| ^^^^^

tests/ui/conditional-compilation/cfg_attr-attr-syntax-validation.stderr

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,20 @@ error[E0539]: malformed `cfg_attr` attribute input
7474
--> $DIR/cfg_attr-attr-syntax-validation.rs:25:1
7575
|
7676
LL | #[cfg_attr(a = b"hi")]
77-
| ^^^^^^^^^^^^^^^-^^^^^^
78-
| |
79-
| help: consider removing the prefix
77+
| ^^^^^^^^^^^^^^^^^^^^^^
8078
|
8179
= note: expected a normal string literal, not a byte string literal
80+
= note: for more information, visit <https://doc.rust-lang.org/reference/conditional-compilation.html#the-cfg_attr-attribute>
81+
help: consider removing the prefix
82+
|
83+
LL - #[cfg_attr(a = b"hi")]
84+
LL + #[cfg_attr(a = "hi")]
85+
|
86+
help: must be of the form
87+
|
88+
LL - #[cfg_attr(a = b"hi")]
89+
LL + #[cfg_attr(predicate, attr1, attr2, ...)]
90+
|
8291

8392
error: expected `,`, found end of `cfg_attr` input
8493
--> $DIR/cfg_attr-attr-syntax-validation.rs:38:16

0 commit comments

Comments
 (0)