Rust: Factor out pluralisation checks in diagnostics

Created on 6 Sep 2019  路  8Comments  路  Source: rust-lang/rust

The following pattern is common in diagnostic messages in rustc:

format!("found {} thing{}", x, if x != 1 { "s" } else { "" })

It would be good to extract all of these pluralisation checks into a function or macro:

format!("found {} thing{}", x, pluralise(x))

There's already one in src/librustc/ty/error.rs, but it's not used anywhere else, so we could move it to somewhere in src/librustc_errors, and replace the occurrences of this pattern with the macro.

https://github.com/rust-lang/rust/blob/0b97726e6c524d2cc0de4c2f5b1284eca010a7b2/src/librustc/ty/error.rs#L85-L89


This issue has been assigned to @V1shvesh via this comment.


C-cleanup E-easy E-help-wanted E-mentor T-compiler

Most helpful comment

@V1shvesh: searching for { "s" } else { "" } and { "" } else { "s" } in rustc, I can see a few cases that weren't caught by https://github.com/rust-lang/rust/pull/64280, e.g. in resolve_lifetime.rs, diagnostics.rs, etc. Let's keep this issue open until they've all been replaced with the new macro.

All 8 comments

I'd love to be assigned to this issue!

@rustbot assign @V1shvesh

So, I have moved the macro under src/librustc_errors in a file called pluralise.rs. I have also located all the occurrences of the pattern.

I am a bit new to Rust, so I still am learning about the module system. How shall I pull the macro into the scope of all the relevant files?

@V1shvesh: I'd put it in src/librustc_errors/lib.rs, so we don't have one new file for a single macro. Then you can use it from use syntax::errors;.

Thanks for pointing me in the right direction @varkor!

@V1shvesh: searching for { "s" } else { "" } and { "" } else { "s" } in rustc, I can see a few cases that weren't caught by https://github.com/rust-lang/rust/pull/64280, e.g. in resolve_lifetime.rs, diagnostics.rs, etc. Let's keep this issue open until they've all been replaced with the new macro.

I have open a pr to refactor the pluralisations remain, @Centril please have a look at it, thanks.

@varkor Oh, my bad. @glorv thanks for helping out!

Was this page helpful?
0 / 5 - 0 ratings