rust/compiler
Dylan DPC 7e7dd1c069
Rollup merge of #97327 - davidtwco:diagnostic-translation-compile-time-validation, r=oli-obk
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..

```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...

```text
error: could not parse Fluent resource
  --> $DIR/test.rs:35:28
   |
LL |         missing_message => "./missing-message.ftl",
   |                            ^^^^^^^^^^^^^^^^^^^^^^^
   |
   = help: see additional errors emitted

error: expected a message field for "missing-message"
 --> ./missing-message.ftl:1:1
  |
1 | missing-message =
  | ^^^^^^^^^^^^^^^^^^
  |
```
...or generating the following code if it succeeds:

```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:

```rust
let mut err = sess.struct_span_err(
    span,
    fluent::typeck::field_multiply_specified_in_initializer
);
err.span_label(
    span,
    fluent::typeck::field_multiply_specified_in_initializer_label
);
err.span_label(
    previous_use_span,
    fluent::typeck::field_multiply_specified_in_initializer_label_previous_use
);
err.emit();
```

I'd like to reduce the verbosity of referring to labels/notes/helps with this scheme (though it wasn't much better before), but I'll leave that for a follow-up.

r? `@oli-obk`
cc `@pvdrz` `@compiler-errors`
2022-05-28 08:45:52 +02:00
..
rustc update jemalloc-sys to jemalloc v5.3 2022-05-25 08:25:15 +02:00
rustc_apfloat Addressed comments by @compiler-errors and @bjorn3 2022-03-30 17:04:46 -04:00
rustc_arena mark FIXMES for all the places found that are probably offset_from 2022-03-29 20:18:28 -04:00
rustc_ast Rollup merge of #97254 - jhpratt:remove-crate-vis, r=cjgillot 2022-05-23 07:43:50 +02:00
rustc_ast_lowering rustc_parse: Move AST -> TokenStream conversion logic to rustc_ast 2022-05-22 12:01:07 +03:00
rustc_ast_passes clippy::complexity fixes 2022-05-26 13:14:24 +02:00
rustc_ast_pretty Rollup merge of #97254 - jhpratt:remove-crate-vis, r=cjgillot 2022-05-23 07:43:50 +02:00
rustc_attr RFC3239: Implement compact cfg(target(..)) 2022-05-24 13:51:36 +02:00
rustc_borrowck Auto merge of #97284 - b-naber:constraint-dyn-impl-suggestion, r=estebank 2022-05-28 04:04:29 +00:00
rustc_builtin_macros Rollup merge of #97458 - estebank:use-self-in-derive-macro, r=compiler-errors 2022-05-28 01:11:50 +02:00
rustc_codegen_cranelift Add flag for stricter checks on uninit/zeroed 2022-05-24 14:26:52 +01:00
rustc_codegen_gcc rustc_codegen_ssa: derive copy and clone for various enums 2022-05-25 10:34:35 +02:00
rustc_codegen_llvm rustc_codegen_ssa: cleanup AtomicOrdering 2022-05-25 10:34:35 +02:00
rustc_codegen_ssa clippy::complexity fixes 2022-05-26 13:14:24 +02:00
rustc_const_eval Auto merge of #96046 - oli-obk:const_typeck, r=cjgillot 2022-05-27 11:31:37 +00:00
rustc_data_structures libcore: Add iter::from_generator which is like iter::from_fn, but for coroutines instead of functions 2022-05-27 01:51:31 +03:00
rustc_driver errors: lazily load fallback fluent bundle 2022-04-13 02:44:59 +01:00
rustc_error_codes rustc: Stricter checking for #[link] attributes 2022-05-15 02:45:47 +03:00
rustc_error_messages macros: introduce fluent_messages macro 2022-05-24 16:48:17 +01:00
rustc_errors macros: introduce fluent_messages macro 2022-05-24 16:48:17 +01:00
rustc_expand Rename ProcMacroDerive as DeriveProcMacro. 2022-05-27 15:58:35 +10:00
rustc_feature Rollup merge of #96913 - Urgau:rfc3239-part2, r=petrochenkov 2022-05-25 10:48:28 +02:00
rustc_fs_util
rustc_graphviz update graphviz links 2022-05-12 18:31:42 +09:00
rustc_hir Lifetime variance fixes for rustdoc 2022-05-22 14:22:40 -07:00
rustc_hir_pretty use hir::Let in hir::Guard 2022-05-09 20:35:58 +08:00
rustc_incremental rename visit item-like methods 2022-05-13 11:46:06 -04:00
rustc_index Auto merge of #95418 - cjgillot:more-disk, r=davidtwco 2022-05-20 20:49:55 +00:00
rustc_infer suggest constraining dyn trait in impl in NLL 2022-05-25 18:13:17 +02:00
rustc_interface Remove the check_mod_intrinsics query 2022-05-24 15:46:23 +00:00
rustc_lexer Reduce max hash in raw strings from u16 to u8 2022-03-23 22:13:55 +01:00
rustc_lint Rollup merge of #97266 - est31:unknown_lints_cfg_attr, r=lcnr 2022-05-25 07:08:42 +09:00
rustc_lint_defs Fix typo 2022-05-23 14:07:40 +02:00
rustc_llvm Add LLVM based mingw-w64 targets 2022-05-13 20:14:15 +02:00
rustc_log clippy::complexity fixes 2022-05-26 13:14:24 +02:00
rustc_macros macros: introduce fluent_messages macro 2022-05-24 16:48:17 +01:00
rustc_metadata Auto merge of #97004 - nnethercote:proc-macro-tweaks, r=eddyb 2022-05-27 06:09:45 +00:00
rustc_middle Auto merge of #97284 - b-naber:constraint-dyn-impl-suggestion, r=estebank 2022-05-28 04:04:29 +00:00
rustc_mir_build clippy::complexity fixes 2022-05-26 13:14:24 +02:00
rustc_mir_dataflow Refactor call terminator to always hold a destination place 2022-05-23 17:49:04 -04:00
rustc_mir_transform Refactor call terminator to always hold a destination place 2022-05-23 17:49:04 -04:00
rustc_monomorphize Auto merge of #97168 - SparrowLii:accesses, r=cjgillot 2022-05-26 11:15:15 +00:00
rustc_parse Minor improvement on else-no-if diagnostic 2022-05-24 15:22:13 -07:00
rustc_parse_format Make rustc_parse_format compile on stable 2022-05-03 11:26:58 +02:00
rustc_passes Remove the check_mod_intrinsics query 2022-05-24 15:46:23 +00:00
rustc_plugin_impl
rustc_privacy Rollup merge of #97096 - tmiasko:reachable-constructor, r=petrochenkov 2022-05-18 07:40:58 +09:00
rustc_query_impl Auto merge of #97239 - jhpratt:remove-crate-vis, r=joshtriplett 2022-05-21 06:38:49 +00:00
rustc_query_system Auto merge of #96094 - Elliot-Roberts:fix_doctests, r=compiler-errors 2022-05-07 06:30:29 +00:00
rustc_resolve Rollup merge of #97240 - TaKO8Ki:improve-errors-about-typos-on-variables, r=compiler-errors 2022-05-24 12:18:30 +09:00
rustc_save_analysis [save-analysis] Reference the variant not enum at struct-literal construction. 2022-05-12 16:34:02 -07:00
rustc_serialize Cache more queries on disk. 2022-05-13 08:06:48 +02:00
rustc_session Add flag for stricter checks on uninit/zeroed 2022-05-24 14:26:52 +01:00
rustc_span Auto merge of #97386 - nnethercote:optimize-pos-adjustments, r=bjorn3 2022-05-26 22:01:19 +00:00
rustc_symbol_mangling Auto merge of #96883 - jackh726:early-binder-2, r=oli-obk 2022-05-14 23:53:11 +00:00
rustc_target Simplify implementation of -Z gcc-ld 2022-05-25 23:55:22 +03:00
rustc_trait_selection Auto merge of #96046 - oli-obk:const_typeck, r=cjgillot 2022-05-27 11:31:37 +00:00
rustc_traits Remove crate visibility usage in compiler 2022-05-20 20:04:54 -04:00
rustc_ty_utils Auto merge of #97012 - oli-obk:🦀_intrinsics, r=davidtwco 2022-05-17 09:39:26 +00:00
rustc_type_ir fix most compiler/ doctests 2022-05-02 17:40:30 -07:00
rustc_typeck Rollup merge of #97327 - davidtwco:diagnostic-translation-compile-time-validation, r=oli-obk 2022-05-28 08:45:52 +02:00