add "was" to pluralize macro and use it

This commit is contained in:
Takayuki Maeda 2022-06-22 14:56:40 +09:00
parent f847261478
commit eb86daa138
5 changed files with 27 additions and 21 deletions

View file

@ -26,6 +26,9 @@ macro_rules! pluralize {
("is", $x:expr) => {
if $x == 1 { "is" } else { "are" }
};
("was", $x:expr) => {
if $x == 1 { "was" } else { "were" }
};
("this", $x:expr) => {
if $x == 1 { "this" } else { "these" }
};

View file

@ -1839,25 +1839,28 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
})
.partition(|field| field.2);
let remaining_private_fields_len = remaining_private_fields.len();
let names = match &remaining_private_fields
.iter()
.map(|(name, _, _)| name.to_string())
.collect::<Vec<_>>()[..]
{
_ if remaining_private_fields_len > 6 => String::new(),
[name] => format!("`{name}` "),
[names @ .., last] => {
let names = names.iter().map(|name| format!("`{name}`")).collect::<Vec<_>>();
format!("{} and `{last}` ", names.join(", "))
}
[] => unreachable!(),
};
err.span_labels(used_private_fields.iter().map(|(_, span, _)| *span), "private field");
err.note(format!(
"... and other private field{s} {names}that were not provided",
s = pluralize!(remaining_private_fields_len),
));
if !remaining_private_fields.is_empty() {
let remaining_private_fields_len = remaining_private_fields.len();
let names = match &remaining_private_fields
.iter()
.map(|(name, _, _)| name.to_string())
.collect::<Vec<_>>()[..]
{
_ if remaining_private_fields_len > 6 => String::new(),
[name] => format!("`{name}` "),
[names @ .., last] => {
let names = names.iter().map(|name| format!("`{name}`")).collect::<Vec<_>>();
format!("{} and `{last}` ", names.join(", "))
}
[] => unreachable!(),
};
err.note(format!(
"... and other private field{s} {names}that {were} not provided",
s = pluralize!(remaining_private_fields_len),
were = pluralize!("was", remaining_private_fields_len),
));
}
err.emit();
}

View file

@ -4,7 +4,7 @@ error: cannot construct `Foo` with struct literal syntax due to private fields
LL | foo::Foo {};
| ^^^^^^^^
|
= note: ... and other private field `you_cant_use_this_field` that were not provided
= note: ... and other private field `you_cant_use_this_field` that was not provided
error: aborting due to previous error

View file

@ -16,7 +16,7 @@ error: cannot construct `Pub` with struct literal syntax due to private fields
LL | foo::Pub {};
| ^^^^^^^^
|
= note: ... and other private field `private` that were not provided
= note: ... and other private field `private` that was not provided
error[E0063]: missing field `y` in initializer of `Enum`
--> $DIR/issue-79593.rs:23:5

View file

@ -4,7 +4,7 @@ error: cannot construct `Foo` with struct literal syntax due to private fields
LL | foo::Foo {};
| ^^^^^^^^
|
= note: ... and other private field `you_cant_use_this_field` that were not provided
= note: ... and other private field `you_cant_use_this_field` that was not provided
error: aborting due to previous error