mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-21 12:07:31 +00:00
Get rid of item.span
- Remove `span` field, adding `Item::span()` instead - Special-case `Impl` and `Module` items - Use dummy spans for primitive items
This commit is contained in:
parent
b412b46cf7
commit
c44c64df71
@ -110,12 +110,12 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Some(Item {
|
Some(Item {
|
||||||
span: Span::dummy(),
|
|
||||||
name: None,
|
name: None,
|
||||||
attrs: Default::default(),
|
attrs: Default::default(),
|
||||||
visibility: Inherited,
|
visibility: Inherited,
|
||||||
def_id: self.cx.next_def_id(item_def_id.krate),
|
def_id: self.cx.next_def_id(item_def_id.krate),
|
||||||
kind: box ImplItem(Impl {
|
kind: box ImplItem(Impl {
|
||||||
|
span: Span::dummy(),
|
||||||
unsafety: hir::Unsafety::Normal,
|
unsafety: hir::Unsafety::Normal,
|
||||||
generics: new_generics,
|
generics: new_generics,
|
||||||
provided_trait_methods: Default::default(),
|
provided_trait_methods: Default::default(),
|
||||||
|
@ -100,12 +100,12 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
|
|||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
impls.push(Item {
|
impls.push(Item {
|
||||||
span: self.cx.tcx.def_span(impl_def_id).clean(self.cx),
|
|
||||||
name: None,
|
name: None,
|
||||||
attrs: Default::default(),
|
attrs: Default::default(),
|
||||||
visibility: Inherited,
|
visibility: Inherited,
|
||||||
def_id: self.cx.next_def_id(impl_def_id.krate),
|
def_id: self.cx.next_def_id(impl_def_id.krate),
|
||||||
kind: box ImplItem(Impl {
|
kind: box ImplItem(Impl {
|
||||||
|
span: self.cx.tcx.def_span(impl_def_id).clean(self.cx),
|
||||||
unsafety: hir::Unsafety::Normal,
|
unsafety: hir::Unsafety::Normal,
|
||||||
generics: (
|
generics: (
|
||||||
self.cx.tcx.generics_of(impl_def_id),
|
self.cx.tcx.generics_of(impl_def_id),
|
||||||
|
@ -6,7 +6,7 @@ use rustc_ast as ast;
|
|||||||
use rustc_data_structures::fx::FxHashSet;
|
use rustc_data_structures::fx::FxHashSet;
|
||||||
use rustc_hir as hir;
|
use rustc_hir as hir;
|
||||||
use rustc_hir::def::{DefKind, Res};
|
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_hir::Mutability;
|
||||||
use rustc_metadata::creader::LoadedMacro;
|
use rustc_metadata::creader::LoadedMacro;
|
||||||
use rustc_middle::ty::{self, TyCtxt};
|
use rustc_middle::ty::{self, TyCtxt};
|
||||||
@ -422,6 +422,7 @@ crate fn build_impl(
|
|||||||
did,
|
did,
|
||||||
None,
|
None,
|
||||||
clean::ImplItem(clean::Impl {
|
clean::ImplItem(clean::Impl {
|
||||||
|
span: clean::types::rustc_span(did, cx.tcx),
|
||||||
unsafety: hir::Unsafety::Normal,
|
unsafety: hir::Unsafety::Normal,
|
||||||
generics,
|
generics,
|
||||||
provided_trait_methods: provided,
|
provided_trait_methods: provided,
|
||||||
@ -459,8 +460,7 @@ fn build_module(
|
|||||||
items.push(clean::Item {
|
items.push(clean::Item {
|
||||||
name: None,
|
name: None,
|
||||||
attrs: box clean::Attributes::default(),
|
attrs: box clean::Attributes::default(),
|
||||||
span: clean::Span::dummy(),
|
def_id: cx.next_def_id(did.krate),
|
||||||
def_id: DefId::local(CRATE_DEF_INDEX),
|
|
||||||
visibility: clean::Public,
|
visibility: clean::Public,
|
||||||
kind: box clean::ImportItem(clean::Import::new_simple(
|
kind: box clean::ImportItem(clean::Import::new_simple(
|
||||||
item.ident.name,
|
item.ident.name,
|
||||||
|
@ -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 mut make_item = |trait_: Option<Type>, for_: Type, items: Vec<Item>| {
|
||||||
let kind = ImplItem(Impl {
|
let kind = ImplItem(Impl {
|
||||||
|
span: types::rustc_span(tcx.hir().local_def_id(hir_id).to_def_id(), tcx),
|
||||||
unsafety: impl_.unsafety,
|
unsafety: impl_.unsafety,
|
||||||
generics: impl_.generics.clean(cx),
|
generics: impl_.generics.clean(cx),
|
||||||
provided_trait_methods: provided.clone(),
|
provided_trait_methods: provided.clone(),
|
||||||
@ -2004,7 +2005,6 @@ fn clean_extern_crate(
|
|||||||
vec![Item {
|
vec![Item {
|
||||||
name: Some(name),
|
name: Some(name),
|
||||||
attrs: box attrs.clean(cx),
|
attrs: box attrs.clean(cx),
|
||||||
span: krate.span.clean(cx),
|
|
||||||
def_id: crate_def_id,
|
def_id: crate_def_id,
|
||||||
visibility: krate.vis.clean(cx),
|
visibility: krate.vis.clean(cx),
|
||||||
kind: box ExternCrateItem { src: orig_name },
|
kind: box ExternCrateItem { src: orig_name },
|
||||||
|
@ -211,7 +211,6 @@ impl ExternalCrate {
|
|||||||
/// directly to the AST's concept of an item; it's a strict superset.
|
/// directly to the AST's concept of an item; it's a strict superset.
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
crate struct Item {
|
crate struct Item {
|
||||||
crate span: Span,
|
|
||||||
/// The name of this item.
|
/// The name of this item.
|
||||||
/// Optional because not every item has a name, e.g. impls.
|
/// Optional because not every item has a name, e.g. impls.
|
||||||
crate name: Option<Symbol>,
|
crate name: Option<Symbol>,
|
||||||
@ -225,14 +224,13 @@ crate struct Item {
|
|||||||
|
|
||||||
// `Item` is used a lot. Make sure it doesn't unintentionally get bigger.
|
// `Item` is used a lot. Make sure it doesn't unintentionally get bigger.
|
||||||
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
|
#[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 {
|
impl fmt::Debug for Item {
|
||||||
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
let def_id: &dyn fmt::Debug = if self.is_fake() { &"**FAKE**" } else { &self.def_id };
|
let def_id: &dyn fmt::Debug = if self.is_fake() { &"**FAKE**" } else { &self.def_id };
|
||||||
|
|
||||||
fmt.debug_struct("Item")
|
fmt.debug_struct("Item")
|
||||||
.field("source", &self.span())
|
|
||||||
.field("name", &self.name)
|
.field("name", &self.name)
|
||||||
.field("attrs", &self.attrs)
|
.field("attrs", &self.attrs)
|
||||||
.field("kind", &self.kind)
|
.field("kind", &self.kind)
|
||||||
@ -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 {
|
impl Item {
|
||||||
crate fn stability<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Option<&'tcx Stability> {
|
crate fn stability<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Option<&'tcx Stability> {
|
||||||
if self.is_fake() { None } else { tcx.lookup_stability(self.def_id) }
|
if self.is_fake() { None } else { tcx.lookup_stability(self.def_id) }
|
||||||
@ -255,12 +263,20 @@ impl Item {
|
|||||||
if self.is_fake() { None } else { tcx.lookup_deprecation(self.def_id) }
|
if self.is_fake() { None } else { tcx.lookup_deprecation(self.def_id) }
|
||||||
}
|
}
|
||||||
|
|
||||||
crate fn span(&self) -> Span {
|
crate fn span(&self, tcx: TyCtxt<'_>) -> Span {
|
||||||
if let ItemKind::ModuleItem(Module { span, .. }) = &*self.kind { *span } else { self.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 fn attr_span(&self, tcx: TyCtxt<'_>) -> rustc_span::Span {
|
||||||
crate::passes::span_of_attrs(&self.attrs).unwrap_or_else(|| self.span().inner())
|
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
|
/// Finds the `doc` attribute as a NameValue and returns the corresponding
|
||||||
@ -304,20 +320,10 @@ impl Item {
|
|||||||
) -> Item {
|
) -> Item {
|
||||||
debug!("name={:?}, def_id={:?}", name, def_id);
|
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 {
|
Item {
|
||||||
def_id,
|
def_id,
|
||||||
kind: box kind,
|
kind: box kind,
|
||||||
name,
|
name,
|
||||||
span: span.clean(cx),
|
|
||||||
attrs,
|
attrs,
|
||||||
visibility: cx.tcx.visibility(def_id).clean(cx),
|
visibility: cx.tcx.visibility(def_id).clean(cx),
|
||||||
}
|
}
|
||||||
@ -2117,6 +2123,7 @@ impl Constant {
|
|||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
crate struct Impl {
|
crate struct Impl {
|
||||||
|
crate span: Span,
|
||||||
crate unsafety: hir::Unsafety,
|
crate unsafety: hir::Unsafety,
|
||||||
crate generics: Generics,
|
crate generics: Generics,
|
||||||
crate provided_trait_methods: FxHashSet<Symbol>,
|
crate provided_trait_methods: FxHashSet<Symbol>,
|
||||||
|
@ -281,15 +281,15 @@ impl<'tcx> Context<'tcx> {
|
|||||||
/// may happen, for example, with externally inlined items where the source
|
/// may happen, for example, with externally inlined items where the source
|
||||||
/// of their crate documentation isn't known.
|
/// of their crate documentation isn't known.
|
||||||
pub(super) fn src_href(&self, item: &clean::Item) -> Option<String> {
|
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;
|
return None;
|
||||||
}
|
}
|
||||||
let mut root = self.root_path();
|
let mut root = self.root_path();
|
||||||
let mut path = String::new();
|
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.
|
// 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(),
|
FileName::Real(ref path) => path.local_path().to_path_buf(),
|
||||||
_ => return None,
|
_ => return None,
|
||||||
};
|
};
|
||||||
@ -323,8 +323,8 @@ impl<'tcx> Context<'tcx> {
|
|||||||
(&*symbol, &path)
|
(&*symbol, &path)
|
||||||
};
|
};
|
||||||
|
|
||||||
let loline = item.span().lo(self.sess()).line;
|
let loline = item.span(self.tcx()).lo(self.sess()).line;
|
||||||
let hiline = item.span().hi(self.sess()).line;
|
let hiline = item.span(self.tcx()).hi(self.sess()).line;
|
||||||
let lines =
|
let lines =
|
||||||
if loline == hiline { loline.to_string() } else { format!("{}-{}", loline, hiline) };
|
if loline == hiline { loline.to_string() } else { format!("{}-{}", loline, hiline) };
|
||||||
Some(format!(
|
Some(format!(
|
||||||
|
@ -1013,7 +1013,7 @@ fn item_macro(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Mac
|
|||||||
Some("macro"),
|
Some("macro"),
|
||||||
None,
|
None,
|
||||||
None,
|
None,
|
||||||
it.span().inner().edition(),
|
it.span(cx.tcx()).inner().edition(),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
document(w, cx, it, None)
|
document(w, cx, it, None)
|
||||||
|
@ -41,11 +41,11 @@ impl DocFolder for SourceCollector<'_, '_> {
|
|||||||
// then we need to render it out to the filesystem.
|
// then we need to render it out to the filesystem.
|
||||||
if self.scx.include_sources
|
if self.scx.include_sources
|
||||||
// skip all synthetic "files"
|
// 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
|
// 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
|
// 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
|
// can't read any of the files (generating html output from json or
|
||||||
// something like that), so just don't include sources for the
|
// something like that), so just don't include sources for the
|
||||||
@ -55,7 +55,7 @@ impl DocFolder for SourceCollector<'_, '_> {
|
|||||||
Ok(()) => true,
|
Ok(()) => true,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
self.scx.tcx.sess.span_err(
|
self.scx.tcx.sess.span_err(
|
||||||
item.span.inner(),
|
item.span(self.scx.tcx).inner(),
|
||||||
&format!("failed to render source code for `{}`: {}", filename, e),
|
&format!("failed to render source code for `{}`: {}", filename, e),
|
||||||
);
|
);
|
||||||
false
|
false
|
||||||
|
@ -40,8 +40,8 @@ impl JsonRenderer<'_> {
|
|||||||
.iter()
|
.iter()
|
||||||
.map(rustc_ast_pretty::pprust::attribute_to_string)
|
.map(rustc_ast_pretty::pprust::attribute_to_string)
|
||||||
.collect();
|
.collect();
|
||||||
let span = item.span();
|
let span = item.span(self.tcx);
|
||||||
let clean::Item { name, attrs: _, kind: _, span: _, visibility, def_id } = item;
|
let clean::Item { name, attrs: _, kind: _, visibility, def_id } = item;
|
||||||
let inner = match *item.kind {
|
let inner = match *item.kind {
|
||||||
clean::StrippedItem(_) => return None,
|
clean::StrippedItem(_) => return None,
|
||||||
_ => from_clean_item(item, self.tcx),
|
_ => from_clean_item(item, self.tcx),
|
||||||
@ -463,6 +463,7 @@ impl FromWithTcx<clean::Impl> for Impl {
|
|||||||
negative_polarity,
|
negative_polarity,
|
||||||
synthetic,
|
synthetic,
|
||||||
blanket_impl,
|
blanket_impl,
|
||||||
|
span: _span,
|
||||||
} = impl_;
|
} = impl_;
|
||||||
Impl {
|
Impl {
|
||||||
is_unsafe: unsafety == rustc_hir::Unsafety::Unsafe,
|
is_unsafe: unsafety == rustc_hir::Unsafety::Unsafe,
|
||||||
|
@ -211,7 +211,7 @@ impl<'a, 'b> fold::DocFolder for CoverageCalculator<'a, 'b> {
|
|||||||
None,
|
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 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 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);
|
let (level, source) = self.ctx.tcx.lint_level_at_node(MISSING_DOCS, hir_id);
|
||||||
|
Loading…
Reference in New Issue
Block a user