Skip to content

Commit 570325d

Browse files
committed
Use the sugary syntax to print the Fn traits in error messages
1 parent 92e9e70 commit 570325d

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

src/librustc/util/ppaux.rs

+17-8
Original file line numberDiff line numberDiff line change
@@ -428,17 +428,19 @@ pub fn ty_to_string<'tcx>(cx: &ctxt<'tcx>, typ: &ty::TyS<'tcx>) -> String {
428428
ty_enum(did, ref substs) | ty_struct(did, ref substs) => {
429429
let base = ty::item_path_str(cx, did);
430430
let generics = ty::lookup_item_type(cx, did).generics;
431-
parameterized(cx, base.as_slice(), substs, &generics)
431+
parameterized(cx, base.as_slice(), substs, &generics, did)
432432
}
433433
ty_trait(box ty::TyTrait {
434434
ref principal, ref bounds
435435
}) => {
436436
let base = ty::item_path_str(cx, principal.def_id);
437437
let trait_def = ty::lookup_trait_def(cx, principal.def_id);
438+
let did = trait_def.trait_ref.def_id;
438439
let ty = parameterized(cx, base.as_slice(),
439-
&principal.substs, &trait_def.generics);
440+
&principal.substs, &trait_def.generics,
441+
did);
440442
let bound_str = bounds.user_string(cx);
441-
let bound_sep = if bound_str.is_empty() { "" } else { "+" };
443+
let bound_sep = if bound_str.is_empty() { "" } else { " + " };
442444
format!("{}{}{}",
443445
ty,
444446
bound_sep,
@@ -484,7 +486,8 @@ pub fn explicit_self_category_to_str(category: &ty::ExplicitSelfCategory)
484486
pub fn parameterized<'tcx>(cx: &ctxt<'tcx>,
485487
base: &str,
486488
substs: &subst::Substs<'tcx>,
487-
generics: &ty::Generics<'tcx>)
489+
generics: &ty::Generics<'tcx>,
490+
did: ast::DefId)
488491
-> String
489492
{
490493
if cx.sess.verbose() {
@@ -537,7 +540,12 @@ pub fn parameterized<'tcx>(cx: &ctxt<'tcx>,
537540
strs.push(ty_to_string(cx, *t))
538541
}
539542

540-
if strs.len() > 0u {
543+
if cx.lang_items.fn_trait_kind(did).is_some() {
544+
format!("{}({}){}",
545+
base,
546+
strs[0][1 .. strs[0].len() - (strs[0].ends_with(",)") as uint+1)],
547+
if &*strs[1] == "()" { String::new() } else { format!(" -> {}", strs[1]) })
548+
} else if strs.len() > 0 {
541549
format!("{}<{}>", base, strs.connect(", "))
542550
} else {
543551
format!("{}", base)
@@ -743,7 +751,7 @@ impl<'tcx> Repr<'tcx> for ty::TraitRef<'tcx> {
743751
let trait_def = ty::lookup_trait_def(tcx, self.def_id);
744752
format!("<{} : {}>",
745753
self.substs.self_ty().repr(tcx),
746-
parameterized(tcx, base.as_slice(), &self.substs, &trait_def.generics))
754+
parameterized(tcx, base.as_slice(), &self.substs, &trait_def.generics, self.def_id))
747755
}
748756
}
749757

@@ -1116,7 +1124,7 @@ impl<'tcx> UserString<'tcx> for ty::ParamBounds<'tcx> {
11161124
for n in self.trait_bounds.iter() {
11171125
result.push(n.user_string(tcx));
11181126
}
1119-
result.connect("+")
1127+
result.connect(" + ")
11201128
}
11211129
}
11221130

@@ -1189,7 +1197,8 @@ impl<'tcx> UserString<'tcx> for ty::TraitRef<'tcx> {
11891197
};
11901198

11911199
let trait_def = ty::lookup_trait_def(tcx, self.def_id);
1192-
parameterized(tcx, base.as_slice(), &trait_ref.substs, &trait_def.generics)
1200+
let did = trait_def.trait_ref.def_id;
1201+
parameterized(tcx, base.as_slice(), &trait_ref.substs, &trait_def.generics, did)
11931202
}
11941203
}
11951204

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(unboxed_closures)]
12+
13+
fn needs_fn<F>(x: F) where F: Fn(int) -> int {}
14+
15+
fn main() {
16+
let _: () = (box |:_: int| {}) as Box<FnOnce(int)>; //~ ERROR object-safe
17+
//~^ ERROR Box<core::ops::FnOnce(int)>
18+
let _: () = (box |&:_: int, int| {}) as Box<Fn(int, int)>;
19+
//~^ ERROR Box<core::ops::Fn(int, int)>
20+
let _: () = (box |&mut:| -> int unimplemented!()) as Box<FnMut() -> int>;
21+
//~^ ERROR Box<core::ops::FnMut() -> int>
22+
23+
needs_fn(1i); //~ ERROR `core::ops::Fn(int) -> int`
24+
}

0 commit comments

Comments
 (0)