Skip to content

Commit 752b0e0

Browse files
make clean::Item::span return option instead of dummy span
1 parent 569788e commit 752b0e0

File tree

7 files changed

+40
-30
lines changed

7 files changed

+40
-30
lines changed

src/librustdoc/clean/types.rs

+7-16
Original file line numberDiff line numberDiff line change
@@ -415,29 +415,28 @@ impl Item {
415415
.unwrap_or(false)
416416
}
417417

418-
pub(crate) fn span(&self, tcx: TyCtxt<'_>) -> Span {
418+
pub(crate) fn span(&self, tcx: TyCtxt<'_>) -> Option<Span> {
419419
let kind = match &*self.kind {
420420
ItemKind::StrippedItem(k) => k,
421421
_ => &*self.kind,
422422
};
423423
match kind {
424-
ItemKind::ModuleItem(Module { span, .. }) => *span,
425-
ItemKind::ImplItem(box Impl { kind: ImplKind::Auto, .. }) => Span::dummy(),
424+
ItemKind::ModuleItem(Module { span, .. }) => Some(*span),
425+
ItemKind::ImplItem(box Impl { kind: ImplKind::Auto, .. }) => None,
426426
ItemKind::ImplItem(box Impl { kind: ImplKind::Blanket(_), .. }) => {
427427
if let ItemId::Blanket { impl_id, .. } = self.item_id {
428-
rustc_span(impl_id, tcx)
428+
Some(rustc_span(impl_id, tcx))
429429
} else {
430430
panic!("blanket impl item has non-blanket ID")
431431
}
432432
}
433-
_ => {
434-
self.item_id.as_def_id().map(|did| rustc_span(did, tcx)).unwrap_or_else(Span::dummy)
435-
}
433+
_ => self.item_id.as_def_id().map(|did| rustc_span(did, tcx)),
436434
}
437435
}
438436

439437
pub(crate) fn attr_span(&self, tcx: TyCtxt<'_>) -> rustc_span::Span {
440-
crate::passes::span_of_attrs(&self.attrs).unwrap_or_else(|| self.span(tcx).inner())
438+
crate::passes::span_of_attrs(&self.attrs)
439+
.unwrap_or_else(|| self.span(tcx).map_or(rustc_span::DUMMY_SP, |span| span.inner()))
441440
}
442441

443442
/// Finds the `doc` attribute as a NameValue and returns the corresponding
@@ -2109,14 +2108,6 @@ impl Span {
21092108
self.0
21102109
}
21112110

2112-
pub(crate) fn dummy() -> Self {
2113-
Self(rustc_span::DUMMY_SP)
2114-
}
2115-
2116-
pub(crate) fn is_dummy(&self) -> bool {
2117-
self.0.is_dummy()
2118-
}
2119-
21202111
pub(crate) fn filename(&self, sess: &Session) -> FileName {
21212112
sess.source_map().span_to_filename(self.0)
21222113
}

src/librustdoc/html/render/context.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -301,13 +301,10 @@ impl<'tcx> Context<'tcx> {
301301
/// may happen, for example, with externally inlined items where the source
302302
/// of their crate documentation isn't known.
303303
pub(super) fn src_href(&self, item: &clean::Item) -> Option<String> {
304-
self.href_from_span(item.span(self.tcx()), true)
304+
self.href_from_span(item.span(self.tcx())?, true)
305305
}
306306

307307
pub(crate) fn href_from_span(&self, span: clean::Span, with_lines: bool) -> Option<String> {
308-
if span.is_dummy() {
309-
return None;
310-
}
311308
let mut root = self.root_path();
312309
let mut path = String::new();
313310
let cnum = span.cnum(self.sess());

src/librustdoc/html/render/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2677,7 +2677,7 @@ fn render_call_locations(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Ite
26772677
let contents = match fs::read_to_string(&path) {
26782678
Ok(contents) => contents,
26792679
Err(err) => {
2680-
let span = item.span(tcx).inner();
2680+
let span = item.span(tcx).map_or(rustc_span::DUMMY_SP, |span| span.inner());
26812681
tcx.sess
26822682
.span_err(span, &format!("failed to read file {}: {}", path.display(), err));
26832683
return false;

src/librustdoc/html/sources.rs

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ impl LocalSourcesCollector<'_, '_> {
5353
fn add_local_source(&mut self, item: &clean::Item) {
5454
let sess = self.tcx.sess;
5555
let span = item.span(self.tcx);
56+
let Some(span) = span else { return };
5657
// skip all synthetic "files"
5758
if !is_real_and_local(span, sess) {
5859
return;
@@ -109,6 +110,7 @@ impl DocVisitor for SourceCollector<'_, '_> {
109110

110111
let tcx = self.cx.tcx();
111112
let span = item.span(tcx);
113+
let Some(span) = span else { return };
112114
let sess = tcx.sess;
113115

114116
// If we're not rendering sources, there's nothing to do.

src/librustdoc/json/conversions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ impl JsonRenderer<'_> {
5959
id: from_item_id_with_name(item_id, self.tcx, name),
6060
crate_id: item_id.krate().as_u32(),
6161
name: name.map(|sym| sym.to_string()),
62-
span: self.convert_span(span),
62+
span: span.and_then(|span| self.convert_span(span)),
6363
visibility: self.convert_visibility(visibility),
6464
docs,
6565
attrs,

src/librustdoc/passes/calculate_doc_coverage.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,6 @@ impl<'a, 'b> DocVisitor for CoverageCalculator<'a, 'b> {
215215
None,
216216
);
217217

218-
let filename = i.span(self.ctx.tcx).filename(self.ctx.sess());
219218
let has_doc_example = tests.found_tests != 0;
220219
// The `expect_def_id()` should be okay because `local_def_id_to_hir_id`
221220
// would presumably panic if a fake `DefIndex` were passed.
@@ -261,13 +260,16 @@ impl<'a, 'b> DocVisitor for CoverageCalculator<'a, 'b> {
261260
let should_have_docs = !should_be_ignored
262261
&& (level != lint::Level::Allow || matches!(source, LintLevelSource::Default));
263262

264-
debug!("counting {:?} {:?} in {:?}", i.type_(), i.name, filename);
265-
self.items.entry(filename).or_default().count_item(
266-
has_docs,
267-
has_doc_example,
268-
should_have_doc_example(self.ctx, i),
269-
should_have_docs,
270-
);
263+
if let Some(span) = i.span(self.ctx.tcx) {
264+
let filename = span.filename(self.ctx.sess());
265+
debug!("counting {:?} {:?} in {:?}", i.type_(), i.name, filename);
266+
self.items.entry(filename).or_default().count_item(
267+
has_docs,
268+
has_doc_example,
269+
should_have_doc_example(self.ctx, i),
270+
should_have_docs,
271+
);
272+
}
271273
}
272274
}
273275

src/test/rustdoc-json/impls/auto.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#![feature(no_core, auto_traits, lang_items)]
2+
#![no_core]
3+
4+
#[lang = "sized"]
5+
trait Sized {}
6+
7+
pub auto trait Bar {}
8+
9+
/// has span
10+
impl Foo {
11+
pub fn baz(&self) {}
12+
}
13+
14+
// Testing spans, so all tests below code
15+
// @is auto.json "$.index[*][?(@.kind=='impl' && @.inner.synthetic==true)].span" null
16+
// @is - "$.index[*][?(@.docs=='has span')].span.begin" "[10, 0]"
17+
// @is - "$.index[*][?(@.docs=='has span')].span.end" "[12, 1]"
18+
pub struct Foo;

0 commit comments

Comments
 (0)