Skip to content

Commit 979c265

Browse files
committed
Check for escape sequences in Fluent resources
1 parent bf57e8a commit 979c265

File tree

9 files changed

+64
-9
lines changed

9 files changed

+64
-9
lines changed

compiler/rustc_codegen_llvm/messages.ftl

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ codegen_llvm_error_calling_dlltool =
2727
Error calling dlltool: {$error}
2828
2929
codegen_llvm_dlltool_fail_import_library =
30-
Dlltool could not create import library: {$stdout}\n{$stderr}
30+
Dlltool could not create import library: {$stdout}
31+
{$stderr}
3132
3233
codegen_llvm_target_feature_disable_or_enable =
3334
the target features {$features} must all be either enabled or disabled together

compiler/rustc_const_eval/messages.ftl

+12-4
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,25 @@ const_eval_unstable_const_fn = `{$def_path}` is not yet stable as a const fn
3939
const_eval_unallowed_mutable_refs =
4040
mutable references are not allowed in the final value of {$kind}s
4141
.teach_note =
42-
References in statics and constants may only refer to immutable values.\n\n
42+
References in statics and constants may only refer to immutable values.
43+
44+
4345
Statics are shared everywhere, and if they refer to mutable data one might violate memory
44-
safety since holding multiple mutable references to shared data is not allowed.\n\n
46+
safety since holding multiple mutable references to shared data is not allowed.
47+
48+
4549
If you really want global mutable state, try using static mut or a global UnsafeCell.
4650
4751
const_eval_unallowed_mutable_refs_raw =
4852
raw mutable references are not allowed in the final value of {$kind}s
4953
.teach_note =
50-
References in statics and constants may only refer to immutable values.\n\n
54+
References in statics and constants may only refer to immutable values.
55+
56+
5157
Statics are shared everywhere, and if they refer to mutable data one might violate memory
52-
safety since holding multiple mutable references to shared data is not allowed.\n\n
58+
safety since holding multiple mutable references to shared data is not allowed.
59+
60+
5361
If you really want global mutable state, try using static mut or a global UnsafeCell.
5462
5563
const_eval_non_const_fmt_macro_call =

compiler/rustc_incremental/messages.ftl

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ incremental_field_associated_value_expected = associated value expected for `{$n
2424
incremental_no_field = no field `{$name}`
2525
2626
incremental_assertion_auto =
27-
`except` specified DepNodes that can not be affected for \"{$name}\": \"{$e}\"
27+
`except` specified DepNodes that can not be affected for "{$name}": "{$e}"
2828
2929
incremental_undefined_clean_dirty_assertions_item =
3030
clean/dirty auto-assertions not yet defined for Node::Item.node={$kind}

compiler/rustc_lint/messages.ftl

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ lint_ty_qualified = usage of qualified `ty::{$ty}`
9191
lint_lintpass_by_hand = implementing `LintPass` by hand
9292
.help = try using `declare_lint_pass!` or `impl_lint_pass!` instead
9393
94-
lint_non_existant_doc_keyword = found non-existing keyword `{$keyword}` used in `#[doc(keyword = \"...\")]`
94+
lint_non_existant_doc_keyword = found non-existing keyword `{$keyword}` used in `#[doc(keyword = "...")]`
9595
.help = only existing keywords are allowed in core/std
9696
9797
lint_diag_out_of_impl =

compiler/rustc_macros/src/diagnostics/fluent.rs

+12
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,18 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok
111111
.emit();
112112
return failed(&crate_name);
113113
}
114+
let mut bad = false;
115+
for esc in ["\\n", "\\\"", "\\'"] {
116+
for _ in resource_contents.matches(esc) {
117+
bad = true;
118+
Diagnostic::spanned(resource_span, Level::Error, format!("invalid escape `{esc}` in Fluent resource"))
119+
.note("Fluent does not interpret these escape sequences (<https://projectfluent.org/fluent/guide/special.html>)")
120+
.emit();
121+
}
122+
}
123+
if bad {
124+
return failed(&crate_name);
125+
}
114126

115127
let resource = match FluentResource::try_new(resource_contents) {
116128
Ok(resource) => resource,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
no_crate_bad_escape = don't use \n, \', or \"

tests/ui-fulldeps/fluent-messages/test.rs

+9
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,12 @@ mod missing_message_ref {
9292
fluent_messages! { "./missing-message-ref.ftl" }
9393
//~^ ERROR referenced message `message` does not exist
9494
}
95+
96+
mod bad_escape {
97+
use super::fluent_messages;
98+
99+
fluent_messages! { "./invalid-escape.ftl" }
100+
//~^ ERROR invalid escape `\n`
101+
//~| ERROR invalid escape `\"`
102+
//~| ERROR invalid escape `\'`
103+
}

tests/ui-fulldeps/fluent-messages/test.stderr

+25-1
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,29 @@ LL | fluent_messages! { "./missing-message-ref.ftl" }
8383
|
8484
= help: you may have meant to use a variable reference (`{$message}`)
8585

86-
error: aborting due to 10 previous errors
86+
error: invalid escape `\n` in Fluent resource
87+
--> $DIR/test.rs:99:24
88+
|
89+
LL | fluent_messages! { "./invalid-escape.ftl" }
90+
| ^^^^^^^^^^^^^^^^^^^^^^
91+
|
92+
= note: os-specific message
93+
94+
error: invalid escape `\"` in Fluent resource
95+
--> $DIR/test.rs:99:24
96+
|
97+
LL | fluent_messages! { "./invalid-escape.ftl" }
98+
| ^^^^^^^^^^^^^^^^^^^^^^
99+
|
100+
= note: os-specific message
101+
102+
error: invalid escape `\'` in Fluent resource
103+
--> $DIR/test.rs:99:24
104+
|
105+
LL | fluent_messages! { "./invalid-escape.ftl" }
106+
| ^^^^^^^^^^^^^^^^^^^^^^
107+
|
108+
= note: os-specific message
109+
110+
error: aborting due to 13 previous errors
87111

tests/ui-fulldeps/internal-lints/existing_doc_keyword.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: found non-existing keyword `tadam` used in `#[doc(keyword = \"...\")]`
1+
error: found non-existing keyword `tadam` used in `#[doc(keyword = "...")]`
22
--> $DIR/existing_doc_keyword.rs:10:1
33
|
44
LL | #[doc(keyword = "tadam")]

0 commit comments

Comments
 (0)