Skip to content

Commit c600797

Browse files
Rollup merge of rust-lang#152235 - JonathanBrouwer:convert_parse, r=JonathanBrouwer
Convert to inline diagnostics in `rustc_parse` This was the most annoying one by far, had to make a few changes to the representation of two errors (no user-facing changes tho), these changes are in separate commits for clarity :) For rust-lang#151366 r? @jdonszelmann
2 parents 19e6e5b + 00dd7db commit c600797

55 files changed

Lines changed: 1532 additions & 2165 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Cargo.lock

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4381,7 +4381,6 @@ dependencies = [
43814381
"rustc_data_structures",
43824382
"rustc_errors",
43834383
"rustc_feature",
4384-
"rustc_fluent_macro",
43854384
"rustc_index",
43864385
"rustc_lexer",
43874386
"rustc_macros",

compiler/rustc_codegen_llvm/src/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,5 +214,5 @@ pub(crate) struct FixedX18InvalidArch<'a> {
214214
}
215215

216216
#[derive(Diagnostic)]
217-
#[diag("`-Zsanitizer-kcfi-arity` requires LLVM 21.0.0 or later.")]
217+
#[diag("`-Zsanitizer-kcfi-arity` requires LLVM 21.0.0 or later")]
218218
pub(crate) struct SanitizerKcfiArityRequiresLLVM2100;

compiler/rustc_codegen_ssa/src/errors.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pub(crate) struct MissingQueryDepGraph {
5959

6060
#[derive(Diagnostic)]
6161
#[diag(
62-
"found malformed codegen unit name `{$user_path}`. codegen units names must always start with the name of the crate (`{$crate_name}` in this case)."
62+
"found malformed codegen unit name `{$user_path}`. codegen units names must always start with the name of the crate (`{$crate_name}` in this case)"
6363
)]
6464
pub(crate) struct MalformedCguName {
6565
#[primary_span]
@@ -562,12 +562,12 @@ pub(crate) struct SelfContainedLinkerMissing;
562562

563563
#[derive(Diagnostic)]
564564
#[diag(
565-
"please ensure that Visual Studio 2017 or later, or Build Tools for Visual Studio were installed with the Visual C++ option."
565+
"please ensure that Visual Studio 2017 or later, or Build Tools for Visual Studio were installed with the Visual C++ option"
566566
)]
567567
pub(crate) struct CheckInstalledVisualStudio;
568568

569569
#[derive(Diagnostic)]
570-
#[diag("VS Code is a different product, and is not sufficient.")]
570+
#[diag("VS Code is a different product, and is not sufficient")]
571571
pub(crate) struct InsufficientVSCodeProduct;
572572

573573
#[derive(Diagnostic)]
@@ -610,13 +610,13 @@ pub(crate) struct LinkerFileStem;
610610

611611
#[derive(Diagnostic)]
612612
#[diag(
613-
"link against the following native artifacts when linking against this static library. The order and any duplication can be significant on some platforms."
613+
"link against the following native artifacts when linking against this static library. The order and any duplication can be significant on some platforms"
614614
)]
615615
pub(crate) struct StaticLibraryNativeArtifacts;
616616

617617
#[derive(Diagnostic)]
618618
#[diag(
619-
"native artifacts to link against have been written to {$path}. The order and any duplication can be significant on some platforms."
619+
"native artifacts to link against have been written to {$path}. The order and any duplication can be significant on some platforms"
620620
)]
621621
pub(crate) struct StaticLibraryNativeArtifactsToFile<'a> {
622622
pub path: &'a Path,

