1
Fork 0
rust/compiler
Matthias Krüger 3aa5734a2c
Rollup merge of #100377 - est31:fluent_grepability, r=davidtwco
Replace - with _ in fluent slugs to improve developer workflows

This is a proposal to smoothen the compiler contribution experience in the face of the move to fluent.

## Context

The fluent project has introduced a layer of abstraction to compiler errors. Previously, people would write down error messages directly in the same file the code was located to emit them. Now, there is a slug that connects the code in the compiler to the error message in the ftl file.

You can look at 7ef610c003 to see an example of the changes:

Old:
```Rust
let msg = format!(
    "bounds on `{}` are most likely incorrect, consider instead \
        using `{}` to detect whether a type can be trivially dropped",
    predicate,
    cx.tcx.def_path_str(needs_drop)
);
lint.build(&msg).emit();
```
New (Rust side):
```Rust
lint.build(fluent::lint::drop_trait_constraints)
    .set_arg("predicate", predicate)
    .set_arg("needs_drop", cx.tcx.def_path_str(needs_drop))
    .emit();
```
New (Fluent side):
```fluent
lint-drop-trait-constraints =
    bounds on `{$predicate}` are most likely incorrect, consider instead using `{$needs_drop}` to detect whether a type can be trivially dropped
```

You will note that in the ftl file, the slug is slightly different from the slug in the Rust file: The ftl slug uses `-` (e.g. `lint-drop-trait-constraints`) while the rust slug uses `::` and `_` (e.g. `lint::drop_trait_constraints`). This choice was probably done due to:

* Rust not accepting `-` in identifiers (as it is an operator)
* fluent not supporting the `:` character in slug names (parse error upon attempts)
* all official fluent documentation using `-` instead of `_`

## The problem

The two different types of slugs, one with `-`, and one with `_`, cause difficulties for contributors. Imagine you don't have perfect knowledge of where stuff is in the compiler (i would say this is most people), and you encounter an error for which you think there is something you could improve that is not just a rewording.

So you want to find out where in the compiler's code that error is being emitted. The best way is via grepping.

1. you grep for the message in the compiler's source code. You discover the ftl file and find out the slug for that error.
2. That slug however contains `-` instead of `_`, so you have to manually translate the `-`'s into `_`s, and furthermore either remove the leading module name, or replace the first `-` with a `::`.
3. you do a second grep to get to the emitting location in the compiler's code.

This translation difficulty in step 2 appears also in the other direction when you want to figure out what some code in the compiler is doing and use error messages to help your understanding. Comments and variable names are way less exposed to users so [are more likely going to lie](cc3c5d2700) than error messages.

I think that at least the `-`→`_` translation which makes up most of step 2 can be removed at low cost.

## The solution

If you look closely, the practice of fluent to use `-` is only a stylistic choice and it is not enforced by fluent implementations, neither the playground nor the one the rust compiler uses, that slugs may not contain `_`. Thus, we can in fact migrate the ftl side to `_`. So now we'll have slugs like  `lint_drop_trait_constraints` on the ftl side. You only have to do one replacement now to get to the Rust slug: remove the first `_` and place a `::` in its stead. I would argue that this change is in fact useful as it allows you to control whether you want to look at the rust side of things or the ftl side of things via changing the query string only: with an increased number of translations checked into the repository, grepping for raw slugs will return the slug in many ftl files, so an explicit step to look for the source code is always useful. In the other direction (rust to fluent), you don't need a translation at all any more, as you can just take the final piece of the slug (e.g. `drop_trait_constraints`) and grep for that. The PR also adds enforcement to forbid usage of `_` in slug names. Internal slug names (those leading with a `-`) are exempt from that enforcement.

As another workflow that benefits from this change, people who add new errors don't have to do that `-` conversion either.

| Before/After | Fluent slug | Rust slug (no change) |
|--|--|--|
| Before | `lint-drop-trait-constraints` | `lint::drop_trait_constraints`|
| After | `lint_drop_trait_constraints` | `lint::drop_trait_constraints`|

Note that I've suggested this previously in the translation thread on zulip. I think it's important to think about non-translator contribution impact of fluent. I have certainly plans for more improvements, but this is a good first step.

