Skip to content

Commit

Permalink
Fix redundant arguments in external_path()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
camelid committed Sep 11, 2021
1 parent c2207f5 commit 5321b35
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 34 deletions.
18 changes: 6 additions & 12 deletions src/librustdoc/clean/mod.rs
Expand Up @@ -164,14 +164,8 @@ impl Clean<Type> for (ty::TraitRef<'_>, &[TypeBinding]) {
);
}
inline::record_extern_fqn(cx, trait_ref.def_id, kind);
let path = external_path(
cx,
trait_ref.def_id,
Some(trait_ref.def_id),
true,
bounds.to_vec(),
trait_ref.substs,
);
let path =
external_path(cx, trait_ref.def_id, true, true, bounds.to_vec(), trait_ref.substs);

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

Expand Down Expand Up @@ -1448,12 +1442,12 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
AdtKind::Enum => ItemType::Enum,
};
inline::record_extern_fqn(cx, did, kind);
let path = external_path(cx, did, None, false, vec![], substs);
let path = external_path(cx, did, false, false, vec![], substs);
ResolvedPath { path, did, is_generic: false }
}
ty::Foreign(did) => {
inline::record_extern_fqn(cx, did, ItemType::ForeignType);
let path = external_path(cx, did, None, false, vec![], InternalSubsts::empty());
let path = external_path(cx, did, false, false, vec![], InternalSubsts::empty());
ResolvedPath { path, did, is_generic: false }
}
ty::Dynamic(ref obj, ref reg) => {
Expand All @@ -1477,7 +1471,7 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {

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

let path = external_path(cx, did, Some(did), false, bindings, substs);
let path = external_path(cx, did, true, false, bindings, substs);
bounds.insert(
0,
PolyTrait {
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/clean/types.rs
Expand Up @@ -1158,7 +1158,7 @@ impl GenericBound {
crate fn maybe_sized(cx: &mut DocContext<'_>) -> GenericBound {
let did = cx.tcx.require_lang_item(LangItem::Sized, None);
let empty = cx.tcx.intern_substs(&[]);
let path = external_path(cx, did, Some(did), false, vec![], empty);
let path = external_path(cx, did, true, false, vec![], empty);
inline::record_extern_fqn(cx, did, ItemType::Trait);
GenericBound::TraitBound(
PolyTrait {
Expand Down
41 changes: 20 additions & 21 deletions src/librustdoc/clean/utils.rs
Expand Up @@ -93,7 +93,8 @@ crate fn krate(cx: &mut DocContext<'_>) -> Crate {

fn external_generic_args(
cx: &mut DocContext<'_>,
trait_did: Option<DefId>,
did: DefId,
is_trait: bool,
has_self: bool,
bindings: Vec<TypeBinding>,
substs: SubstsRef<'_>,
Expand Down Expand Up @@ -121,32 +122,30 @@ fn external_generic_args(
})
.collect();

match trait_did {
// Attempt to sugar an external path like Fn<(A, B,), C> to Fn(A, B) -> C
Some(did) if cx.tcx.fn_trait_kind_from_lang_item(did).is_some() => {
assert!(ty_kind.is_some());
let inputs = match ty_kind {
Some(ty::Tuple(ref tys)) => tys.iter().map(|t| t.expect_ty().clean(cx)).collect(),
_ => return GenericArgs::AngleBracketed { args, bindings },
};
let output = None;
// FIXME(#20299) return type comes from a projection now
// match types[1].kind {
// ty::Tuple(ref v) if v.is_empty() => None, // -> ()
// _ => Some(types[1].clean(cx))
// };
GenericArgs::Parenthesized { inputs, output }
}
_ => GenericArgs::AngleBracketed { args, bindings },
if is_trait && cx.tcx.fn_trait_kind_from_lang_item(did).is_some() {
assert!(ty_kind.is_some());
let inputs = match ty_kind {
Some(ty::Tuple(ref tys)) => tys.iter().map(|t| t.expect_ty().clean(cx)).collect(),
_ => return GenericArgs::AngleBracketed { args, bindings },
};
let output = None;
// FIXME(#20299) return type comes from a projection now
// match types[1].kind {
// ty::Tuple(ref v) if v.is_empty() => None, // -> ()
// _ => Some(types[1].clean(cx))
// };
GenericArgs::Parenthesized { inputs, output }
} else {
GenericArgs::AngleBracketed { args, bindings }
}
}

/// trait_did should be set to a trait's DefId if called on a TraitRef, in order to sugar
/// `is_trait` should be set to `true` if called on a `TraitRef`, in order to sugar
/// from `Fn<(A, B,), C>` to `Fn(A, B) -> C`
pub(super) fn external_path(
cx: &mut DocContext<'_>,
did: DefId,
trait_did: Option<DefId>,
is_trait: bool,
has_self: bool,
bindings: Vec<TypeBinding>,
substs: SubstsRef<'_>,
Expand All @@ -158,7 +157,7 @@ pub(super) fn external_path(
res: Res::Def(def_kind, did),
segments: vec![PathSegment {
name,
args: external_generic_args(cx, trait_did, has_self, bindings, substs),
args: external_generic_args(cx, did, is_trait, has_self, bindings, substs),
}],
}
}
Expand Down

0 comments on commit 5321b35

Please sign in to comment.