Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rustdoc] Box some more ItemKind variants #79975

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/librustdoc/clean/auto_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,15 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
};

Some(Item {
source: Span::empty(),
source: Span::dummy(),
name: None,
attrs: Default::default(),
visibility: Inherited,
def_id: self.cx.next_def_id(param_env_def_id.krate),
stability: None,
const_stability: None,
deprecation: None,
kind: ImplItem(Impl {
kind: ImplItem(box Impl {
unsafety: hir::Unsafety::Normal,
generics: new_generics,
provided_trait_methods: Default::default(),
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/clean/blanket_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
stability: None,
const_stability: None,
deprecation: None,
kind: ImplItem(Impl {
kind: ImplItem(box Impl {
unsafety: hir::Unsafety::Normal,
generics: (
self.cx.tcx.generics_of(impl_def_id),
Expand Down
8 changes: 4 additions & 4 deletions src/librustdoc/clean/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ crate fn try_inline(
}
Res::Def(DefKind::Fn, did) => {
record_extern_fqn(cx, did, clean::TypeKind::Function);
clean::FunctionItem(build_external_function(cx, did))
clean::FunctionItem(box build_external_function(cx, did))
}
Res::Def(DefKind::Struct, did) => {
record_extern_fqn(cx, did, clean::TypeKind::Struct);
Expand All @@ -77,7 +77,7 @@ crate fn try_inline(
Res::Def(DefKind::TyAlias, did) => {
record_extern_fqn(cx, did, clean::TypeKind::Typedef);
ret.extend(build_impls(cx, Some(parent_module), did, attrs));
clean::TypedefItem(build_type_alias(cx, did), false)
clean::TypedefItem(box build_type_alias(cx, did), false)
}
Res::Def(DefKind::Enum, did) => {
record_extern_fqn(cx, did, clean::TypeKind::Enum);
Expand Down Expand Up @@ -438,7 +438,7 @@ crate fn build_impl(
ret.push(clean::Item::from_def_id_and_parts(
did,
None,
clean::ImplItem(clean::Impl {
clean::ImplItem(box clean::Impl {
unsafety: hir::Unsafety::Normal,
generics,
provided_trait_methods: provided,
Expand Down Expand Up @@ -479,7 +479,7 @@ fn build_module(cx: &DocContext<'_>, did: DefId, visited: &mut FxHashSet<DefId>)
items.push(clean::Item {
name: None,
attrs: clean::Attributes::default(),
source: clean::Span::empty(),
source: clean::Span::dummy(),
def_id: DefId::local(CRATE_DEF_INDEX),
visibility: clean::Public,
stability: None,
Expand Down
49 changes: 14 additions & 35 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use rustc_middle::ty::{self, AdtKind, Lift, Ty, TyCtxt};
use rustc_mir::const_eval::{is_const_fn, is_min_const_fn, is_unstable_const_fn};
use rustc_span::hygiene::{AstPass, MacroKind};
use rustc_span::symbol::{kw, sym, Ident, Symbol};
use rustc_span::{self, ExpnKind, Pos};
use rustc_span::{self, ExpnKind};
use rustc_typeck::hir_ty_to_ty;

use std::collections::hash_map::Entry;
Expand Down Expand Up @@ -919,7 +919,7 @@ fn clean_fn_or_proc_macro(
} else {
hir::Constness::NotConst
};
FunctionItem(func)
FunctionItem(box func)
}
}
}
Expand Down Expand Up @@ -1076,7 +1076,7 @@ impl Clean<Item> for hir::TraitItem<'_> {
{
m.header.constness = hir::Constness::NotConst;
}
MethodItem(m, None)
MethodItem(box m, None)
}
hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Required(ref names)) => {
let (generics, decl) = enter_impl_trait(cx, || {
Expand All @@ -1090,7 +1090,7 @@ impl Clean<Item> for hir::TraitItem<'_> {
{
t.header.constness = hir::Constness::NotConst;
}
TyMethodItem(t)
TyMethodItem(box t)
}
hir::TraitItemKind::Type(ref bounds, ref default) => {
AssocTypeItem(bounds.clean(cx), default.clean(cx))
Expand All @@ -1116,12 +1116,12 @@ impl Clean<Item> for hir::ImplItem<'_> {
{
m.header.constness = hir::Constness::NotConst;
}
MethodItem(m, Some(self.defaultness))
MethodItem(box 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)
TypedefItem(box Typedef { type_, generics: Generics::default(), item_type }, true)
}
};
Item::from_def_id_and_parts(local_did, Some(self.ident.name.clean(cx)), inner, cx)
Expand Down Expand Up @@ -1185,7 +1185,7 @@ impl Clean<Item> for ty::AssocItem {
ty::TraitContainer(_) => None,
};
MethodItem(
Function {
box Function {
generics,
decl,
header: hir::FnHeader {
Expand All @@ -1200,7 +1200,7 @@ impl Clean<Item> for ty::AssocItem {
defaultness,
)
} else {
TyMethodItem(Function {
TyMethodItem(box Function {
generics,
decl,
header: hir::FnHeader {
Expand Down Expand Up @@ -1270,7 +1270,7 @@ impl Clean<Item> for ty::AssocItem {
let type_ = cx.tcx.type_of(self.def_id).clean(cx);
let item_type = type_.def_id().and_then(|did| inline::build_ty(cx, did));
TypedefItem(
Typedef {
box Typedef {
type_,
generics: Generics { params: Vec::new(), where_predicates: Vec::new() },
item_type,
Expand Down Expand Up @@ -1881,29 +1881,8 @@ impl Clean<VariantKind> for hir::VariantData<'_> {
}

impl Clean<Span> for rustc_span::Span {
fn clean(&self, cx: &DocContext<'_>) -> Span {
if self.is_dummy() {
return Span::empty();
}

// Get the macro invocation instead of the definition,
// in case the span is result of a macro expansion.
// (See rust-lang/rust#39726)
let span = self.source_callsite();

let sm = cx.sess().source_map();
let filename = sm.span_to_filename(span);
let lo = sm.lookup_char_pos(span.lo());
let hi = sm.lookup_char_pos(span.hi());
Span {
filename,
cnum: lo.file.cnum,
loline: lo.line,
locol: lo.col.to_usize(),
hiline: hi.line,
hicol: hi.col.to_usize(),
original: span,
}
fn clean(&self, _cx: &DocContext<'_>) -> Span {
Span::from_rustc_span(*self)
}
}

Expand Down Expand Up @@ -2002,7 +1981,7 @@ impl Clean<Vec<Item>> for (&hir::Item<'_>, Option<Symbol>) {
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 },
box Typedef { type_: rustdoc_ty, generics: generics.clean(cx), item_type },
false,
)
}
Expand Down Expand Up @@ -2109,7 +2088,7 @@ fn clean_impl(impl_: &hir::Item<'_>, cx: &DocContext<'_>) -> Vec<Item> {
_ => None,
});
let make_item = |trait_: Option<Type>, for_: Type, items: Vec<Item>| {
let kind = ImplItem(Impl {
let kind = ImplItem(box Impl {
unsafety,
generics: generics.clean(cx),
provided_trait_methods: provided.clone(),
Expand Down Expand Up @@ -2284,7 +2263,7 @@ impl Clean<Item> for (&hir::ForeignItem<'_>, Option<Symbol>) {
(generics.clean(cx), (&**decl, &names[..]).clean(cx))
});
let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
ForeignFunctionItem(Function {
ForeignFunctionItem(box Function {
decl,
generics,
header: hir::FnHeader {
Expand Down
68 changes: 40 additions & 28 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_feature::UnstableFeatures;
use rustc_hir as hir;
use rustc_hir::def::Res;
use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
use rustc_hir::def_id::{CrateNum, DefId};
use rustc_hir::lang_items::LangItem;
use rustc_hir::Mutability;
use rustc_index::vec::IndexVec;
use rustc_middle::ty::{AssocKind, TyCtxt};
use rustc_session::Session;
use rustc_span::hygiene::MacroKind;
use rustc_span::source_map::DUMMY_SP;
use rustc_span::symbol::{kw, sym, Ident, Symbol, SymbolStr};
use rustc_span::{self, FileName};
use rustc_span::{self, FileName, Loc};
use rustc_target::abi::VariantIdx;
use rustc_target::spec::abi::Abi;
use smallvec::{smallvec, SmallVec};
Expand Down Expand Up @@ -304,24 +305,26 @@ crate enum ItemKind {
StructItem(Struct),
UnionItem(Union),
EnumItem(Enum),
FunctionItem(Function),
FunctionItem(Box<Function>),
ModuleItem(Module),
TypedefItem(Typedef, bool /* is associated type */),
TypedefItem(Box<Typedef>, bool /* is associated type */),
OpaqueTyItem(OpaqueTy),
StaticItem(Static),
ConstantItem(Constant),
TraitItem(Trait),
TraitAliasItem(TraitAlias),
ImplItem(Impl),
// Impl is 400 bytes (!!), so box it to avoid penalizing other items
// FIXME(jyn514): calculate this information on demand instead
ImplItem(Box<Impl>),
/// A method signature only. Used for required methods in traits (ie,
/// non-default-methods).
TyMethodItem(Function),
TyMethodItem(Box<Function>),
/// A method with a body.
MethodItem(Function, Option<hir::Defaultness>),
MethodItem(Box<Function>, Option<hir::Defaultness>),
StructFieldItem(Type),
VariantItem(Variant),
/// `fn`s from an extern block
ForeignFunctionItem(Function),
ForeignFunctionItem(Box<Function>),
/// `static`s from an extern block
ForeignStaticItem(Static),
/// `type`s from an extern block
Expand Down Expand Up @@ -1609,32 +1612,41 @@ crate enum VariantKind {
Struct(VariantStruct),
}

/// Small wrapper around `rustc_span::Span` that adds helper methods and enforces calling `source_callsite`.
#[derive(Clone, Debug)]
crate struct Span {
crate filename: FileName,
crate cnum: CrateNum,
crate loline: usize,
crate locol: usize,
crate hiline: usize,
crate hicol: usize,
crate original: rustc_span::Span,
}
crate struct Span(rustc_span::Span);

impl Span {
crate fn empty() -> Span {
Span {
filename: FileName::Anon(0),
cnum: LOCAL_CRATE,
loline: 0,
locol: 0,
hiline: 0,
hicol: 0,
original: rustc_span::DUMMY_SP,
}
crate fn from_rustc_span(sp: rustc_span::Span) -> Self {
// Get the macro invocation instead of the definition,
// in case the span is result of a macro expansion.
// (See rust-lang/rust#39726)
Self(sp.source_callsite())
}

crate fn dummy() -> Self {
Self(rustc_span::DUMMY_SP)
}

crate fn span(&self) -> rustc_span::Span {
self.original
self.0
}

crate fn filename(&self, sess: &Session) -> FileName {
sess.source_map().span_to_filename(self.0)
}

crate fn lo(&self, sess: &Session) -> Loc {
sess.source_map().lookup_char_pos(self.0.lo())
}

crate fn hi(&self, sess: &Session) -> Loc {
sess.source_map().lookup_char_pos(self.0.hi())
}

crate fn cnum(&self, sess: &Session) -> CrateNum {
// FIXME: is there a time when the lo and hi crate would be different?
self.lo(sess).file.cnum
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/librustdoc/formats/renderer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::sync::Arc;

use rustc_data_structures::sync::Lrc;
use rustc_session::Session;
use rustc_span::edition::Edition;

use crate::clean;
Expand All @@ -19,6 +21,7 @@ crate trait FormatRenderer: Clone {
render_info: RenderInfo,
edition: Edition,
cache: &mut Cache,
sess: Lrc<Session>,
) -> Result<(Self, clean::Crate), Error>;

/// Renders a single non-module item. This means no recursive sub-item rendering is required.
Expand Down Expand Up @@ -49,6 +52,7 @@ crate fn run_format<T: FormatRenderer>(
render_info: RenderInfo,
diag: &rustc_errors::Handler,
edition: Edition,
sess: Lrc<Session>,
) -> Result<(), Error> {
let (krate, mut cache) = Cache::from_krate(
render_info.clone(),
Expand All @@ -59,7 +63,7 @@ crate fn run_format<T: FormatRenderer>(
);

let (mut format_renderer, mut krate) =
T::init(krate, options, render_info, edition, &mut cache)?;
T::init(krate, options, render_info, edition, &mut cache, sess)?;

let cache = Arc::new(cache);
// Freeze the cache now that the index has been built. Put an Arc into TLS for future
Expand Down
Loading