Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

macros: introduce fluent_messages macro #97327

Merged

Commits on May 24, 2022

  1. macros: change code block language

    With `ignore (rust)` rather than `ignore (pseudo-Rust)` my editor
    highlights the code in the block, which is nicer.
    
    Signed-off-by: David Wood <david.wood@huawei.com>
    davidtwco committed May 24, 2022
    Configuration menu
    Copy the full SHA
    6e85efd View commit details
    Browse the repository at this point in the history
  2. macros: introduce fluent_messages macro

    Adds a new `fluent_messages` macro which performs compile-time
    validation of the compiler's Fluent resources (i.e. that the resources
    parse and don't multiply define the same messages) and generates
    constants that make using those messages in diagnostics more ergonomic.
    
    For example, given the following invocation of the macro..
    
    ```ignore (rust)
    fluent_messages! {
        typeck => "./typeck.ftl",
    }
    ```
    ..where `typeck.ftl` has the following contents..
    
    ```fluent
    typeck-field-multiply-specified-in-initializer =
        field `{$ident}` specified more than once
        .label = used more than once
        .label-previous-use = first use of `{$ident}`
    ```
    ...then the macro parse the Fluent resource, emitting a diagnostic if it
    fails to do so, and will generate the following code:
    
    ```ignore (rust)
    pub static DEFAULT_LOCALE_RESOURCES: &'static [&'static str] = &[
        include_str!("./typeck.ftl"),
    ];
    
    mod fluent_generated {
        mod typeck {
            pub const field_multiply_specified_in_initializer: DiagnosticMessage =
                DiagnosticMessage::fluent("typeck-field-multiply-specified-in-initializer");
            pub const field_multiply_specified_in_initializer_label_previous_use: DiagnosticMessage =
                DiagnosticMessage::fluent_attr(
                    "typeck-field-multiply-specified-in-initializer",
                    "previous-use-label"
                );
        }
    }
    ```
    
    When emitting a diagnostic, the generated constants can be used as
    follows:
    
    ```ignore (rust)
    let mut err = sess.struct_span_err(
        span,
        fluent::typeck::field_multiply_specified_in_initializer
    );
    err.span_default_label(span);
    err.span_label(
        previous_use_span,
        fluent::typeck::field_multiply_specified_in_initializer_label_previous_use
    );
    err.emit();
    ```
    
    Signed-off-by: David Wood <david.wood@huawei.com>
    davidtwco committed May 24, 2022
    Configuration menu
    Copy the full SHA
    552eb32 View commit details
    Browse the repository at this point in the history
  3. typeck: use typed fluent identifiers for diags

    Use new typed Fluent identifiers for the "missing type parameters"
    diagnostic in the typeck crate which was manually creating
    `DiagnosticMessage`s previously.
    
    Signed-off-by: David Wood <david.wood@huawei.com>
    davidtwco committed May 24, 2022
    Configuration menu
    Copy the full SHA
    ce9901f View commit details
    Browse the repository at this point in the history