1
Fork 0

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.
This commit is contained in:
Joshua Nelson 2020-10-18 11:27:16 -04:00
parent a192e5d9c2
commit 6278daac54
3 changed files with 186 additions and 155 deletions

View file

@ -1067,6 +1067,7 @@ impl Clean<TypeKind> for hir::def::DefKind {
impl Clean<Item> for hir::TraitItem<'_> { impl Clean<Item> for hir::TraitItem<'_> {
fn clean(&self, cx: &DocContext<'_>) -> Item { fn clean(&self, cx: &DocContext<'_>) -> Item {
let local_did = cx.tcx.hir().local_def_id(self.hir_id).to_def_id(); let local_did = cx.tcx.hir().local_def_id(self.hir_id).to_def_id();
cx.with_param_env(local_did, || {
let inner = match self.kind { let inner = match self.kind {
hir::TraitItemKind::Const(ref ty, default) => { hir::TraitItemKind::Const(ref ty, default) => {
AssocConstItem(ty.clean(cx), default.map(|e| print_const_expr(cx, e))) AssocConstItem(ty.clean(cx), default.map(|e| print_const_expr(cx, e)))
@ -1085,7 +1086,8 @@ impl Clean<Item> for hir::TraitItem<'_> {
(self.generics.clean(cx), (&*sig.decl, &names[..]).clean(cx)) (self.generics.clean(cx), (&*sig.decl, &names[..]).clean(cx))
}); });
let (all_types, ret_types) = get_all_types(&generics, &decl, 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 }; let mut t =
Function { header: sig.header, decl, generics, all_types, ret_types };
if t.header.constness == hir::Constness::Const if t.header.constness == hir::Constness::Const
&& is_unstable_const_fn(cx.tcx, local_did).is_some() && is_unstable_const_fn(cx.tcx, local_did).is_some()
{ {
@ -1098,12 +1100,14 @@ impl Clean<Item> for hir::TraitItem<'_> {
} }
}; };
Item::from_def_id_and_parts(local_did, Some(self.ident.name), inner, cx) Item::from_def_id_and_parts(local_did, Some(self.ident.name), inner, cx)
})
} }
} }
impl Clean<Item> for hir::ImplItem<'_> { impl Clean<Item> for hir::ImplItem<'_> {
fn clean(&self, cx: &DocContext<'_>) -> Item { fn clean(&self, cx: &DocContext<'_>) -> Item {
let local_did = cx.tcx.hir().local_def_id(self.hir_id).to_def_id(); let local_did = cx.tcx.hir().local_def_id(self.hir_id).to_def_id();
cx.with_param_env(local_did, || {
let inner = match self.kind { let inner = match self.kind {
hir::ImplItemKind::Const(ref ty, expr) => { hir::ImplItemKind::Const(ref ty, expr) => {
AssocConstItem(ty.clean(cx), Some(print_const_expr(cx, expr))) AssocConstItem(ty.clean(cx), Some(print_const_expr(cx, expr)))
@ -1124,6 +1128,7 @@ impl Clean<Item> for hir::ImplItem<'_> {
} }
}; };
Item::from_def_id_and_parts(local_did, Some(self.ident.name), inner, cx) Item::from_def_id_and_parts(local_did, Some(self.ident.name), inner, cx)
})
} }
} }
@ -1396,7 +1401,7 @@ fn clean_qpath(hir_ty: &hir::Ty<'_>, cx: &DocContext<'_>) -> Type {
hir::QPath::Resolved(Some(ref qself), ref p) => { hir::QPath::Resolved(Some(ref qself), ref p) => {
// Try to normalize `<X as Y>::T` to a type // Try to normalize `<X as Y>::T` to a type
let ty = hir_ty_to_ty(cx.tcx, hir_ty); 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); return normalized_value.clean(cx);
} }
@ -1498,21 +1503,16 @@ impl Clean<Type> for hir::Ty<'_> {
} }
/// Returns `None` if the type could not be normalized /// 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::infer::TyCtxtInferExt;
use crate::rustc_trait_selection::traits::query::normalize::AtExt; use crate::rustc_trait_selection::traits::query::normalize::AtExt;
use rustc_middle::traits::ObligationCause; use rustc_middle::traits::ObligationCause;
use rustc_middle::ty::ParamEnv;
// Try to normalize `<X as Y>::T` to a type // Try to normalize `<X as Y>::T` to a type
// FIXME: rustdoc won't be able to perform 'partial' normalization let lifted = ty.lift_to_tcx(cx.tcx).unwrap();
// until this param env is actually correct let normalized = cx.tcx.infer_ctxt().enter(|infcx| {
// '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| {
infcx infcx
.at(&ObligationCause::dummy(), param_env) .at(&ObligationCause::dummy(), cx.param_env.get())
.normalize(lifted) .normalize(lifted)
.map(|resolved| infcx.resolve_vars_if_possible(resolved.value)) .map(|resolved| infcx.resolve_vars_if_possible(resolved.value))
}); });
@ -1531,7 +1531,7 @@ fn normalize(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
impl<'tcx> Clean<Type> for Ty<'tcx> { impl<'tcx> Clean<Type> for Ty<'tcx> {
fn clean(&self, cx: &DocContext<'_>) -> Type { fn clean(&self, cx: &DocContext<'_>) -> Type {
debug!("cleaning type: {:?}", self); 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() { match *ty.kind() {
ty::Never => Never, ty::Never => Never,
ty::Bool => Primitive(PrimitiveType::Bool), ty::Bool => Primitive(PrimitiveType::Bool),
@ -1984,6 +1984,7 @@ impl Clean<Vec<Item>> for (&hir::Item<'_>, Option<Ident>) {
Some(ident) => ident.name, Some(ident) => ident.name,
None => cx.tcx.hir().name(item.hir_id), None => cx.tcx.hir().name(item.hir_id),
}; };
cx.with_param_env(def_id, || {
let kind = match item.kind { let kind = match item.kind {
ItemKind::Static(ty, mutability, body_id) => StaticItem(Static { ItemKind::Static(ty, mutability, body_id) => StaticItem(Static {
type_: ty.clean(cx), type_: ty.clean(cx),
@ -2035,8 +2036,10 @@ impl Clean<Vec<Item>> for (&hir::Item<'_>, Option<Ident>) {
clean_fn_or_proc_macro(item, sig, generics, body_id, &mut name, cx) 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) => { hir::ItemKind::Trait(is_auto, unsafety, ref generics, ref bounds, ref item_ids) => {
let items = let items = item_ids
item_ids.iter().map(|ti| cx.tcx.hir().trait_item(ti.id).clean(cx)).collect(); .iter()
.map(|ti| cx.tcx.hir().trait_item(ti.id).clean(cx))
.collect();
let attrs = item.attrs.clean(cx); let attrs = item.attrs.clean(cx);
let is_spotlight = attrs.has_doc_flag(sym::spotlight); let is_spotlight = attrs.has_doc_flag(sym::spotlight);
TraitItem(Trait { TraitItem(Trait {
@ -2055,6 +2058,7 @@ impl Clean<Vec<Item>> for (&hir::Item<'_>, Option<Ident>) {
}; };
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)]
})
} }
} }
@ -2272,11 +2276,13 @@ impl Clean<Vec<Item>> for doctree::Import<'_> {
impl Clean<Item> for (&hir::ForeignItem<'_>, Option<Ident>) { impl Clean<Item> for (&hir::ForeignItem<'_>, Option<Ident>) {
fn clean(&self, cx: &DocContext<'_>) -> Item { fn clean(&self, cx: &DocContext<'_>) -> Item {
let (item, renamed) = self; let (item, renamed) = self;
cx.with_param_env(cx.tcx.hir().local_def_id(item.hir_id).to_def_id(), || {
let kind = match item.kind { let kind = match item.kind {
hir::ForeignItemKind::Fn(ref decl, ref names, ref generics) => { hir::ForeignItemKind::Fn(ref decl, ref names, ref generics) => {
let abi = cx.tcx.hir().get_foreign_abi(item.hir_id); let abi = cx.tcx.hir().get_foreign_abi(item.hir_id);
let (generics, decl) = let (generics, decl) = enter_impl_trait(cx, || {
enter_impl_trait(cx, || (generics.clean(cx), (&**decl, &names[..]).clean(cx))); (generics.clean(cx), (&**decl, &names[..]).clean(cx))
});
let (all_types, ret_types) = get_all_types(&generics, &decl, cx); let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
ForeignFunctionItem(Function { ForeignFunctionItem(Function {
decl, decl,
@ -2291,13 +2297,21 @@ impl Clean<Item> for (&hir::ForeignItem<'_>, Option<Ident>) {
ret_types, ret_types,
}) })
} }
hir::ForeignItemKind::Static(ref ty, mutability) => { hir::ForeignItemKind::Static(ref ty, mutability) => ForeignStaticItem(Static {
ForeignStaticItem(Static { type_: ty.clean(cx), mutability, expr: String::new() }) type_: ty.clean(cx),
} mutability,
expr: String::new(),
}),
hir::ForeignItemKind::Type => ForeignTypeItem, 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,
)
})
} }
} }

View file

@ -15,7 +15,7 @@ use rustc_interface::interface;
use rustc_middle::hir::map::Map; use rustc_middle::hir::map::Map;
use rustc_middle::middle::cstore::CrateStore; use rustc_middle::middle::cstore::CrateStore;
use rustc_middle::middle::privacy::AccessLevels; use rustc_middle::middle::privacy::AccessLevels;
use rustc_middle::ty::{Ty, TyCtxt}; use rustc_middle::ty::{ParamEnv, Ty, TyCtxt};
use rustc_resolve as resolve; use rustc_resolve as resolve;
use rustc_session::config::{self, CrateType, ErrorOutputType}; use rustc_session::config::{self, CrateType, ErrorOutputType};
use rustc_session::lint; use rustc_session::lint;
@ -25,7 +25,7 @@ use rustc_span::source_map;
use rustc_span::symbol::sym; use rustc_span::symbol::sym;
use rustc_span::DUMMY_SP; use rustc_span::DUMMY_SP;
use std::cell::RefCell; use std::cell::{Cell, RefCell};
use std::mem; use std::mem;
use std::rc::Rc; use std::rc::Rc;
@ -42,6 +42,10 @@ crate type ExternalPaths = FxHashMap<DefId, (Vec<String>, clean::TypeKind)>;
crate struct DocContext<'tcx> { crate struct DocContext<'tcx> {
crate tcx: TyCtxt<'tcx>, crate tcx: TyCtxt<'tcx>,
crate resolver: Rc<RefCell<interface::BoxedResolver>>, crate resolver: Rc<RefCell<interface::BoxedResolver>>,
/// Used for normalization.
///
/// Most of this logic is copied from rustc_lint::late.
crate param_env: Cell<ParamEnv<'tcx>>,
/// Later on moved into `CACHE_KEY` /// Later on moved into `CACHE_KEY`
crate renderinfo: RefCell<RenderInfo>, crate renderinfo: RefCell<RenderInfo>,
/// Later on moved through `clean::Crate` into `CACHE_KEY` /// Later on moved through `clean::Crate` into `CACHE_KEY`
@ -79,6 +83,13 @@ impl<'tcx> DocContext<'tcx> {
&self.tcx.sess &self.tcx.sess
} }
crate fn with_param_env<T, F: FnOnce() -> T>(&self, def_id: DefId, f: F) -> T {
let old_param_env = self.param_env.replace(self.tcx.param_env(def_id));
let ret = f();
self.param_env.set(old_param_env);
ret
}
crate fn enter_resolver<F, R>(&self, f: F) -> R crate fn enter_resolver<F, R>(&self, f: F) -> R
where where
F: FnOnce(&mut resolve::Resolver<'_>) -> R, F: FnOnce(&mut resolve::Resolver<'_>) -> R,
@ -524,6 +535,7 @@ fn run_global_ctxt(
let mut ctxt = DocContext { let mut ctxt = DocContext {
tcx, tcx,
resolver, resolver,
param_env: Cell::new(ParamEnv::empty()),
external_traits: Default::default(), external_traits: Default::default(),
active_extern_traits: Default::default(), active_extern_traits: Default::default(),
renderinfo: RefCell::new(renderinfo), renderinfo: RefCell::new(renderinfo),

View file

@ -16,7 +16,7 @@ pub fn f() -> <usize as Trait>::X {
} }
pub struct S { pub struct S {
// @has 'normalize_assoc_item/struct.S.html' '//span[@id="structfield.box_me_up"]' 'box_me_up: Box<S>' // @has 'normalize_assoc_item/struct.S.html' '//span[@id="structfield.box_me_up"]' 'box_me_up: Box<S, Global>'
pub box_me_up: <S as Trait>::X, pub box_me_up: <S as Trait>::X,
// @has 'normalize_assoc_item/struct.S.html' '//span[@id="structfield.generic"]' 'generic: (usize, isize)' // @has 'normalize_assoc_item/struct.S.html' '//span[@id="structfield.generic"]' 'generic: (usize, isize)'
pub generic: <Generic<usize> as Trait>::X, pub generic: <Generic<usize> as Trait>::X,
@ -61,3 +61,8 @@ pub const A: <usize as Lifetimes<'static>>::Y = &0;
extern crate inner; extern crate inner;
// @has 'normalize_assoc_item/fn.foo.html' '//pre[@class="rust fn"]' "pub fn foo() -> i32" // @has 'normalize_assoc_item/fn.foo.html' '//pre[@class="rust fn"]' "pub fn foo() -> i32"
pub use inner::foo; pub use inner::foo;
// @has 'normalize_assoc_item/fn.h.html' '//pre[@class="rust fn"]' "pub fn h<T>() -> IntoIter<T, Global>"
pub fn h<T>() -> <Vec<T> as IntoIterator>::IntoIter {
vec![].into_iter()
}