rollup merge of #19912: P1start/fn-formatting

This is to encourage the use of the sugary syntax instead of the `<>` syntax, which will not be usable post-1.0. Rustdoc [still uses the `<>` syntax](https://github.com/rust-lang/rust/issues/19909), so if a rustdoc wizard is looking for something to do, it would be nice to use the parenthetical syntax there as well. (I tried to patch rustdoc as well, but failed…)
This commit is contained in:
Alex Crichton 2014-12-17 08:35:26 -08:00
commit b496adaefb
2 changed files with 41 additions and 8 deletions

View File

@ -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,
@ -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() {
@ -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)
@ -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))
}
}
@ -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(" + ")
}
}
@ -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)
}
}

View File

@ -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`
}