compiler/rustc_const_eval/src/errors.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ pub(crate) struct UnallowedHeapAllocations {
309309
pub span: Span,
310310
pub kind: ConstContext,
311311
#[note(
312-
"The runtime heap is not yet available at compile-time, so no runtime heap allocations can be created."
312+
"the runtime heap is not yet available at compile-time, so no runtime heap allocations can be created"
313313
)]
314314
pub teach: bool,
315315
}
@@ -347,7 +347,7 @@ pub(crate) struct InteriorMutableBorrowEscaping {
347347
#[diag("constant evaluation is taking a long time")]
348348
#[note(
349349
"this lint makes sure the compiler doesn't get stuck due to infinite loops in const eval.
350-
If your compilation actually takes a long time, you can safely allow the lint."
350+
If your compilation actually takes a long time, you can safely allow the lint"
351351
)]
352352
pub struct LongRunning {
353353
#[help("the constant being evaluated")]

compiler/rustc_driver_impl/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ pub fn default_translator() -> Translator {
114114
pub static DEFAULT_LOCALE_RESOURCES: &[&str] = &[
115115
// tidy-alphabetical-start
116116
rustc_lint::DEFAULT_LOCALE_RESOURCE,
117-
rustc_parse::DEFAULT_LOCALE_RESOURCE,
118117
// tidy-alphabetical-end
119118
];
120119

compiler/rustc_driver_impl/src/session_diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub(crate) struct RlinkCorruptFile<'a> {
5151
}
5252

5353
#[derive(Diagnostic)]
54-
#[diag("the compiler unexpectedly panicked. this is a bug.")]
54+
#[diag("the compiler unexpectedly panicked. This is a bug")]
5555
pub(crate) struct Ice;
5656

5757
#[derive(Diagnostic)]

compiler/rustc_hir_typeck/src/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1145,7 +1145,7 @@ pub(crate) struct CastThinPointerToWidePointer<'tcx> {
11451145
pub expr_ty: Ty<'tcx>,
11461146
pub cast_ty: Ty<'tcx>,
11471147
#[note(
1148-
"Thin pointers are \"simple\" pointers: they are purely a reference to a
1148+
"thin pointers are \"simple\" pointers: they are purely a reference to a
11491149
memory address.
11501150
11511151
Wide pointers are pointers referencing \"Dynamically Sized Types\" (also

compiler/rustc_incremental/src/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ pub(crate) struct DeleteWorkProduct<'a> {
281281

282282
#[derive(Diagnostic)]
283283
#[diag(
284-
"corrupt incremental compilation artifact found at `{$path}`. This file will automatically be ignored and deleted. If you see this message repeatedly or can provoke it without manually manipulating the compiler's artifacts, please file an issue. The incremental compilation system relies on hardlinks and filesystem locks behaving correctly, and may not deal well with OS crashes, so whatever information you can provide about your filesystem or other state may be very relevant."
284+
"corrupt incremental compilation artifact found at `{$path}`. This file will automatically be ignored and deleted. If you see this message repeatedly or can provoke it without manually manipulating the compiler's artifacts, please file an issue. The incremental compilation system relies on hardlinks and filesystem locks behaving correctly, and may not deal well with OS crashes, so whatever information you can provide about your filesystem or other state may be very relevant"
285285
)]
286286
pub(crate) struct CorruptFile<'a> {
287287
pub path: &'a Path,

