Skip to content
/ rust Public
forked from rust-lang/rust

Commit b0d30bf

Browse files
authored
Rollup merge of rust-lang#121860 - mu001999:master, r=Nilstrieb
Add a tidy check that checks whether the fluent slugs only appear once As `````@Nilstrieb````` said in rust-lang#121828 (comment): > Might make sense to have a tidy check that checks whether the fluent slugs only appear once in the source code and lint for that there's a tidy check already for sorting We can get the tidy check error: ``` tidy check tidy error: /path/to/rust/compiler/rustc_const_eval/messages.ftl: message `const_eval_invalid_align` is not used tidy error: /path/to/rust/compiler/rustc_lint/messages.ftl: message `lint_trivial_untranslatable_diag` is not used tidy error: /path/to/rust/compiler/rustc_parse/messages.ftl: message `parse_invalid_literal_suffix` is not used tidy error: /path/to/rust/compiler/rustc_infer/messages.ftl: message `infer_need_type_info_in_coroutine` is not used tidy error: /path/to/rust/compiler/rustc_passes/messages.ftl: message `passes_expr_not_allowed_in_context` is not used tidy error: /path/to/rust/compiler/rustc_passes/messages.ftl: message `passes_layout` is not used tidy error: /path/to/rust/compiler/rustc_parse/messages.ftl: message `parse_not_supported` is not used ``` r? `````@Nilstrieb`````
2 parents 5d75182 + d88c7ff commit b0d30bf

File tree

8 files changed

+96
-28
lines changed

8 files changed

+96
-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

+52-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,27 @@ 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+
all_defined_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+
if let Some(defined_filename) = all_defined_msgs.get(name.as_str()) {
27+
tidy_error!(
28+
bad,
29+
"{filename}: message `{}` is already defined in {}",
30+
name.as_str(),
31+
defined_filename,
32+
);
33+
}
34+
35+
all_defined_msgs.insert(name.as_str().to_owned(), filename.to_owned());
36+
1937
if let Some(next) = matches.peek() {
20-
let name = m.get(1).unwrap();
2138
let next = next.get(1).unwrap();
2239
if name.as_str() > next.as_str() {
2340
tidy_error!(
@@ -34,13 +51,29 @@ run `./x.py test tidy --bless` to sort the file correctly",
3451
}
3552
}
3653

37-
fn sort_messages(fluent: &str) -> String {
54+
fn sort_messages(
55+
filename: &str,
56+
fluent: &str,
57+
bad: &mut bool,
58+
all_defined_msgs: &mut HashMap<String, String>,
59+
) -> String {
3860
let mut chunks = vec![];
3961
let mut cur = String::new();
4062
for line in fluent.lines() {
41-
if MESSAGE.is_match(line) {
63+
if let Some(name) = MESSAGE.find(line) {
64+
if let Some(defined_filename) = all_defined_msgs.get(name.as_str()) {
65+
tidy_error!(
66+
bad,
67+
"{filename}: message `{}` is already defined in {}",
68+
name.as_str(),
69+
defined_filename,
70+
);
71+
}
72+
73+
all_defined_msgs.insert(name.as_str().to_owned(), filename.to_owned());
4274
chunks.push(std::mem::take(&mut cur));
4375
}
76+
4477
cur += line;
4578
cur.push('\n');
4679
}
@@ -53,20 +86,33 @@ fn sort_messages(fluent: &str) -> String {
5386
}
5487

5588
pub fn check(path: &Path, bless: bool, bad: &mut bool) {
89+
let mut all_defined_msgs = HashMap::new();
5690
walk(
5791
path,
5892
|path, is_dir| filter_dirs(path) || (!is_dir && filter_fluent(path)),
5993
&mut |ent, contents| {
6094
if bless {
61-
let sorted = sort_messages(contents);
95+
let sorted = sort_messages(
96+
ent.path().to_str().unwrap(),
97+
contents,
98+
bad,
99+
&mut all_defined_msgs,
100+
);
62101
if sorted != contents {
63102
let mut f =
64103
OpenOptions::new().write(true).truncate(true).open(ent.path()).unwrap();
65104
f.write(sorted.as_bytes()).unwrap();
66105
}
67106
} else {
68-
check_alphabetic(ent.path().to_str().unwrap(), contents, bad);
107+
check_alphabetic(
108+
ent.path().to_str().unwrap(),
109+
contents,
110+
bad,
111+
&mut all_defined_msgs,
112+
);
69113
}
70114
},
71115
);
116+
117+
crate::fluent_used::check(path, all_defined_msgs, bad);
72118
}

src/tools/tidy/src/fluent_used.rs

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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_not_appeared_yet: &mut HashMap<String, String>,
15+
msgs_appeared_only_once: &mut HashMap<String, String>,
16+
) {
17+
// we don't just check messages never appear in Rust files,
18+
// because messages can be used as parts of other fluent messages in Fluent files,
19+
// so we do checking messages appear only once in all Rust and Fluent files.
20+
let mut matches = WORD.find_iter(contents);
21+
while let Some(name) = matches.next() {
22+
if let Some((name, filename)) = msgs_not_appeared_yet.remove_entry(name.as_str()) {
23+
// if one msg appears for the first time,
24+
// remove it from `msgs_not_appeared_yet` and insert it into `msgs_appeared_only_once`.
25+
msgs_appeared_only_once.insert(name, filename);
26+
} else {
27+
// if one msg appears for the second time,
28+
// remove it from `msgs_appeared_only_once`.
29+
msgs_appeared_only_once.remove(name.as_str());
30+
}
31+
}
32+
}
33+
34+
pub fn check(path: &Path, mut all_defined_msgs: HashMap<String, String>, bad: &mut bool) {
35+
let mut msgs_appear_only_once = HashMap::new();
36+
walk(path, |path, _| filter_dirs(path), &mut |_, contents| {
37+
filter_used_messages(contents, &mut all_defined_msgs, &mut msgs_appear_only_once);
38+
});
39+
40+
for (name, filename) in msgs_appear_only_once {
41+
tidy_error!(bad, "{filename}: message `{}` is not used", name,);
42+
}
43+
}

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)