Skip to content

Commit

Permalink
Track ParamEnvs properly
Browse files Browse the repository at this point in the history
This uses the same `with_param_env` pattern that late lints use.
Thanks to all the doctree refactors, this was very easy to add.
  • Loading branch information
jyn514 committed Nov 24, 2020
1 parent a192e5d commit 6278daa
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 155 deletions.
318 changes: 166 additions & 152 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1067,63 +1067,68 @@ impl Clean<TypeKind> for hir::def::DefKind {
impl Clean<Item> for hir::TraitItem<'_> {
fn clean(&self, cx: &DocContext<'_>) -> Item {
let local_did = cx.tcx.hir().local_def_id(self.hir_id).to_def_id();
let inner = match self.kind {
hir::TraitItemKind::Const(ref ty, default) => {
AssocConstItem(ty.clean(cx), default.map(|e| print_const_expr(cx, e)))
}
hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Provided(body)) => {
let mut m = (sig, &self.generics, body).clean(cx);
if m.header.constness == hir::Constness::Const
&& is_unstable_const_fn(cx.tcx, local_did).is_some()
{
m.header.constness = hir::Constness::NotConst;
cx.with_param_env(local_did, || {
let inner = match self.kind {
hir::TraitItemKind::Const(ref ty, default) => {
AssocConstItem(ty.clean(cx), default.map(|e| print_const_expr(cx, e)))
}
MethodItem(m, None)
}
hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Required(ref names)) => {
let (generics, decl) = enter_impl_trait(cx, || {
(self.generics.clean(cx), (&*sig.decl, &names[..]).clean(cx))
});
let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
let mut t = Function { header: sig.header, decl, generics, all_types, ret_types };
if t.header.constness == hir::Constness::Const
&& is_unstable_const_fn(cx.tcx, local_did).is_some()
{
t.header.constness = hir::Constness::NotConst;
hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Provided(body)) => {
let mut m = (sig, &self.generics, body).clean(cx);
if m.header.constness == hir::Constness::Const
&& is_unstable_const_fn(cx.tcx, local_did).is_some()
{
m.header.constness = hir::Constness::NotConst;
}
MethodItem(m, None)
}
TyMethodItem(t)
}
hir::TraitItemKind::Type(ref bounds, ref default) => {
AssocTypeItem(bounds.clean(cx), default.clean(cx))
}
};
Item::from_def_id_and_parts(local_did, Some(self.ident.name), inner, cx)
hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Required(ref names)) => {
let (generics, decl) = enter_impl_trait(cx, || {
(self.generics.clean(cx), (&*sig.decl, &names[..]).clean(cx))
});
let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
let mut t =
Function { header: sig.header, decl, generics, all_types, ret_types };
if t.header.constness == hir::Constness::Const
&& is_unstable_const_fn(cx.tcx, local_did).is_some()
{
t.header.constness = hir::Constness::NotConst;
}
TyMethodItem(t)
}
hir::TraitItemKind::Type(ref bounds, ref default) => {
AssocTypeItem(bounds.clean(cx), default.clean(cx))
}
};
Item::from_def_id_and_parts(local_did, Some(self.ident.name), inner, cx)
})
}
}

