Skip to content

Commit be66802

Browse files
Auto merge of #149043 - aDotInTheVoid:is-this-real-is-this-out-of-spite-does-it-matter, r=<try>
rustdoc-json: add rlib path to ExternalCrate to enable robust crate resolution try-job: dist-various-1
2 parents 7281a3b + a47dff8 commit be66802

File tree

9 files changed

+116
-6
lines changed

9 files changed

+116
-6
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3336,6 +3336,7 @@ dependencies = [
33363336
"libc",
33373337
"object 0.37.3",
33383338
"regex",
3339+
"rustdoc-json-types",
33393340
"serde_json",
33403341
"similar",
33413342
"wasmparser 0.236.1",

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`].

src/tools/run-make-support/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ wasmparser = { version = "0.236", default-features = false, features = ["std", "
2222

2323
# Shared with bootstrap and compiletest
2424
build_helper = { path = "../../build_helper" }
25+
# Shared with rustdoc
26+
rustdoc-json-types = { path = "../../rustdoc-json-types" }
2527

2628
[lib]
2729
crate-type = ["lib", "dylib"]

src/tools/run-make-support/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub mod rfs {
3434
}
3535

3636
// Re-exports of third-party library crates.
37-
pub use {bstr, gimli, libc, object, regex, serde_json, similar, wasmparser};
37+
pub use {bstr, gimli, libc, object, regex, rustdoc_json_types, serde_json, similar, wasmparser};
3838

3939
// Helpers for building names of output artifacts that are potentially target-specific.
4040
pub use crate::artifact_names::{
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#![no_std]
2+
3+
pub struct S;
4+
5+
pub use trans_dep::S as TransDep;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#![no_std]
2+
3+
pub type FromDep = dep::S;
4+
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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#![no_std]
2+
3+
pub struct S;

0 commit comments

Comments
 (0)