diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs index e7575468640..c5562ae3b7f 100644 --- a/src/librustdoc/clean/inline.rs +++ b/src/librustdoc/clean/inline.rs @@ -18,7 +18,7 @@ use rustc::hir; use rustc::hir::def::{Def, CtorKind}; use rustc::hir::def_id::DefId; use rustc::hir::print as pprust; -use rustc::ty::{self, TyCtxt}; +use rustc::ty; use rustc::util::nodemap::FxHashSet; use rustc_const_eval::lookup_const_by_id; @@ -43,17 +43,13 @@ use super::Clean; /// of a vector of items if it was successfully expanded. pub fn try_inline(cx: &DocContext, id: ast::NodeId, into: Option) -> Option> { - let tcx = match cx.tcx_opt() { - Some(tcx) => tcx, - None => return None, - }; - let def = match tcx.expect_def_or_none(id) { + let def = match cx.tcx.expect_def_or_none(id) { Some(def) => def, None => return None, }; let did = def.def_id(); if did.is_local() { return None } - try_inline_def(cx, tcx, def).map(|vec| { + try_inline_def(cx, def).map(|vec| { vec.into_iter().map(|mut item| { match into { Some(into) if item.name.is_some() => { @@ -66,39 +62,38 @@ pub fn try_inline(cx: &DocContext, id: ast::NodeId, into: Option) }) } -fn try_inline_def<'a, 'tcx>(cx: &DocContext, tcx: TyCtxt<'a, 'tcx, 'tcx>, - def: Def) -> Option> { +fn try_inline_def(cx: &DocContext, def: Def) -> Option> { + let tcx = cx.tcx; let mut ret = Vec::new(); - let did = def.def_id(); let inner = match def { Def::Trait(did) => { record_extern_fqn(cx, did, clean::TypeKind::Trait); - ret.extend(build_impls(cx, tcx, did)); - clean::TraitItem(build_external_trait(cx, tcx, did)) + ret.extend(build_impls(cx, did)); + clean::TraitItem(build_external_trait(cx, did)) } Def::Fn(did) => { record_extern_fqn(cx, did, clean::TypeKind::Function); - clean::FunctionItem(build_external_function(cx, tcx, did)) + clean::FunctionItem(build_external_function(cx, did)) } Def::Struct(did) => { record_extern_fqn(cx, did, clean::TypeKind::Struct); - ret.extend(build_impls(cx, tcx, did)); - clean::StructItem(build_struct(cx, tcx, did)) + ret.extend(build_impls(cx, did)); + clean::StructItem(build_struct(cx, did)) } Def::Union(did) => { record_extern_fqn(cx, did, clean::TypeKind::Union); - ret.extend(build_impls(cx, tcx, did)); - clean::UnionItem(build_union(cx, tcx, did)) + ret.extend(build_impls(cx, did)); + clean::UnionItem(build_union(cx, did)) } Def::TyAlias(did) => { record_extern_fqn(cx, did, clean::TypeKind::Typedef); - ret.extend(build_impls(cx, tcx, did)); - clean::TypedefItem(build_type_alias(cx, tcx, did), false) + ret.extend(build_impls(cx, did)); + clean::TypedefItem(build_type_alias(cx, did), false) } Def::Enum(did) => { record_extern_fqn(cx, did, clean::TypeKind::Enum); - ret.extend(build_impls(cx, tcx, did)); - clean::EnumItem(build_enum(cx, tcx, did)) + ret.extend(build_impls(cx, did)); + clean::EnumItem(build_enum(cx, did)) } // Assume that the enum type is reexported next to the variant, and // variants don't show up in documentation specially. @@ -108,23 +103,24 @@ fn try_inline_def<'a, 'tcx>(cx: &DocContext, tcx: TyCtxt<'a, 'tcx, 'tcx>, Def::StructCtor(..) => return Some(Vec::new()), Def::Mod(did) => { record_extern_fqn(cx, did, clean::TypeKind::Module); - clean::ModuleItem(build_module(cx, tcx, did)) + clean::ModuleItem(build_module(cx, did)) } Def::Static(did, mtbl) => { record_extern_fqn(cx, did, clean::TypeKind::Static); - clean::StaticItem(build_static(cx, tcx, did, mtbl)) + clean::StaticItem(build_static(cx, did, mtbl)) } Def::Const(did) => { record_extern_fqn(cx, did, clean::TypeKind::Const); - clean::ConstantItem(build_const(cx, tcx, did)) + clean::ConstantItem(build_const(cx, did)) } _ => return None, }; + let did = def.def_id(); cx.renderinfo.borrow_mut().inlined.insert(did); ret.push(clean::Item { source: clean::Span::empty(), name: Some(tcx.item_name(did).to_string()), - attrs: load_attrs(cx, tcx, did), + attrs: load_attrs(cx, did), inner: inner, visibility: Some(clean::Public), stability: tcx.lookup_stability(did).clean(cx), @@ -134,9 +130,8 @@ fn try_inline_def<'a, 'tcx>(cx: &DocContext, tcx: TyCtxt<'a, 'tcx, 'tcx>, Some(ret) } -pub fn load_attrs<'a, 'tcx>(cx: &DocContext, tcx: TyCtxt<'a, 'tcx, 'tcx>, - did: DefId) -> clean::Attributes { - tcx.get_attrs(did).clean(cx) +pub fn load_attrs(cx: &DocContext, did: DefId) -> clean::Attributes { + cx.tcx.get_attrs(did).clean(cx) } /// Record an external fully qualified name in the external_paths cache. @@ -144,27 +139,24 @@ pub fn load_attrs<'a, 'tcx>(cx: &DocContext, tcx: TyCtxt<'a, 'tcx, 'tcx>, /// These names are used later on by HTML rendering to generate things like /// source links back to the original item. pub fn record_extern_fqn(cx: &DocContext, did: DefId, kind: clean::TypeKind) { - if let Some(tcx) = cx.tcx_opt() { - let crate_name = tcx.sess.cstore.crate_name(did.krate).to_string(); - let relative = tcx.def_path(did).data.into_iter().filter_map(|elem| { - // extern blocks have an empty name - let s = elem.data.to_string(); - if !s.is_empty() { - Some(s) - } else { - None - } - }); - let fqn = once(crate_name).chain(relative).collect(); - cx.renderinfo.borrow_mut().external_paths.insert(did, (fqn, kind)); - } + let crate_name = cx.tcx.sess.cstore.crate_name(did.krate).to_string(); + let relative = cx.tcx.def_path(did).data.into_iter().filter_map(|elem| { + // extern blocks have an empty name + let s = elem.data.to_string(); + if !s.is_empty() { + Some(s) + } else { + None + } + }); + let fqn = once(crate_name).chain(relative).collect(); + cx.renderinfo.borrow_mut().external_paths.insert(did, (fqn, kind)); } -pub fn build_external_trait<'a, 'tcx>(cx: &DocContext, tcx: TyCtxt<'a, 'tcx, 'tcx>, - did: DefId) -> clean::Trait { - let def = tcx.lookup_trait_def(did); - let trait_items = tcx.associated_items(did).map(|item| item.clean(cx)).collect(); - let predicates = tcx.item_predicates(did); +pub fn build_external_trait(cx: &DocContext, did: DefId) -> clean::Trait { + let def = cx.tcx.lookup_trait_def(did); + let trait_items = cx.tcx.associated_items(did).map(|item| item.clean(cx)).collect(); + let predicates = cx.tcx.item_predicates(did); let generics = (def.generics, &predicates).clean(cx); let generics = filter_non_trait_generics(did, generics); let (generics, supertrait_bounds) = separate_supertrait_bounds(generics); @@ -176,45 +168,42 @@ pub fn build_external_trait<'a, 'tcx>(cx: &DocContext, tcx: TyCtxt<'a, 'tcx, 'tc } } -fn build_external_function<'a, 'tcx>(cx: &DocContext, tcx: TyCtxt<'a, 'tcx, 'tcx>, - did: DefId) -> clean::Function { - let ty = tcx.item_type(did); +fn build_external_function(cx: &DocContext, did: DefId) -> clean::Function { + let ty = cx.tcx.item_type(did); let (decl, style, abi) = match ty.sty { ty::TyFnDef(.., ref f) => ((did, &f.sig).clean(cx), f.unsafety, f.abi), _ => panic!("bad function"), }; - let constness = if tcx.sess.cstore.is_const_fn(did) { + let constness = if cx.tcx.sess.cstore.is_const_fn(did) { hir::Constness::Const } else { hir::Constness::NotConst }; - let predicates = tcx.item_predicates(did); + let predicates = cx.tcx.item_predicates(did); clean::Function { decl: decl, - generics: (tcx.item_generics(did), &predicates).clean(cx), + generics: (cx.tcx.item_generics(did), &predicates).clean(cx), unsafety: style, constness: constness, abi: abi, } } -fn build_enum<'a, 'tcx>(cx: &DocContext, tcx: TyCtxt<'a, 'tcx, 'tcx>, - did: DefId) -> clean::Enum { - let predicates = tcx.item_predicates(did); +fn build_enum(cx: &DocContext, did: DefId) -> clean::Enum { + let predicates = cx.tcx.item_predicates(did); clean::Enum { - generics: (tcx.item_generics(did), &predicates).clean(cx), + generics: (cx.tcx.item_generics(did), &predicates).clean(cx), variants_stripped: false, - variants: tcx.lookup_adt_def(did).variants.clean(cx), + variants: cx.tcx.lookup_adt_def(did).variants.clean(cx), } } -fn build_struct<'a, 'tcx>(cx: &DocContext, tcx: TyCtxt<'a, 'tcx, 'tcx>, - did: DefId) -> clean::Struct { - let predicates = tcx.item_predicates(did); - let variant = tcx.lookup_adt_def(did).struct_variant(); +fn build_struct(cx: &DocContext, did: DefId) -> clean::Struct { + let predicates = cx.tcx.item_predicates(did); + let variant = cx.tcx.lookup_adt_def(did).struct_variant(); clean::Struct { struct_type: match variant.ctor_kind { @@ -222,44 +211,41 @@ fn build_struct<'a, 'tcx>(cx: &DocContext, tcx: TyCtxt<'a, 'tcx, 'tcx>, CtorKind::Fn => doctree::Tuple, CtorKind::Const => doctree::Unit, }, - generics: (tcx.item_generics(did), &predicates).clean(cx), + generics: (cx.tcx.item_generics(did), &predicates).clean(cx), fields: variant.fields.clean(cx), fields_stripped: false, } } -fn build_union<'a, 'tcx>(cx: &DocContext, tcx: TyCtxt<'a, 'tcx, 'tcx>, - did: DefId) -> clean::Union { - let predicates = tcx.item_predicates(did); - let variant = tcx.lookup_adt_def(did).struct_variant(); +fn build_union(cx: &DocContext, did: DefId) -> clean::Union { + let predicates = cx.tcx.item_predicates(did); + let variant = cx.tcx.lookup_adt_def(did).struct_variant(); clean::Union { struct_type: doctree::Plain, - generics: (tcx.item_generics(did), &predicates).clean(cx), + generics: (cx.tcx.item_generics(did), &predicates).clean(cx), fields: variant.fields.clean(cx), fields_stripped: false, } } -fn build_type_alias<'a, 'tcx>(cx: &DocContext, tcx: TyCtxt<'a, 'tcx, 'tcx>, - did: DefId) -> clean::Typedef { - let predicates = tcx.item_predicates(did); +fn build_type_alias(cx: &DocContext, did: DefId) -> clean::Typedef { + let predicates = cx.tcx.item_predicates(did); clean::Typedef { - type_: tcx.item_type(did).clean(cx), - generics: (tcx.item_generics(did), &predicates).clean(cx), + type_: cx.tcx.item_type(did).clean(cx), + generics: (cx.tcx.item_generics(did), &predicates).clean(cx), } } -pub fn build_impls<'a, 'tcx>(cx: &DocContext, - tcx: TyCtxt<'a, 'tcx, 'tcx>, - did: DefId) -> Vec { +pub fn build_impls(cx: &DocContext, did: DefId) -> Vec { + let tcx = cx.tcx; tcx.populate_inherent_implementations_for_type_if_necessary(did); let mut impls = Vec::new(); if let Some(i) = tcx.inherent_impls.borrow().get(&did) { for &did in i.iter() { - build_impl(cx, tcx, did, &mut impls); + build_impl(cx, did, &mut impls); } } // If this is the first time we've inlined something from another crate, then @@ -277,7 +263,7 @@ pub fn build_impls<'a, 'tcx>(cx: &DocContext, cx.populated_all_crate_impls.set(true); for did in tcx.sess.cstore.implementations_of_trait(None) { - build_impl(cx, tcx, did, &mut impls); + build_impl(cx, did, &mut impls); } // Also try to inline primitive impls from other crates. @@ -303,22 +289,20 @@ pub fn build_impls<'a, 'tcx>(cx: &DocContext, for def_id in primitive_impls.iter().filter_map(|&def_id| def_id) { if !def_id.is_local() { - build_impl(cx, tcx, def_id, &mut impls); + build_impl(cx, def_id, &mut impls); } } impls } -pub fn build_impl<'a, 'tcx>(cx: &DocContext, - tcx: TyCtxt<'a, 'tcx, 'tcx>, - did: DefId, - ret: &mut Vec) { +pub fn build_impl(cx: &DocContext, did: DefId, ret: &mut Vec) { if !cx.renderinfo.borrow_mut().inlined.insert(did) { return } - let attrs = load_attrs(cx, tcx, did); + let attrs = load_attrs(cx, did); + let tcx = cx.tcx; let associated_trait = tcx.impl_trait_ref(did); // Only inline impl if the implemented trait is @@ -440,15 +424,15 @@ pub fn build_impl<'a, 'tcx>(cx: &DocContext, clean::RegionBound(..) => unreachable!(), } }); - if trait_.def_id() == cx.deref_trait_did.get() { + if trait_.def_id() == tcx.lang_items.deref_trait() { super::build_deref_target_impls(cx, &trait_items, ret); } let provided = trait_.def_id().map(|did| { - cx.tcx().provided_trait_methods(did) - .into_iter() - .map(|meth| meth.name.to_string()) - .collect() + tcx.provided_trait_methods(did) + .into_iter() + .map(|meth| meth.name.to_string()) + .collect() }).unwrap_or(FxHashSet()); ret.push(clean::Item { @@ -471,26 +455,24 @@ pub fn build_impl<'a, 'tcx>(cx: &DocContext, }); } -fn build_module<'a, 'tcx>(cx: &DocContext, tcx: TyCtxt<'a, 'tcx, 'tcx>, - did: DefId) -> clean::Module { +fn build_module(cx: &DocContext, did: DefId) -> clean::Module { let mut items = Vec::new(); - fill_in(cx, tcx, did, &mut items); + fill_in(cx, did, &mut items); return clean::Module { items: items, is_crate: false, }; - fn fill_in<'a, 'tcx>(cx: &DocContext, tcx: TyCtxt<'a, 'tcx, 'tcx>, - did: DefId, items: &mut Vec) { + fn fill_in(cx: &DocContext, did: DefId, items: &mut Vec) { // If we're reexporting a reexport it may actually reexport something in // two namespaces, so the target may be listed twice. Make sure we only // visit each node at most once. let mut visited = FxHashSet(); - for item in tcx.sess.cstore.item_children(did) { + for item in cx.tcx.sess.cstore.item_children(did) { let def_id = item.def.def_id(); - if tcx.sess.cstore.visibility(def_id) == ty::Visibility::Public { + if cx.tcx.sess.cstore.visibility(def_id) == ty::Visibility::Public { if !visited.insert(def_id) { continue } - if let Some(i) = try_inline_def(cx, tcx, item.def) { + if let Some(i) = try_inline_def(cx, item.def) { items.extend(i) } } @@ -498,9 +480,8 @@ fn build_module<'a, 'tcx>(cx: &DocContext, tcx: TyCtxt<'a, 'tcx, 'tcx>, } } -fn build_const<'a, 'tcx>(cx: &DocContext, tcx: TyCtxt<'a, 'tcx, 'tcx>, - did: DefId) -> clean::Constant { - let (expr, ty) = lookup_const_by_id(tcx, did, None).unwrap_or_else(|| { +fn build_const(cx: &DocContext, did: DefId) -> clean::Constant { + let (expr, ty) = lookup_const_by_id(cx.tcx, did, None).unwrap_or_else(|| { panic!("expected lookup_const_by_id to succeed for {:?}", did); }); debug!("converting constant expr {:?} to snippet", expr); @@ -508,16 +489,14 @@ fn build_const<'a, 'tcx>(cx: &DocContext, tcx: TyCtxt<'a, 'tcx, 'tcx>, debug!("got snippet {}", sn); clean::Constant { - type_: ty.map(|t| t.clean(cx)).unwrap_or_else(|| tcx.item_type(did).clean(cx)), + type_: ty.map(|t| t.clean(cx)).unwrap_or_else(|| cx.tcx.item_type(did).clean(cx)), expr: sn } } -fn build_static<'a, 'tcx>(cx: &DocContext, tcx: TyCtxt<'a, 'tcx, 'tcx>, - did: DefId, - mutable: bool) -> clean::Static { +fn build_static(cx: &DocContext, did: DefId, mutable: bool) -> clean::Static { clean::Static { - type_: tcx.item_type(did).clean(cx), + type_: cx.tcx.item_type(did).clean(cx), mutability: if mutable {clean::Mutable} else {clean::Immutable}, expr: "\n\n\n".to_string(), // trigger the "[definition]" links } diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 18cb9630f49..a19ec4e8b5e 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -58,11 +58,11 @@ mod simplify; // extract the stability index for a node from tcx, if possible fn get_stability(cx: &DocContext, def_id: DefId) -> Option { - cx.tcx_opt().and_then(|tcx| tcx.lookup_stability(def_id)).clean(cx) + cx.tcx.lookup_stability(def_id).clean(cx) } fn get_deprecation(cx: &DocContext, def_id: DefId) -> Option { - cx.tcx_opt().and_then(|tcx| tcx.lookup_deprecation(def_id)).clean(cx) + cx.tcx.lookup_deprecation(def_id).clean(cx) } pub trait Clean { @@ -125,20 +125,17 @@ impl<'a, 'tcx> Clean for visit_ast::RustdocVisitor<'a, 'tcx> { use rustc::session::config::Input; use ::visit_lib::LibEmbargoVisitor; - if let Some(t) = cx.tcx_opt() { - cx.deref_trait_did.set(t.lang_items.deref_trait()); - cx.renderinfo.borrow_mut().deref_trait_did = cx.deref_trait_did.get(); - cx.deref_mut_trait_did.set(t.lang_items.deref_mut_trait()); - cx.renderinfo.borrow_mut().deref_mut_trait_did = cx.deref_mut_trait_did.get(); + { + let mut r = cx.renderinfo.borrow_mut(); + r.deref_trait_did = cx.tcx.lang_items.deref_trait(); + r.deref_mut_trait_did = cx.tcx.lang_items.deref_mut_trait(); } let mut externs = Vec::new(); for cnum in cx.sess().cstore.crates() { externs.push((cnum, CrateNum(cnum).clean(cx))); - if cx.tcx_opt().is_some() { - // Analyze doc-reachability for extern items - LibEmbargoVisitor::new(cx).visit_lib(cnum); - } + // Analyze doc-reachability for extern items + LibEmbargoVisitor::new(cx).visit_lib(cnum); } externs.sort_by(|&(a, _), &(b, _)| a.cmp(&b)); @@ -234,12 +231,10 @@ impl Clean for CrateNum { fn clean(&self, cx: &DocContext) -> ExternalCrate { let mut primitives = Vec::new(); let root = DefId { krate: self.0, index: CRATE_DEF_INDEX }; - cx.tcx_opt().map(|tcx| { - for item in tcx.sess.cstore.item_children(root) { - let attrs = inline::load_attrs(cx, tcx, item.def.def_id()); - PrimitiveType::find(&attrs).map(|prim| primitives.push(prim)); - } - }); + for item in cx.tcx.sess.cstore.item_children(root) { + let attrs = inline::load_attrs(cx, item.def.def_id()); + PrimitiveType::find(&attrs).map(|prim| primitives.push(prim)); + } ExternalCrate { name: cx.sess().cstore.crate_name(self.0).to_string(), attrs: cx.sess().cstore.item_attrs(root).clean(cx), @@ -449,7 +444,7 @@ impl Clean for doctree::Module { visibility: self.vis.clean(cx), stability: self.stab.clean(cx), deprecation: self.depr.clean(cx), - def_id: cx.map.local_def_id(self.id), + def_id: cx.tcx.map.local_def_id(self.id), inner: ModuleItem(Module { is_crate: self.is_crate, items: items @@ -571,7 +566,7 @@ impl Clean for hir::TyParam { fn clean(&self, cx: &DocContext) -> TyParam { TyParam { name: self.name.clean(cx), - did: cx.map.local_def_id(self.id), + did: cx.tcx.map.local_def_id(self.id), bounds: self.bounds.clean(cx), default: self.default.clean(cx), } @@ -608,11 +603,9 @@ impl TyParamBound { fn is_sized_bound(&self, cx: &DocContext) -> bool { use rustc::hir::TraitBoundModifier as TBM; - if let Some(tcx) = cx.tcx_opt() { - if let TyParamBound::TraitBound(PolyTrait { ref trait_, .. }, TBM::None) = *self { - if trait_.def_id() == tcx.lang_items.sized_trait() { - return true; - } + if let TyParamBound::TraitBound(PolyTrait { ref trait_, .. }, TBM::None) = *self { + if trait_.def_id() == cx.tcx.lang_items.sized_trait() { + return true; } } false @@ -633,9 +626,9 @@ fn external_path_params(cx: &DocContext, trait_did: Option, has_self: boo let lifetimes = substs.regions().filter_map(|v| v.clean(cx)).collect(); let types = substs.types().skip(has_self as usize).collect::>(); - match (trait_did, cx.tcx_opt()) { + match trait_did { // Attempt to sugar an external path like Fn<(A, B,), C> to Fn(A, B) -> C - (Some(did), Some(ref tcx)) if tcx.lang_items.fn_trait_kind(did).is_some() => { + Some(did) if cx.tcx.lang_items.fn_trait_kind(did).is_some() => { assert_eq!(types.len(), 1); let inputs = match types[0].sty { ty::TyTuple(ref tys) => tys.iter().map(|t| t.clean(cx)).collect(), @@ -658,7 +651,7 @@ fn external_path_params(cx: &DocContext, trait_did: Option, has_self: boo output: output } }, - (..) => { + _ => { PathParameters::AngleBracketed { lifetimes: lifetimes, types: types.clean(cx), @@ -683,10 +676,7 @@ fn external_path(cx: &DocContext, name: &str, trait_did: Option, has_self impl Clean for ty::BuiltinBound { fn clean(&self, cx: &DocContext) -> TyParamBound { - let tcx = match cx.tcx_opt() { - Some(tcx) => tcx, - None => return RegionBound(Lifetime::statik()) - }; + let tcx = cx.tcx; let empty = tcx.intern_substs(&[]); let (did, path) = match *self { ty::BoundSend => @@ -717,12 +707,8 @@ impl Clean for ty::BuiltinBound { impl<'tcx> Clean for ty::TraitRef<'tcx> { fn clean(&self, cx: &DocContext) -> TyParamBound { - let tcx = match cx.tcx_opt() { - Some(tcx) => tcx, - None => return RegionBound(Lifetime::statik()) - }; inline::record_extern_fqn(cx, self.def_id, TypeKind::Trait); - let path = external_path(cx, &tcx.item_name(self.def_id).as_str(), + let path = external_path(cx, &cx.tcx.item_name(self.def_id).as_str(), Some(self.def_id), true, vec![], self.substs); debug!("ty::TraitRef\n subst: {:?}\n", self.substs); @@ -789,18 +775,16 @@ impl Lifetime { impl Clean for hir::Lifetime { fn clean(&self, cx: &DocContext) -> Lifetime { - if let Some(tcx) = cx.tcx_opt() { - let def = tcx.named_region_map.defs.get(&self.id).cloned(); - match def { - Some(DefEarlyBoundRegion(_, node_id)) | - Some(DefLateBoundRegion(_, node_id)) | - Some(DefFreeRegion(_, node_id)) => { - if let Some(lt) = cx.lt_substs.borrow().get(&node_id).cloned() { - return lt; - } + let def = cx.tcx.named_region_map.defs.get(&self.id).cloned(); + match def { + Some(DefEarlyBoundRegion(_, node_id)) | + Some(DefLateBoundRegion(_, node_id)) | + Some(DefFreeRegion(_, node_id)) => { + if let Some(lt) = cx.lt_substs.borrow().get(&node_id).cloned() { + return lt; } - _ => {} } + _ => {} } Lifetime(self.name.to_string()) } @@ -1122,7 +1106,7 @@ impl Clean for doctree::Function { visibility: self.vis.clean(cx), stability: self.stab.clean(cx), deprecation: self.depr.clean(cx), - def_id: cx.map.local_def_id(self.id), + def_id: cx.tcx.map.local_def_id(self.id), inner: FunctionItem(Function { decl: self.decl.clean(cx), generics: self.generics.clean(cx), @@ -1173,10 +1157,10 @@ impl Clean for hir::FnDecl { impl<'a, 'tcx> Clean for (DefId, &'a ty::PolyFnSig<'tcx>) { fn clean(&self, cx: &DocContext) -> FnDecl { let (did, sig) = *self; - let mut names = if cx.map.as_local_node_id(did).is_some() { + let mut names = if cx.tcx.map.as_local_node_id(did).is_some() { vec![].into_iter() } else { - cx.tcx().sess.cstore.fn_arg_names(did).into_iter() + cx.tcx.sess.cstore.fn_arg_names(did).into_iter() }.peekable(); FnDecl { output: Return(sig.0.output.clean(cx)), @@ -1264,7 +1248,7 @@ impl Clean for doctree::Trait { name: Some(self.name.clean(cx)), attrs: self.attrs.clean(cx), source: self.whence.clean(cx), - def_id: cx.map.local_def_id(self.id), + def_id: cx.tcx.map.local_def_id(self.id), visibility: self.vis.clean(cx), stability: self.stab.clean(cx), deprecation: self.depr.clean(cx), @@ -1314,10 +1298,10 @@ impl Clean for hir::TraitItem { name: Some(self.name.clean(cx)), attrs: self.attrs.clean(cx), source: self.span.clean(cx), - def_id: cx.map.local_def_id(self.id), + def_id: cx.tcx.map.local_def_id(self.id), visibility: None, - stability: get_stability(cx, cx.map.local_def_id(self.id)), - deprecation: get_deprecation(cx, cx.map.local_def_id(self.id)), + stability: get_stability(cx, cx.tcx.map.local_def_id(self.id)), + deprecation: get_deprecation(cx, cx.tcx.map.local_def_id(self.id)), inner: inner } } @@ -1346,10 +1330,10 @@ impl Clean for hir::ImplItem { name: Some(self.name.clean(cx)), source: self.span.clean(cx), attrs: self.attrs.clean(cx), - def_id: cx.map.local_def_id(self.id), + def_id: cx.tcx.map.local_def_id(self.id), visibility: self.vis.clean(cx), - stability: get_stability(cx, cx.map.local_def_id(self.id)), - deprecation: get_deprecation(cx, cx.map.local_def_id(self.id)), + stability: get_stability(cx, cx.tcx.map.local_def_id(self.id)), + deprecation: get_deprecation(cx, cx.tcx.map.local_def_id(self.id)), inner: inner } } @@ -1359,13 +1343,13 @@ impl<'tcx> Clean for ty::AssociatedItem { fn clean(&self, cx: &DocContext) -> Item { let inner = match self.kind { ty::AssociatedKind::Const => { - let ty = cx.tcx().item_type(self.def_id); + let ty = cx.tcx.item_type(self.def_id); AssociatedConstItem(ty.clean(cx), None) } ty::AssociatedKind::Method => { - let generics = (cx.tcx().item_generics(self.def_id), - &cx.tcx().item_predicates(self.def_id)).clean(cx); - let fty = match cx.tcx().item_type(self.def_id).sty { + let generics = (cx.tcx.item_generics(self.def_id), + &cx.tcx.item_predicates(self.def_id)).clean(cx); + let fty = match cx.tcx.item_type(self.def_id).sty { ty::TyFnDef(_, _, f) => f, _ => unreachable!() }; @@ -1374,9 +1358,9 @@ impl<'tcx> Clean for ty::AssociatedItem { if self.method_has_self_argument { let self_ty = match self.container { ty::ImplContainer(def_id) => { - cx.tcx().item_type(def_id) + cx.tcx.item_type(def_id) } - ty::TraitContainer(_) => cx.tcx().mk_self_type() + ty::TraitContainer(_) => cx.tcx.mk_self_type() }; let self_arg_ty = *fty.sig.input(0).skip_binder(); if self_arg_ty == self_ty { @@ -1422,8 +1406,8 @@ impl<'tcx> Clean for ty::AssociatedItem { // are actually located on the trait/impl itself, so we need to load // all of the generics from there and then look for bounds that are // applied to this associated type in question. - let def = cx.tcx().lookup_trait_def(did); - let predicates = cx.tcx().item_predicates(did); + let def = cx.tcx.lookup_trait_def(did); + let predicates = cx.tcx.item_predicates(did); let generics = (def.generics, &predicates).clean(cx); generics.where_predicates.iter().filter_map(|pred| { let (name, self_type, trait_, bounds) = match *pred { @@ -1459,7 +1443,7 @@ impl<'tcx> Clean for ty::AssociatedItem { } let ty = if self.defaultness.has_value() { - Some(cx.tcx().item_type(self.def_id)) + Some(cx.tcx.item_type(self.def_id)) } else { None }; @@ -1474,7 +1458,7 @@ impl<'tcx> Clean for ty::AssociatedItem { stability: get_stability(cx, self.def_id), deprecation: get_deprecation(cx, self.def_id), def_id: self.def_id, - attrs: inline::load_attrs(cx, cx.tcx(), self.def_id), + attrs: inline::load_attrs(cx, self.def_id), source: Span::empty(), inner: inner, } @@ -1727,53 +1711,44 @@ impl Clean for hir::Ty { type_: box m.ty.clean(cx)}, TySlice(ref ty) => Vector(box ty.clean(cx)), TyArray(ref ty, ref e) => { - let n = if let Some(tcx) = cx.tcx_opt() { - use rustc_const_math::{ConstInt, ConstUsize}; - use rustc_const_eval::eval_const_expr; - use rustc::middle::const_val::ConstVal; - match eval_const_expr(tcx, e) { - ConstVal::Integral(ConstInt::Usize(u)) => match u { - ConstUsize::Us16(u) => u.to_string(), - ConstUsize::Us32(u) => u.to_string(), - ConstUsize::Us64(u) => u.to_string(), - }, - // after type checking this can't fail - _ => unreachable!(), - } - } else { - pprust::expr_to_string(e) + use rustc_const_math::{ConstInt, ConstUsize}; + use rustc_const_eval::eval_const_expr; + use rustc::middle::const_val::ConstVal; + + let n = match eval_const_expr(cx.tcx, e) { + ConstVal::Integral(ConstInt::Usize(u)) => match u { + ConstUsize::Us16(u) => u.to_string(), + ConstUsize::Us32(u) => u.to_string(), + ConstUsize::Us64(u) => u.to_string(), + }, + // after type checking this can't fail + _ => unreachable!(), }; FixedVector(box ty.clean(cx), n) }, TyTup(ref tys) => Tuple(tys.clean(cx)), TyPath(None, ref path) => { - let tcx_and_def = cx.tcx_opt().map(|tcx| (tcx, tcx.expect_def(self.id))); - if let Some((_, def)) = tcx_and_def { - if let Some(new_ty) = cx.ty_substs.borrow().get(&def).cloned() { - return new_ty; - } + let def = cx.tcx.expect_def(self.id); + if let Some(new_ty) = cx.ty_substs.borrow().get(&def).cloned() { + return new_ty; } - let tcx_and_alias = tcx_and_def.and_then(|(tcx, def)| { - if let Def::TyAlias(def_id) = def { - // Substitute private type aliases - tcx.map.as_local_node_id(def_id).and_then(|node_id| { - if !cx.access_levels.borrow().is_exported(def_id) { - Some((tcx, &tcx.map.expect_item(node_id).node)) - } else { - None - } - }) - } else { - None + let mut alias = None; + if let Def::TyAlias(def_id) = def { + // Substitute private type aliases + if let Some(node_id) = cx.tcx.map.as_local_node_id(def_id) { + if !cx.access_levels.borrow().is_exported(def_id) { + alias = Some(&cx.tcx.map.expect_item(node_id).node); + } } - }); - if let Some((tcx, &hir::ItemTy(ref ty, ref generics))) = tcx_and_alias { + }; + + if let Some(&hir::ItemTy(ref ty, ref generics)) = alias { let provided_params = &path.segments.last().unwrap().parameters; let mut ty_substs = FxHashMap(); let mut lt_substs = FxHashMap(); for (i, ty_param) in generics.ty_params.iter().enumerate() { - let ty_param_def = tcx.expect_def(ty_param.id); + let ty_param_def = cx.tcx.expect_def(ty_param.id); if let Some(ty) = provided_params.types().get(i).cloned() .cloned() { ty_substs.insert(ty_param_def, ty.unwrap().clean(cx)); @@ -1841,9 +1816,7 @@ impl<'tcx> Clean for ty::Ty<'tcx> { ty::TyFloat(float_ty) => Primitive(float_ty.into()), ty::TyStr => Primitive(PrimitiveType::Str), ty::TyBox(t) => { - let box_did = cx.tcx_opt().and_then(|tcx| { - tcx.lang_items.owned_box() - }); + let box_did = cx.tcx.lang_items.owned_box(); lang_struct(cx, box_did, t, "Box", Unique) } ty::TySlice(ty) => Vector(box ty.clean(cx)), @@ -1863,7 +1836,7 @@ impl<'tcx> Clean for ty::Ty<'tcx> { type_params: Vec::new(), where_predicates: Vec::new() }, - decl: (cx.map.local_def_id(ast::CRATE_NODE_ID), &fty.sig).clean(cx), + decl: (cx.tcx.map.local_def_id(ast::CRATE_NODE_ID), &fty.sig).clean(cx), abi: fty.abi, }), ty::TyAdt(def, substs) => { @@ -1874,7 +1847,7 @@ impl<'tcx> Clean for ty::Ty<'tcx> { AdtKind::Enum => TypeKind::Enum, }; inline::record_extern_fqn(cx, did, kind); - let path = external_path(cx, &cx.tcx().item_name(did).as_str(), + let path = external_path(cx, &cx.tcx.item_name(did).as_str(), None, false, vec![], substs); ResolvedPath { path: path, @@ -1901,7 +1874,7 @@ impl<'tcx> Clean for ty::Ty<'tcx> { }); } - let path = external_path(cx, &cx.tcx().item_name(did).as_str(), + let path = external_path(cx, &cx.tcx.item_name(did).as_str(), Some(did), false, bindings, obj.principal.0.substs); ResolvedPath { path: path, @@ -1919,9 +1892,9 @@ impl<'tcx> Clean for ty::Ty<'tcx> { ty::TyAnon(def_id, substs) => { // Grab the "TraitA + TraitB" from `impl TraitA + TraitB`, // by looking up the projections associated with the def_id. - let item_predicates = cx.tcx().item_predicates(def_id); - let substs = cx.tcx().lift(&substs).unwrap(); - let bounds = item_predicates.instantiate(cx.tcx(), substs); + let item_predicates = cx.tcx.item_predicates(def_id); + let substs = cx.tcx.lift(&substs).unwrap(); + let bounds = item_predicates.instantiate(cx.tcx, substs); ImplTrait(bounds.predicates.into_iter().filter_map(|predicate| { predicate.to_opt_poly_trait_ref().clean(cx) }).collect()) @@ -1942,9 +1915,9 @@ impl Clean for hir::StructField { attrs: self.attrs.clean(cx), source: self.span.clean(cx), visibility: self.vis.clean(cx), - stability: get_stability(cx, cx.map.local_def_id(self.id)), - deprecation: get_deprecation(cx, cx.map.local_def_id(self.id)), - def_id: cx.map.local_def_id(self.id), + stability: get_stability(cx, cx.tcx.map.local_def_id(self.id)), + deprecation: get_deprecation(cx, cx.tcx.map.local_def_id(self.id)), + def_id: cx.tcx.map.local_def_id(self.id), inner: StructFieldItem(self.ty.clean(cx)), } } @@ -1954,7 +1927,7 @@ impl<'tcx> Clean for ty::FieldDefData<'tcx, 'static> { fn clean(&self, cx: &DocContext) -> Item { Item { name: Some(self.name).clean(cx), - attrs: cx.tcx().get_attrs(self.did).clean(cx), + attrs: cx.tcx.get_attrs(self.did).clean(cx), source: Span::empty(), visibility: self.vis.clean(cx), stability: get_stability(cx, self.did), @@ -2005,7 +1978,7 @@ impl Clean for doctree::Struct { name: Some(self.name.clean(cx)), attrs: self.attrs.clean(cx), source: self.whence.clean(cx), - def_id: cx.map.local_def_id(self.id), + def_id: cx.tcx.map.local_def_id(self.id), visibility: self.vis.clean(cx), stability: self.stab.clean(cx), deprecation: self.depr.clean(cx), @@ -2025,7 +1998,7 @@ impl Clean for doctree::Union { name: Some(self.name.clean(cx)), attrs: self.attrs.clean(cx), source: self.whence.clean(cx), - def_id: cx.map.local_def_id(self.id), + def_id: cx.tcx.map.local_def_id(self.id), visibility: self.vis.clean(cx), stability: self.stab.clean(cx), deprecation: self.depr.clean(cx), @@ -2072,7 +2045,7 @@ impl Clean for doctree::Enum { name: Some(self.name.clean(cx)), attrs: self.attrs.clean(cx), source: self.whence.clean(cx), - def_id: cx.map.local_def_id(self.id), + def_id: cx.tcx.map.local_def_id(self.id), visibility: self.vis.clean(cx), stability: self.stab.clean(cx), deprecation: self.depr.clean(cx), @@ -2099,7 +2072,7 @@ impl Clean for doctree::Variant { visibility: None, stability: self.stab.clean(cx), deprecation: self.depr.clean(cx), - def_id: cx.map.local_def_id(self.def.id()), + def_id: cx.tcx.map.local_def_id(self.def.id()), inner: VariantItem(Variant { kind: self.def.clean(cx), }), @@ -2124,7 +2097,7 @@ impl<'tcx> Clean for ty::VariantDefData<'tcx, 'static> { Item { source: Span::empty(), name: Some(field.name.clean(cx)), - attrs: cx.tcx().get_attrs(field.did).clean(cx), + attrs: cx.tcx.get_attrs(field.did).clean(cx), visibility: field.vis.clean(cx), def_id: field.did, stability: get_stability(cx, field.did), @@ -2137,7 +2110,7 @@ impl<'tcx> Clean for ty::VariantDefData<'tcx, 'static> { }; Item { name: Some(self.name.clean(cx)), - attrs: inline::load_attrs(cx, cx.tcx(), self.did), + attrs: inline::load_attrs(cx, self.did), source: Span::empty(), visibility: Some(Inherited), def_id: self.did, @@ -2322,7 +2295,7 @@ impl Clean for doctree::Typedef { name: Some(self.name.clean(cx)), attrs: self.attrs.clean(cx), source: self.whence.clean(cx), - def_id: cx.map.local_def_id(self.id.clone()), + def_id: cx.tcx.map.local_def_id(self.id.clone()), visibility: self.vis.clean(cx), stability: self.stab.clean(cx), deprecation: self.depr.clean(cx), @@ -2374,7 +2347,7 @@ impl Clean for doctree::Static { name: Some(self.name.clean(cx)), attrs: self.attrs.clean(cx), source: self.whence.clean(cx), - def_id: cx.map.local_def_id(self.id), + def_id: cx.tcx.map.local_def_id(self.id), visibility: self.vis.clean(cx), stability: self.stab.clean(cx), deprecation: self.depr.clean(cx), @@ -2399,7 +2372,7 @@ impl Clean for doctree::Constant { name: Some(self.name.clean(cx)), attrs: self.attrs.clean(cx), source: self.whence.clean(cx), - def_id: cx.map.local_def_id(self.id), + def_id: cx.tcx.map.local_def_id(self.id), visibility: self.vis.clean(cx), stability: self.stab.clean(cx), deprecation: self.depr.clean(cx), @@ -2460,24 +2433,22 @@ impl Clean> for doctree::Impl { // If this impl block is an implementation of the Deref trait, then we // need to try inlining the target's inherent impl blocks as well. - if trait_.def_id() == cx.deref_trait_did.get() { + if trait_.def_id() == cx.tcx.lang_items.deref_trait() { build_deref_target_impls(cx, &items, &mut ret); } - let provided = trait_.def_id().and_then(|did| { - cx.tcx_opt().map(|tcx| { - tcx.provided_trait_methods(did) - .into_iter() - .map(|meth| meth.name.to_string()) - .collect() - }) + let provided = trait_.def_id().map(|did| { + cx.tcx.provided_trait_methods(did) + .into_iter() + .map(|meth| meth.name.to_string()) + .collect() }).unwrap_or(FxHashSet()); ret.push(Item { name: None, attrs: self.attrs.clean(cx), source: self.whence.clean(cx), - def_id: cx.map.local_def_id(self.id), + def_id: cx.tcx.map.local_def_id(self.id), visibility: self.vis.clean(cx), stability: self.stab.clean(cx), deprecation: self.depr.clean(cx), @@ -2498,10 +2469,7 @@ impl Clean> for doctree::Impl { fn build_deref_target_impls(cx: &DocContext, items: &[Item], ret: &mut Vec) { - let tcx = match cx.tcx_opt() { - Some(t) => t, - None => return, - }; + let tcx = cx.tcx; for item in items { let target = match item.inner { @@ -2511,7 +2479,7 @@ fn build_deref_target_impls(cx: &DocContext, let primitive = match *target { ResolvedPath { did, .. } if did.is_local() => continue, ResolvedPath { did, .. } => { - ret.extend(inline::build_impls(cx, tcx, did)); + ret.extend(inline::build_impls(cx, did)); continue } _ => match target.primitive_type() { @@ -2542,7 +2510,7 @@ fn build_deref_target_impls(cx: &DocContext, }; if let Some(did) = did { if !did.is_local() { - inline::build_impl(cx, tcx, did, ret); + inline::build_impl(cx, did, ret); } } } @@ -2560,7 +2528,7 @@ impl Clean for doctree::DefaultImpl { name: None, attrs: self.attrs.clean(cx), source: self.whence.clean(cx), - def_id: cx.map.local_def_id(self.id), + def_id: cx.tcx.map.local_def_id(self.id), visibility: Some(Public), stability: None, deprecation: None, @@ -2644,7 +2612,7 @@ impl Clean> for doctree::Import { name: None, attrs: self.attrs.clean(cx), source: self.whence.clean(cx), - def_id: cx.map.local_def_id(ast::CRATE_NODE_ID), + def_id: cx.tcx.map.local_def_id(ast::CRATE_NODE_ID), visibility: self.vis.clean(cx), stability: None, deprecation: None, @@ -2723,10 +2691,10 @@ impl Clean for hir::ForeignItem { name: Some(self.name.clean(cx)), attrs: self.attrs.clean(cx), source: self.span.clean(cx), - def_id: cx.map.local_def_id(self.id), + def_id: cx.tcx.map.local_def_id(self.id), visibility: self.vis.clean(cx), - stability: get_stability(cx, cx.map.local_def_id(self.id)), - deprecation: get_deprecation(cx, cx.map.local_def_id(self.id)), + stability: get_stability(cx, cx.tcx.map.local_def_id(self.id)), + deprecation: get_deprecation(cx, cx.tcx.map.local_def_id(self.id)), inner: inner, } } @@ -2793,22 +2761,7 @@ fn resolve_type(cx: &DocContext, path: Path, id: ast::NodeId) -> Type { debug!("resolve_type({:?},{:?})", path, id); - let tcx = match cx.tcx_opt() { - Some(tcx) => tcx, - // If we're extracting tests, this return value's accuracy is not - // important, all we want is a string representation to help people - // figure out what doctests are failing. - None => { - let did = DefId::local(DefIndex::from_u32(0)); - return ResolvedPath { - path: path, - typarams: None, - did: did, - is_generic: false - }; - } - }; - let def = tcx.expect_def(id); + let def = cx.tcx.expect_def(id); debug!("resolve_type: def={:?}", def); let is_generic = match def { @@ -2833,8 +2786,6 @@ fn resolve_type(cx: &DocContext, fn register_def(cx: &DocContext, def: Def) -> DefId { debug!("register_def({:?})", def); - let tcx = cx.tcx(); - let (did, kind) = match def { Def::Fn(i) => (i, TypeKind::Function), Def::TyAlias(i) => (i, TypeKind::Typedef), @@ -2844,7 +2795,7 @@ fn register_def(cx: &DocContext, def: Def) -> DefId { Def::Union(i) => (i, TypeKind::Union), Def::Mod(i) => (i, TypeKind::Module), Def::Static(i, _) => (i, TypeKind::Static), - Def::Variant(i) => (tcx.parent_def_id(i).unwrap(), TypeKind::Enum), + Def::Variant(i) => (cx.tcx.parent_def_id(i).unwrap(), TypeKind::Enum), Def::SelfTy(Some(def_id), _) => (def_id, TypeKind::Trait), Def::SelfTy(_, Some(impl_def_id)) => { return impl_def_id @@ -2854,7 +2805,7 @@ fn register_def(cx: &DocContext, def: Def) -> DefId { if did.is_local() { return did } inline::record_extern_fqn(cx, did, kind); if let TypeKind::Trait = kind { - let t = inline::build_external_trait(cx, tcx, did); + let t = inline::build_external_trait(cx, did); cx.external_traits.borrow_mut().insert(did, t); } did @@ -2868,9 +2819,7 @@ fn resolve_use_source(cx: &DocContext, path: Path, id: ast::NodeId) -> ImportSou } fn resolve_def(cx: &DocContext, id: ast::NodeId) -> Option { - cx.tcx_opt().and_then(|tcx| { - tcx.expect_def_or_none(id).map(|def| register_def(cx, def)) - }) + cx.tcx.expect_def_or_none(id).map(|def| register_def(cx, def)) } #[derive(Clone, RustcEncodable, RustcDecodable, Debug)] @@ -2889,7 +2838,7 @@ impl Clean for doctree::Macro { visibility: Some(Public), stability: self.stab.clean(cx), deprecation: self.depr.clean(cx), - def_id: cx.map.local_def_id(self.id), + def_id: cx.tcx.map.local_def_id(self.id), inner: MacroItem(Macro { source: format!("macro_rules! {} {{\n{}}}", name, diff --git a/src/librustdoc/clean/simplify.rs b/src/librustdoc/clean/simplify.rs index 19e084905aa..7240f0aedbd 100644 --- a/src/librustdoc/clean/simplify.rs +++ b/src/librustdoc/clean/simplify.rs @@ -153,7 +153,7 @@ fn trait_is_same_or_supertrait(cx: &DocContext, child: DefId, if child == trait_ { return true } - let predicates = cx.tcx().item_super_predicates(child).predicates; + let predicates = cx.tcx.item_super_predicates(child).predicates; predicates.iter().filter_map(|pred| { if let ty::Predicate::Trait(ref pred) = *pred { if pred.0.trait_ref.self_ty().is_self() { diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs index a25cb0bacc5..7d7b7fead58 100644 --- a/src/librustdoc/core.rs +++ b/src/librustdoc/core.rs @@ -7,7 +7,6 @@ // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. -pub use self::MaybeTyped::*; use rustc_lint; use rustc_driver::{driver, target_features, abort_on_err}; @@ -42,21 +41,12 @@ use html::render::RenderInfo; pub use rustc::session::config::Input; pub use rustc::session::search_paths::SearchPaths; -/// Are we generating documentation (`Typed`) or tests (`NotTyped`)? -pub enum MaybeTyped<'a, 'tcx: 'a> { - Typed(TyCtxt<'a, 'tcx, 'tcx>), - NotTyped(&'a session::Session) -} - pub type ExternalPaths = FxHashMap, clean::TypeKind)>; pub struct DocContext<'a, 'tcx: 'a> { - pub map: &'a hir_map::Map<'tcx>, - pub maybe_typed: MaybeTyped<'a, 'tcx>, + pub tcx: TyCtxt<'a, 'tcx, 'tcx>, pub input: Input, pub populated_all_crate_impls: Cell, - pub deref_trait_did: Cell>, - pub deref_mut_trait_did: Cell>, // Note that external items for which `doc(hidden)` applies to are shown as // non-reachable while local items aren't. This is because we're reusing // the access levels from crateanalysis. @@ -77,24 +67,9 @@ pub struct DocContext<'a, 'tcx: 'a> { pub export_map: ExportMap, } -impl<'b, 'tcx> DocContext<'b, 'tcx> { - pub fn sess<'a>(&'a self) -> &'a session::Session { - match self.maybe_typed { - Typed(tcx) => &tcx.sess, - NotTyped(ref sess) => sess - } - } - - pub fn tcx_opt<'a>(&'a self) -> Option> { - match self.maybe_typed { - Typed(tcx) => Some(tcx), - NotTyped(_) => None - } - } - - pub fn tcx<'a>(&'a self) -> TyCtxt<'a, 'tcx, 'tcx> { - let tcx_opt = self.tcx_opt(); - tcx_opt.expect("tcx not present") +impl<'a, 'tcx> DocContext<'a, 'tcx> { + pub fn sess(&self) -> &session::Session { + &self.tcx.sess } /// Call the closure with the given parameters set as @@ -208,12 +183,9 @@ pub fn run_core(search_paths: SearchPaths, }; let ctxt = DocContext { - map: &tcx.map, - maybe_typed: Typed(tcx), + tcx: tcx, input: input, populated_all_crate_impls: Cell::new(false), - deref_trait_did: Cell::new(None), - deref_mut_trait_did: Cell::new(None), access_levels: RefCell::new(access_levels), external_traits: Default::default(), renderinfo: Default::default(), @@ -221,11 +193,11 @@ pub fn run_core(search_paths: SearchPaths, lt_substs: Default::default(), export_map: export_map, }; - debug!("crate: {:?}", ctxt.map.krate()); + debug!("crate: {:?}", tcx.map.krate()); let krate = { let mut v = RustdocVisitor::new(&ctxt); - v.visit(ctxt.map.krate()); + v.visit(tcx.map.krate()); v.clean(&ctxt) }; diff --git a/src/librustdoc/visit_ast.rs b/src/librustdoc/visit_ast.rs index f1eb65ee16e..8ed0567d820 100644 --- a/src/librustdoc/visit_ast.rs +++ b/src/librustdoc/visit_ast.rs @@ -65,18 +65,13 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { } fn stability(&self, id: ast::NodeId) -> Option { - self.cx.tcx_opt().and_then(|tcx| { - self.cx.map.opt_local_def_id(id) - .and_then(|def_id| tcx.lookup_stability(def_id)) - .cloned() - }) + self.cx.tcx.map.opt_local_def_id(id) + .and_then(|def_id| self.cx.tcx.lookup_stability(def_id)).cloned() } fn deprecation(&self, id: ast::NodeId) -> Option { - self.cx.tcx_opt().and_then(|tcx| { - self.cx.map.opt_local_def_id(id) - .and_then(|def_id| tcx.lookup_deprecation(def_id)) - }) + self.cx.tcx.map.opt_local_def_id(id) + .and_then(|def_id| self.cx.tcx.lookup_deprecation(def_id)) } pub fn visit(&mut self, krate: &hir::Crate) { @@ -196,7 +191,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { let orig_inside_public_path = self.inside_public_path; self.inside_public_path &= vis == hir::Public; for i in &m.item_ids { - let item = self.cx.map.expect_item(i.id); + let item = self.cx.tcx.map.expect_item(i.id); self.visit_item(item, None, &mut om); } self.inside_public_path = orig_inside_public_path; @@ -279,9 +274,9 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { glob: bool, om: &mut Module, please_inline: bool) -> bool { fn inherits_doc_hidden(cx: &core::DocContext, mut node: ast::NodeId) -> bool { - while let Some(id) = cx.map.get_enclosing_scope(node) { + while let Some(id) = cx.tcx.map.get_enclosing_scope(node) { node = id; - if cx.map.attrs(node).lists("doc").has_word("hidden") { + if cx.tcx.map.attrs(node).lists("doc").has_word("hidden") { return true; } if node == ast::CRATE_NODE_ID { @@ -291,10 +286,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { false } - let tcx = match self.cx.tcx_opt() { - Some(tcx) => tcx, - None => return false - }; + let tcx = self.cx.tcx; let def = tcx.expect_def(id); let def_did = def.def_id(); @@ -308,7 +300,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { // made reachable by cross-crate inlining which we're checking here. // (this is done here because we need to know this upfront) if !def_did.is_local() && !is_no_inline { - let attrs = clean::inline::load_attrs(self.cx, tcx, def_did); + let attrs = clean::inline::load_attrs(self.cx, def_did); let self_is_hidden = attrs.lists("doc").has_word("hidden"); match def { Def::Trait(did) | @@ -347,7 +339,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { match it.node { hir::ItemMod(ref m) => { for i in &m.item_ids { - let i = self.cx.map.expect_item(i.id); + let i = self.cx.tcx.map.expect_item(i.id); self.visit_item(i, None, om); } } @@ -506,7 +498,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { // regardless of where they're located. if !self.inlining { let items = item_ids.iter() - .map(|ii| self.cx.map.impl_item(ii.id).clone()) + .map(|ii| self.cx.tcx.map.impl_item(ii.id).clone()) .collect(); let i = Impl { unsafety: unsafety, diff --git a/src/librustdoc/visit_lib.rs b/src/librustdoc/visit_lib.rs index 172d070c55d..cee292f9915 100644 --- a/src/librustdoc/visit_lib.rs +++ b/src/librustdoc/visit_lib.rs @@ -49,7 +49,7 @@ impl<'a, 'b, 'tcx> LibEmbargoVisitor<'a, 'b, 'tcx> { // Updates node level and returns the updated level fn update(&mut self, did: DefId, level: Option) -> Option { - let is_hidden = self.cx.tcx().get_attrs(did).lists("doc").has_word("hidden"); + let is_hidden = self.cx.tcx.get_attrs(did).lists("doc").has_word("hidden"); let old_level = self.access_levels.map.get(&did).cloned(); // Accessibility levels can only grow