1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//! **No message ships with a placeholder still in it.**
//!
//! `rust-i18n`'s `t!` leaves a `%{name}` in its output when the call names no argument for it, and nothing else
//! complains: the string compiles, the translation key is right, and the first reader to see `%{written}` is a user.
//! Measured twice, both in diagnostics the editor shows:
//!
//! ```text
//! cstyle_cast.rs this is a C-style cast to `%{written}` — the four named casts …
//! use_auto.rs `%{name}` is declared by its full iterator spelling — write `auto %{name}` …
//! ```
//!
//! The first was found by a real `arglist.c` diagnostics pull on neovim, in the popup text of a C-style cast. The
//! second was found by this scan looking for the first.
//!
//! # Why a source scan
//!
//! The messages are built in dozens of places whose inputs a test would have to construct — a check needs a scope
//! tree, a type, an index. What is wanted here is not "this message renders" but "every message *can* render", and
//! that is a property of the call's text: each `%{name}` in the format string needs a `name = …` argument in the same
//! call. So this reads the crate's sources rather than running them.
use std::path::{Path, PathBuf};
/// Every `.rs` file under `directory`, recursively.
fn rust_files(directory: &Path, found: &mut Vec<PathBuf>) {
let Ok(entries) = std::fs::read_dir(directory) else {
return;
};
for entry in entries.flatten() {
let path = entry.path();
if path.is_dir() {
rust_files(&path, found);
} else if path.extension().is_some_and(|extension| extension == "rs") {
found.push(path);
}
}
}
/// `text` with everything inside a string literal blanked out, so that parentheses in a *message* cannot be mistaken
/// for the call's own. `"` and `\` are kept, and newlines are kept so that offsets still line up.
///
/// **Bytes, not characters**: a blanked character is replaced by as many spaces as its UTF-8 length, because the
/// offsets this returns are used to slice the *original* source (`…` is three bytes, and one space per character
/// put every call after it one byte out of place).
fn without_string_contents(text: &str) -> String {
let mut masked = String::with_capacity(text.len());
let mut characters = text.chars().peekable();
while let Some(character) = characters.next() {
match character {
'"' => {
masked.push('"');
while let Some(inner) = characters.next() {
match inner {
'\\' => {
// An escape consumes the next character, whatever it is — `\"` does not close a string.
let escaped = characters.next().unwrap_or(' ');
masked.push(' ');
masked.extend(std::iter::repeat_n(' ', escaped.len_utf8()));
}
'"' => {
masked.push('"');
break;
}
'\n' => masked.push('\n'),
_ => masked.extend(std::iter::repeat_n(' ', inner.len_utf8())),
}
}
}
_ => masked.push(character),
}
}
masked
}
/// The index just past the `)` that closes the parenthesis at `open`.
fn end_of_call(masked: &str, open: usize) -> Option<usize> {
let mut depth = 0usize;
for (offset, character) in masked[open..].char_indices() {
match character {
'(' => depth += 1,
')' => {
depth -= 1;
if depth == 0 {
return Some(open + offset + 1);
}
}
_ => {}
}
}
None
}
#[test]
fn every_t_call_fills_its_placeholders() {
let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("src");
let mut files = Vec::new();
rust_files(&root, &mut files);
assert!(!files.is_empty(), "the crate's sources are where they are");
let mut unfilled: Vec<String> = Vec::new();
let mut calls = 0usize;
for file in &files {
let Ok(source) = std::fs::read_to_string(file) else {
continue;
};
let masked = without_string_contents(&source);
let mut from = 0usize;
while let Some(found) = masked[from..].find("t!(") {
let at = from + found;
from = at + 3;
let open = at + 2; // the `(` of `t!(`
let Some(close) = end_of_call(&masked, open) else {
continue;
};
// The call's own text, from the *original* source: the masked copy only decided where it ends.
let call = &source[open..close];
if !call.contains("%{") {
continue;
}
calls += 1;
let mut names: Vec<&str> = Vec::new();
let mut rest = call;
while let Some(start) = rest.find("%{") {
let after = &rest[start + 2..];
let Some(end) = after.find('}') else {
break;
};
let name = &after[..end];
if !name.is_empty()
&& name
.chars()
.all(|character| character.is_alphanumeric() || character == '_')
&& !names.contains(&name)
{
names.push(name);
}
rest = &after[end + 1..];
}
for name in names {
// **A `name = …` argument**, not just the word: `%{name}` inside the message is not an argument, and
// the masked text is what the search runs over so a message mentioning `written = x` cannot satisfy it.
let argument = format!("{name} =");
if !masked[open..close].contains(&argument) {
let line = source[..at].matches('\n').count() + 1;
unfilled.push(format!(
"{}:{line}: `%{{{name}}}` has no `{name} =` argument",
file.file_name().unwrap_or_default().to_string_lossy()
));
}
}
}
}
assert!(
calls > 0,
"the scan found no `t!` call with a placeholder at all, which means it is looking at nothing"
);
assert!(
unfilled.is_empty(),
"these messages would show a placeholder to the user:\n {}",
unfilled.join("\n ")
);
}