Skip to content

Commit 457ed4e

Browse files
committed
rustdoc-json: add rlib path to ExternalCrate to enable robust crate resolution
Historically, it's not been possible to robustly resolve a cross-crate item in rustdoc-json. If you had a `Id` that wasn't in `Crate::index` (because it was defined in a different crate), you could only look it up it `Crate::paths`. But there, you don't get the full information, only an `ItemSummary`. This tells you the `path` and the `crate_id`. But knowing the `crate_id` isn't enough to be able to build/find the rustdoc-json output with this item. It's only use is to get a `ExternalCrate` (via `Crate::external_crates`). But that only tells you the `name` (as a string). This isn't enough to uniquely identify a crate, as there could be multiple versions/features [1] [2]. This was originally proposed to be solved via LukeMathWalker's `--orchestrator-id` proposal (https://www.github.com/rust-lang/compiler-team/issues/635). But that requires invasive changes to cargo/rustc. This PR instead implements Urgau's proposal to re-use the path to a crate's rmeta/rlib as a unique identifer. Callers can use that to determine which package it corresponds to in the language of the build-system above rustc. E.g. for cargo, `cargo rustdoc --message-format=json --output-format=json -Zunstable-options`). (Once you've found the right external crate's rustdoc-json output, you still need to resolve the path->id in that crate. But that's """just""" a matter of walking the module tree. We should probably still make that nicer (by, for example, allowing sharing `Id`s between rustdoc-json document), but that's a future concern) For some notes from RustWeek 2025, where this was designed, see https://hackmd.io/0jkdguobTnW7nXoGKAxfEQ [1]: https://www.github.com/rust-lang/compiler-team/issues/635#issue-1714254865 § Problem [2]: https://rust-lang.zulipchat.com/#narrow/channel/266220-t-rustdoc/topic/Identifying.20external.20crates.20in.20Rustdoc.20JSON/with/352701211
1 parent 8c569c1 commit 457ed4e

File tree

6 files changed

+108
-5
lines changed

6 files changed

+108
-5
lines changed

src/librustdoc/json/mod.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,13 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
302302
ExternalLocation::Remote(s) => Some(s.clone()),
303303
_ => None,
304304
},
305+
path: self
306+
.tcx
307+
.used_crate_source(*crate_num)
308+
.paths()
309+
.next()
310+
.expect("crate should have at least 1 path")
311+
.clone(),
305312
},
306313
)
307314
})
@@ -339,20 +346,21 @@ mod size_asserts {
339346
// tidy-alphabetical-start
340347
static_assert_size!(AssocItemConstraint, 112);
341348
static_assert_size!(Crate, 184);
342-
static_assert_size!(ExternalCrate, 48);
343349
static_assert_size!(FunctionPointer, 168);
344350
static_assert_size!(GenericArg, 80);
345351
static_assert_size!(GenericArgs, 104);
346352
static_assert_size!(GenericBound, 72);
347353
static_assert_size!(GenericParamDef, 136);
348354
static_assert_size!(Impl, 304);
349-
// `Item` contains a `PathBuf`, which is different sizes on different OSes.
350-
static_assert_size!(Item, 528 + size_of::<std::path::PathBuf>());
351355
static_assert_size!(ItemSummary, 32);
352356
static_assert_size!(PolyTrait, 64);
353357
static_assert_size!(PreciseCapturingArg, 32);
354358
static_assert_size!(TargetFeature, 80);
355359
static_assert_size!(Type, 80);
356360
static_assert_size!(WherePredicate, 160);
357361
// tidy-alphabetical-end
362+
363+
// These contains a `PathBuf`, which is different sizes on different OSes.
364+
static_assert_size!(Item, 528 + size_of::<std::path::PathBuf>());
365+
static_assert_size!(ExternalCrate, 48 + size_of::<std::path::PathBuf>());
358366
}

src/rustdoc-json-types/lib.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ pub type FxHashMap<K, V> = HashMap<K, V>; // re-export for use in src/librustdoc
3737
// will instead cause conflicts. See #94591 for more. (This paragraph and the "Latest feature" line
3838
// are deliberately not in a doc comment, because they need not be in public docs.)
3939
//
40-
// Latest feature: Add `ItemKind::Attribute`.
41-
pub const FORMAT_VERSION: u32 = 56;
40+
// Latest feature: Add `ExternCrate::path`.
41+
pub const FORMAT_VERSION: u32 = 57;
4242

