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