compiler/rustc_interface/src/interface.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,9 @@ pub struct Compiler {
5353
pub(crate) fn parse_cfg(dcx: DiagCtxtHandle<'_>, cfgs: Vec<String>) -> Cfg {
5454
cfgs.into_iter()
5555
.map(|s| {
56-
let psess = ParseSess::emitter_with_note(
57-
vec![rustc_parse::DEFAULT_LOCALE_RESOURCE],
58-
format!("this occurred on the command line: `--cfg={s}`"),
59-
);
56+
let psess = ParseSess::emitter_with_note(format!(
57+
"this occurred on the command line: `--cfg={s}`"
58+
));
6059
let filename = FileName::cfg_spec_source_code(&s);
6160

6261
macro_rules! error {
@@ -125,10 +124,9 @@ pub(crate) fn parse_check_cfg(dcx: DiagCtxtHandle<'_>, specs: Vec<String>) -> Ch
125124
let mut check_cfg = CheckCfg { exhaustive_names, exhaustive_values, ..CheckCfg::default() };
126125

127126
for s in specs {
128-
let psess = ParseSess::emitter_with_note(
129-
vec![rustc_parse::DEFAULT_LOCALE_RESOURCE],
130-
format!("this occurred on the command line: `--check-cfg={s}`"),
131-
);
127+
let psess = ParseSess::emitter_with_note(format!(
128+
"this occurred on the command line: `--check-cfg={s}`"
129+
));
132130
let filename = FileName::cfg_spec_source_code(&s);
133131

134132
const VISIT: &str =

compiler/rustc_macros/src/diagnostics/message.rs

Lines changed: 60 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use fluent_syntax::ast::{Expression, InlineExpression, Pattern, PatternElement};
33
use proc_macro2::{Span, TokenStream};
44
use quote::quote;
55
use syn::Path;
6+
use syn::ext::IdentExt;
67
use synstructure::{Structure, VariantInfo};
78

89
use crate::diagnostics::error::span_err;
@@ -23,9 +24,7 @@ impl Message {
2324
quote! { crate::fluent_generated::#slug }
2425
}
2526
Message::Inline(message_span, message) => {
26-
if let Some(variant) = variant {
27-
verify_fluent_message(*message_span, &message, variant);
28-
}
27+
verify_fluent_message(*message_span, &message, variant);
2928
quote! { rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed(#message)) }
3029
}
3130
}
@@ -86,30 +85,37 @@ impl Message {
8685
}
8786
}
8887

89-
fn verify_fluent_message(msg_span: Span, message: &str, variant: &VariantInfo<'_>) {
88+
fn verify_fluent_message(msg_span: Span, message_str: &str, variant: Option<&VariantInfo<'_>>) {
9089
// Parse the fluent message
9190
const GENERATED_MSG_ID: &str = "generated_msg";
92-
let resource = FluentResource::try_new(format!("{GENERATED_MSG_ID} = {message}\n")).unwrap();
91+
let resource =
92+
FluentResource::try_new(format!("{GENERATED_MSG_ID} = {message_str}\n")).unwrap();
9393
assert_eq!(resource.entries().count(), 1);
9494
let Some(fluent_syntax::ast::Entry::Message(message)) = resource.get_entry(0) else {
9595
panic!("Did not parse into a message")
9696
};
9797

9898
// Check if all variables are used
99-
let fields: Vec<String> = variant
100-
.bindings()
101-
.iter()
102-
.flat_map(|b| b.ast().ident.as_ref())
103-
.map(|id| id.to_string())
104-
.collect();
105-
for variable in variable_references(&message) {
106-
if !fields.iter().any(|f| f == variable) {
107-
span_err(msg_span.unwrap(), format!("Variable `{variable}` not found in diagnostic "))
99+
if let Some(variant) = variant {
100+
let fields: Vec<String> = variant
101+
.bindings()
102+
.iter()
103+
.flat_map(|b| b.ast().ident.as_ref())
104+
.map(|id| id.unraw().to_string())
105+
.collect();
106+
for variable in variable_references(&message) {
107+
if !fields.iter().any(|f| f == variable) {
108+
span_err(
109+
msg_span.unwrap(),
110+
format!("Variable `{variable}` not found in diagnostic "),
111+
)
108112
.help(format!("Available fields: {:?}", fields.join(", ")))
109113
.emit();
114+
}
110115
}
111-
// assert!(, );
112116
}
117+
118+
verify_message_style(msg_span, message_str);
113119
}
114120

115121
fn variable_references<'a>(msg: &fluent_syntax::ast::Message<&'a str>) -> Vec<&'a str> {
@@ -136,3 +142,42 @@ fn variable_references<'a>(msg: &fluent_syntax::ast::Message<&'a str>) -> Vec<&'
136142
}
137143
refs
138144
}
145+
146+
const ALLOWED_CAPITALIZED_WORDS: &[&str] = &[
147+
// tidy-alphabetical-start
148+
"ABI",
149+
"ABIs",
150+
"ADT",
151+
"C-variadic",
152+
"CGU-reuse",
153+
"Cargo",
154+
"Ferris",
155+
"GCC",
156+
"MIR",
157+
"NaNs",
158+
"OK",
159+
"Rust",
160+
"Unicode",
161+
"VS",
162+
// tidy-alphabetical-end
163+
];
164+
165+
/// See: https://rustc-dev-guide.rust-lang.org/diagnostics.html#diagnostic-output-style-guide
166+
fn verify_message_style(msg_span: Span, message: &str) {
167+
// Verify that message starts with lowercase char
168+
let Some(first_word) = message.split_whitespace().next() else {
169+
span_err(msg_span.unwrap(), "message must not be empty").emit();
170+
return;
171+
};
172+
let first_char = first_word.chars().next().expect("Word is not empty");
173+
if first_char.is_uppercase() && !ALLOWED_CAPITALIZED_WORDS.contains(&first_word) {
174+
span_err(msg_span.unwrap(), "message `{value}` starts with an uppercase letter. Fix it or add it to `ALLOWED_CAPITALIZED_WORDS`").emit();
175+
return;
176+
}
177+
178+
// Verify that message does not end in `.`
179+
if message.ends_with(".") && !message.ends_with("...") {
180+
span_err(msg_span.unwrap(), "message `{value}` ends with a period").emit();
181+
return;
182+
}
183+
}

0 commit comments

Comments
 (0)