Skip to content

Commit 8aeaf75

Browse files
committed
Add a tidy check that checks whether the fluent slugs only appear once
1 parent e612d07 commit 8aeaf75

File tree

8 files changed

+56
-28
lines changed

8 files changed

+56
-28
lines changed

compiler/rustc_const_eval/messages.ftl

-3
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,6 @@ const_eval_intern_kind = {$kind ->
146146
*[other] {""}
147147
}
148148
149-
const_eval_invalid_align =
150-
align has to be a power of 2
151-
152149
const_eval_invalid_align_details =
153150
invalid align passed to `{$name}`: {$align} is {$err_kind ->
154151
[not_power_of_two] not a power of 2

compiler/rustc_infer/messages.ftl

-8
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,6 @@ infer_more_targeted = {$has_param_name ->
181181
182182
infer_msl_introduces_static = introduces a `'static` lifetime requirement
183183
infer_msl_unmet_req = because this has an unmet lifetime requirement
184-
infer_need_type_info_in_coroutine =
185-
type inside {$coroutine_kind ->
186-
[async_block] `async` block
187-
[async_closure] `async` closure
188-
[async_fn] `async fn` body
189-
*[coroutine] coroutine
190-
} must be known in this context
191-
192184
193185
infer_nothing = {""}
194186

compiler/rustc_lint/messages.ftl

-2
Original file line numberDiff line numberDiff line change
@@ -562,8 +562,6 @@ lint_suspicious_double_ref_clone =
562562
lint_suspicious_double_ref_deref =
563563
using `.deref()` on a double reference, which returns `{$ty}` instead of dereferencing the inner type
564564
565-
lint_trivial_untranslatable_diag = diagnostic with static strings only
566-
567565
lint_ty_qualified = usage of qualified `ty::{$ty}`
568566
.suggestion = try importing it and using it unqualified
569567

compiler/rustc_parse/messages.ftl

-4
Original file line numberDiff line numberDiff line change
@@ -392,9 +392,6 @@ parse_invalid_identifier_with_leading_number = identifiers cannot start with a n
392392
393393
parse_invalid_interpolated_expression = invalid interpolated expression
394394
395-
parse_invalid_literal_suffix = suffixes on {$kind} literals are invalid
396-
.label = invalid suffix `{$suffix}`
397-
398395
parse_invalid_literal_suffix_on_tuple_index = suffixes on a tuple index are invalid
399396
.label = invalid suffix `{$suffix}`
400397
.tuple_exception_line_1 = `{$suffix}` is *temporarily* accepted on tuple index fields as it was incorrectly accepted on stable for a few releases
@@ -609,7 +606,6 @@ parse_nonterminal_expected_item_keyword = expected an item keyword
609606
parse_nonterminal_expected_lifetime = expected a lifetime, found `{$token}`
610607
611608
parse_nonterminal_expected_statement = expected a statement
612-
parse_not_supported = not supported
613609
614610
parse_note_edition_guide = for more on editions, read https://doc.rust-lang.org/edition-guide
615611

compiler/rustc_passes/messages.ftl

-5
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,6 @@ passes_export_name =
302302
attribute should be applied to a free function, impl method or static
303303
.label = not a free function, impl method or static
304304
305-
passes_expr_not_allowed_in_context =
306-
{$expr} is not allowed in a `{$context}`
307-
308305
passes_extern_main =
309306
the `main` function cannot be declared in an `extern` block
310307
@@ -405,8 +402,6 @@ passes_lang_item_on_incorrect_target =
405402
`{$name}` language item must be applied to a {$expected_target}
406403
.label = attribute should be applied to a {$expected_target}, not a {$actual_target}
407404
408-
passes_layout =
409-
layout error: {$layout_error}
410405
passes_layout_abi =
411406
abi: {$abi}
412407
passes_layout_align =

src/tools/tidy/src/fluent_alphabetical.rs

+19-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Checks that all Flunt files have messages in alphabetical order
22
33
use crate::walk::{filter_dirs, walk};
4+
use std::collections::HashMap;
45
use std::{fs::OpenOptions, io::Write, path::Path};
56

67
use regex::Regex;
@@ -13,11 +14,18 @@ fn filter_fluent(path: &Path) -> bool {
1314
if let Some(ext) = path.extension() { ext.to_str() != Some("ftl") } else { true }
1415
}
1516

16-
fn check_alphabetic(filename: &str, fluent: &str, bad: &mut bool) {
17+
fn check_alphabetic(
18+
filename: &str,
19+
fluent: &str,
20+
bad: &mut bool,
21+
msgs: &mut HashMap<String, String>,
22+
) {
1723
let mut matches = MESSAGE.captures_iter(fluent).peekable();
1824
while let Some(m) = matches.next() {
25+
let name = m.get(1).unwrap();
26+
msgs.insert(name.as_str().to_owned(), filename.to_owned());
27+
1928
if let Some(next) = matches.peek() {
20-
let name = m.get(1).unwrap();
2129
let next = next.get(1).unwrap();
2230
if name.as_str() > next.as_str() {
2331
tidy_error!(
@@ -34,13 +42,15 @@ run `./x.py test tidy --bless` to sort the file correctly",
3442
}
3543
}
3644

37-
fn sort_messages(fluent: &str) -> String {
45+
fn sort_messages(filename: &str, fluent: &str, msgs: &mut HashMap<String, String>) -> String {
3846
let mut chunks = vec![];
3947
let mut cur = String::new();
4048
for line in fluent.lines() {
41-
if MESSAGE.is_match(line) {
49+
if let Some(name) = MESSAGE.find(line) {
50+
msgs.insert(name.as_str().to_owned(), filename.to_owned());
4251
chunks.push(std::mem::take(&mut cur));
4352
}
53+
4454
cur += line;
4555
cur.push('\n');
4656
}
@@ -53,20 +63,23 @@ fn sort_messages(fluent: &str) -> String {
5363
}
5464

5565
pub fn check(path: &Path, bless: bool, bad: &mut bool) {
66+
let mut msgs = HashMap::new();
5667
walk(
5768
path,
5869
|path, is_dir| filter_dirs(path) || (!is_dir && filter_fluent(path)),
5970
&mut |ent, contents| {
6071
if bless {
61-
let sorted = sort_messages(contents);
72+
let sorted = sort_messages(ent.path().to_str().unwrap(), contents, &mut msgs);
6273
if sorted != contents {
6374
let mut f =
6475
OpenOptions::new().write(true).truncate(true).open(ent.path()).unwrap();
6576
f.write(sorted.as_bytes()).unwrap();
6677
}
6778
} else {
68-
check_alphabetic(ent.path().to_str().unwrap(), contents, bad);
79+
check_alphabetic(ent.path().to_str().unwrap(), contents, bad, &mut msgs);
6980
}
7081
},
7182
);
83+
84+
crate::fluent_used::check(path, &mut msgs, bad);
7285
}

src/tools/tidy/src/fluent_used.rs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//! Checks that all Fluent messages appear at least twice
2+
3+
use crate::walk::{filter_dirs, walk};
4+
use regex::Regex;
5+
use std::collections::HashMap;
6+
use std::path::Path;
7+
8+
lazy_static::lazy_static! {
9+
static ref WORD: Regex = Regex::new(r"\w+").unwrap();
10+
}
11+
12+
fn filter_used_messages(
13+
contents: &str,
14+
msgs: &mut HashMap<String, String>,
15+
unused_msgs: &mut HashMap<String, String>,
16+
) {
17+
let mut matches = WORD.find_iter(contents);
18+
while let Some(name) = matches.next() {
19+
if let Some((name, filename)) = msgs.remove_entry(name.as_str()) {
20+
unused_msgs.insert(name, filename);
21+
} else {
22+
unused_msgs.remove(name.as_str());
23+
}
24+
}
25+
}
26+
27+
pub fn check(path: &Path, msgs: &mut HashMap<String, String>, bad: &mut bool) {
28+
let mut unused_msgs = HashMap::new();
29+
walk(path, |path, _| filter_dirs(path), &mut |_, contents| {
30+
filter_used_messages(contents, msgs, &mut unused_msgs);
31+
});
32+
33+
for (name, filename) in unused_msgs {
34+
tidy_error!(bad, "{filename}: message `{}` is not used", name,);
35+
}
36+
}

src/tools/tidy/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ pub mod ext_tool_checks;
6565
pub mod extdeps;
6666
pub mod features;
6767
pub mod fluent_alphabetical;
68+
mod fluent_used;
6869
pub(crate) mod iter_header;
6970
pub mod mir_opt_tests;
7071
pub mod pal;

0 commit comments

Comments
 (0)