Skip to content

Use the sugary syntax to print the Fn traits in error messages #19912

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 17, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions src/librustc/util/ppaux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,17 +428,19 @@ pub fn ty_to_string<'tcx>(cx: &ctxt<'tcx>, typ: &ty::TyS<'tcx>) -> String {
ty_enum(did, ref substs) | ty_struct(did, ref substs) => {
let base = ty::item_path_str(cx, did);
let generics = ty::lookup_item_type(cx, did).generics;
parameterized(cx, base.as_slice(), substs, &generics)
parameterized(cx, base.as_slice(), substs, &generics, did)
}
ty_trait(box ty::TyTrait {
ref principal, ref bounds
}) => {
let base = ty::item_path_str(cx, principal.def_id);
let trait_def = ty::lookup_trait_def(cx, principal.def_id);
let did = trait_def.trait_ref.def_id;
let ty = parameterized(cx, base.as_slice(),
&principal.substs, &trait_def.generics);
&principal.substs, &trait_def.generics,
did);
let bound_str = bounds.user_string(cx);
let bound_sep = if bound_str.is_empty() { "" } else { "+" };
let bound_sep = if bound_str.is_empty() { "" } else { " + " };
format!("{}{}{}",
ty,
bound_sep,
Expand Down Expand Up @@ -484,7 +486,8 @@ pub fn explicit_self_category_to_str(category: &ty::ExplicitSelfCategory)
pub fn parameterized<'tcx>(cx: &ctxt<'tcx>,
base: &str,
substs: &subst::Substs<'tcx>,
generics: &ty::Generics<'tcx>)
generics: &ty::Generics<'tcx>,
did: ast::DefId)
-> String
{
if cx.sess.verbose() {
Expand Down Expand Up @@ -537,7 +540,12 @@ pub fn parameterized<'tcx>(cx: &ctxt<'tcx>,
strs.push(ty_to_string(cx, *t))
}

if strs.len() > 0u {
if cx.lang_items.fn_trait_kind(did).is_some() {
format!("{}({}){}",
base,
strs[0][1 .. strs[0].len() - (strs[0].ends_with(",)") as uint+1)],
if &*strs[1] == "()" { String::new() } else { format!(" -> {}", strs[1]) })
} else if strs.len() > 0 {
format!("{}<{}>", base, strs.connect(", "))
} else {
format!("{}", base)
Expand Down Expand Up @@ -743,7 +751,7 @@ impl<'tcx> Repr<'tcx> for ty::TraitRef<'tcx> {
let trait_def = ty::lookup_trait_def(tcx, self.def_id);
format!("<{} : {}>",
self.substs.self_ty().repr(tcx),
parameterized(tcx, base.as_slice(), &self.substs, &trait_def.generics))
parameterized(tcx, base.as_slice(), &self.substs, &trait_def.generics, self.def_id))
}
}

Expand Down Expand Up @@ -1116,7 +1124,7 @@ impl<'tcx> UserString<'tcx> for ty::ParamBounds<'tcx> {
for n in self.trait_bounds.iter() {
result.push(n.user_string(tcx));
}
result.connect("+")
result.connect(" + ")
}
}

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

let trait_def = ty::lookup_trait_def(tcx, self.def_id);
parameterized(tcx, base.as_slice(), &trait_ref.substs, &trait_def.generics)
let did = trait_def.trait_ref.def_id;
parameterized(tcx, base.as_slice(), &trait_ref.substs, &trait_def.generics, did)
}
}

Expand Down
24 changes: 24 additions & 0 deletions src/test/compile-fail/fn-trait-formatting.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(unboxed_closures)]

fn needs_fn<F>(x: F) where F: Fn(int) -> int {}

fn main() {
let _: () = (box |:_: int| {}) as Box<FnOnce(int)>; //~ ERROR object-safe
//~^ ERROR Box<core::ops::FnOnce(int)>
let _: () = (box |&:_: int, int| {}) as Box<Fn(int, int)>;
//~^ ERROR Box<core::ops::Fn(int, int)>
let _: () = (box |&mut:| -> int unimplemented!()) as Box<FnMut() -> int>;
//~^ ERROR Box<core::ops::FnMut() -> int>

needs_fn(1i); //~ ERROR `core::ops::Fn(int) -> int`
}