Skip to content

Commit 19fadf6

Browse files
committed
auto merge of #12678 : lifthrasiir/rust/pluralize, r=alexcrichton
While we are not yet ready for compiler i18n, this also keeps the error handling code clean. The set of altered error messages was obtained by grepping for `"s"` and `(s)`, so there might be some missing messages.
2 parents dc18659 + 2145445 commit 19fadf6

File tree

4 files changed

+114
-107
lines changed

4 files changed

+114
-107
lines changed

src/librustc/middle/trans/intrinsic.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -376,17 +376,14 @@ pub fn trans_intrinsic(ccx: @CrateContext,
376376
ast_map::NodeExpr(e) => e.span,
377377
_ => fail!("transmute has non-expr arg"),
378378
};
379-
let pluralize = |n| if 1 == n { "" } else { "s" };
380379
ccx.sess.span_fatal(sp,
381-
format!("transmute called on types with \
382-
different sizes: {} ({} bit{}) to \
383-
{} ({} bit{})",
384-
ty_to_str(ccx.tcx, in_type),
385-
in_type_size,
386-
pluralize(in_type_size),
387-
ty_to_str(ccx.tcx, out_type),
388-
out_type_size,
389-
pluralize(out_type_size)));
380+
format!("transmute called on types with different sizes: \
381+
{intype} ({insize, plural, =1{# bit} other{# bits}}) to \
382+
{outtype} ({outsize, plural, =1{# bit} other{# bits}})",
383+
intype = ty_to_str(ccx.tcx, in_type),
384+
insize = in_type_size as uint,
385+
outtype = ty_to_str(ccx.tcx, out_type),
386+
outsize = out_type_size as uint));
390387
}
391388

392389
if !return_type_is_void(ccx, out_type) {

src/librustc/middle/typeck/check/_match.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -247,12 +247,13 @@ pub fn check_pat_variant(pcx: &pat_ctxt, pat: &ast::Pat, path: &ast::Path,
247247
if arg_len > 0 {
248248
// N-ary variant.
249249
if arg_len != subpats_len {
250-
let s = format!("this pattern has {} field{}, but the corresponding {} has {} field{}",
251-
subpats_len,
252-
if subpats_len == 1u { ~"" } else { ~"s" },
253-
kind_name,
254-
arg_len,
255-
if arg_len == 1u { ~"" } else { ~"s" });
250+
let s = format!("this pattern has \
251+
{npat, plural, =1{# field} other{# fields}}, \
252+
but the corresponding {kind} has \
253+
{narg, plural, =1{# field} other{# fields}}",
254+
npat = subpats_len,
255+
kind = kind_name,
256+
narg = arg_len);
256257
tcx.sess.span_err(pat.span, s);
257258
error_happened = true;
258259
}
@@ -266,11 +267,11 @@ pub fn check_pat_variant(pcx: &pat_ctxt, pat: &ast::Pat, path: &ast::Path,
266267
}
267268
} else if subpats_len > 0 {
268269
tcx.sess.span_err(pat.span,
269-
format!("this pattern has {} field{}, but the corresponding {} has no \
270-
fields",
271-
subpats_len,
272-
if subpats_len == 1u { "" } else { "s" },
273-
kind_name));
270+
format!("this pattern has \
271+
{npat, plural, =1{# field} other{# fields}}, \
272+
but the corresponding {kind} has no fields",
273+
npat = subpats_len,
274+
kind = kind_name));
274275
error_happened = true;
275276
}
276277

src/librustc/middle/typeck/check/mod.rs

+93-84
Original file line numberDiff line numberDiff line change
@@ -813,24 +813,26 @@ fn compare_impl_method(tcx: ty::ctxt,
813813
if num_impl_m_type_params != num_trait_m_type_params {
814814
tcx.sess.span_err(
815815
impl_m_span,
816-
format!("method `{}` has {} type parameter(s), but its trait \
817-
declaration has {} type parameter(s)",
818-
token::get_ident(trait_m.ident),
819-
num_impl_m_type_params,
820-
num_trait_m_type_params));
816+
format!("method `{method}` has {nimpl, plural, =1{# type parameter} \
817+
other{# type parameters}}, \
818+
but its trait declaration has {ntrait, plural, =1{# type parameter} \
819+
other{# type parameters}}",
820+
method = token::get_ident(trait_m.ident),
821+
nimpl = num_impl_m_type_params,
822+
ntrait = num_trait_m_type_params));
821823
return;
822824
}
823825

824826
if impl_m.fty.sig.inputs.len() != trait_m.fty.sig.inputs.len() {
825827
tcx.sess.span_err(
826828
impl_m_span,
827-
format!("method `{}` has {} parameter{} \
828-
but the declaration in trait `{}` has {}",
829-
token::get_ident(trait_m.ident),
830-
impl_m.fty.sig.inputs.len(),
831-
if impl_m.fty.sig.inputs.len() == 1 { "" } else { "s" },
832-
ty::item_path_str(tcx, trait_m.def_id),
833-
trait_m.fty.sig.inputs.len()));
829+
format!("method `{method}` has {nimpl, plural, =1{# parameter} \
830+
other{# parameters}} \
831+
but the declaration in trait `{trait}` has {ntrait}",
832+
method = token::get_ident(trait_m.ident),
833+
nimpl = impl_m.fty.sig.inputs.len(),
834+
trait = ty::item_path_str(tcx, trait_m.def_id),
835+
ntrait = trait_m.fty.sig.inputs.len()));
834836
return;
835837
}
836838

@@ -865,13 +867,16 @@ fn compare_impl_method(tcx: ty::ctxt,
865867
{
866868
tcx.sess.span_err(
867869
impl_m_span,
868-
format!("in method `{}`, \
869-
type parameter {} has {} trait bound(s), but the \
870-
corresponding type parameter in \
871-
the trait declaration has {} trait bound(s)",
872-
token::get_ident(trait_m.ident),
873-
i, impl_param_def.bounds.trait_bounds.len(),
874-
trait_param_def.bounds.trait_bounds.len()));
870+
format!("in method `{method}`, \
871+
type parameter {typaram} has \
872+
{nimpl, plural, =1{# trait bound} other{# trait bounds}}, \
873+
but the corresponding type parameter in \
874+
the trait declaration has \
875+
{ntrait, plural, =1{# trait bound} other{# trait bounds}}",
876+
method = token::get_ident(trait_m.ident),
877+
typaram = i,
878+
nimpl = impl_param_def.bounds.trait_bounds.len(),
879+
ntrait = trait_param_def.bounds.trait_bounds.len()));
875880
return;
876881
}
877882
}
@@ -1507,10 +1512,12 @@ fn check_type_parameter_positions_in_path(function_context: @FnCtxt,
15071512
function_context.tcx()
15081513
.sess
15091514
.span_err(path.span,
1510-
format!("expected {} lifetime parameter(s), \
1511-
found {} lifetime parameter(s)",
1512-
trait_region_parameter_count,
1513-
supplied_region_parameter_count));
1515+
format!("expected {nexpected, plural, =1{# lifetime parameter} \
1516+
other{# lifetime parameters}}, \
1517+
found {nsupplied, plural, =1{# lifetime parameter} \
1518+
other{# lifetime parameters}}",
1519+
nexpected = trait_region_parameter_count,
1520+
nsupplied = supplied_region_parameter_count));
15141521
}
15151522

15161523
// Make sure the number of type parameters supplied on the trait
@@ -1522,49 +1529,47 @@ fn check_type_parameter_positions_in_path(function_context: @FnCtxt,
15221529
.len();
15231530
let supplied_ty_param_count = trait_segment.types.len();
15241531
if supplied_ty_param_count < required_ty_param_count {
1525-
let trait_count_suffix = if required_ty_param_count == 1 {
1526-
""
1527-
} else {
1528-
"s"
1529-
};
1530-
let supplied_count_suffix = if supplied_ty_param_count == 1 {
1531-
""
1532+
let msg = if required_ty_param_count < generics.type_param_defs().len() {
1533+
format!("the {trait_or_impl} referenced by this path needs at least \
1534+
{nexpected, plural, =1{# type parameter} \
1535+
other{# type parameters}}, \
1536+
but {nsupplied, plural, =1{# type parameter} \
1537+
other{# type parameters}} were supplied",
1538+
trait_or_impl = name,
1539+
nexpected = required_ty_param_count,
1540+
nsupplied = supplied_ty_param_count)
15321541
} else {
1533-
"s"
1542+
format!("the {trait_or_impl} referenced by this path needs \
1543+
{nexpected, plural, =1{# type parameter} \
1544+
other{# type parameters}}, \
1545+
but {nsupplied, plural, =1{# type parameter} \
1546+
other{# type parameters}} were supplied",
1547+
trait_or_impl = name,
1548+
nexpected = required_ty_param_count,
1549+
nsupplied = supplied_ty_param_count)
15341550
};
1535-
let needs = if required_ty_param_count < generics.type_param_defs().len() {
1536-
"needs at least"
1537-
} else {
1538-
"needs"
1539-
};
1540-
function_context.tcx().sess.span_err(path.span,
1541-
format!("the {} referenced by this path {} {} type \
1542-
parameter{}, but {} type parameter{} were supplied",
1543-
name, needs,
1544-
required_ty_param_count, trait_count_suffix,
1545-
supplied_ty_param_count, supplied_count_suffix))
1551+
function_context.tcx().sess.span_err(path.span, msg)
15461552
} else if supplied_ty_param_count > formal_ty_param_count {
1547-
let trait_count_suffix = if formal_ty_param_count == 1 {
1548-
""
1553+
let msg = if required_ty_param_count < generics.type_param_defs().len() {
1554+
format!("the {trait_or_impl} referenced by this path needs at most \
1555+
{nexpected, plural, =1{# type parameter} \
1556+
other{# type parameters}}, \
1557+
but {nsupplied, plural, =1{# type parameter} \
1558+
other{# type parameters}} were supplied",
1559+
trait_or_impl = name,
1560+
nexpected = formal_ty_param_count,
1561+
nsupplied = supplied_ty_param_count)
15491562
} else {
1550-
"s"
1563+
format!("the {trait_or_impl} referenced by this path needs \
1564+
{nexpected, plural, =1{# type parameter} \
1565+
other{# type parameters}}, \
1566+
but {nsupplied, plural, =1{# type parameter} \
1567+
other{# type parameters}} were supplied",
1568+
trait_or_impl = name,
1569+
nexpected = formal_ty_param_count,
1570+
nsupplied = supplied_ty_param_count)
15511571
};
1552-
let supplied_count_suffix = if supplied_ty_param_count == 1 {
1553-
""
1554-
} else {
1555-
"s"
1556-
};
1557-
let needs = if required_ty_param_count < generics.type_param_defs().len() {
1558-
"needs at most"
1559-
} else {
1560-
"needs"
1561-
};
1562-
function_context.tcx().sess.span_err(path.span,
1563-
format!("the {} referenced by this path {} {} type \
1564-
parameter{}, but {} type parameter{} were supplied",
1565-
name, needs,
1566-
formal_ty_param_count, trait_count_suffix,
1567-
supplied_ty_param_count, supplied_count_suffix))
1572+
function_context.tcx().sess.span_err(path.span, msg)
15681573
}
15691574
}
15701575
_ => {
@@ -1665,24 +1670,25 @@ pub fn check_expr_with_unifier(fcx: @FnCtxt,
16651670
fn_inputs.map(|a| *a)
16661671
} else {
16671672
let msg = format!(
1668-
"this function takes at least {} parameter{} \
1669-
but {} parameter{} supplied",
1670-
expected_arg_count,
1671-
if expected_arg_count == 1 {""} else {"s"},
1672-
supplied_arg_count,
1673-
if supplied_arg_count == 1 {" was"} else {"s were"});
1673+
"this function takes at least {nexpected, plural, =1{# parameter} \
1674+
other{# parameters}} \
1675+
but {nsupplied, plural, =1{# parameter was} \
1676+
other{# parameters were}} supplied",
1677+
nexpected = expected_arg_count,
1678+
nsupplied = supplied_arg_count);
16741679

16751680
tcx.sess.span_err(sp, msg);
16761681

16771682
err_args(supplied_arg_count)
16781683
}
16791684
} else {
16801685
let msg = format!(
1681-
"this function takes {} parameter{} \
1682-
but {} parameter{} supplied",
1683-
expected_arg_count, if expected_arg_count == 1 {""} else {"s"},
1684-
supplied_arg_count,
1685-
if supplied_arg_count == 1 {" was"} else {"s were"});
1686+
"this function takes {nexpected, plural, =1{# parameter} \
1687+
other{# parameters}} \
1688+
but {nsupplied, plural, =1{# parameter was} \
1689+
other{# parameters were}} supplied",
1690+
nexpected = expected_arg_count,
1691+
nsupplied = supplied_arg_count);
16861692

16871693
tcx.sess.span_err(sp, msg);
16881694

@@ -2384,13 +2390,9 @@ pub fn check_expr_with_unifier(fcx: @FnCtxt,
23842390
}
23852391

23862392
tcx.sess.span_err(span,
2387-
format!("missing field{}: {}",
2388-
if missing_fields.len() == 1 {
2389-
""
2390-
} else {
2391-
"s"
2392-
},
2393-
missing_fields.connect(", ")));
2393+
format!("missing {nfields, plural, =1{field} other{fields}}: {fields}",
2394+
nfields = missing_fields.len(),
2395+
fields = missing_fields.connect(", ")));
23942396
}
23952397
}
23962398

@@ -3556,8 +3558,12 @@ pub fn check_enum_variants(ccx: @CrateCtxt,
35563558

35573559
let hint = ty::lookup_repr_hint(ccx.tcx, ast::DefId { krate: ast::LOCAL_CRATE, node: id });
35583560
if hint != attr::ReprAny && vs.len() <= 1 {
3559-
ccx.tcx.sess.span_err(sp, format!("unsupported representation for {}variant enum",
3560-
if vs.len() == 1 { "uni" } else { "zero-" }))
3561+
let msg = if vs.len() == 1 {
3562+
"unsupported representation for univariant enum"
3563+
} else {
3564+
"unsupported representation for zero-variant enum"
3565+
};
3566+
ccx.tcx.sess.span_err(sp, msg)
35613567
}
35623568

35633569
let variants = do_check(ccx, vs, id, hint);
@@ -3666,9 +3672,12 @@ pub fn instantiate_path(fcx: @FnCtxt,
36663672
if num_supplied_regions != 0 {
36673673
fcx.ccx.tcx.sess.span_err(
36683674
span,
3669-
format!("expected {} lifetime parameter(s), \
3670-
found {} lifetime parameter(s)",
3671-
num_expected_regions, num_supplied_regions));
3675+
format!("expected {nexpected, plural, =1{# lifetime parameter} \
3676+
other{# lifetime parameters}}, \
3677+
found {nsupplied, plural, =1{# lifetime parameter} \
3678+
other{# lifetime parameters}}",
3679+
nexpected = num_expected_regions,
3680+
nsupplied = num_supplied_regions));
36723681
}
36733682

36743683
opt_vec::from(fcx.infcx().next_region_vars(

src/test/compile-fail/bad-mid-path-type-params.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ impl Trait<int> for S2 {
4343
fn foo<'a>() {
4444
let _ = S::new::<int,f64>(1, 1.0);
4545
//~^ ERROR the impl referenced by this path needs 1 type parameter, but 0 type parameters were supplied
46-
let _ = S::<'a,int>::new::<f64>(1, 1.0); //~ ERROR expected 0 lifetime parameter(s)
46+
let _ = S::<'a,int>::new::<f64>(1, 1.0); //~ ERROR expected 0 lifetime parameters
4747
let _: S2 = Trait::new::<int,f64>(1, 1.0);
4848
//~^ ERROR the trait referenced by this path needs 1 type parameter, but 0 type parameters were supplied
49-
let _: S2 = Trait::<'a,int>::new::<f64>(1, 1.0); //~ ERROR expected 0 lifetime parameter(s)
49+
let _: S2 = Trait::<'a,int>::new::<f64>(1, 1.0); //~ ERROR expected 0 lifetime parameters
5050
}
5151

5252
fn main() {}

0 commit comments

Comments
 (0)