Skip to content

Commit 673d0db

Browse files
committed
Auto merge of #82994 - camelid:rename-source-to-span, r=jyn514
Rename `source` to `span` and `span` to `source` - Rename `clean::Item.source` to `span` - Rename `clean::Span::span()` to `clean::Span::inner()` - Rename `rustdoc_json_types::Item.source` to `span` - rustdoc-json: Rename `Import.span` to `Import.source` *See also the [discussion on Zulip][z] (this is a bit more than discussed in that conversation, but all the changes are related).* r? `@jyn514` [z]: https://rust-lang.zulipchat.com/#narrow/stream/182449-t-compiler.2Fhelp/topic/get.20span.20of.20file.20from.20name/near/229603729
2 parents 79e5814 + d5f2bb2 commit 673d0db

20 files changed

+57
-49
lines changed

src/librustdoc/clean/auto_trait.rs

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

112112
Some(Item {
113-
source: Span::dummy(),
113+
span: Span::dummy(),
114114
name: None,
115115
attrs: Default::default(),
116116
visibility: Inherited,

src/librustdoc/clean/blanket_impl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
102102
.collect();
103103

104104
impls.push(Item {
105-
source: self.cx.tcx.def_span(impl_def_id).clean(self.cx),
105+
span: self.cx.tcx.def_span(impl_def_id).clean(self.cx),
106106
name: None,
107107
attrs: Default::default(),
108108
visibility: Inherited,

src/librustdoc/clean/inline.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ fn build_module(
459459
items.push(clean::Item {
460460
name: None,
461461
attrs: box clean::Attributes::default(),
462-
source: clean::Span::dummy(),
462+
span: clean::Span::dummy(),
463463
def_id: DefId::local(CRATE_DEF_INDEX),
464464
visibility: clean::Public,
465465
kind: box clean::ImportItem(clean::Import::new_simple(

src/librustdoc/clean/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ impl Clean<Item> for doctree::Module<'_> {
235235
ModuleItem(Module { is_crate: self.is_crate, items }),
236236
cx,
237237
);
238-
Item { source: span.clean(cx), ..what_rustc_thinks }
238+
Item { span: span.clean(cx), ..what_rustc_thinks }
239239
}
240240
}
241241

@@ -2132,7 +2132,7 @@ fn clean_extern_crate(
21322132
vec![Item {
21332133
name: Some(name),
21342134
attrs: box attrs.clean(cx),
2135-
source: krate.span.clean(cx),
2135+
span: krate.span.clean(cx),
21362136
def_id: crate_def_id,
21372137
visibility: krate.vis.clean(cx),
21382138
kind: box ExternCrateItem { src: orig_name },

src/librustdoc/clean/types.rs

+21-11
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,14 @@ crate struct ExternalCrate {
8181
/// directly to the AST's concept of an item; it's a strict superset.
8282
#[derive(Clone)]
8383
crate struct Item {
84-
/// Stringified span
85-
crate source: Span,
86-
/// Not everything has a name. E.g., impls
84+
crate span: Span,
85+
/// The name of this item.
86+
/// Optional because not every item has a name, e.g. impls.
8787
crate name: Option<Symbol>,
8888
crate attrs: Box<Attributes>,
8989
crate visibility: Visibility,
90+
/// Information about this item that is specific to what kind of item it is.
91+
/// E.g., struct vs enum vs function.
9092
crate kind: Box<ItemKind>,
9193
crate def_id: DefId,
9294
}
@@ -100,7 +102,7 @@ impl fmt::Debug for Item {
100102
let def_id: &dyn fmt::Debug = if self.is_fake() { &"**FAKE**" } else { &self.def_id };
101103

102104
fmt.debug_struct("Item")
103-
.field("source", &self.source)
105+
.field("source", &self.span)
104106
.field("name", &self.name)
105107
.field("attrs", &self.attrs)
106108
.field("kind", &self.kind)
@@ -165,7 +167,7 @@ impl Item {
165167
debug!("name={:?}, def_id={:?}", name, def_id);
166168

167169
// `span_if_local()` lies about functions and only gives the span of the function signature
168-
let source = def_id.as_local().map_or_else(
170+
let span = def_id.as_local().map_or_else(
169171
|| cx.tcx.def_span(def_id),
170172
|local| {
171173
let hir = cx.tcx.hir();
@@ -177,7 +179,7 @@ impl Item {
177179
def_id,
178180
kind: box kind,
179181
name,
180-
source: source.clean(cx),
182+
span: span.clean(cx),
181183
attrs,
182184
visibility: cx.tcx.visibility(def_id).clean(cx),
183185
}
@@ -559,6 +561,8 @@ impl<'a> FromIterator<&'a DocFragment> for String {
559561
}
560562
}
561563

564+
/// The attributes on an [`Item`], including attributes like `#[derive(...)]` and `#[inline]`,
565+
/// as well as doc comments.
562566
#[derive(Clone, Debug, Default)]
563567
crate struct Attributes {
564568
crate doc_strings: Vec<DocFragment>,
@@ -1798,8 +1802,13 @@ impl From<hir::PrimTy> for PrimitiveType {
17981802

17991803
#[derive(Copy, Clone, Debug)]
18001804
crate enum Visibility {
1805+
/// `pub`
18011806
Public,
1807+
/// Visibility inherited from parent.
1808+
///
1809+
/// For example, this is the visibility of private items and of enum variants.
18021810
Inherited,
1811+
/// `pub(crate)`, `pub(super)`, or `pub(in path::to::somewhere)`
18031812
Restricted(DefId),
18041813
}
18051814

@@ -1848,7 +1857,8 @@ crate enum Variant {
18481857
Struct(VariantStruct),
18491858
}
18501859

1851-
/// Small wrapper around `rustc_span::Span` that adds helper methods and enforces calling `source_callsite`.
1860+
/// Small wrapper around [`rustc_span::Span]` that adds helper methods
1861+
/// and enforces calling [`rustc_span::Span::source_callsite()`].
18521862
#[derive(Clone, Debug)]
18531863
crate struct Span(rustc_span::Span);
18541864

@@ -1860,12 +1870,12 @@ impl Span {
18601870
Self(sp.source_callsite())
18611871
}
18621872

1863-
crate fn dummy() -> Self {
1864-
Self(rustc_span::DUMMY_SP)
1873+
crate fn inner(&self) -> rustc_span::Span {
1874+
self.0
18651875
}
18661876

1867-
crate fn span(&self) -> rustc_span::Span {
1868-
self.0
1877+
crate fn dummy() -> Self {
1878+
Self(rustc_span::DUMMY_SP)
18691879
}
18701880

18711881
crate fn is_dummy(&self) -> bool {

src/librustdoc/html/render/context.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -228,15 +228,15 @@ impl<'tcx> Context<'tcx> {
228228
/// may happen, for example, with externally inlined items where the source
229229
/// of their crate documentation isn't known.
230230
pub(super) fn src_href(&self, item: &clean::Item) -> Option<String> {
231-
if item.source.is_dummy() {
231+
if item.span.is_dummy() {
232232
return None;
233233
}
234234
let mut root = self.root_path();
235235
let mut path = String::new();
236-
let cnum = item.source.cnum(self.sess());
236+
let cnum = item.span.cnum(self.sess());
237237

238238
// We can safely ignore synthetic `SourceFile`s.
239-
let file = match item.source.filename(self.sess()) {
239+
let file = match item.span.filename(self.sess()) {
240240
FileName::Real(ref path) => path.local_path().to_path_buf(),
241241
_ => return None,
242242
};
@@ -270,8 +270,8 @@ impl<'tcx> Context<'tcx> {
270270
(&*symbol, &path)
271271
};
272272

273-
let loline = item.source.lo(self.sess()).line;
274-
let hiline = item.source.hi(self.sess()).line;
273+
let loline = item.span.lo(self.sess()).line;
274+
let hiline = item.span.hi(self.sess()).line;
275275
let lines =
276276
if loline == hiline { loline.to_string() } else { format!("{}-{}", loline, hiline) };
277277
Some(format!(

src/librustdoc/html/render/print_item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -937,7 +937,7 @@ fn item_macro(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Mac
937937
Some("macro"),
938938
None,
939939
None,
940-
it.source.span().edition(),
940+
it.span.inner().edition(),
941941
);
942942
});
943943
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.source.filename(self.sess()).is_real()
44+
&& item.span.filename(self.sess()).is_real()
4545
// skip non-local files
46-
&& item.source.cnum(self.sess()) == LOCAL_CRATE
46+
&& item.span.cnum(self.sess()) == LOCAL_CRATE
4747
{
48-
let filename = item.source.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

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use std::collections::HashSet;
2424
impl JsonRenderer<'_> {
2525
pub(super) fn convert_item(&self, item: clean::Item) -> Option<Item> {
2626
let deprecation = item.deprecation(self.tcx);
27-
let clean::Item { source, name, attrs, kind, visibility, def_id } = item;
27+
let clean::Item { span, name, attrs, kind, visibility, def_id } = item;
2828
let inner = match *kind {
2929
clean::StrippedItem(_) => return None,
3030
x => from_clean_item_kind(x, self.tcx, &name),
@@ -33,7 +33,7 @@ impl JsonRenderer<'_> {
3333
id: from_def_id(def_id),
3434
crate_id: def_id.krate.as_u32(),
3535
name: name.map(|sym| sym.to_string()),
36-
source: self.convert_span(source),
36+
span: self.convert_span(span),
3737
visibility: self.convert_visibility(visibility),
3838
docs: attrs.collapsed_doc_value(),
3939
links: attrs
@@ -503,13 +503,13 @@ impl From<clean::Import> for Import {
503503
use clean::ImportKind::*;
504504
match import.kind {
505505
Simple(s) => Import {
506-
span: import.source.path.whole_name(),
506+
source: import.source.path.whole_name(),
507507
name: s.to_string(),
508508
id: import.source.did.map(from_def_id),
509509
glob: false,
510510
},
511511
Glob => Import {
512-
span: import.source.path.whole_name(),
512+
source: import.source.path.whole_name(),
513513
name: import.source.path.last_name().to_string(),
514514
id: import.source.did.map(from_def_id),
515515
glob: true,

src/librustdoc/json/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ impl JsonRenderer<'tcx> {
109109
.map(Clone::clone),
110110
visibility: types::Visibility::Public,
111111
inner: types::ItemEnum::Trait(trait_item.clone().into()),
112-
source: None,
112+
span: None,
113113
docs: Default::default(),
114114
links: Default::default(),
115115
attrs: Default::default(),
@@ -246,7 +246,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
246246
)
247247
})
248248
.collect(),
249-
format_version: 4,
249+
format_version: 5,
250250
};
251251
let mut p = self.out_path.clone();
252252
p.push(output.index.get(&output.root).unwrap().name.clone().unwrap());

src/librustdoc/passes/calculate_doc_coverage.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ impl<'a, 'b> fold::DocFolder for CoverageCalculator<'a, 'b> {
212212
return Some(i);
213213
}
214214
clean::ImplItem(ref impl_) => {
215-
let filename = i.source.filename(self.ctx.sess());
215+
let filename = i.span.filename(self.ctx.sess());
216216
if let Some(ref tr) = impl_.trait_ {
217217
debug!(
218218
"impl {:#} for {:#} in {}",
@@ -243,7 +243,7 @@ impl<'a, 'b> fold::DocFolder for CoverageCalculator<'a, 'b> {
243243
None,
244244
);
245245

246-
let filename = i.source.filename(self.ctx.sess());
246+
let filename = i.span.filename(self.ctx.sess());
247247
let has_doc_example = tests.found_tests != 0;
248248
let hir_id = self.ctx.tcx.hir().local_def_id_to_hir_id(i.def_id.expect_local());
249249
let (level, source) = self.ctx.tcx.lint_level_at_node(MISSING_DOCS, hir_id);

src/librustdoc/passes/check_code_block_syntax.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
8686
// We couldn't calculate the span of the markdown block that had the error, so our
8787
// diagnostics are going to be a bit lacking.
8888
let mut diag = self.cx.sess().struct_span_warn(
89-
super::span_of_attrs(&item.attrs).unwrap_or(item.source.span()),
89+
super::span_of_attrs(&item.attrs).unwrap_or(item.span.inner()),
9090
"doc comment contains an invalid Rust code block",
9191
);
9292

@@ -110,7 +110,7 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
110110
impl<'a, 'tcx> DocFolder for SyntaxChecker<'a, 'tcx> {
111111
fn fold_item(&mut self, item: clean::Item) -> Option<clean::Item> {
112112
if let Some(dox) = &item.attrs.collapsed_doc_value() {
113-
let sp = span_of_attrs(&item.attrs).unwrap_or(item.source.span());
113+
let sp = span_of_attrs(&item.attrs).unwrap_or(item.span.inner());
114114
let extra = crate::html::markdown::ExtraInfo::new_did(self.cx.tcx, item.def_id, sp);
115115
for code_block in markdown::rust_code_blocks(&dox, &extra) {
116116
self.check_rust_syntax(&item, &dox, code_block);

src/librustdoc/passes/collect_intra_doc_links.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1226,9 +1226,7 @@ impl LinkCollector<'_, '_> {
12261226
&ori_link.range,
12271227
&item.attrs,
12281228
)
1229-
.unwrap_or_else(|| {
1230-
span_of_attrs(&item.attrs).unwrap_or(item.source.span())
1231-
});
1229+
.unwrap_or_else(|| span_of_attrs(&item.attrs).unwrap_or(item.span.inner()));
12321230

12331231
rustc_session::parse::feature_err(
12341232
&self.cx.tcx.sess.parse_sess,
@@ -1693,7 +1691,7 @@ fn report_diagnostic(
16931691
};
16941692

16951693
let attrs = &item.attrs;
1696-
let sp = span_of_attrs(attrs).unwrap_or(item.source.span());
1694+
let sp = span_of_attrs(attrs).unwrap_or(item.span.inner());
16971695

16981696
tcx.struct_span_lint_hir(lint, hir_id, sp, |lint| {
16991697
let mut diag = lint.build(msg);

src/librustdoc/passes/doc_test_lints.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ crate fn look_for_tests<'tcx>(cx: &DocContext<'tcx>, dox: &str, item: &Item) {
9797
if tests.found_tests == 0 && cx.tcx.sess.is_nightly_build() {
9898
if should_have_doc_example(cx, &item) {
9999
debug!("reporting error for {:?} (hir_id={:?})", item, hir_id);
100-
let sp = span_of_attrs(&item.attrs).unwrap_or(item.source.span());
100+
let sp = span_of_attrs(&item.attrs).unwrap_or(item.span.inner());
101101
cx.tcx.struct_span_lint_hir(
102102
crate::lint::MISSING_DOC_CODE_EXAMPLES,
103103
hir_id,
@@ -109,7 +109,7 @@ crate fn look_for_tests<'tcx>(cx: &DocContext<'tcx>, dox: &str, item: &Item) {
109109
cx.tcx.struct_span_lint_hir(
110110
crate::lint::PRIVATE_DOC_TESTS,
111111
hir_id,
112-
span_of_attrs(&item.attrs).unwrap_or(item.source.span()),
112+
span_of_attrs(&item.attrs).unwrap_or(item.span.inner()),
113113
|lint| lint.build("documentation test in private item").emit(),
114114
);
115115
}

src/librustdoc/passes/html_tags.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ impl<'a, 'tcx> DocFolder for InvalidHtmlTagsLinter<'a, 'tcx> {
181181
let sp = match super::source_span_for_markdown_range(tcx, &dox, range, &item.attrs)
182182
{
183183
Some(sp) => sp,
184-
None => span_of_attrs(&item.attrs).unwrap_or(item.source.span()),
184+
None => span_of_attrs(&item.attrs).unwrap_or(item.span.inner()),
185185
};
186186
tcx.struct_span_lint_hir(crate::lint::INVALID_HTML_TAGS, hir_id, sp, |lint| {
187187
lint.build(msg).emit()

src/librustdoc/passes/non_autolinks.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl<'a, 'tcx> DocFolder for NonAutolinksLinter<'a, 'tcx> {
7272
let report_diag = |cx: &DocContext<'_>, msg: &str, url: &str, range: Range<usize>| {
7373
let sp = super::source_span_for_markdown_range(cx.tcx, &dox, &range, &item.attrs)
7474
.or_else(|| span_of_attrs(&item.attrs))
75-
.unwrap_or(item.source.span());
75+
.unwrap_or(item.span.inner());
7676
cx.tcx.struct_span_lint_hir(crate::lint::NON_AUTOLINKS, hir_id, sp, |lint| {
7777
lint.build(msg)
7878
.span_suggestion(

src/rustdoc-json-types/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pub struct Item {
6464
pub name: Option<String>,
6565
/// The source location of this item (absent if it came from a macro expansion or inline
6666
/// assembly).
67-
pub source: Option<Span>,
67+
pub span: Option<Span>,
6868
/// By default all documented items are public, but you can tell rustdoc to output private items
6969
/// so this field is needed to differentiate.
7070
pub visibility: Visibility,
@@ -461,7 +461,7 @@ pub struct Impl {
461461
#[serde(rename_all = "snake_case")]
462462
pub struct Import {
463463
/// The full path being imported.
464-
pub span: String,
464+
pub source: String,
465465
/// May be different from the last segment of `source` when renaming imports:
466466
/// `use source as name;`
467467
pub name: String,

src/test/rustdoc-json/nested.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub mod l1 {
2424
// @has - "$.index[*][?(@.name=='l3')].inner.items[*]" $l4_id
2525
pub struct L4;
2626
}
27-
// @is nested.json "$.index[*][?(@.inner.span=='l3::L4')].kind" \"import\"
28-
// @is - "$.index[*][?(@.inner.span=='l3::L4')].inner.glob" false
27+
// @is nested.json "$.index[*][?(@.inner.source=='l3::L4')].kind" \"import\"
28+
// @is - "$.index[*][?(@.inner.source=='l3::L4')].inner.glob" false
2929
pub use l3::L4;
3030
}

src/test/rustdoc-json/reexport/rename_public.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ pub mod inner {
1313
// @set import_id = - "$.index[*][?(@.inner.name=='NewName')].id"
1414
// @!has - "$.index[*][?(@.inner.name=='Public')]"
1515
// @has - "$.index[*][?(@.name=='rename_public')].inner.items[*]" $import_id
16-
// @is - "$.index[*][?(@.inner.name=='NewName')].inner.span" \"inner::Public\"
16+
// @is - "$.index[*][?(@.inner.name=='NewName')].inner.source" \"inner::Public\"
1717
pub use inner::Public as NewName;

src/test/rustdoc-json/reexport/simple_public.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ pub mod inner {
1414

1515
// @set import_id = - "$.index[*][?(@.inner.name=='Public')].id"
1616
// @has - "$.index[*][?(@.name=='simple_public')].inner.items[*]" $import_id
17-
// @is - "$.index[*][?(@.inner.name=='Public')].inner.span" \"inner::Public\"
17+
// @is - "$.index[*][?(@.inner.name=='Public')].inner.source" \"inner::Public\"
1818
pub use inner::Public;

0 commit comments

Comments
 (0)