4343
/// The root of the emitted JSON blob.
4444
///
@@ -135,6 +135,12 @@ pub struct ExternalCrate {
135135
pub name: String,
136136
/// The root URL at which the crate's documentation lives.
137137
pub html_root_url: Option<String>,
138+
139+
/// A path from where this crate was loaded.
140+
///
141+
/// This will typically be a `.rlib` or `.rmeta`. It can be used to determine which crate
142+
/// this was in terms of whatever build-system invoked rustc.
143+
pub path: PathBuf,
138144
}
139145

140146
/// Information about an external (not defined in the local crate) [`Item`].
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/// dep.rs
2+
pub struct S;
3+
4+
pub use trans_dep::S as TransDep;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub type FromDep = dep::S;
2+
pub type FromTransDep = dep::TransDep;
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
use std::path::PathBuf;
2+
3+
use run_make_support::rustdoc_json_types::{Crate, ItemEnum, Path, Type, TypeAlias};
4+
use run_make_support::{cwd, rfs, rust_lib_name, rustc, rustdoc, serde_json};
5+
6+
fn main() {
7+
rustc().input("trans_dep.rs").edition("2024").crate_type("lib").run();
8+
9+
rustc()
10+
.input("dep.rs")
11+
.edition("2024")
12+
.crate_type("lib")
13+
.extern_("trans_dep", rust_lib_name("trans_dep"))
14+
.run();
15+
16+
rustdoc()
17+
.input("entry.rs")
18+
.edition("2024")
19+
.output_format("json")
20+
.library_search_path(cwd())
21+
.extern_("dep", rust_lib_name("dep"))
22+
.arg("-Zunstable-options")
23+
.run();
24+
25+
let bytes = rfs::read("doc/entry.json");
26+
27+
let krate: Crate = serde_json::from_slice(&bytes).expect("output should be valid json");
28+
29+
let root_item = &krate.index[&krate.root];
30+
let ItemEnum::Module(root_mod) = &root_item.inner else { panic!("expected ItemEnum::Module") };
31+
32+
assert_eq!(root_mod.items.len(), 2);
33+
34+
let items = root_mod.items.iter().map(|id| &krate.index[id]).collect::<Vec<_>>();
35+
36+
let from_dep = items
37+
.iter()
38+
.filter(|item| item.name.as_deref() == Some("FromDep"))
39+
.next()
40+
.expect("there should be en item called FromDep");
41+
42+
let from_trans_dep = items
43+
.iter()
44+
.filter(|item| item.name.as_deref() == Some("FromTransDep"))
45+
.next()
46+
.expect("there should be en item called FromDep");
47+
48+
let ItemEnum::TypeAlias(TypeAlias {
49+
type_: Type::ResolvedPath(Path { id: from_dep_id, .. }),
50+
..
51+
}) = &from_dep.inner
52+
else {
53+
panic!("Expected FromDep to be a TypeAlias");
54+
};
55+
56+
let ItemEnum::TypeAlias(TypeAlias {
57+
type_: Type::ResolvedPath(Path { id: from_trans_dep_id, .. }),
58+
..
59+
}) = &from_trans_dep.inner
60+
else {
61+
panic!("Expected FromDep to be a TypeAlias");
62+
};
63+
64+
assert_eq!(krate.index.get(from_dep_id), None);
65+
assert_eq!(krate.index.get(from_trans_dep_id), None);
66+
67+
let from_dep_externalinfo = &krate.paths[from_dep_id];
68+
let from_trans_dep_externalinfo = &krate.paths[from_trans_dep_id];
69+
70+
let dep_crate_id = from_dep_externalinfo.crate_id;
71+
let trans_dep_crate_id = from_trans_dep_externalinfo.crate_id;
72+
73+
let dep = &krate.external_crates[&dep_crate_id];
74+
let trans_dep = &krate.external_crates[&trans_dep_crate_id];
75+
76+
assert_eq!(dep.name, "dep");
77+
assert_eq!(trans_dep.name, "trans_dep");
78+
79+
assert_eq!(dep.path, cwd().join(rust_lib_name("dep")));
80+
assert_eq!(trans_dep.path, cwd().join(rust_lib_name("trans_dep")));
81+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/// trans_dep.rs
2+
pub struct S;

0 commit comments

Comments
 (0)