rustc_fluent_macro/lib.rs
1// tidy-alphabetical-start
2#![allow(internal_features)]
3#![allow(rustc::default_hash_types)]
4#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
5#![doc(rust_logo)]
6#![feature(proc_macro_diagnostic)]
7#![feature(rustdoc_internals)]
8#![feature(track_path)]
9// tidy-alphabetical-end
10
11use proc_macro::TokenStream;
12
13mod 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: DiagMessage =
41/// DiagMessage::fluent("typeck_field_multiply_specified_in_initializer");
42/// pub const field_multiply_specified_in_initializer_label_previous_use: DiagMessage =
43/// DiagMessage::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]
69pub fn fluent_messages(input: TokenStream) -> TokenStream {
70 fluent::fluent_messages(input)
71}