``@rustbot`` label A-diagnostics
2022-08-15 20:11:34 +02:00
..
rustc Don't rerun the build script for the compiler each time on linux 2022-07-10 23:57:25 -05:00
rustc_apfloat Warn about dead tuple struct fields 2022-08-03 12:17:23 +02:00
rustc_arena Stabilize ptr_const_cast 2022-08-10 17:22:58 +02:00
rustc_ast Adjust cfgs 2022-08-12 16:28:15 -04:00
rustc_ast_lowering Auto merge of #96745 - ehuss:even-more-attribute-validation, r=cjgillot 2022-08-15 05:50:54 +00:00
rustc_ast_passes Adjust cfgs 2022-08-12 16:28:15 -04:00
rustc_ast_pretty Rollup merge of #100350 - jhpratt:stringify-vis, r=cjgillot 2022-08-11 22:53:06 +02:00
rustc_attr Adjust cfgs 2022-08-12 16:28:15 -04:00
rustc_borrowck TypeError can be Copy 2022-08-14 19:58:46 +00:00
rustc_builtin_macros Rollup merge of #100277 - m-ou-se:format-args-1, r=compiler-errors 2022-08-15 10:28:10 +02:00
rustc_codegen_cranelift also update anyhow in codegen_cranelift 2022-08-08 09:04:26 -04:00
rustc_codegen_gcc Introduce an ArchiveBuilderBuilder 2022-07-28 09:08:47 +00:00
rustc_codegen_llvm Auto merge of #98393 - michaelwoerister:new-cpp-like-enum-debuginfo, r=wesleywiser 2022-08-15 12:59:53 +00:00
rustc_codegen_ssa Auto merge of #98393 - michaelwoerister:new-cpp-like-enum-debuginfo, r=wesleywiser 2022-08-15 12:59:53 +00:00
rustc_const_eval Rollup merge of #100438 - compiler-errors:issue-100360, r=lcnr 2022-08-13 14:10:07 -07:00
rustc_data_structures remove some manual hash stable impls 2022-07-29 12:48:28 +02:00
rustc_driver Error on broken pipe but do not ICE 2022-08-02 01:08:50 +01:00
rustc_error_codes region_outlives_predicate no snapshot 2022-07-21 13:09:01 +02:00
rustc_error_messages Rollup merge of #100377 - est31:fluent_grepability, r=davidtwco 2022-08-15 20:11:34 +02:00
rustc_errors Adjust cfgs 2022-08-12 16:28:15 -04:00
rustc_expand Adjust cfgs 2022-08-12 16:28:15 -04:00
rustc_feature Rollup merge of #96478 - WaffleLapkin:rustc_default_body_unstable, r=Aaron1011 2022-08-09 17:34:50 +05:30
rustc_fs_util
rustc_graphviz Fully stabilize NLL 2022-06-03 17:16:41 -04:00
rustc_hir Add visitors for PatField and ExprField. 2022-08-11 21:48:39 -07:00
rustc_hir_pretty Auto merge of #96745 - ehuss:even-more-attribute-validation, r=cjgillot 2022-08-15 05:50:54 +00:00
rustc_incremental Remove trait_of_item query. 2022-08-01 21:39:26 +02:00
rustc_index debuginfo: Change C++-like encoding for enums. 2022-08-12 10:53:07 +02:00
rustc_infer TypeError can be Copy 2022-08-14 19:58:46 +00:00
rustc_interface Adjust cfgs 2022-08-12 16:28:15 -04:00
rustc_lexer Shrink Token. 2022-08-01 08:53:04 +10:00
rustc_lint Auto merge of #96745 - ehuss:even-more-attribute-validation, r=cjgillot 2022-08-15 05:50:54 +00:00
rustc_lint_defs Warn about dead tuple struct fields 2022-08-03 12:17:23 +02:00
rustc_llvm debuginfo: Change C++-like encoding for enums. 2022-08-12 10:53:07 +02:00
rustc_log clippy::complexity fixes 2022-05-26 13:14:24 +02:00
rustc_macros Change fluent_messages macro to expect _ slugs instead of - slugs 2022-08-12 22:26:08 +02:00
rustc_metadata Adjust cfgs 2022-08-12 16:28:15 -04:00
rustc_middle Auto merge of #96745 - ehuss:even-more-attribute-validation, r=cjgillot 2022-08-15 05:50:54 +00:00
rustc_mir_build Adjust cfgs 2022-08-12 16:28:15 -04:00
rustc_mir_dataflow Remove redundant TransferWrapper struct 2022-08-01 17:08:19 +02:00
rustc_mir_transform Adjust cfgs 2022-08-12 16:28:15 -04:00
rustc_monomorphize Refuse to codegen an upstream static. 2022-08-10 18:30:12 +02:00
rustc_parse Rollup merge of #100566 - TaKO8Ki:use-create-snapshot-for-diagnostic, r=cjgillot 2022-08-15 10:28:14 +02:00
rustc_parse_format add a comment about what we can parse now 2022-08-04 20:43:35 +09:00
rustc_passes Rollup merge of #100031 - GoldsteinE:try-removing-the-field, r=michaelwoerister 2022-08-15 20:11:32 +02:00
rustc_plugin_impl remove currently unused deps 2022-06-13 22:20:51 +03:00
rustc_privacy Adjust cfgs 2022-08-12 16:28:15 -04:00
rustc_query_impl Use LocalDefId for closures more 2022-07-30 15:59:17 -05:00
rustc_query_system Remove useless closure. 2022-07-29 22:11:23 +02:00
rustc_resolve Rollup merge of #100367 - fmease:fix-100365, r=compiler-errors 2022-08-13 21:06:49 -07:00
rustc_save_analysis Remove DefId from AssocItemContainer. 2022-08-01 21:38:45 +02:00
rustc_serialize Upgrade indexmap and thorin-dwp to use hashbrown 0.12 2022-07-17 07:05:58 -07:00
rustc_session Adjust cfgs 2022-08-12 16:28:15 -04:00
rustc_smir Rustfmt 2022-06-02 10:29:00 +00:00
rustc_span Rollup merge of #100031 - GoldsteinE:try-removing-the-field, r=michaelwoerister 2022-08-15 20:11:32 +02:00
rustc_symbol_mangling Auto merge of #95548 - rcvalle:rust-cfi-2, r=nagisa 2022-07-24 01:22:36 +00:00
rustc_target rustc_target: Update some old naming around self contained linking 2022-08-12 18:47:13 +03:00
rustc_trait_selection TypeError can be Copy 2022-08-14 19:58:46 +00:00
rustc_traits Rollup merge of #100095 - jackh726:early-binder, r=lcnr 2022-08-04 22:25:04 +02:00
rustc_transmute safe transmute: fix broken intradoc link 2022-08-02 14:44:23 +00:00
rustc_ty_utils Add bound_impl_subject and bound_return_ty 2022-08-03 01:02:46 -04:00
rustc_type_ir Inline DebruijnIndex methods 2022-07-22 17:00:49 +02:00
rustc_typeck Auto merge of #100569 - matthiaskrgr:rollup-9450lzs, r=matthiaskrgr 2022-08-15 15:25:31 +00:00