Skip to content

Commit 6278daa

Browse files
committed
Track ParamEnvs properly
This uses the same `with_param_env` pattern that late lints use. Thanks to all the doctree refactors, this was very easy to add.
1 parent a192e5d commit 6278daa

File tree

3 files changed

+186
-155
lines changed

3 files changed

+186
-155
lines changed

src/librustdoc/clean/mod.rs

+166-152
Original file line numberDiff line numberDiff line change
@@ -1067,63 +1067,68 @@ impl Clean<TypeKind> for hir::def::DefKind {
10671067
impl Clean<Item> for hir::TraitItem<'_> {
10681068
fn clean(&self, cx: &DocContext<'_>) -> Item {
10691069
let local_did = cx.tcx.hir().local_def_id(self.hir_id).to_def_id();
1070-
let inner = match self.kind {
1071-
hir::TraitItemKind::Const(ref ty, default) => {
1072-
AssocConstItem(ty.clean(cx), default.map(|e| print_const_expr(cx, e)))
1073-
}
1074-
hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Provided(body)) => {
1075-
let mut m = (sig, &self.generics, body).clean(cx);
1076-
if m.header.constness == hir::Constness::Const
1077-
&& is_unstable_const_fn(cx.tcx, local_did).is_some()
1078-
{
1079-
m.header.constness = hir::Constness::NotConst;
1070+
cx.with_param_env(local_did, || {
1071+
let inner = match self.kind {
1072+
hir::TraitItemKind::Const(ref ty, default) => {
1073+
AssocConstItem(ty.clean(cx), default.map(|e| print_const_expr(cx, e)))
10801074
}
1081-
MethodItem(m, None)
1082-
}
1083-
hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Required(ref names)) => {
1084-
let (generics, decl) = enter_impl_trait(cx, || {
1085-
(self.generics.clean(cx), (&*sig.decl, &names[..]).clean(cx))
1086-
});
1087-
let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
1088-
let mut t = Function { header: sig.header, decl, generics, all_types, ret_types };
1089-
if t.header.constness == hir::Constness::Const
1090-
&& is_unstable_const_fn(cx.tcx, local_did).is_some()
1091-
{
1092-
t.header.constness = hir::Constness::NotConst;
1075+
hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Provided(body)) => {
1076+
let mut m = (sig, &self.generics, body).clean(cx);
1077+
if m.header.constness == hir::Constness::Const
1078+
&& is_unstable_const_fn(cx.tcx, local_did).is_some()
1079+
{
1080+
m.header.constness = hir::Constness::NotConst;
1081+
}
1082+
MethodItem(m, None)
10931083
}
1094-
TyMethodItem(t)
1095-
}
1096-
hir::TraitItemKind::Type(ref bounds, ref default) => {
1097-
AssocTypeItem(bounds.clean(cx), default.clean(cx))
1098-
}
1099-
};
1100-
Item::from_def_id_and_parts(local_did, Some(self.ident.name), inner, cx)
1084+
hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Required(ref names)) => {
1085+
let (generics, decl) = enter_impl_trait(cx, || {
1086+
(self.generics.clean(cx), (&*sig.decl, &names[..]).clean(cx))
1087+
});
1088+
let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
1089+
let mut t =
1090+
Function { header: sig.header, decl, generics, all_types, ret_types };
1091+
if t.header.constness == hir::Constness::Const
1092+
&& is_unstable_const_fn(cx.tcx, local_did).is_some()
1093+
{
1094+
t.header.constness = hir::Constness::NotConst;
1095+
}
1096+
TyMethodItem(t)
1097+
}
1098+
hir::TraitItemKind::Type(ref bounds, ref default) => {
1099+
AssocTypeItem(bounds.clean(cx), default.clean(cx))
1100+
}
1101+
};
1102+
Item::from_def_id_and_parts(local_did, Some(self.ident.name), inner, cx)
1103+
})
11011104
}
11021105
}
11031106

