]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_fluent_macro/src/lib.rs
New upstream version 1.76.0+dfsg1
[rustc.git] / compiler / rustc_fluent_macro / src / lib.rs
1 #![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
2 #![doc(rust_logo)]
3 #![allow(internal_features)]
4 #![feature(rustdoc_internals)]
5 #![feature(proc_macro_diagnostic)]
6 #![feature(proc_macro_span)]
7 #![deny(rustc::untranslatable_diagnostic)]
8 #![deny(rustc::diagnostic_outside_of_impl)]
9 #![allow(rustc::default_hash_types)]
10
11 use proc_macro::TokenStream;
12
13 mod fluent;
14
15 /// Implements the `fluent_messages` macro, which performs compile-time validation of the
16 /// compiler's Fluent resources (i.e. that the resources parse and don't multiply define the same
17 /// messages) and generates constants that make using those messages in diagnostics more ergonomic.
18 ///
19 /// For example, given the following invocation of the macro..
20 ///
21 /// ```ignore (rust)
22 /// fluent_messages! { "./typeck.ftl" }
23 /// ```
24 /// ..where `typeck.ftl` has the following contents..
25 ///
26 /// ```fluent
27 /// typeck_field_multiply_specified_in_initializer =
28 /// field `{$ident}` specified more than once
29 /// .label = used more than once
30 /// .label_previous_use = first use of `{$ident}`
31 /// ```
32 /// ...then the macro parse the Fluent resource, emitting a diagnostic if it fails to do so, and
33 /// will generate the following code:
34 ///
35 /// ```ignore (rust)
36 /// pub static DEFAULT_LOCALE_RESOURCE: &'static [&'static str] = include_str!("./typeck.ftl");
37 ///
38 /// mod fluent_generated {
39 /// mod typeck {
40 /// pub const field_multiply_specified_in_initializer: DiagnosticMessage =
41 /// DiagnosticMessage::fluent("typeck_field_multiply_specified_in_initializer");
42 /// pub const field_multiply_specified_in_initializer_label_previous_use: DiagnosticMessage =
43 /// DiagnosticMessage::fluent_attr(
44 /// "typeck_field_multiply_specified_in_initializer",
45 /// "previous_use_label"
46 /// );
47 /// }
48 /// }
49 /// ```
50 /// When emitting a diagnostic, the generated constants can be used as follows:
51 ///
52 /// ```ignore (rust)
53 /// let mut err = sess.struct_span_err(
54 /// span,
55 /// fluent::typeck::field_multiply_specified_in_initializer
56 /// );
57 /// err.span_default_label(span);
58 /// err.span_label(
59 /// previous_use_span,
60 /// fluent::typeck::field_multiply_specified_in_initializer_label_previous_use
61 /// );
62 /// err.emit();
63 /// ```
64 ///
65 /// Note: any crate using this macro must also have a dependency on
66 /// `rustc_errors`, because the generated code refers to things from that
67 /// crate.
68 #[proc_macro]
69 pub fn fluent_messages(input: TokenStream) -> TokenStream {
70 fluent::fluent_messages(input)
71 }