Skip to content

Commit

Permalink
Get rid of item.span
Browse files Browse the repository at this point in the history
- Remove `span` field, adding `Item::span()` instead
- Special-case `Impl` and `Module` items
- Use dummy spans for primitive items
  • Loading branch information
jyn514 committed Apr 24, 2021
1 parent b412b46 commit c44c64d
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/librustdoc/clean/auto_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
};

Some(Item {
span: Span::dummy(),
name: None,
attrs: Default::default(),
visibility: Inherited,
def_id: self.cx.next_def_id(item_def_id.krate),
kind: box ImplItem(Impl {
span: Span::dummy(),
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 @@ -100,12 +100,12 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
.collect();

impls.push(Item {
span: self.cx.tcx.def_span(impl_def_id).clean(self.cx),
name: None,
attrs: Default::default(),
visibility: Inherited,
def_id: self.cx.next_def_id(impl_def_id.krate),
kind: box ImplItem(Impl {
span: self.cx.tcx.def_span(impl_def_id).clean(self.cx),
unsafety: hir::Unsafety::Normal,
generics: (
self.cx.tcx.generics_of(impl_def_id),
Expand Down
6 changes: 3 additions & 3 deletions src/librustdoc/clean/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use rustc_ast as ast;
use rustc_data_structures::fx::FxHashSet;
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX};
use rustc_hir::def_id::DefId;
use rustc_hir::Mutability;
use rustc_metadata::creader::LoadedMacro;
use rustc_middle::ty::{self, TyCtxt};
Expand Down Expand Up @@ -422,6 +422,7 @@ crate fn build_impl(
did,
None,
clean::ImplItem(clean::Impl {
span: clean::types::rustc_span(did, cx.tcx),
unsafety: hir::Unsafety::Normal,
generics,
provided_trait_methods: provided,
Expand Down Expand Up @@ -459,8 +460,7 @@ fn build_module(
items.push(clean::Item {
name: None,
attrs: box clean::Attributes::default(),
span: clean::Span::dummy(),
def_id: DefId::local(CRATE_DEF_INDEX),
def_id: cx.next_def_id(did.krate),
visibility: clean::Public,
kind: box clean::ImportItem(clean::Import::new_simple(
item.ident.name,
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1945,6 +1945,7 @@ fn clean_impl(impl_: &hir::Impl<'_>, hir_id: hir::HirId, cx: &mut DocContext<'_>
});
let mut make_item = |trait_: Option<Type>, for_: Type, items: Vec<Item>| {
let kind = ImplItem(Impl {
span: types::rustc_span(tcx.hir().local_def_id(hir_id).to_def_id(), tcx),
unsafety: impl_.unsafety,
generics: impl_.generics.clean(cx),
provided_trait_methods: provided.clone(),
Expand Down Expand Up @@ -2004,7 +2005,6 @@ fn clean_extern_crate(
vec![Item {
name: Some(name),
attrs: box attrs.clean(cx),
span: krate.span.clean(cx),
def_id: crate_def_id,
visibility: krate.vis.clean(cx),
kind: box ExternCrateItem { src: orig_name },
Expand Down
41 changes: 24 additions & 17 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ impl ExternalCrate {
/// directly to the AST's concept of an item; it's a strict superset.
#[derive(Clone)]
crate struct Item {
crate span: Span,
/// The name of this item.
/// Optional because not every item has a name, e.g. impls.
crate name: Option<Symbol>,
Expand All @@ -225,14 +224,13 @@ crate struct Item {

// `Item` is used a lot. Make sure it doesn't unintentionally get bigger.
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
rustc_data_structures::static_assert_size!(Item, 48);
rustc_data_structures::static_assert_size!(Item, 40);

impl fmt::Debug for Item {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
let def_id: &dyn fmt::Debug = if self.is_fake() { &"**FAKE**" } else { &self.def_id };

fmt.debug_struct("Item")
.field("source", &self.span())
.field("name", &self.name)
.field("attrs", &self.attrs)
.field("kind", &self.kind)
Expand All @@ -242,6 +240,16 @@ impl fmt::Debug for Item {
}
}

crate fn rustc_span(def_id: DefId, tcx: TyCtxt<'_>) -> Span {
Span::from_rustc_span(def_id.as_local().map_or_else(
|| tcx.def_span(def_id),
|local| {
let hir = tcx.hir();
hir.span_with_body(hir.local_def_id_to_hir_id(local))
},
))
}

impl Item {
crate fn stability<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Option<&'tcx Stability> {
if self.is_fake() { None } else { tcx.lookup_stability(self.def_id) }
Expand All @@ -255,12 +263,20 @@ impl Item {
if self.is_fake() { None } else { tcx.lookup_deprecation(self.def_id) }
}

crate fn span(&self) -> Span {
if let ItemKind::ModuleItem(Module { span, .. }) = &*self.kind { *span } else { self.span }
crate fn span(&self, tcx: TyCtxt<'_>) -> Span {
if let ItemKind::ModuleItem(Module { span, .. }) | ItemKind::ImplItem(Impl { span, .. }) =
&*self.kind
{
*span
} else if self.is_fake() {
Span::dummy()
} else {
rustc_span(self.def_id, tcx)
}
}

crate fn attr_span(&self, _tcx: TyCtxt<'_>) -> rustc_span::Span {
crate::passes::span_of_attrs(&self.attrs).unwrap_or_else(|| self.span().inner())
crate fn attr_span(&self, tcx: TyCtxt<'_>) -> rustc_span::Span {
crate::passes::span_of_attrs(&self.attrs).unwrap_or_else(|| self.span(tcx).inner())
}

/// Finds the `doc` attribute as a NameValue and returns the corresponding
Expand Down Expand Up @@ -304,20 +320,10 @@ impl Item {
) -> Item {
debug!("name={:?}, def_id={:?}", name, def_id);

// `span_if_local()` lies about functions and only gives the span of the function signature
let span = def_id.as_local().map_or_else(
|| cx.tcx.def_span(def_id),
|local| {
let hir = cx.tcx.hir();
hir.span_with_body(hir.local_def_id_to_hir_id(local))
},
);

Item {
def_id,
kind: box kind,
name,
span: span.clean(cx),
attrs,
visibility: cx.tcx.visibility(def_id).clean(cx),
}
Expand Down Expand Up @@ -2117,6 +2123,7 @@ impl Constant {

#[derive(Clone, Debug)]
crate struct Impl {
crate span: Span,
crate unsafety: hir::Unsafety,
crate generics: Generics,
crate provided_trait_methods: FxHashSet<Symbol>,
Expand Down
10 changes: 5 additions & 5 deletions src/librustdoc/html/render/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,15 +281,15 @@ impl<'tcx> Context<'tcx> {
/// may happen, for example, with externally inlined items where the source
/// of their crate documentation isn't known.
pub(super) fn src_href(&self, item: &clean::Item) -> Option<String> {
if item.span().is_dummy() {
if item.span(self.tcx()).is_dummy() {
return None;
}
let mut root = self.root_path();
let mut path = String::new();
let cnum = item.span().cnum(self.sess());
let cnum = item.span(self.tcx()).cnum(self.sess());

// We can safely ignore synthetic `SourceFile`s.
let file = match item.span().filename(self.sess()) {
let file = match item.span(self.tcx()).filename(self.sess()) {
FileName::Real(ref path) => path.local_path().to_path_buf(),
_ => return None,
};
Expand Down Expand Up @@ -323,8 +323,8 @@ impl<'tcx> Context<'tcx> {
(&*symbol, &path)
};

let loline = item.span().lo(self.sess()).line;
let hiline = item.span().hi(self.sess()).line;
let loline = item.span(self.tcx()).lo(self.sess()).line;
let hiline = item.span(self.tcx()).hi(self.sess()).line;
let lines =
if loline == hiline { loline.to_string() } else { format!("{}-{}", loline, hiline) };
Some(format!(
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1013,7 +1013,7 @@ fn item_macro(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Mac
Some("macro"),
None,
None,
it.span().inner().edition(),
it.span(cx.tcx()).inner().edition(),
);
});
document(w, cx, it, None)
Expand Down
8 changes: 4 additions & 4 deletions src/librustdoc/html/sources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ impl DocFolder for SourceCollector<'_, '_> {
// then we need to render it out to the filesystem.
if self.scx.include_sources
// skip all synthetic "files"
&& item.span().filename(self.sess()).is_real()
&& item.span(self.scx.tcx).filename(self.sess()).is_real()
// skip non-local files
&& item.span().cnum(self.sess()) == LOCAL_CRATE
&& item.span(self.scx.tcx).cnum(self.sess()) == LOCAL_CRATE
{
let filename = item.span().filename(self.sess());
let filename = item.span(self.scx.tcx).filename(self.sess());
// If it turns out that we couldn't read this file, then we probably
// can't read any of the files (generating html output from json or
// something like that), so just don't include sources for the
Expand All @@ -55,7 +55,7 @@ impl DocFolder for SourceCollector<'_, '_> {
Ok(()) => true,
Err(e) => {
self.scx.tcx.sess.span_err(
item.span.inner(),
item.span(self.scx.tcx).inner(),
&format!("failed to render source code for `{}`: {}", filename, e),
);
false
Expand Down
5 changes: 3 additions & 2 deletions src/librustdoc/json/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ impl JsonRenderer<'_> {
.iter()
.map(rustc_ast_pretty::pprust::attribute_to_string)
.collect();
let span = item.span();
let clean::Item { name, attrs: _, kind: _, span: _, visibility, def_id } = item;
let span = item.span(self.tcx);
let clean::Item { name, attrs: _, kind: _, visibility, def_id } = item;
let inner = match *item.kind {
clean::StrippedItem(_) => return None,
_ => from_clean_item(item, self.tcx),
Expand Down Expand Up @@ -463,6 +463,7 @@ impl FromWithTcx<clean::Impl> for Impl {
negative_polarity,
synthetic,
blanket_impl,
span: _span,
} = impl_;
Impl {
is_unsafe: unsafety == rustc_hir::Unsafety::Unsafe,
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/passes/calculate_doc_coverage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ impl<'a, 'b> fold::DocFolder for CoverageCalculator<'a, 'b> {
None,
);

let filename = i.span().filename(self.ctx.sess());
let filename = i.span(self.ctx.tcx).filename(self.ctx.sess());
let has_doc_example = tests.found_tests != 0;
let hir_id = self.ctx.tcx.hir().local_def_id_to_hir_id(i.def_id.expect_local());
let (level, source) = self.ctx.tcx.lint_level_at_node(MISSING_DOCS, hir_id);
Expand Down

0 comments on commit c44c64d

Please sign in to comment.