11041107
impl Clean<Item> for hir::ImplItem<'_> {
11051108
fn clean(&self, cx: &DocContext<'_>) -> Item {
11061109
let local_did = cx.tcx.hir().local_def_id(self.hir_id).to_def_id();
1107-
let inner = match self.kind {
1108-
hir::ImplItemKind::Const(ref ty, expr) => {
1109-
AssocConstItem(ty.clean(cx), Some(print_const_expr(cx, expr)))
1110-
}
1111-
hir::ImplItemKind::Fn(ref sig, body) => {
1112-
let mut m = (sig, &self.generics, body).clean(cx);
1113-
if m.header.constness == hir::Constness::Const
1114-
&& is_unstable_const_fn(cx.tcx, local_did).is_some()
1115-
{
1116-
m.header.constness = hir::Constness::NotConst;
1110+
cx.with_param_env(local_did, || {
1111+
let inner = match self.kind {
1112+
hir::ImplItemKind::Const(ref ty, expr) => {
1113+
AssocConstItem(ty.clean(cx), Some(print_const_expr(cx, expr)))
11171114
}
1118-
MethodItem(m, Some(self.defaultness))
1119-
}
1120-
hir::ImplItemKind::TyAlias(ref ty) => {
1121-
let type_ = ty.clean(cx);
1122-
let item_type = type_.def_id().and_then(|did| inline::build_ty(cx, did));
1123-
TypedefItem(Typedef { type_, generics: Generics::default(), item_type }, true)
1124-
}
1125-
};
1126-
Item::from_def_id_and_parts(local_did, Some(self.ident.name), inner, cx)
1115+
hir::ImplItemKind::Fn(ref sig, body) => {
1116+
let mut m = (sig, &self.generics, body).clean(cx);
1117+
if m.header.constness == hir::Constness::Const
1118+
&& is_unstable_const_fn(cx.tcx, local_did).is_some()
1119+
{
1120+
m.header.constness = hir::Constness::NotConst;
1121+
}
1122+
MethodItem(m, Some(self.defaultness))
1123+
}
1124+
hir::ImplItemKind::TyAlias(ref ty) => {
1125+
let type_ = ty.clean(cx);
1126+
let item_type = type_.def_id().and_then(|did| inline::build_ty(cx, did));
1127+
TypedefItem(Typedef { type_, generics: Generics::default(), item_type }, true)
1128+
}
1129+
};
1130+
Item::from_def_id_and_parts(local_did, Some(self.ident.name), inner, cx)
1131+
})
11271132
}
11281133
}
11291134

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

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

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

