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

Replace - with _ in fluent slugs to improve developer workflows #100377

Merged
merged 4 commits into from
Aug 15, 2022

Conversation

est31
Copy link
Member

@est31 est31 commented Aug 10, 2022

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 7ef610c to see an example of the changes:

Old:

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

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

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

@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Aug 10, 2022
@rust-highfive
Copy link
Collaborator

r? @cjgillot

(rust-highfive has picked a reviewer for you, use r? to override)

@rustbot rustbot added the A-diagnostics Area: Messages for errors, warnings, and lints label Aug 10, 2022
@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 10, 2022
@est31 est31 force-pushed the fluent_grepability branch from 123d562 to 02ff933 Compare August 10, 2022 15:19
@compiler-errors
Copy link
Member

cc @davidtwco

@compiler-errors
Copy link
Member

I think this change is fine, we really should be using underscores for grep-ability.

@davidtwco
Copy link
Member

r? @davidtwco

@rust-highfive rust-highfive assigned davidtwco and unassigned cjgillot Aug 10, 2022
Copy link
Member

@davidtwco davidtwco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm okay with this change - I don't anticipate any issues with an all-_ slug scheme and it increases uniformity.

Looks like you need to fix src/test/run-make/translation which relies on overriding one of the built-in slugs.

// `typeck-foo-bar` => `foo_bar` (in `typeck.ftl`)
// `const-eval-baz` => `baz` (in `const_eval.ftl`)
let snake_name = Ident::new(
if name.contains('-') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you submit a PR to the dev guide documenting this and updating the examples there too? Please ping me on it :)

(this can be after this lands, just so that it happens)

@rust-log-analyzer

This comment has been minimized.

@deltragon
Copy link
Contributor

Is it possible to mix - and _ in a slug? If so, would it make sense to make it lint::drop_trait_constraints in Rust and lint-drop_trait_constraints in fluent, to differentiate which part can be grepped for?
Because with this change, grepping for lint_drop_trait_constraints would still not result in anything, but if the slug in fluent is somewhat split by one part using - and another using _ it might nudge people to grep for drop_trait_constraints instead.

@est31
Copy link
Member Author

est31 commented Aug 10, 2022

Thanks @davidtwco and @compiler-errors for the positive feedback!

@deltragon from a technical point of view, it's possible, yes. And at least both in Kate and in VS Codium - constitutes a double click boundary while _ unifies, so you can obtain the part you can grep for more quickly. On the other hand, it looks a bit weird compared to pure - or _ slugs. And the naming scheme might be harder to understand, idk. Personally I'd be fine with both pure _ slugs and the mixed scheme. Note that for helper slugs, one needs a starting - so some mixing is present already. I'm interested in what others think.

The only allowed chars for fluent message identifiers are ones for which u8::is_ascii_alphanumeric returns true as well as - and _ (and at the start only ascii alphabetic chars are allowed), so, barring modifications of fluent itself, any solution would have to be built out of those.

@est31 est31 force-pushed the fluent_grepability branch from 02ff933 to cc5dab1 Compare August 10, 2022 20:46
@davidtwco
Copy link
Member

@deltragon from a technical point of view, it's possible, yes. And at least both in Kate and in VS Codium - constitutes a double click boundary while _ unifies, so you can obtain the part you can grep for more quickly. On the other hand, it looks a bit weird compared to pure - or _ slugs. And the naming scheme might be harder to understand, idk. Personally I'd be fine with both pure _ slugs and the mixed scheme. Note that for helper slugs, one needs a starting - so some mixing is present already. I'm interested in what others think.

I don't have a strong opinion on this - either would be fine with me. I guess I'd prefer that if we do have mixed - and _ that the - seperator is used only once, between the crate name and the slug, but not within the slug or within the crate name.

@compiler-errors compiler-errors added A-translation Area: Translation infrastructure, and migrating existing diagnostics to SessionDiagnostic and removed A-diagnostics Area: Messages for errors, warnings, and lints labels Aug 11, 2022
@est31 est31 force-pushed the fluent_grepability branch 2 times, most recently from 6eb5ab4 to ddc1941 Compare August 12, 2022 07:51
@davidtwco
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Aug 12, 2022

📌 Commit ddc194171118535d473ff7195b4e4e1763d86abd has been approved by davidtwco

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 12, 2022
est31 added a commit to est31/rustc-dev-guide that referenced this pull request Aug 12, 2022
est31 added a commit to est31/rustc-dev-guide that referenced this pull request Aug 12, 2022
est31 added a commit to est31/rustc-dev-guide that referenced this pull request Aug 12, 2022
@bors

This comment was marked as resolved.

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Aug 12, 2022
est31 added 2 commits August 12, 2022 22:22
Having to replace - with _ (and vice versa) makes the slugs less greppable
and thus constitutes a contributor roadblock.

Result of running this repeatedly up until reaching a fixpoint:

find compiler/rustc_error_messages/locales/en-US/ -type f -exec sed -i 's/\(.+\)-\(.*\)=/\1_\2=/' {} \;

Plus some fixes to update usages of slugs leading with -.
@est31 est31 force-pushed the fluent_grepability branch from ddc1941 to 66e4ddb Compare August 12, 2022 20:23
est31 added 2 commits August 12, 2022 22:26
For the most part, the macro actually worked with _ slugs, but the prefix_something -> prefix::something
conversion was not implemented.

We don't want to accept - slugs for consistency reasons.
We thus error if a name is found with - inside.
This ensures a consistent style.
@est31 est31 force-pushed the fluent_grepability branch from 66e4ddb to ca16a8d Compare August 12, 2022 20:26
@est31
Copy link
Member Author

est31 commented Aug 14, 2022

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 14, 2022
@est31
Copy link
Member Author

est31 commented Aug 15, 2022

Can someone add r=davidtwco? I've just rebased it. Thanks.

@davidtwco
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Aug 15, 2022

📌 Commit ca16a8d has been approved by davidtwco

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 15, 2022
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Aug 15, 2022
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 7ef610c 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](rust-lang@cc3c5d2) 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
bors added a commit to rust-lang-ci/rust that referenced this pull request Aug 15, 2022
…iaskrgr

Rollup of 10 pull requests

Successful merges:

 - rust-lang#100031 (improve "try ignoring the field" diagnostic)
 - rust-lang#100325 (Rustdoc-Json: Don't remove impls for items imported from private modules)
 - rust-lang#100377 (Replace - with _ in fluent slugs to improve developer workflows)
 - rust-lang#100458 (Adjust span of fn argument declaration)
 - rust-lang#100514 (Delay span bug when failing to normalize negative coherence impl subject due to other malformed impls)
 - rust-lang#100528 (Support 1st group of RISC-V Bitmanip backend target features)
 - rust-lang#100559 (Parser simplifications)
 - rust-lang#100568 (Fix STD build for ESP-IDF)
 - rust-lang#100582 ([rustdoc] Fix handling of stripped enum variant in JSON output format)
 - rust-lang#100586 (Reland changes replacing num_cpus with available_parallelism )

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 3aa5734 into rust-lang:master Aug 15, 2022
@rustbot rustbot added this to the 1.65.0 milestone Aug 15, 2022
est31 added a commit to est31/rustc-dev-guide that referenced this pull request Aug 16, 2022
est31 added a commit to est31/rustc-dev-guide that referenced this pull request Aug 16, 2022
est31 added a commit to est31/rustc-dev-guide that referenced this pull request Aug 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-translation Area: Translation infrastructure, and migrating existing diagnostics to SessionDiagnostic S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants