rustc_fluent_macro/
lib.rs

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