15071511
// Try to normalize `<X as Y>::T` to a type
1508-
// FIXME: rustdoc won't be able to perform 'partial' normalization
1509-
// until this param env is actually correct
1510-
// 'partial': `<Vec<T> as IntoIterator>::IntoIter>` -> `vec::IntoIter<T>`
1511-
let param_env = ParamEnv::empty();
1512-
let lifted = ty.lift_to_tcx(tcx).unwrap();
1513-
let normalized = tcx.infer_ctxt().enter(|infcx| {
1512+
let lifted = ty.lift_to_tcx(cx.tcx).unwrap();
1513+
let normalized = cx.tcx.infer_ctxt().enter(|infcx| {
15141514
infcx
1515-
.at(&ObligationCause::dummy(), param_env)
1515+
.at(&ObligationCause::dummy(), cx.param_env.get())
15161516
.normalize(lifted)
15171517
.map(|resolved| infcx.resolve_vars_if_possible(resolved.value))
15181518
});
@@ -1531,7 +1531,7 @@ fn normalize(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
15311531
impl<'tcx> Clean<Type> for Ty<'tcx> {
15321532
fn clean(&self, cx: &DocContext<'_>) -> Type {
15331533
debug!("cleaning type: {:?}", self);
1534-
let ty = normalize(cx.tcx, self.lift_to_tcx(cx.tcx).unwrap()).unwrap_or(self);
1534+
let ty = normalize(cx, self.lift_to_tcx(cx.tcx).unwrap()).unwrap_or(self);
15351535
match *ty.kind() {
15361536
ty::Never => Never,
15371537
ty::Bool => Primitive(PrimitiveType::Bool),
@@ -1984,77 +1984,81 @@ impl Clean<Vec<Item>> for (&hir::Item<'_>, Option<Ident>) {
19841984
Some(ident) => ident.name,
19851985
None => cx.tcx.hir().name(item.hir_id),
19861986
};
1987-
let kind = match item.kind {
1988-
ItemKind::Static(ty, mutability, body_id) => StaticItem(Static {
1989-
type_: ty.clean(cx),
1990-
mutability,
1991-
expr: print_const_expr(cx, body_id),
1992-
}),
1993-
ItemKind::Const(ty, body_id) => ConstantItem(Constant {
1994-
type_: ty.clean(cx),
1995-
expr: print_const_expr(cx, body_id),
1996-
value: print_evaluated_const(cx, def_id),
1997-
is_literal: is_literal_expr(cx, body_id.hir_id),
1998-
}),
1999-
ItemKind::OpaqueTy(ref ty) => OpaqueTyItem(OpaqueTy {
2000-
bounds: ty.bounds.clean(cx),
2001-
generics: ty.generics.clean(cx),
2002-
}),
2003-
ItemKind::TyAlias(ty, ref generics) => {
2004-
let rustdoc_ty = ty.clean(cx);
2005-
let item_type = rustdoc_ty.def_id().and_then(|did| inline::build_ty(cx, did));
2006-
TypedefItem(
2007-
Typedef { type_: rustdoc_ty, generics: generics.clean(cx), item_type },
2008-
false,
2009-
)
2010-
}
2011-
ItemKind::Enum(ref def, ref generics) => EnumItem(Enum {
2012-
variants: def.variants.iter().map(|v| v.clean(cx)).collect(),
2013-
generics: generics.clean(cx),
2014-
variants_stripped: false,
2015-
}),
2016-
ItemKind::TraitAlias(ref generics, bounds) => TraitAliasItem(TraitAlias {
2017-
generics: generics.clean(cx),
2018-
bounds: bounds.clean(cx),
2019-
}),
2020-
ItemKind::Union(ref variant_data, ref generics) => UnionItem(Union {
2021-
struct_type: doctree::struct_type_from_def(&variant_data),
2022-
generics: generics.clean(cx),
2023-
fields: variant_data.fields().clean(cx),
2024-
fields_stripped: false,
2025-
}),
2026-
ItemKind::Struct(ref variant_data, ref generics) => StructItem(Struct {
2027-
struct_type: doctree::struct_type_from_def(&variant_data),
2028-
generics: generics.clean(cx),
2029-
fields: variant_data.fields().clean(cx),
2030-
fields_stripped: false,
2031-
}),
2032-
ItemKind::Impl { .. } => return clean_impl(item, cx),
2033-
// proc macros can have a name set by attributes
2034-
ItemKind::Fn(ref sig, ref generics, body_id) => {
2035-
clean_fn_or_proc_macro(item, sig, generics, body_id, &mut name, cx)
2036-
}
2037-
hir::ItemKind::Trait(is_auto, unsafety, ref generics, ref bounds, ref item_ids) => {
2038-
let items =
2039-
item_ids.iter().map(|ti| cx.tcx.hir().trait_item(ti.id).clean(cx)).collect();
2040-
let attrs = item.attrs.clean(cx);
2041-
let is_spotlight = attrs.has_doc_flag(sym::spotlight);
2042-
TraitItem(Trait {
2043-
unsafety,
2044-
items,
1987+
cx.with_param_env(def_id, || {
1988+
let kind = match item.kind {
1989+
ItemKind::Static(ty, mutability, body_id) => StaticItem(Static {
1990+
type_: ty.clean(cx),
1991+
mutability,
1992+
expr: print_const_expr(cx, body_id),
1993+
}),
1994+
ItemKind::Const(ty, body_id) => ConstantItem(Constant {
1995+
type_: ty.clean(cx),
1996+
expr: print_const_expr(cx, body_id),
1997+
value: print_evaluated_const(cx, def_id),
1998+
is_literal: is_literal_expr(cx, body_id.hir_id),
1999+
}),
2000+
ItemKind::OpaqueTy(ref ty) => OpaqueTyItem(OpaqueTy {
2001+
bounds: ty.bounds.clean(cx),
2002+
generics: ty.generics.clean(cx),
2003+
}),
2004+
ItemKind::TyAlias(ty, ref generics) => {
2005+
let rustdoc_ty = ty.clean(cx);
2006+
let item_type = rustdoc_ty.def_id().and_then(|did| inline::build_ty(cx, did));
2007+
TypedefItem(
2008+
Typedef { type_: rustdoc_ty, generics: generics.clean(cx), item_type },
2009+
false,
2010+
)
2011+
}
2012+
ItemKind::Enum(ref def, ref generics) => EnumItem(Enum {
2013+
variants: def.variants.iter().map(|v| v.clean(cx)).collect(),
2014+
generics: generics.clean(cx),
2015+
variants_stripped: false,
2016+
}),
2017+
ItemKind::TraitAlias(ref generics, bounds) => TraitAliasItem(TraitAlias {
20452018
generics: generics.clean(cx),
20462019
bounds: bounds.clean(cx),
2047-
is_spotlight,
2048-
is_auto: is_auto.clean(cx),
2049-
})
2050-
}
2051-
ItemKind::ExternCrate(orig_name) => {
2052-
return clean_extern_crate(item, name, orig_name, cx);
2053-
}
2054-
_ => unreachable!("not yet converted"),
2055-
};
2020+
}),
2021+
ItemKind::Union(ref variant_data, ref generics) => UnionItem(Union {
2022+
struct_type: doctree::struct_type_from_def(&variant_data),
2023+
generics: generics.clean(cx),
2024+
fields: variant_data.fields().clean(cx),
2025+
fields_stripped: false,
2026+
}),
2027+
ItemKind::Struct(ref variant_data, ref generics) => StructItem(Struct {
2028+
struct_type: doctree::struct_type_from_def(&variant_data),
2029+
generics: generics.clean(cx),
2030+
fields: variant_data.fields().clean(cx),
2031+
fields_stripped: false,
2032+
}),
2033+
ItemKind::Impl { .. } => return clean_impl(item, cx),
2034+
// proc macros can have a name set by attributes
2035+
ItemKind::Fn(ref sig, ref generics, body_id) => {
2036+
clean_fn_or_proc_macro(item, sig, generics, body_id, &mut name, cx)
2037+
}
2038+
hir::ItemKind::Trait(is_auto, unsafety, ref generics, ref bounds, ref item_ids) => {
2039+
let items = item_ids
2040+
.iter()
2041+
.map(|ti| cx.tcx.hir().trait_item(ti.id).clean(cx))
2042+
.collect();
2043+
let attrs = item.attrs.clean(cx);
2044+
let is_spotlight = attrs.has_doc_flag(sym::spotlight);
2045+
TraitItem(Trait {
2046+
unsafety,
2047+
items,
2048+
generics: generics.clean(cx),
2049+
bounds: bounds.clean(cx),
2050+
is_spotlight,
2051+
is_auto: is_auto.clean(cx),
2052+
})
2053+
}
2054+
ItemKind::ExternCrate(orig_name) => {
2055+
return clean_extern_crate(item, name, orig_name, cx);
2056+
}
2057+
_ => unreachable!("not yet converted"),
2058+
};
20562059

2057-
vec![Item::from_def_id_and_parts(def_id, Some(name), kind, cx)]
2060+
vec![Item::from_def_id_and_parts(def_id, Some(name), kind, cx)]
2061+
})
20582062
}
20592063
}
20602064

@@ -2272,32 +2276,42 @@ impl Clean<Vec<Item>> for doctree::Import<'_> {
22722276
impl Clean<Item> for (&hir::ForeignItem<'_>, Option<Ident>) {
22732277
fn clean(&self, cx: &DocContext<'_>) -> Item {
22742278
let (item, renamed) = self;
2275-
let kind = match item.kind {
2276-
hir::ForeignItemKind::Fn(ref decl, ref names, ref generics) => {
2277-
let abi = cx.tcx.hir().get_foreign_abi(item.hir_id);
2278-
let (generics, decl) =
2279-
enter_impl_trait(cx, || (generics.clean(cx), (&**decl, &names[..]).clean(cx)));
2280-
let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
2281-
ForeignFunctionItem(Function {
2282-
decl,
2283-
generics,
2284-
header: hir::FnHeader {
2285-
unsafety: hir::Unsafety::Unsafe,
2286-
abi,
2287-
constness: hir::Constness::NotConst,
2288-
asyncness: hir::IsAsync::NotAsync,
2289-
},
2290-
all_types,
2291-
ret_types,
2292-
})
2293-
}
2294-
hir::ForeignItemKind::Static(ref ty, mutability) => {
2295-
ForeignStaticItem(Static { type_: ty.clean(cx), mutability, expr: String::new() })
2296-
}
2297-
hir::ForeignItemKind::Type => ForeignTypeItem,
2298-
};
2279+
cx.with_param_env(cx.tcx.hir().local_def_id(item.hir_id).to_def_id(), || {
2280+
let kind = match item.kind {
2281+
hir::ForeignItemKind::Fn(ref decl, ref names, ref generics) => {
2282+
let abi = cx.tcx.hir().get_foreign_abi(item.hir_id);
2283+
let (generics, decl) = enter_impl_trait(cx, || {
2284+
(generics.clean(cx), (&**decl, &names[..]).clean(cx))
2285+
});
2286+
let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
2287+
ForeignFunctionItem(Function {
2288+
decl,
2289+
generics,
2290+
header: hir::FnHeader {
2291+
unsafety: hir::Unsafety::Unsafe,
2292+
abi,
2293+
constness: hir::Constness::NotConst,
2294+
asyncness: hir::IsAsync::NotAsync,
2295+
},
2296+
all_types,
2297+
ret_types,
2298+
})
2299+
}
2300+
hir::ForeignItemKind::Static(ref ty, mutability) => ForeignStaticItem(Static {
2301+
type_: ty.clean(cx),
2302+
mutability,
2303+
expr: String::new(),
2304+
}),
2305+
hir::ForeignItemKind::Type => ForeignTypeItem,
2306+
};
22992307

2300-
Item::from_hir_id_and_parts(item.hir_id, Some(renamed.unwrap_or(item.ident).name), kind, cx)
2308+
Item::from_hir_id_and_parts(
2309+
item.hir_id,
2310+
Some(renamed.unwrap_or(item.ident).name),
2311+
kind,
2312+
cx,
2313+
)
2314+
})
23012315
}
23022316
}
23032317

0 commit comments

Comments
 (0)