Skip to content

Commit b412b46

Browse files
committed
Do the hard part first
The only bit failing was the module, so change that before removing the `span` field.
1 parent a4b6214 commit b412b46

File tree

9 files changed

+33
-20
lines changed

9 files changed

+33
-20
lines changed

src/librustdoc/clean/inline.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,8 @@ fn build_module(
487487
}
488488
}
489489

490-
clean::Module { items }
490+
let span = clean::Span::from_rustc_span(cx.tcx.def_span(did));
491+
clean::Module { items, span }
491492
}
492493

493494
crate fn print_inlined_const(tcx: TyCtxt<'_>, did: DefId) -> String {

src/librustdoc/clean/mod.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ impl Clean<Item> for doctree::Module<'_> {
100100

101101
// determine if we should display the inner contents or
102102
// the outer `mod` item for the source code.
103-
let span = {
103+
let span = Span::from_rustc_span({
104104
let sm = cx.sess().source_map();
105105
let outer = sm.lookup_char_pos(self.where_outer.lo());
106106
let inner = sm.lookup_char_pos(self.where_inner.lo());
@@ -111,11 +111,14 @@ impl Clean<Item> for doctree::Module<'_> {
111111
// mod foo; (and a separate SourceFile for the contents)
112112
self.where_inner
113113
}
114-
};
114+
});
115115

116-
let what_rustc_thinks =
117-
Item::from_hir_id_and_parts(self.id, Some(self.name), ModuleItem(Module { items }), cx);
118-
Item { span: span.clean(cx), ..what_rustc_thinks }
116+
Item::from_hir_id_and_parts(
117+
self.id,
118+
Some(self.name),
119+
ModuleItem(Module { items, span }),
120+
cx,
121+
)
119122
}
120123
}
121124

src/librustdoc/clean/types.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ impl fmt::Debug for Item {
232232
let def_id: &dyn fmt::Debug = if self.is_fake() { &"**FAKE**" } else { &self.def_id };
233233

234234
fmt.debug_struct("Item")
235-
.field("source", &self.span)
235+
.field("source", &self.span())
236236
.field("name", &self.name)
237237
.field("attrs", &self.attrs)
238238
.field("kind", &self.kind)
@@ -255,8 +255,12 @@ impl Item {
255255
if self.is_fake() { None } else { tcx.lookup_deprecation(self.def_id) }
256256
}
257257

258+
crate fn span(&self) -> Span {
259+
if let ItemKind::ModuleItem(Module { span, .. }) = &*self.kind { *span } else { self.span }
260+
}
261+
258262
crate fn attr_span(&self, _tcx: TyCtxt<'_>) -> rustc_span::Span {
259-
crate::passes::span_of_attrs(&self.attrs).unwrap_or_else(|| self.span.inner())
263+
crate::passes::span_of_attrs(&self.attrs).unwrap_or_else(|| self.span().inner())
260264
}
261265

262266
/// Finds the `doc` attribute as a NameValue and returns the corresponding
@@ -609,6 +613,7 @@ impl ItemKind {
609613
#[derive(Clone, Debug)]
610614
crate struct Module {
611615
crate items: Vec<Item>,
616+
crate span: Span,
612617
}
613618

614619
crate struct ListAttributesIter<'a> {

src/librustdoc/fold.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,10 @@ crate trait DocFolder: Sized {
8080
}
8181

8282
fn fold_mod(&mut self, m: Module) -> Module {
83-
Module { items: m.items.into_iter().filter_map(|i| self.fold_item(i)).collect() }
83+
Module {
84+
span: m.span,
85+
items: m.items.into_iter().filter_map(|i| self.fold_item(i)).collect(),
86+
}
8487
}
8588

8689
fn fold_crate(&mut self, mut c: Crate) -> Crate {

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().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().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().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().lo(self.sess()).line;
327+
let hiline = item.span().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().inner().edition(),
10171017
);
10181018
});
10191019
document(w, cx, it, None)

src/librustdoc/html/sources.rs

+3-3
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().filename(self.sess()).is_real()
4545
// skip non-local files
46-
&& item.span.cnum(self.sess()) == LOCAL_CRATE
46+
&& item.span().cnum(self.sess()) == LOCAL_CRATE
4747
{
48-
let filename = item.span.filename(self.sess());
48+
let filename = item.span().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

src/librustdoc/json/conversions.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ impl JsonRenderer<'_> {
4040
.iter()
4141
.map(rustc_ast_pretty::pprust::attribute_to_string)
4242
.collect();
43-
let clean::Item { span, name, attrs: _, kind: _, visibility, def_id } = item;
43+
let span = item.span();
44+
let clean::Item { name, attrs: _, kind: _, span: _, visibility, def_id } = item;
4445
let inner = match *item.kind {
4546
clean::StrippedItem(_) => return None,
4647
_ => from_clean_item(item, self.tcx),

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().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)