Skip to content

Commit 0e574fb

Browse files
committed
Fix the JSON backend
This was simpler than expected.
1 parent 4fa95b3 commit 0e574fb

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

src/librustdoc/json/conversions.rs

+21-21
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ use std::convert::From;
66

77
use rustc_ast::ast;
88
use rustc_span::def_id::{DefId, CRATE_DEF_INDEX};
9+
use rustc_span::Pos;
910

1011
use crate::clean;
1112
use crate::doctree;
1213
use crate::formats::item_type::ItemType;
1314
use crate::json::types::*;
15+
use crate::json::JsonRenderer;
1416

15-
impl From<clean::Item> for Option<Item> {
16-
fn from(item: clean::Item) -> Self {
17+
impl JsonRenderer {
18+
pub(super) fn convert_item(&self, item: clean::Item) -> Option<Item> {
1719
let item_type = ItemType::from(&item);
1820
let clean::Item {
1921
source,
@@ -32,7 +34,7 @@ impl From<clean::Item> for Option<Item> {
3234
id: def_id.into(),
3335
crate_id: def_id.krate.as_u32(),
3436
name,
35-
source: source.into(),
37+
source: self.convert_span(source),
3638
visibility: visibility.into(),
3739
docs: attrs.collapsed_doc_value().unwrap_or_default(),
3840
links: attrs
@@ -53,25 +55,23 @@ impl From<clean::Item> for Option<Item> {
5355
}),
5456
}
5557
}
56-
}
5758

58-
impl From<clean::Span> for Option<Span> {
59-
#[allow(unreachable_code)]
60-
fn from(span: clean::Span) -> Self {
61-
// TODO: this should actually work
62-
// Unfortunately this requires rethinking the whole framework,
63-
// since this now needs a context and not just .into().
64-
match span.filename(todo!()) {
65-
rustc_span::FileName::Real(name) => Some(Span {
66-
filename: match name {
67-
rustc_span::RealFileName::Named(path) => path,
68-
rustc_span::RealFileName::Devirtualized { local_path, virtual_name: _ } => {
69-
local_path
70-
}
71-
},
72-
begin: todo!(),
73-
end: todo!(),
74-
}),
59+
fn convert_span(&self, span: clean::Span) -> Option<Span> {
60+
match span.filename(&self.sess) {
61+
rustc_span::FileName::Real(name) => {
62+
let hi = span.hi(&self.sess);
63+
let lo = span.lo(&self.sess);
64+
Some(Span {
65+
filename: match name {
66+
rustc_span::RealFileName::Named(path) => path,
67+
rustc_span::RealFileName::Devirtualized { local_path, virtual_name: _ } => {
68+
local_path
69+
}
70+
},
71+
begin: (lo.line, lo.col.to_usize()),
72+
end: (hi.line, hi.col.to_usize()),
73+
})
74+
}
7575
_ => None,
7676
}
7777
}

src/librustdoc/json/mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use crate::html::render::cache::ExternalLocation;
2626

2727
#[derive(Clone)]
2828
crate struct JsonRenderer {
29+
sess: Lrc<Session>,
2930
/// A mapping of IDs that contains all local items for this crate which gets output as a top
3031
/// level field of the JSON blob.
3132
index: Rc<RefCell<FxHashMap<types::Id, types::Item>>>,
@@ -126,11 +127,12 @@ impl FormatRenderer for JsonRenderer {
126127
_render_info: RenderInfo,
127128
_edition: Edition,
128129
_cache: &mut Cache,
129-
_sess: Lrc<Session>,
130+
sess: Lrc<Session>,
130131
) -> Result<(Self, clean::Crate), Error> {
131132
debug!("Initializing json renderer");
132133
Ok((
133134
JsonRenderer {
135+
sess,
134136
index: Rc::new(RefCell::new(FxHashMap::default())),
135137
out_path: options.output,
136138
},
@@ -146,7 +148,7 @@ impl FormatRenderer for JsonRenderer {
146148
item.kind.inner_items().for_each(|i| self.item(i.clone(), cache).unwrap());
147149

148150
let id = item.def_id;
149-
if let Some(mut new_item) = item.into(): Option<types::Item> {
151+
if let Some(mut new_item) = self.convert_item(item) {
150152
if let types::ItemEnum::TraitItem(ref mut t) = new_item.inner {
151153
t.implementors = self.get_trait_implementors(id, cache)
152154
} else if let types::ItemEnum::StructItem(ref mut s) = new_item.inner {

0 commit comments

Comments
 (0)