impl Clean<Item> for hir::ImplItem<'_> {
fn clean(&self, cx: &DocContext<'_>) -> Item {
let local_did = cx.tcx.hir().local_def_id(self.hir_id).to_def_id();
let inner = match self.kind {
hir::ImplItemKind::Const(ref ty, expr) => {
AssocConstItem(ty.clean(cx), Some(print_const_expr(cx, expr)))
}
hir::ImplItemKind::Fn(ref sig, body) => {
let mut m = (sig, &self.generics, body).clean(cx);
if m.header.constness == hir::Constness::Const
&& is_unstable_const_fn(cx.tcx, local_did).is_some()
{
m.header.constness = hir::Constness::NotConst;
cx.with_param_env(local_did, || {
let inner = match self.kind {
hir::ImplItemKind::Const(ref ty, expr) => {
AssocConstItem(ty.clean(cx), Some(print_const_expr(cx, expr)))
}
MethodItem(m, Some(self.defaultness))
}
hir::ImplItemKind::TyAlias(ref ty) => {
let type_ = ty.clean(cx);
let item_type = type_.def_id().and_then(|did| inline::build_ty(cx, did));
TypedefItem(Typedef { type_, generics: Generics::default(), item_type }, true)
}
};
Item::from_def_id_and_parts(local_did, Some(self.ident.name), inner, cx)
hir::ImplItemKind::Fn(ref sig, body) => {
let mut m = (sig, &self.generics, body).clean(cx);
if m.header.constness == hir::Constness::Const
&& is_unstable_const_fn(cx.tcx, local_did).is_some()
{
m.header.constness = hir::Constness::NotConst;
}
MethodItem(m, Some(self.defaultness))
}
hir::ImplItemKind::TyAlias(ref ty) => {
let type_ = ty.clean(cx);
let item_type = type_.def_id().and_then(|did| inline::build_ty(cx, did));
TypedefItem(Typedef { type_, generics: Generics::default(), item_type }, true)
}
};
Item::from_def_id_and_parts(local_did, Some(self.ident.name), inner, cx)
})
}
}

