Skip to content

Commit c44c64d

Browse files
committed
Get rid of item.span
- Remove `span` field, adding `Item::span()` instead - Special-case `Impl` and `Module` items - Use dummy spans for primitive items
1 parent b412b46 commit c44c64d

File tree

10 files changed

+44
-36
lines changed

10 files changed

+44
-36
lines changed

src/librustdoc/clean/auto_trait.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,12 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
110110
};
111111

112112
Some(Item {
113-
span: Span::dummy(),
114113
name: None,
115114
attrs: Default::default(),
116115
visibility: Inherited,
117116
def_id: self.cx.next_def_id(item_def_id.krate),
118117
kind: box ImplItem(Impl {
118+
span: Span::dummy(),
119119
unsafety: hir::Unsafety::Normal,
120120
generics: new_generics,
121121
provided_trait_methods: Default::default(),

src/librustdoc/clean/blanket_impl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,12 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
100100
.collect();
101101

102102
impls.push(Item {
103-
span: self.cx.tcx.def_span(impl_def_id).clean(self.cx),
104103
name: None,
105104
attrs: Default::default(),
106105
visibility: Inherited,
107106
def_id: self.cx.next_def_id(impl_def_id.krate),
108107
kind: box ImplItem(Impl {
108+
span: self.cx.tcx.def_span(impl_def_id).clean(self.cx),
109109
unsafety: hir::Unsafety::Normal,
110110
generics: (
111111
self.cx.tcx.generics_of(impl_def_id),

src/librustdoc/clean/inline.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_ast as ast;
66
use rustc_data_structures::fx::FxHashSet;
77
use rustc_hir as hir;
88
use rustc_hir::def::{DefKind, Res};
9-
use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX};
9+
use rustc_hir::def_id::DefId;
1010
use rustc_hir::Mutability;
1111
use rustc_metadata::creader::LoadedMacro;
1212
use rustc_middle::ty::{self, TyCtxt};
@@ -422,6 +422,7 @@ crate fn build_impl(
422422
did,
423423
None,
424424
clean::ImplItem(clean::Impl {
425+
span: clean::types::rustc_span(did, cx.tcx),
425426
unsafety: hir::Unsafety::Normal,
426427
generics,
427428
provided_trait_methods: provided,
@@ -459,8 +460,7 @@ fn build_module(
459460
items.push(clean::Item {
460461
name: None,
461462
attrs: box clean::Attributes::default(),
462-
span: clean::Span::dummy(),
463-
def_id: DefId::local(CRATE_DEF_INDEX),
463+
def_id: cx.next_def_id(did.krate),
464464
visibility: clean::Public,
465465
kind: box clean::ImportItem(clean::Import::new_simple(
466466
item.ident.name,

src/librustdoc/clean/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1945,6 +1945,7 @@ fn clean_impl(impl_: &hir::Impl<'_>, hir_id: hir::HirId, cx: &mut DocContext<'_>
19451945
});
19461946
let mut make_item = |trait_: Option<Type>, for_: Type, items: Vec<Item>| {
19471947
let kind = ImplItem(Impl {
1948+
span: types::rustc_span(tcx.hir().local_def_id(hir_id).to_def_id(), tcx),
19481949
unsafety: impl_.unsafety,
19491950
generics: impl_.generics.clean(cx),
19501951
provided_trait_methods: provided.clone(),
@@ -2004,7 +2005,6 @@ fn clean_extern_crate(
20042005
vec![Item {
20052006
name: Some(name),
20062007
attrs: box attrs.clean(cx),
2007-
span: krate.span.clean(cx),
20082008
def_id: crate_def_id,
20092009
visibility: krate.vis.clean(cx),
20102010
kind: box ExternCrateItem { src: orig_name },

src/librustdoc/clean/types.rs

+24-17
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,6 @@ impl ExternalCrate {
211211
/// directly to the AST's concept of an item; it's a strict superset.
212212
#[derive(Clone)]
213213
crate struct Item {
214-
crate span: Span,
215214
/// The name of this item.
216215
/// Optional because not every item has a name, e.g. impls.
217216
crate name: Option<Symbol>,
@@ -225,14 +224,13 @@ crate struct Item {
225224

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

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

234233
fmt.debug_struct("Item")
235-
.field("source", &self.span())
236234
.field("name", &self.name)
237235
.field("attrs", &self.attrs)
238236
.field("kind", &self.kind)
@@ -242,6 +240,16 @@ impl fmt::Debug for Item {
242240
}
243241
}
244242

243+
crate fn rustc_span(def_id: DefId, tcx: TyCtxt<'_>) -> Span {
244+
Span::from_rustc_span(def_id.as_local().map_or_else(
245+
|| tcx.def_span(def_id),
246+
|local| {
247+
let hir = tcx.hir();
248+
hir.span_with_body(hir.local_def_id_to_hir_id(local))
249+
},
250+
))
251+
}
252+
245253
impl Item {
246254
crate fn stability<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Option<&'tcx Stability> {
247255
if self.is_fake() { None } else { tcx.lookup_stability(self.def_id) }
@@ -255,12 +263,20 @@ impl Item {
255263
if self.is_fake() { None } else { tcx.lookup_deprecation(self.def_id) }
256264
}
257265

258-
crate fn span(&self) -> Span {
259-
if let ItemKind::ModuleItem(Module { span, .. }) = &*self.kind { *span } else { self.span }
266+
crate fn span(&self, tcx: TyCtxt<'_>) -> Span {
267+
if let ItemKind::ModuleItem(Module { span, .. }) | ItemKind::ImplItem(Impl { span, .. }) =
268+
&*self.kind
269+
{
270+
*span
271+
} else if self.is_fake() {
272+
Span::dummy()
273+
} else {
274+
rustc_span(self.def_id, tcx)
275+
}
260276
}
261277

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

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

307-
// `span_if_local()` lies about functions and only gives the span of the function signature
308-
let span = def_id.as_local().map_or_else(
309-
|| cx.tcx.def_span(def_id),
310-
|local| {
311-
let hir = cx.tcx.hir();
312-
hir.span_with_body(hir.local_def_id_to_hir_id(local))
313-
},
314-
);
315-
316323
Item {
317324
def_id,
318325
kind: box kind,
319326
name,
320-
span: span.clean(cx),
321327
attrs,
322328
visibility: cx.tcx.visibility(def_id).clean(cx),
323329
}
@@ -2117,6 +2123,7 @@ impl Constant {
21172123

21182124
#[derive(Clone, Debug)]
21192125
crate struct Impl {
2126+
crate span: Span,
21202127
crate unsafety: hir::Unsafety,
21212128
crate generics: Generics,
21222129
crate provided_trait_methods: FxHashSet<Symbol>,

src/librustdoc/html/render/context.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -281,15 +281,15 @@ impl<'tcx> Context<'tcx> {
281281
/// may happen, for example, with externally inlined items where the source
282282
/// of their crate documentation isn't known.
283283
pub(super) fn src_href(&self, item: &clean::Item) -> Option<String> {
284-
if item.span().is_dummy() {
284+
if item.span(self.tcx()).is_dummy() {
285285
return None;
286286
}
287287
let mut root = self.root_path();
288288
let mut path = String::new();
289-
let cnum = item.span().cnum(self.sess());
289+
let cnum = item.span(self.tcx()).cnum(self.sess());
290290

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

326-
let loline = item.span().lo(self.sess()).line;
327-
let hiline = item.span().hi(self.sess()).line;
326+
let loline = item.span(self.tcx()).lo(self.sess()).line;
327+
let hiline = item.span(self.tcx()).hi(self.sess()).line;
328328
let lines =
329329
if loline == hiline { loline.to_string() } else { format!("{}-{}", loline, hiline) };
330330
Some(format!(

src/librustdoc/html/render/print_item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,7 @@ fn item_macro(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Mac
10131013
Some("macro"),
10141014
None,
10151015
None,
1016-
it.span().inner().edition(),
1016+
it.span(cx.tcx()).inner().edition(),
10171017
);
10181018
});
10191019
document(w, cx, it, None)

src/librustdoc/html/sources.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ impl DocFolder for SourceCollector<'_, '_> {
4141
// then we need to render it out to the filesystem.
4242
if self.scx.include_sources
4343
// skip all synthetic "files"
44-
&& item.span().filename(self.sess()).is_real()
44+
&& item.span(self.scx.tcx).filename(self.sess()).is_real()
4545
// skip non-local files
46-
&& item.span().cnum(self.sess()) == LOCAL_CRATE
46+
&& item.span(self.scx.tcx).cnum(self.sess()) == LOCAL_CRATE
4747
{
48-
let filename = item.span().filename(self.sess());
48+
let filename = item.span(self.scx.tcx).filename(self.sess());
4949
// If it turns out that we couldn't read this file, then we probably
5050
// can't read any of the files (generating html output from json or
5151
// something like that), so just don't include sources for the
@@ -55,7 +55,7 @@ impl DocFolder for SourceCollector<'_, '_> {
5555
Ok(()) => true,
5656
Err(e) => {
5757
self.scx.tcx.sess.span_err(
58-
item.span.inner(),
58+
item.span(self.scx.tcx).inner(),
5959
&format!("failed to render source code for `{}`: {}", filename, e),
6060
);
6161
false

src/librustdoc/json/conversions.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ impl JsonRenderer<'_> {
4040
.iter()
4141
.map(rustc_ast_pretty::pprust::attribute_to_string)
4242
.collect();
43-
let span = item.span();
44-
let clean::Item { name, attrs: _, kind: _, span: _, visibility, def_id } = item;
43+
let span = item.span(self.tcx);
44+
let clean::Item { name, attrs: _, kind: _, visibility, def_id } = item;
4545
let inner = match *item.kind {
4646
clean::StrippedItem(_) => return None,
4747
_ => from_clean_item(item, self.tcx),
@@ -463,6 +463,7 @@ impl FromWithTcx<clean::Impl> for Impl {
463463
negative_polarity,
464464
synthetic,
465465
blanket_impl,
466+
span: _span,
466467
} = impl_;
467468
Impl {
468469
is_unsafe: unsafety == rustc_hir::Unsafety::Unsafe,

src/librustdoc/passes/calculate_doc_coverage.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ impl<'a, 'b> fold::DocFolder for CoverageCalculator<'a, 'b> {
211211
None,
212212
);
213213

214-
let filename = i.span().filename(self.ctx.sess());
214+
let filename = i.span(self.ctx.tcx).filename(self.ctx.sess());
215215
let has_doc_example = tests.found_tests != 0;
216216
let hir_id = self.ctx.tcx.hir().local_def_id_to_hir_id(i.def_id.expect_local());
217217
let (level, source) = self.ctx.tcx.lint_level_at_node(MISSING_DOCS, hir_id);

0 commit comments

Comments
 (0)