Skip to content

Commit 9d31498

Browse files
authored
Rollup merge of rust-lang#152276 - JonathanBrouwer:fmt-check, r=jdonszelmann
Add message format checking Checks the indentation of diagnostic messages, it checks that everything is at least as indented as the attribute that contains it r? @jdonszelmann (Anyone else, also feel free to review, just assigning to Jana because she's been reviewing the other PRs)
2 parents 98a3fa2 + 99c6009 commit 9d31498

File tree

9 files changed

+163
-127
lines changed

9 files changed

+163
-127
lines changed

compiler/rustc_const_eval/src/errors.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ pub(crate) struct UnstableInStableExposed {
106106
pub is_function_call2: bool,
107107
#[suggestion(
108108
"if the {$is_function_call2 ->
109-
[true] caller
110-
*[false] function
111-
} is not (yet) meant to be exposed to stable const contexts, add `#[rustc_const_unstable]`",
109+
[true] caller
110+
*[false] function
111+
} is not (yet) meant to be exposed to stable const contexts, add `#[rustc_const_unstable]`",
112112
code = "#[rustc_const_unstable(feature = \"...\", issue = \"...\")]\n",
113113
applicability = "has-placeholders"
114114
)]
@@ -300,11 +300,11 @@ pub(crate) struct UnallowedHeapAllocations {
300300
#[primary_span]
301301
#[label(
302302
r#"allocation not allowed in {$kind ->
303-
[const] constant
304-
[static] static
305-
[const_fn] constant function
306-
*[other] {""}
307-
}s"#
303+
[const] constant
304+
[static] static
305+
[const_fn] constant function
306+
*[other] {""}
307+
}s"#
308308
)]
309309
pub span: Span,
310310
pub kind: ConstContext,
@@ -539,20 +539,20 @@ pub enum NonConstClosureNote {
539539
},
540540
#[note(
541541
r#"function pointers need an RFC before allowed to be called in {$kind ->
542-
[const] constant
543-
[static] static
544-
[const_fn] constant function
545-
*[other] {""}
546-
}s"#
542+
[const] constant
543+
[static] static
544+
[const_fn] constant function
545+
*[other] {""}
546+
}s"#
547547
)]
548548
FnPtr,
549549
#[note(
550550
r#"closures need an RFC before allowed to be called in {$kind ->
551-
[const] constant
552-
[static] static
553-
[const_fn] constant function
554-
*[other] {""}
555-
}s"#
551+
[const] constant
552+
[static] static
553+
[const_fn] constant function
554+
*[other] {""}
555+
}s"#
556556
)]
557557
Closure,
558558
}
@@ -608,11 +608,11 @@ pub struct LiveDrop<'tcx> {
608608
#[primary_span]
609609
#[label(
610610
r#"the destructor for this type cannot be evaluated in {$kind ->
611-
[const] constant
612-
[static] static
613-
[const_fn] constant function
614-
*[other] {""}
615-
}s"#
611+
[const] constant
612+
[static] static
613+
[const_fn] constant function
614+
*[other] {""}
615+
}s"#
616616
)]
617617
pub span: Span,
618618
pub kind: ConstContext,

compiler/rustc_macros/src/diagnostics/diagnostic_builder.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,11 @@ impl DiagnosticDeriveVariantBuilder {
203203
)
204204
.emit();
205205
}
206-
self.message =
207-
Some(Message { message_span: message.span(), value: message.value() });
206+
self.message = Some(Message {
207+
attr_span: attr.span(),
208+
message_span: message.span(),
209+
value: message.value(),
210+
});
208211
}
209212

210213
// Parse arguments

compiler/rustc_macros/src/diagnostics/message.rs

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::diagnostics::error::span_err;
99

1010
#[derive(Clone)]
1111
pub(crate) struct Message {
12+
pub attr_span: Span,
1213
pub message_span: Span,
1314
pub value: String,
1415
}
@@ -19,12 +20,18 @@ impl Message {
1920
/// For subdiagnostics, we cannot check this.
2021
pub(crate) fn diag_message(&self, variant: Option<&VariantInfo<'_>>) -> TokenStream {
2122
let message = &self.value;
22-
verify_fluent_message(self.message_span, &message, variant);
23+
self.verify(variant);
2324
quote! { rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed(#message)) }
2425
}
26+
27+
fn verify(&self, variant: Option<&VariantInfo<'_>>) {
28+
verify_variables_used(self.message_span, &self.value, variant);
29+
verify_message_style(self.message_span, &self.value);
30+
verify_message_formatting(self.attr_span, self.message_span, &self.value);
31+
}
2532
}
2633

27-
fn verify_fluent_message(msg_span: Span, message_str: &str, variant: Option<&VariantInfo<'_>>) {
34+
fn verify_variables_used(msg_span: Span, message_str: &str, variant: Option<&VariantInfo<'_>>) {
2835
// Parse the fluent message
2936
const GENERATED_MSG_ID: &str = "generated_msg";
3037
let resource =
@@ -53,8 +60,6 @@ fn verify_fluent_message(msg_span: Span, message_str: &str, variant: Option<&Var
5360
}
5461
}
5562
}
56-
57-
verify_message_style(msg_span, message_str);
5863
}
5964

6065
fn variable_references<'a>(msg: &fluent_syntax::ast::Message<&'a str>) -> Vec<&'a str> {
@@ -120,3 +125,29 @@ fn verify_message_style(msg_span: Span, message: &str) {
120125
return;
121126
}
122127
}
128+
129+
/// Verifies that the message is properly indented into the code
130+
fn verify_message_formatting(attr_span: Span, msg_span: Span, message: &str) {
131+
// Find the indent at the start of the message (`column()` is one-indexed)
132+
let start = attr_span.unwrap().column() - 1;
133+
134+
for line in message.lines().skip(1) {
135+
if line.is_empty() {
136+
continue;
137+
}
138+
let indent = line.chars().take_while(|c| *c == ' ').count();
139+
if indent < start {
140+
span_err(
141+
msg_span.unwrap(),
142+
format!("message is not properly indented. {indent} < {start}"),
143+
)
144+
.emit();
145+
return;
146+
}
147+
if indent % 4 != 0 {
148+
span_err(msg_span.unwrap(), "message is not indented with a multiple of 4 spaces")
149+
.emit();
150+
return;
151+
}
152+
}
153+
}

