Skip to content

Commit d69787f

Browse files
committed
Auto merge of #111195 - GuillaumeGomez:fix-ice-intra-doc-link, r=petrochenkov
Prevent crash when a path is not resolved in intra-doc link Fixes #111189. cc `@petrochenkov` r? `@notriddle`
2 parents 82faf5e + bcdfda1 commit d69787f

File tree

4 files changed

+40
-8
lines changed

4 files changed

+40
-8
lines changed

compiler/rustc_resolve/src/late.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_ast::*;
1717
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
1818
use rustc_errors::{Applicability, DiagnosticArgValue, DiagnosticId, IntoDiagnosticArg};
1919
use rustc_hir::def::Namespace::{self, *};
20-
use rustc_hir::def::{self, CtorKind, DefKind, LifetimeRes, PartialRes, PerNS};
20+
use rustc_hir::def::{self, CtorKind, DefKind, LifetimeRes, NonMacroAttrKind, PartialRes, PerNS};
2121
use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID, LOCAL_CRATE};
2222
use rustc_hir::{BindingAnnotation, PrimTy, TraitCandidate};
2323
use rustc_middle::middle::resolve_bound_vars::Set1;
@@ -4287,12 +4287,12 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
42874287
}
42884288
}
42894289

4290-
fn resolve_and_cache_rustdoc_path(&mut self, path_str: &str, ns: Namespace) -> bool {
4290+
fn resolve_and_cache_rustdoc_path(&mut self, path_str: &str, ns: Namespace) -> Option<Res> {
42914291
// FIXME: This caching may be incorrect in case of multiple `macro_rules`
42924292
// items with the same name in the same module.
42934293
// Also hygiene is not considered.
42944294
let mut doc_link_resolutions = std::mem::take(&mut self.r.doc_link_resolutions);
4295-
let res = doc_link_resolutions
4295+
let res = *doc_link_resolutions
42964296
.entry(self.parent_scope.module.nearest_parent_mod().expect_local())
42974297
.or_default()
42984298
.entry((Symbol::intern(path_str), ns))
@@ -4307,8 +4307,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
43074307
return None;
43084308
}
43094309
res
4310-
})
4311-
.is_some();
4310+
});
43124311
self.r.doc_link_resolutions = doc_link_resolutions;
43134312
res
43144313
}
@@ -4343,8 +4342,10 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
43434342
let mut any_resolved = false;
43444343
let mut need_assoc = false;
43454344
for ns in [TypeNS, ValueNS, MacroNS] {
4346-
if self.resolve_and_cache_rustdoc_path(&path_str, ns) {
4347-
any_resolved = true;
4345+
if let Some(res) = self.resolve_and_cache_rustdoc_path(&path_str, ns) {
4346+
// Rustdoc ignores tool attribute resolutions and attempts
4347+
// to resolve their prefixes for diagnostics.
4348+
any_resolved = !matches!(res, Res::NonMacroAttr(NonMacroAttrKind::Tool));
43484349
} else if ns != MacroNS {
43494350
need_assoc = true;
43504351
}

src/librustdoc/passes/collect_intra_doc_links.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ impl TryFrom<ResolveRes> for Res {
149149
Def(kind, id) => Ok(Res::Def(kind, id)),
150150
PrimTy(prim) => Ok(Res::Primitive(PrimitiveType::from_hir(prim))),
151151
// e.g. `#[derive]`
152-
NonMacroAttr(..) | Err => Result::Err(()),
152+
ToolMod | NonMacroAttr(..) | Err => Result::Err(()),
153153
other => bug!("unrecognized res {:?}", other),
154154
}
155155
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Regression test for <https://github.com/rust-lang/rust/issues/111189>.
2+
// This test ensures that it doesn't crash.
3+
4+
#![deny(warnings)]
5+
6+
/// #[rustfmt::skip]
7+
//~^ ERROR unresolved link to `rustfmt::skip`
8+
/// #[clippy::whatever]
9+
//~^ ERROR unresolved link to `clippy::whatever`
10+
pub fn foo() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error: unresolved link to `rustfmt::skip`
2+
--> $DIR/issue-111189-resolution-ice.rs:6:7
3+
|
4+
LL | /// #[rustfmt::skip]
5+
| ^^^^^^^^^^^^^ no item named `rustfmt` in scope
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/issue-111189-resolution-ice.rs:4:9
9+
|
10+
LL | #![deny(warnings)]
11+
| ^^^^^^^^
12+
= note: `#[deny(rustdoc::broken_intra_doc_links)]` implied by `#[deny(warnings)]`
13+
14+
error: unresolved link to `clippy::whatever`
15+
--> $DIR/issue-111189-resolution-ice.rs:8:7
16+
|
17+
LL | /// #[clippy::whatever]
18+
| ^^^^^^^^^^^^^^^^ no item named `clippy` in scope
19+
20+
error: aborting due to 2 previous errors
21+

0 commit comments

Comments
 (0)