Skip to content

Commit db534b3

Browse files
committed
Auto merge of rust-lang#75176 - jyn514:impl-link, r=GuillaumeGomez,petrochenkov
Fix intra-doc links for cross-crate re-exports of default trait methods The original fix for this was very simple: rust-lang#58972 ignored `extern_traits` because before rust-lang#65983 was fixed, they would always fail to resolve, giving spurious warnings. So the first commit just undoes that change, so extern traits are now seen by the `collect_intra_doc_links` pass. There are also some minor changes in `librustdoc/fold.rs` to avoid borrowing the `extern_traits` RefCell more than once at a time. However, that brought up a much more thorny problem. `rustc_resolve` started giving 'error: cannot find a built-in macro with name `cfg`' when documenting `libproc_macro` (I still haven't been able to reproduce on anything smaller than the full standard library). The chain of events looked like this (thanks @eddyb for the help debugging!): 0. `x.py build --stage 1` builds the standard library and creates a sysroot 1. `cargo doc` does something like `cargo check` to create `rmeta`s for all the crates (unrelated to what was built above) 2. the `cargo check`-like `libcore-*.rmeta` is loaded as a transitive dependency *and claims ownership* of builtin macros 3. `rustdoc` later tries to resolve some path in a doc link 4. suggestion logic fires and loads "extern prelude" crates by name 5. the sysroot `libcore-*.rlib` is loaded and *fails to claim ownership* of builtin macros `rustc_resolve` gives the error after step 5. However, `rustdoc` doesn't need suggestions at all - `resolve_str_path_error` completely discards the `ResolutionError`! The fix implemented in this PR is to skip the suggestion logic for `resolve_ast_path`: pass `record_used: false` and skip `lookup_import_candidates` when `record_used` isn't set. It's possible that if/when rust-lang#74207 is implemented this will need a more in-depth fix which returns a `ResolutionError` from `compile_macro`, to allow rustdoc to reuse the suggestions from rustc_resolve. However, that's a much larger change and there's no need for it yet, so I haven't implemented it here. Fixes rust-lang#73829. r? @GuillaumeGomez
2 parents b1d092c + 9131d23 commit db534b3

File tree

7 files changed

+27
-25
lines changed

7 files changed

+27
-25
lines changed

src/librustc_metadata/creader.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ impl<'a> CrateLoader<'a> {
222222
let mut ret = None;
223223
self.cstore.iter_crate_data(|cnum, data| {
224224
if data.name() != name {
225+
tracing::trace!("{} did not match {}", data.name(), name);
225226
return;
226227
}
227228

@@ -230,7 +231,10 @@ impl<'a> CrateLoader<'a> {
230231
ret = Some(cnum);
231232
return;
232233
}
233-
Some(..) => return,
234+
Some(hash) => {
235+
debug!("actual hash {} did not match expected {}", hash, data.hash());
236+
return;
237+
}
234238
None => {}
235239
}
236240

@@ -273,6 +277,11 @@ impl<'a> CrateLoader<'a> {
273277
.1;
274278
if kind.matches(prev_kind) {
275279
ret = Some(cnum);
280+
} else {
281+
debug!(
282+
"failed to load existing crate {}; kind {:?} did not match prev_kind {:?}",
283+
name, kind, prev_kind
284+
);
276285
}
277286
});
278287
ret

src/librustc_resolve/lib.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -2385,8 +2385,12 @@ impl<'a> Resolver<'a> {
23852385
Res::Def(DefKind::Mod, _) => true,
23862386
_ => false,
23872387
};
2388-
let mut candidates =
2389-
self.lookup_import_candidates(ident, TypeNS, parent_scope, is_mod);
2388+
// Don't look up import candidates if this is a speculative resolve
2389+
let mut candidates = if record_used {
2390+
self.lookup_import_candidates(ident, TypeNS, parent_scope, is_mod)
2391+
} else {
2392+
Vec::new()
2393+
};
23902394
candidates.sort_by_cached_key(|c| {
23912395
(c.path.segments.len(), pprust::path_to_string(&c.path))
23922396
});
@@ -3200,7 +3204,7 @@ impl<'a> Resolver<'a> {
32003204
&Segment::from_path(path),
32013205
Some(ns),
32023206
parent_scope,
3203-
true,
3207+
false,
32043208
path.span,
32053209
CrateLint::No,
32063210
) {

src/librustdoc/clean/inline.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,9 @@ pub fn record_extern_trait(cx: &DocContext<'_>, did: DefId) {
628628
}
629629
}
630630

631-
cx.active_extern_traits.borrow_mut().insert(did);
631+
{
632+
cx.active_extern_traits.borrow_mut().insert(did);
633+
}
632634

633635
debug!("record_extern_trait: {:?}", did);
634636
let trait_ = build_external_trait(cx, did);

src/librustdoc/core.rs

+1
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ pub fn run_core(
439439
resolver.borrow_mut().access(|resolver| {
440440
sess.time("load_extern_crates", || {
441441
for extern_name in &extern_names {
442+
debug!("loading extern crate {}", extern_name);
442443
resolver
443444
.resolve_str_path_error(
444445
DUMMY_SP,

src/librustdoc/fold.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,11 @@ pub trait DocFolder: Sized {
9393
c.module = c.module.take().and_then(|module| self.fold_item(module));
9494

9595
{
96-
let mut guard = c.external_traits.borrow_mut();
97-
let external_traits = std::mem::replace(&mut *guard, Default::default());
98-
*guard = external_traits
99-
.into_iter()
100-
.map(|(k, mut v)| {
101-
v.items = v.items.into_iter().filter_map(|i| self.fold_item(i)).collect();
102-
(k, v)
103-
})
104-
.collect();
96+
let external_traits = { std::mem::take(&mut *c.external_traits.borrow_mut()) };
97+
for (k, mut v) in external_traits {
98+
v.items = v.items.into_iter().filter_map(|i| self.fold_item(i)).collect();
99+
c.external_traits.borrow_mut().insert(k, v);
100+
}
105101
}
106102
c
107103
}

src/librustdoc/passes/collect_intra_doc_links.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
161161
return Some(res.map_id(|_| panic!("unexpected id")));
162162
}
163163
if let Some(module_id) = parent_id {
164+
debug!("resolving {} as a macro in the module {:?}", path_str, module_id);
164165
if let Ok((_, res)) =
165166
resolver.resolve_str_path_error(DUMMY_SP, path_str, MacroNS, module_id)
166167
{
@@ -972,15 +973,6 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
972973
self.fold_item_recur(item)
973974
}
974975
}
975-
976-
// FIXME: if we can resolve intra-doc links from other crates, we can use the stock
977-
// `fold_crate`, but until then we should avoid scanning `krate.external_traits` since those
978-
// will never resolve properly
979-
fn fold_crate(&mut self, mut c: Crate) -> Crate {
980-
c.module = c.module.take().and_then(|module| self.fold_item(module));
981-
982-
c
983-
}
984976
}
985977

986978
#[derive(Copy, Clone, Debug, PartialEq, Eq)]

src/test/rustdoc/intra-doc-crate/traits.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// ignore-test
2-
// ^ this is https://github.com/rust-lang/rust/issues/73829
31
// aux-build:traits.rs
42
// build-aux-docs
53
// ignore-tidy-line-length

0 commit comments

Comments
 (0)