From 38ab20dbf7469aa5a3cb0d7bfd3700281aa46467 Mon Sep 17 00:00:00 2001 From: V1shvesh <22243137+V1shvesh@users.noreply.github.com> Date: Sun, 8 Sep 2019 14:57:03 +0530 Subject: [PATCH 1/4] Add pluralise macro Adress issue #64238. Create a macro to be used for pluralisation check throughout rustc codebase. --- src/librustc_errors/lib.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/librustc_errors/lib.rs b/src/librustc_errors/lib.rs index 6585633e00af8..b5341a2b727a5 100644 --- a/src/librustc_errors/lib.rs +++ b/src/librustc_errors/lib.rs @@ -845,3 +845,11 @@ impl Level { } } } + +#[macro_export] +macro_rules! pluralise { + ($x:expr) => { + if $x != 1 { "s" } else { "" } + }; +} + From c2552065819288a91a7e2646d95bf53765304b4f Mon Sep 17 00:00:00 2001 From: V1shvesh <22243137+V1shvesh@users.noreply.github.com> Date: Sun, 8 Sep 2019 15:00:08 +0530 Subject: [PATCH 2/4] Refactor Pluralisation Modify files performing pluralisation checks to incorporate the dedicated macro located in `syntax::errors`. --- src/librustc/ty/error.rs | 7 +------ src/librustc_typeck/astconv.rs | 3 ++- src/librustc_typeck/check/compare_method.rs | 9 +++++---- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/librustc/ty/error.rs b/src/librustc/ty/error.rs index f67526ea4a1d9..62910ec320494 100644 --- a/src/librustc/ty/error.rs +++ b/src/librustc/ty/error.rs @@ -4,6 +4,7 @@ use std::borrow::Cow; use std::fmt; use rustc_target::spec::abi; use syntax::ast; +use syntax::errors::pluralise; use errors::{Applicability, DiagnosticBuilder}; use syntax_pos::Span; @@ -82,12 +83,6 @@ impl<'tcx> fmt::Display for TypeError<'tcx> { } }; - macro_rules! pluralise { - ($x:expr) => { - if $x != 1 { "s" } else { "" } - }; - } - match *self { CyclicTy(_) => write!(f, "cyclic type of infinite size"), Mismatch => write!(f, "types differ"), diff --git a/src/librustc_typeck/astconv.rs b/src/librustc_typeck/astconv.rs index 9e52eae88ef45..e9ca0f3d978ca 100644 --- a/src/librustc_typeck/astconv.rs +++ b/src/librustc_typeck/astconv.rs @@ -23,6 +23,7 @@ use rustc_target::spec::abi; use crate::require_c_abi_if_c_variadic; use smallvec::SmallVec; use syntax::ast; +use syntax::errors::pluralise; use syntax::feature_gate::{GateIssue, emit_feature_err}; use syntax::util::lev_distance::find_best_match_for_name; use syntax::symbol::sym; @@ -377,7 +378,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { quantifier, bound, kind, - if bound != 1 { "s" } else { "" }, + pluralise!(bound), )) }; diff --git a/src/librustc_typeck/check/compare_method.rs b/src/librustc_typeck/check/compare_method.rs index 8e187b7e05b51..f22499f547272 100644 --- a/src/librustc_typeck/check/compare_method.rs +++ b/src/librustc_typeck/check/compare_method.rs @@ -10,6 +10,7 @@ use rustc::util::common::ErrorReported; use errors::{Applicability, DiagnosticId}; use syntax_pos::Span; +use syntax::errors::pluralise; use super::{Inherited, FnCtxt, potentially_plural_count}; @@ -648,9 +649,9 @@ fn compare_number_of_generics<'tcx>( declaration has {} {kind} parameter{}", trait_.ident, impl_count, - if impl_count != 1 { "s" } else { "" }, + pluralise!(impl_count), trait_count, - if trait_count != 1 { "s" } else { "" }, + pluralise!(trait_count), kind = kind, ), DiagnosticId::Error("E0049".into()), @@ -665,7 +666,7 @@ fn compare_number_of_generics<'tcx>( "expected {} {} parameter{}", trait_count, kind, - if trait_count != 1 { "s" } else { "" }, + pluralise!(trait_count), )); } for span in spans { @@ -680,7 +681,7 @@ fn compare_number_of_generics<'tcx>( "found {} {} parameter{}{}", impl_count, kind, - if impl_count != 1 { "s" } else { "" }, + pluralise!(impl_count), suffix.unwrap_or_else(|| String::new()), )); } From fc4375a895c899e937892c5666372cf14d35c6df Mon Sep 17 00:00:00 2001 From: V1shvesh <22243137+V1shvesh@users.noreply.github.com> Date: Sun, 8 Sep 2019 22:58:28 +0530 Subject: [PATCH 3/4] Remove extra trailing newline --- src/librustc_errors/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/librustc_errors/lib.rs b/src/librustc_errors/lib.rs index b5341a2b727a5..20338a33ef684 100644 --- a/src/librustc_errors/lib.rs +++ b/src/librustc_errors/lib.rs @@ -852,4 +852,3 @@ macro_rules! pluralise { if $x != 1 { "s" } else { "" } }; } - From 7457ef858026d1858e9a36c7fad7a95773d0632d Mon Sep 17 00:00:00 2001 From: V1shvesh <22243137+V1shvesh@users.noreply.github.com> Date: Sun, 8 Sep 2019 23:01:43 +0530 Subject: [PATCH 4/4] Dedent macro definition --- src/librustc_errors/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/librustc_errors/lib.rs b/src/librustc_errors/lib.rs index 20338a33ef684..c1fba416d6433 100644 --- a/src/librustc_errors/lib.rs +++ b/src/librustc_errors/lib.rs @@ -848,7 +848,7 @@ impl Level { #[macro_export] macro_rules! pluralise { - ($x:expr) => { - if $x != 1 { "s" } else { "" } - }; + ($x:expr) => { + if $x != 1 { "s" } else { "" } + }; }