Skip to content

Commit d49361a

Browse files
authored
Rollup merge of rust-lang#139919 - GuillaumeGomez:rustdoc-json-1-indexed, r=aDotInTheVoid
Make rustdoc JSON Span column 1-based, just like line numbers Fixes rust-lang#139906. This PR does two things: 1. It makes column 1-indexed as well, just like lines. 2. It updates documentation about them to mention that they are 1-indexed. I think it's better for coherency to have them both 1-indexed instead of the weird mix we used to have. Docs for `line` and `col` fields can be found [here](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_span/struct.Loc.html#structfield.line). And finally: it adds a regression test to ensure they are indeed 1-indexed. r? `@aDotInTheVoid`
2 parents dff14f0 + 076016d commit d49361a

File tree

4 files changed

+11
-7
lines changed

4 files changed

+11
-7
lines changed

Diff for: src/librustdoc/json/conversions.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ impl JsonRenderer<'_> {
8484
let lo = span.lo(self.sess());
8585
Some(Span {
8686
filename: local_path,
87-
begin: (lo.line, lo.col.to_usize()),
88-
end: (hi.line, hi.col.to_usize()),
87+
begin: (lo.line, lo.col.to_usize() + 1),
88+
end: (hi.line, hi.col.to_usize() + 1),
8989
})
9090
} else {
9191
None

Diff for: src/rustdoc-json-types/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub type FxHashMap<K, V> = HashMap<K, V>; // re-export for use in src/librustdoc
3030
/// This integer is incremented with every breaking change to the API,
3131
/// and is returned along with the JSON blob as [`Crate::format_version`].
3232
/// Consuming code should assert that this value matches the format version(s) that it supports.
33-
pub const FORMAT_VERSION: u32 = 44;
33+
pub const FORMAT_VERSION: u32 = 45;
3434

3535
/// The root of the emitted JSON blob.
3636
///
@@ -205,9 +205,9 @@ pub struct Item {
205205
pub struct Span {
206206
/// The path to the source file for this span relative to the path `rustdoc` was invoked with.
207207
pub filename: PathBuf,
208-
/// Zero indexed Line and Column of the first character of the `Span`
208+
/// One indexed Line and Column of the first character of the `Span`.
209209
pub begin: (usize, usize),
210-
/// Zero indexed Line and Column of the last character of the `Span`
210+
/// One indexed Line and Column of the last character of the `Span`.
211211
pub end: (usize, usize),
212212
}
213213

Diff for: tests/rustdoc-json/impls/auto.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ impl Foo {
1515
}
1616

1717
// Testing spans, so all tests below code
18-
//@ is "$.index[?(@.docs=='has span')].span.begin" "[13, 0]"
19-
//@ is "$.index[?(@.docs=='has span')].span.end" "[15, 1]"
18+
//@ is "$.index[?(@.docs=='has span')].span.begin" "[13, 1]"
19+
//@ is "$.index[?(@.docs=='has span')].span.end" "[15, 2]"
2020
// FIXME: this doesn't work due to https://github.com/freestrings/jsonpath/issues/91
2121
// is "$.index[?(@.inner.impl.is_synthetic==true)].span" null
2222
pub struct Foo;

Diff for: tests/rustdoc-json/span.rs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pub mod bar {}
2+
// This test ensures that spans are 1-indexed.
3+
//@ is "$.index[?(@.name=='span')].span.begin" "[1, 1]"
4+
//@ is "$.index[?(@.name=='bar')].span.begin" "[1, 1]"

0 commit comments

Comments
 (0)