Expand Down Expand Up @@ -1396,7 +1401,7 @@ fn clean_qpath(hir_ty: &hir::Ty<'_>, cx: &DocContext<'_>) -> Type {
hir::QPath::Resolved(Some(ref qself), ref p) => {
// Try to normalize `<X as Y>::T` to a type
let ty = hir_ty_to_ty(cx.tcx, hir_ty);
if let Some(normalized_value) = normalize(cx.tcx, ty) {
if let Some(normalized_value) = normalize(cx, ty) {
return normalized_value.clean(cx);
}

Expand Down Expand Up @@ -1498,21 +1503,16 @@ impl Clean<Type> for hir::Ty<'_> {
}

/// Returns `None` if the type could not be normalized
fn normalize(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
fn normalize(cx: &DocContext<'tcx>, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
use crate::rustc_trait_selection::infer::TyCtxtInferExt;
use crate::rustc_trait_selection::traits::query::normalize::AtExt;
use rustc_middle::traits::ObligationCause;
use rustc_middle::ty::ParamEnv;

// Try to normalize `<X as Y>::T` to a type
// FIXME: rustdoc won't be able to perform 'partial' normalization
// until this param env is actually correct
// 'partial': `<Vec<T> as IntoIterator>::IntoIter>` -> `vec::IntoIter<T>`
let param_env = ParamEnv::empty();
let lifted = ty.lift_to_tcx(tcx).unwrap();
let normalized = tcx.infer_ctxt().enter(|infcx| {
let lifted = ty.lift_to_tcx(cx.tcx).unwrap();
let normalized = cx.tcx.infer_ctxt().enter(|infcx| {
infcx
.at(&ObligationCause::dummy(), param_env)
.at(&ObligationCause::dummy(), cx.param_env.get())
.normalize(lifted)
.map(|resolved| infcx.resolve_vars_if_possible(resolved.value))
});
Expand All @@ -1531,7 +1531,7 @@ fn normalize(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
impl<'tcx> Clean<Type> for Ty<'tcx> {
fn clean(&self, cx: &DocContext<'_>) -> Type {
debug!("cleaning type: {:?}", self);
let ty = normalize(cx.tcx, self.lift_to_tcx(cx.tcx).unwrap()).unwrap_or(self);
let ty = normalize(cx, self.lift_to_tcx(cx.tcx).unwrap()).unwrap_or(self);
match *ty.kind() {
ty::Never => Never,
ty::Bool => Primitive(PrimitiveType::Bool),
Expand Down Expand Up @@ -1984,77 +1984,81 @@ impl Clean<Vec<Item>> for (&hir::Item<'_>, Option<Ident>) {
Some(ident) => ident.name,
None => cx.tcx.hir().name(item.hir_id),
};
let kind = match item.kind {
ItemKind::Static(ty, mutability, body_id) => StaticItem(Static {
type_: ty.clean(cx),
mutability,
expr: print_const_expr(cx, body_id),
}),
ItemKind::Const(ty, body_id) => ConstantItem(Constant {
type_: ty.clean(cx),
expr: print_const_expr(cx, body_id),
value: print_evaluated_const(cx, def_id),
is_literal: is_literal_expr(cx, body_id.hir_id),
}),
ItemKind::OpaqueTy(ref ty) => OpaqueTyItem(OpaqueTy {
bounds: ty.bounds.clean(cx),
generics: ty.generics.clean(cx),
}),
ItemKind::TyAlias(ty, ref generics) => {
let rustdoc_ty = ty.clean(cx);
let item_type = rustdoc_ty.def_id().and_then(|did| inline::build_ty(cx, did));
TypedefItem(
Typedef { type_: rustdoc_ty, generics: generics.clean(cx), item_type },
false,
)
}
ItemKind::Enum(ref def, ref generics) => EnumItem(Enum {
variants: def.variants.iter().map(|v| v.clean(cx)).collect(),
generics: generics.clean(cx),
variants_stripped: false,
}),
ItemKind::TraitAlias(ref generics, bounds) => TraitAliasItem(TraitAlias {
generics: generics.clean(cx),
bounds: bounds.clean(cx),
}),
ItemKind::Union(ref variant_data, ref generics) => UnionItem(Union {
struct_type: doctree::struct_type_from_def(&variant_data),
generics: generics.clean(cx),
fields: variant_data.fields().clean(cx),
fields_stripped: false,
}),
ItemKind::Struct(ref variant_data, ref generics) => StructItem(Struct {
struct_type: doctree::struct_type_from_def(&variant_data),
generics: generics.clean(cx),
fields: variant_data.fields().clean(cx),
fields_stripped: false,
}),
ItemKind::Impl { .. } => return clean_impl(item, cx),
// proc macros can have a name set by attributes
ItemKind::Fn(ref sig, ref generics, body_id) => {
clean_fn_or_proc_macro(item, sig, generics, body_id, &mut name, cx)
}
hir::ItemKind::Trait(is_auto, unsafety, ref generics, ref bounds, ref item_ids) => {
let items =
item_ids.iter().map(|ti| cx.tcx.hir().trait_item(ti.id).clean(cx)).collect();
let attrs = item.attrs.clean(cx);
let is_spotlight = attrs.has_doc_flag(sym::spotlight);
TraitItem(Trait {
unsafety,
items,
cx.with_param_env(def_id, || {
let kind = match item.kind {
ItemKind::Static(ty, mutability, body_id) => StaticItem(Static {
type_: ty.clean(cx),
mutability,
expr: print_const_expr(cx, body_id),
}),
ItemKind::Const(ty, body_id) => ConstantItem(Constant {
type_: ty.clean(cx),
expr: print_const_expr(cx, body_id),
value: print_evaluated_const(cx, def_id),
is_literal: is_literal_expr(cx, body_id.hir_id),
}),
ItemKind::OpaqueTy(ref ty) => OpaqueTyItem(OpaqueTy {
bounds: ty.bounds.clean(cx),
generics: ty.generics.clean(cx),
}),
ItemKind::TyAlias(ty, ref generics) => {
let rustdoc_ty = ty.clean(cx);
let item_type = rustdoc_ty.def_id().and_then(|did| inline::build_ty(cx, did));
TypedefItem(
Typedef { type_: rustdoc_ty, generics: generics.clean(cx), item_type },
false,
)
}
ItemKind::Enum(ref def, ref generics) => EnumItem(Enum {
variants: def.variants.iter().map(|v| v.clean(cx)).collect(),
generics: generics.clean(cx),
variants_stripped: false,
}),
ItemKind::TraitAlias(ref generics, bounds) => TraitAliasItem(TraitAlias {
generics: generics.clean(cx),
bounds: bounds.clean(cx),
is_spotlight,
is_auto: is_auto.clean(cx),
})
}
ItemKind::ExternCrate(orig_name) => {
return clean_extern_crate(item, name, orig_name, cx);
}
_ => unreachable!("not yet converted"),
};
}),
ItemKind::Union(ref variant_data, ref generics) => UnionItem(Union {
struct_type: doctree::struct_type_from_def(&variant_data),
generics: generics.clean(cx),
fields: variant_data.fields().clean(cx),
fields_stripped: false,
}),
ItemKind::Struct(ref variant_data, ref generics) => StructItem(Struct {
struct_type: doctree::struct_type_from_def(&variant_data),
generics: generics.clean(cx),
fields: variant_data.fields().clean(cx),
fields_stripped: false,
}),
ItemKind::Impl { .. } => return clean_impl(item, cx),
// proc macros can have a name set by attributes
ItemKind::Fn(ref sig, ref generics, body_id) => {
clean_fn_or_proc_macro(item, sig, generics, body_id, &mut name, cx)
}
hir::ItemKind::Trait(is_auto, unsafety, ref generics, ref bounds, ref item_ids) => {
let items = item_ids
.iter()
.map(|ti| cx.tcx.hir().trait_item(ti.id).clean(cx))
.collect();
let attrs = item.attrs.clean(cx);
let is_spotlight = attrs.has_doc_flag(sym::spotlight);
TraitItem(Trait {
unsafety,
items,
generics: generics.clean(cx),
bounds: bounds.clean(cx),
is_spotlight,
is_auto: is_auto.clean(cx),
})
}
ItemKind::ExternCrate(orig_name) => {
return clean_extern_crate(item, name, orig_name, cx);
}
_ => unreachable!("not yet converted"),
};

vec![Item::from_def_id_and_parts(def_id, Some(name), kind, cx)]
vec![Item::from_def_id_and_parts(def_id, Some(name), kind, cx)]
})
}
}

Expand Down Expand Up @@ -2272,32 +2276,42 @@ impl Clean<Vec<Item>> for doctree::Import<'_> {
impl Clean<Item> for (&hir::ForeignItem<'_>, Option<Ident>) {
fn clean(&self, cx: &DocContext<'_>) -> Item {
let (item, renamed) = self;
let kind = match item.kind {
hir::ForeignItemKind::Fn(ref decl, ref names, ref generics) => {
let abi = cx.tcx.hir().get_foreign_abi(item.hir_id);
let (generics, decl) =
enter_impl_trait(cx, || (generics.clean(cx), (&**decl, &names[..]).clean(cx)));
let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
ForeignFunctionItem(Function {
decl,
generics,
header: hir::FnHeader {
unsafety: hir::Unsafety::Unsafe,
abi,
constness: hir::Constness::NotConst,
asyncness: hir::IsAsync::NotAsync,
},
all_types,
ret_types,
})
}
hir::ForeignItemKind::Static(ref ty, mutability) => {
ForeignStaticItem(Static { type_: ty.clean(cx), mutability, expr: String::new() })
}
hir::ForeignItemKind::Type => ForeignTypeItem,
};
cx.with_param_env(cx.tcx.hir().local_def_id(item.hir_id).to_def_id(), || {
let kind = match item.kind {
hir::ForeignItemKind::Fn(ref decl, ref names, ref generics) => {
let abi = cx.tcx.hir().get_foreign_abi(item.hir_id);
let (generics, decl) = enter_impl_trait(cx, || {
(generics.clean(cx), (&**decl, &names[..]).clean(cx))
});
let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
ForeignFunctionItem(Function {
decl,
generics,
header: hir::FnHeader {
unsafety: hir::Unsafety::Unsafe,
abi,
constness: hir::Constness::NotConst,
asyncness: hir::IsAsync::NotAsync,
},
all_types,
ret_types,
})
}
hir::ForeignItemKind::Static(ref ty, mutability) => ForeignStaticItem(Static {
type_: ty.clean(cx),
mutability,
expr: String::new(),
}),
hir::ForeignItemKind::Type => ForeignTypeItem,
};

Item::from_hir_id_and_parts(item.hir_id, Some(renamed.unwrap_or(item.ident).name), kind, cx)
Item::from_hir_id_and_parts(
item.hir_id,
Some(renamed.unwrap_or(item.ident).name),
kind,
cx,
)
})
}
}

Expand Down
Loading

0 comments on commit 6278daa

Please sign in to comment.