Skip to content

Commit 5321b35

Browse files
committed
Fix redundant arguments in external_path()
If the path is for a trait, it is always true that `trait_did == Some(did)`, so instead, `external_path()` now takes an `is_trait` boolean.
1 parent c2207f5 commit 5321b35

File tree

3 files changed

+27
-34
lines changed

3 files changed

+27
-34
lines changed

Diff for: src/librustdoc/clean/mod.rs

+6-12
Original file line numberDiff line numberDiff line change
@@ -164,14 +164,8 @@ impl Clean<Type> for (ty::TraitRef<'_>, &[TypeBinding]) {
164164
);
165165
}
166166
inline::record_extern_fqn(cx, trait_ref.def_id, kind);
167-
let path = external_path(
168-
cx,
169-
trait_ref.def_id,
170-
Some(trait_ref.def_id),
171-
true,
172-
bounds.to_vec(),
173-
trait_ref.substs,
174-
);
167+
let path =
168+
external_path(cx, trait_ref.def_id, true, true, bounds.to_vec(), trait_ref.substs);
175169

176170
debug!("ty::TraitRef\n subst: {:?}\n", trait_ref.substs);
177171

@@ -1448,12 +1442,12 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
14481442
AdtKind::Enum => ItemType::Enum,
14491443
};
14501444
inline::record_extern_fqn(cx, did, kind);
1451-
let path = external_path(cx, did, None, false, vec![], substs);
1445+
let path = external_path(cx, did, false, false, vec![], substs);
14521446
ResolvedPath { path, did, is_generic: false }
14531447
}
14541448
ty::Foreign(did) => {
14551449
inline::record_extern_fqn(cx, did, ItemType::ForeignType);
1456-
let path = external_path(cx, did, None, false, vec![], InternalSubsts::empty());
1450+
let path = external_path(cx, did, false, false, vec![], InternalSubsts::empty());
14571451
ResolvedPath { path, did, is_generic: false }
14581452
}
14591453
ty::Dynamic(ref obj, ref reg) => {
@@ -1477,7 +1471,7 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
14771471

14781472
for did in dids {
14791473
let empty = cx.tcx.intern_substs(&[]);
1480-
let path = external_path(cx, did, Some(did), false, vec![], empty);
1474+
let path = external_path(cx, did, true, false, vec![], empty);
14811475
inline::record_extern_fqn(cx, did, ItemType::Trait);
14821476
let bound = PolyTrait {
14831477
trait_: ResolvedPath { path, did, is_generic: false },
@@ -1494,7 +1488,7 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
14941488
});
14951489
}
14961490

1497-
let path = external_path(cx, did, Some(did), false, bindings, substs);
1491+
let path = external_path(cx, did, true, false, bindings, substs);
14981492
bounds.insert(
14991493
0,
15001494
PolyTrait {

Diff for: src/librustdoc/clean/types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1158,7 +1158,7 @@ impl GenericBound {
11581158
crate fn maybe_sized(cx: &mut DocContext<'_>) -> GenericBound {
11591159
let did = cx.tcx.require_lang_item(LangItem::Sized, None);
11601160
let empty = cx.tcx.intern_substs(&[]);
1161-
let path = external_path(cx, did, Some(did), false, vec![], empty);
1161+
let path = external_path(cx, did, true, false, vec![], empty);
11621162
inline::record_extern_fqn(cx, did, ItemType::Trait);
11631163
GenericBound::TraitBound(
11641164
PolyTrait {

Diff for: src/librustdoc/clean/utils.rs

+20-21
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ crate fn krate(cx: &mut DocContext<'_>) -> Crate {
9393

9494
fn external_generic_args(
9595
cx: &mut DocContext<'_>,
96-
trait_did: Option<DefId>,
96+
did: DefId,
97+
is_trait: bool,
9798
has_self: bool,
9899
bindings: Vec<TypeBinding>,
99100
substs: SubstsRef<'_>,
@@ -121,32 +122,30 @@ fn external_generic_args(
121122
})
122123
.collect();
123124

124-
match trait_did {
125-
// Attempt to sugar an external path like Fn<(A, B,), C> to Fn(A, B) -> C
126-
Some(did) if cx.tcx.fn_trait_kind_from_lang_item(did).is_some() => {
127-
assert!(ty_kind.is_some());
128-
let inputs = match ty_kind {
129-
Some(ty::Tuple(ref tys)) => tys.iter().map(|t| t.expect_ty().clean(cx)).collect(),
130-
_ => return GenericArgs::AngleBracketed { args, bindings },
131-
};
132-
let output = None;
133-
// FIXME(#20299) return type comes from a projection now
134-
// match types[1].kind {
135-
// ty::Tuple(ref v) if v.is_empty() => None, // -> ()
136-
// _ => Some(types[1].clean(cx))
137-
// };
138-
GenericArgs::Parenthesized { inputs, output }
139-
}
140-
_ => GenericArgs::AngleBracketed { args, bindings },
125+
if is_trait && cx.tcx.fn_trait_kind_from_lang_item(did).is_some() {
126+
assert!(ty_kind.is_some());
127+
let inputs = match ty_kind {
128+
Some(ty::Tuple(ref tys)) => tys.iter().map(|t| t.expect_ty().clean(cx)).collect(),
129+
_ => return GenericArgs::AngleBracketed { args, bindings },
130+
};
131+
let output = None;
132+
// FIXME(#20299) return type comes from a projection now
133+
// match types[1].kind {
134+
// ty::Tuple(ref v) if v.is_empty() => None, // -> ()
135+
// _ => Some(types[1].clean(cx))
136+
// };
137+
GenericArgs::Parenthesized { inputs, output }
138+
} else {
139+
GenericArgs::AngleBracketed { args, bindings }
141140
}
142141
}
143142

144-
/// trait_did should be set to a trait's DefId if called on a TraitRef, in order to sugar
143+
/// `is_trait` should be set to `true` if called on a `TraitRef`, in order to sugar
145144
/// from `Fn<(A, B,), C>` to `Fn(A, B) -> C`
146145
pub(super) fn external_path(
147146
cx: &mut DocContext<'_>,
148147
did: DefId,
149-
trait_did: Option<DefId>,
148+
is_trait: bool,
150149
has_self: bool,
151150
bindings: Vec<TypeBinding>,
152151
substs: SubstsRef<'_>,
@@ -158,7 +157,7 @@ pub(super) fn external_path(
158157
res: Res::Def(def_kind, did),
159158
segments: vec![PathSegment {
160159
name,
161-
args: external_generic_args(cx, trait_did, has_self, bindings, substs),
160+
args: external_generic_args(cx, did, is_trait, has_self, bindings, substs),
162161
}],
163162
}
164163
}

0 commit comments

Comments
 (0)