Skip to content

Commit b434d95

Browse files
committed
Clean up clean::GenericArgs::AngleBracketed
1 parent 227b6a3 commit b434d95

File tree

3 files changed

+73
-85
lines changed

3 files changed

+73
-85
lines changed

src/librustdoc/clean/auto_trait.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,13 @@ impl<'a, 'tcx, 'rcx, 'cstore> AutoTraitFinder<'a, 'tcx, 'rcx, 'cstore> {
452452
.expect("segments were empty");
453453

454454
let (old_input, old_output) = match last_segment.args {
455-
GenericArgs::AngleBracketed { types, .. } => (types, None),
455+
GenericArgs::AngleBracketed { args, .. } => {
456+
let types = args.iter().filter_map(|arg| match arg {
457+
GenericArg::Type(ty) => Some(ty.clone()),
458+
_ => None,
459+
}).collect();
460+
(types, None)
461+
}
456462
GenericArgs::Parenthesized { inputs, output, .. } => {
457463
(inputs, output)
458464
}

src/librustdoc/clean/mod.rs

Lines changed: 61 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ use rustc::middle::resolve_lifetime as rl;
3333
use rustc::ty::fold::TypeFolder;
3434
use rustc::middle::lang_items;
3535
use rustc::mir::interpret::GlobalId;
36-
use rustc::hir::{self, GenericArg, HirVec};
36+
use rustc::hir::{self, HirVec};
3737
use rustc::hir::def::{self, Def, CtorKind};
3838
use rustc::hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
39-
use rustc::ty::subst::Substs;
39+
use rustc::ty::subst::{Substs, UnpackedKind};
4040
use rustc::ty::{self, TyCtxt, Region, RegionVid, Ty, AdtKind};
4141
use rustc::middle::stability;
4242
use rustc::util::nodemap::{FxHashMap, FxHashSet};
@@ -1069,43 +1069,41 @@ impl Clean<GenericBound> for hir::GenericBound {
10691069

10701070
fn external_generic_args(cx: &DocContext, trait_did: Option<DefId>, has_self: bool,
10711071
bindings: Vec<TypeBinding>, substs: &Substs) -> GenericArgs {
1072-
let lifetimes = substs.regions().filter_map(|lt| lt.clean(cx)).collect();
1073-
let types = substs.types().skip(has_self as usize).collect::<Vec<_>>();
1074-
let consts = substs.consts().map(|ct| ct.clean(cx)).collect();
1072+
let mut skip_self = has_self;
1073+
let mut first_ty_sty = None;
1074+
let args: Vec<_> = substs.iter().filter_map(|kind| match kind.unpack() {
1075+
UnpackedKind::Lifetime(lt) => {
1076+
lt.clean(cx).and_then(|lt| Some(GenericArg::Lifetime(lt)))
1077+
}
1078+
UnpackedKind::Type(_) if skip_self => {
1079+
skip_self = false;
1080+
None
1081+
}
1082+
UnpackedKind::Type(ty) => {
1083+
first_ty_sty = Some(&ty.sty);
1084+
Some(GenericArg::Type(ty.clean(cx)))
1085+
}
1086+
UnpackedKind::Const(ct) => Some(GenericArg::Const(ct.clean(cx))),
1087+
}).collect();
10751088

10761089
match trait_did {
10771090
// Attempt to sugar an external path like Fn<(A, B,), C> to Fn(A, B) -> C
10781091
Some(did) if cx.tcx.lang_items().fn_trait_kind(did).is_some() => {
10791092
assert_eq!(types.len(), 1);
10801093
let inputs = match types[0].sty {
10811094
ty::Tuple(ref tys) => tys.iter().map(|t| t.clean(cx)).collect(),
1082-
_ => {
1083-
return GenericArgs::AngleBracketed {
1084-
lifetimes,
1085-
types: types.clean(cx),
1086-
consts,
1087-
bindings,
1088-
}
1089-
}
1095+
_ => return GenericArgs::AngleBracketed { args, bindings },
10901096
};
10911097
let output = None;
10921098
// FIXME(#20299) return type comes from a projection now
10931099
// match types[1].sty {
10941100
// ty::Tuple(ref v) if v.is_empty() => None, // -> ()
10951101
// _ => Some(types[1].clean(cx))
10961102
// };
1097-
GenericArgs::Parenthesized {
1098-
inputs,
1099-
output,
1100-
}
1103+
GenericArgs::Parenthesized { inputs, output }
11011104
},
11021105
_ => {
1103-
GenericArgs::AngleBracketed {
1104-
lifetimes,
1105-
types: types.clean(cx),
1106-
consts,
1107-
bindings,
1108-
}
1106+
GenericArgs::AngleBracketed { args, bindings }
11091107
}
11101108
}
11111109
}
@@ -2260,12 +2258,15 @@ impl Type {
22602258
}
22612259
}
22622260

2263-
pub fn generics(&self) -> Option<&[Type]> {
2261+
pub fn generics(&self) -> Option<Vec<Type>> {
22642262
match *self {
22652263
ResolvedPath { ref path, .. } => {
22662264
path.segments.last().and_then(|seg| {
2267-
if let GenericArgs::AngleBracketed { ref types, .. } = seg.args {
2268-
Some(&**types)
2265+
if let GenericArgs::AngleBracketed { ref args, .. } = seg.args {
2266+
Some(args.iter().filter_map(|arg| match arg {
2267+
GenericArg::Type(ty) => Some(ty.clone()),
2268+
_ => None,
2269+
}).collect())
22692270
} else {
22702271
None
22712272
}
@@ -2477,7 +2478,7 @@ impl Clean<Type> for hir::Ty {
24772478
let mut j = 0;
24782479
let lifetime = generic_args.args.iter().find_map(|arg| {
24792480
match arg {
2480-
GenericArg::Lifetime(lt) => {
2481+
hir::GenericArg::Lifetime(lt) => {
24812482
if indices.lifetimes == j {
24822483
return Some(lt);
24832484
}
@@ -2502,7 +2503,7 @@ impl Clean<Type> for hir::Ty {
25022503
let mut j = 0;
25032504
let type_ = generic_args.args.iter().find_map(|arg| {
25042505
match arg {
2505-
GenericArg::Type(ty) => {
2506+
hir::GenericArg::Type(ty) => {
25062507
if indices.types == j {
25072508
return Some(ty);
25082509
}
@@ -2525,7 +2526,7 @@ impl Clean<Type> for hir::Ty {
25252526
let mut j = 0;
25262527
let const_ = generic_args.args.iter().find_map(|arg| {
25272528
match arg {
2528-
GenericArg::Const(ct) => {
2529+
hir::GenericArg::Const(ct) => {
25292530
if indices.consts == j {
25302531
return Some(ct);
25312532
}
@@ -3117,13 +3118,27 @@ impl Clean<Path> for hir::Path {
31173118
}
31183119
}
31193120

3121+
#[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
3122+
pub enum GenericArg {
3123+
Lifetime(Lifetime),
3124+
Type(Type),
3125+
Const(Constant),
3126+
}
3127+
3128+
impl fmt::Display for GenericArg {
3129+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3130+
match self {
3131+
GenericArg::Lifetime(lt) => lt.fmt(f),
3132+
GenericArg::Type(ty) => ty.fmt(f),
3133+
GenericArg::Const(ct) => ct.fmt(f),
3134+
}
3135+
}
3136+
}
3137+
31203138
#[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
31213139
pub enum GenericArgs {
31223140
AngleBracketed {
3123-
// TODO(const_generics): clean this up
3124-
lifetimes: Vec<Lifetime>,
3125-
types: Vec<Type>,
3126-
consts: Vec<Constant>,
3141+
args: Vec<GenericArg>,
31273142
bindings: Vec<TypeBinding>,
31283143
},
31293144
Parenthesized {
@@ -3141,24 +3156,19 @@ impl Clean<GenericArgs> for hir::GenericArgs {
31413156
output: if output != Type::Tuple(Vec::new()) { Some(output) } else { None }
31423157
}
31433158
} else {
3144-
let (mut lifetimes, mut types, mut consts) = (vec![], vec![], vec![]);
3145-
let mut elided_lifetimes = true;
3146-
for arg in &self.args {
3147-
match arg {
3148-
GenericArg::Lifetime(lt) => {
3149-
if !lt.is_elided() {
3150-
elided_lifetimes = false;
3151-
}
3152-
lifetimes.push(lt.clean(cx));
3153-
}
3154-
GenericArg::Type(ty) => types.push(ty.clean(cx)),
3155-
GenericArg::Const(ct) => consts.push(ct.clean(cx)),
3156-
}
3157-
}
3159+
let elide_lifetimes = self.args.iter().all(|arg| match arg {
3160+
hir::GenericArg::Lifetime(lt) => lt.is_elided(),
3161+
_ => true,
3162+
});
31583163
GenericArgs::AngleBracketed {
3159-
lifetimes: if elided_lifetimes { vec![] } else { lifetimes },
3160-
types,
3161-
consts,
3164+
args: self.args.iter().filter_map(|arg| match arg {
3165+
hir::GenericArg::Lifetime(lt) if !elide_lifetimes => {
3166+
Some(GenericArg::Lifetime(lt.clean(cx)))
3167+
}
3168+
hir::GenericArg::Lifetime(_) => None,
3169+
hir::GenericArg::Type(ty) => Some(GenericArg::Type(ty.clean(cx))),
3170+
hir::GenericArg::Const(ct) => Some(GenericArg::Const(ct.clean(cx))),
3171+
}).collect(),
31623172
bindings: self.bindings.clean(cx),
31633173
}
31643174
}
@@ -3210,9 +3220,7 @@ fn strip_path(path: &Path) -> Path {
32103220
PathSegment {
32113221
name: s.name.clone(),
32123222
args: GenericArgs::AngleBracketed {
3213-
lifetimes: vec![],
3214-
types: vec![],
3215-
consts: vec![],
3223+
args: vec![],
32163224
bindings: vec![],
32173225
}
32183226
}

src/librustdoc/html/format.rs

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -309,50 +309,24 @@ impl fmt::Display for clean::GenericBound {
309309
impl fmt::Display for clean::GenericArgs {
310310
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
311311
match *self {
312-
clean::GenericArgs::AngleBracketed {
313-
ref lifetimes,
314-
ref types,
315-
ref consts,
316-
ref bindings,
317-
} => {
318-
// TODO(const_generics): clean this up
319-
if !lifetimes.is_empty() ||
320-
!types.is_empty() ||
321-
!consts.is_empty() ||
312+
clean::GenericArgs::AngleBracketed { ref args, ref bindings } => {
313+
if !args.is_empty() ||
322314
!bindings.is_empty() {
323315
if f.alternate() {
324316
f.write_str("<")?;
325317
} else {
326318
f.write_str("&lt;")?;
327319
}
328320
let mut comma = false;
329-
for lifetime in lifetimes {
330-
if comma {
331-
f.write_str(", ")?;
332-
}
333-
comma = true;
334-
write!(f, "{}", *lifetime)?;
335-
}
336-
for ty in types {
337-
if comma {
338-
f.write_str(", ")?;
339-
}
340-
comma = true;
341-
if f.alternate() {
342-
write!(f, "{:#}", *ty)?;
343-
} else {
344-
write!(f, "{}", *ty)?;
345-
}
346-
}
347-
for ct in consts {
321+
for arg in args {
348322
if comma {
349323
f.write_str(", ")?;
350324
}
351325
comma = true;
352326
if f.alternate() {
353-
write!(f, "{:#}", *ct)?;
327+
write!(f, "{:#}", *arg)?;
354328
} else {
355-
write!(f, "{}", *ct)?;
329+
write!(f, "{}", *arg)?;
356330
}
357331
}
358332
for binding in bindings {

0 commit comments

Comments
 (0)