rustc_fluent_macro/
lib.rs

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