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