compiler/rustc_macros/src/diagnostics/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ impl SubdiagnosticVariant {
708708
}
709709
if !input.is_empty() { input.parse::<Token![,]>()?; }
710710
if is_first {
711-
message = Some(Message { message_span: inline_message.span(), value: inline_message.value() });
711+
message = Some(Message { attr_span: attr.span(), message_span: inline_message.span(), value: inline_message.value() });
712712
is_first = false;
713713
} else {
714714
span_err(inline_message.span().unwrap(), "a diagnostic message must be the first argument to the attribute").emit();

compiler/rustc_mir_build/src/errors.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,12 @@ pub(crate) struct UnsafeOpInUnsafeFnCallToFunctionWithRequiresUnsafe {
198198
pub(crate) missing_target_features: DiagArgValue,
199199
pub(crate) missing_target_features_count: usize,
200200
#[note("the {$build_target_features} target {$build_target_features_count ->
201-
[1] feature
202-
*[count] features
201+
[1] feature
202+
*[count] features
203203
} being enabled in the build configuration does not remove the requirement to list {$build_target_features_count ->
204-
[1] it
205-
*[count] them
206-
} in `#[target_feature]`")]
204+
[1] it
205+
*[count] them
206+
} in `#[target_feature]`")]
207207
pub(crate) note: bool,
208208
pub(crate) build_target_features: DiagArgValue,
209209
pub(crate) build_target_features_count: usize,
@@ -532,12 +532,12 @@ pub(crate) struct CallToFunctionWithRequiresUnsafe {
532532
pub(crate) missing_target_features: DiagArgValue,
533533
pub(crate) missing_target_features_count: usize,
534534
#[note("the {$build_target_features} target {$build_target_features_count ->
535-
[1] feature
536-
*[count] features
537-
} being enabled in the build configuration does not remove the requirement to list {$build_target_features_count ->
538-
[1] it
539-
*[count] them
540-
} in `#[target_feature]`")]
535+
[1] feature
536+
*[count] features
537+
} being enabled in the build configuration does not remove the requirement to list {$build_target_features_count ->
538+
[1] it
539+
*[count] them
540+
} in `#[target_feature]`")]
541541
pub(crate) note: bool,
542542
pub(crate) build_target_features: DiagArgValue,
543543
pub(crate) build_target_features_count: usize,
@@ -1264,9 +1264,9 @@ pub(crate) struct InterpretedAsConstSugg {
12641264
pub(crate) enum SuggestLet {
12651265
#[multipart_suggestion(
12661266
"you might want to use `if let` to ignore the {$count ->
1267-
[one] variant that isn't
1268-
*[other] variants that aren't
1269-
} matched",
1267+
[one] variant that isn't
1268+
*[other] variants that aren't
1269+
} matched",
12701270
applicability = "has-placeholders"
12711271
)]
12721272
If {
@@ -1278,9 +1278,9 @@ pub(crate) enum SuggestLet {
12781278
},
12791279
#[suggestion(
12801280
"you might want to use `let...else` to handle the {$count ->
1281-
[one] variant that isn't
1282-
*[other] variants that aren't
1283-
} matched",
1281+
[one] variant that isn't
1282+
*[other] variants that aren't
1283+
} matched",
12841284
code = " else {{ todo!() }}",
12851285
applicability = "has-placeholders"
12861286
)]

compiler/rustc_monomorphize/src/errors.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,9 @@ pub(crate) struct AbiRequiredTargetFeature<'a> {
162162
#[primary_span]
163163
#[label(
164164
"function {$is_call ->
165-
[true] called
166-
*[false] defined
167-
} here"
165+
[true] called
166+
*[false] defined
167+
} here"
168168
)]
169169
pub span: Span,
170170
pub required_feature: &'a str,

compiler/rustc_parse/src/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1151,7 +1151,7 @@ pub(crate) enum MatchArmBodyWithoutBracesSugg {
11511151
#[multipart_suggestion(
11521152
"surround the {$num_statements ->
11531153
[one] statement
1154-
*[other] statements
1154+
*[other] statements
11551155
} with a body",
11561156
applicability = "machine-applicable"
11571157
)]

compiler/rustc_passes/src/errors.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,8 +1417,8 @@ pub(crate) struct DuplicateEiiImpls {
14171417
pub second_crate: Symbol,
14181418

14191419
#[note("in addition to these two, { $num_additional_crates ->
1420-
[one] another implementation was found in crate {$additional_crate_names}
1421-
*[other] more implementations were also found in the following crates: {$additional_crate_names}
1420+
[one] another implementation was found in crate {$additional_crate_names}
1421+
*[other] more implementations were also found in the following crates: {$additional_crate_names}
14221422
}")]
14231423
pub additional_crates: Option<()>,
14241424

0 commit comments

Comments
 (0)