Skip to content

Commit 0e5a209

Browse files
committed
Auto merge of #58058 - QuietMisdreavus:use-attr, r=GuillaumeGomez
rustdoc: don't try to get a DefId for a Def that doesn't have one Fixes #58054 The compiler allows you to write a `use` statement for a built-in non-macro attribute, since `use proc_macro` can apply to both the `proc_macro` crate and the `#[proc_macro]` attribute. However, if you write a use statement for something that *doesn't* have this crossover, rustdoc will try to use it the same way as anything else... which resulted in an ICE because it tried to pull a DefId for something that didn't have one. This PR makes rustdoc skip those lookups when it encounters them, allowing it to properly process and render these imports.
2 parents 65e647c + c955f17 commit 0e5a209

File tree

4 files changed

+18
-6
lines changed

4 files changed

+18
-6
lines changed

src/librustdoc/clean/inline.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,11 @@ use super::Clean;
3737
/// and `Some` of a vector of items if it was successfully expanded.
3838
pub fn try_inline(cx: &DocContext, def: Def, name: ast::Name, visited: &mut FxHashSet<DefId>)
3939
-> Option<Vec<clean::Item>> {
40-
if def == Def::Err { return None }
41-
let did = def.def_id();
40+
let did = if let Some(did) = def.opt_def_id() {
41+
did
42+
} else {
43+
return None;
44+
};
4245
if did.is_local() { return None }
4346
let mut ret = Vec::new();
4447
let inner = match def {

src/librustdoc/clean/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3835,7 +3835,7 @@ pub fn register_def(cx: &DocContext, def: Def) -> DefId {
38353835

38363836
fn resolve_use_source(cx: &DocContext, path: Path) -> ImportSource {
38373837
ImportSource {
3838-
did: if path.def == Def::Err {
3838+
did: if path.def.opt_def_id().is_none() {
38393839
None
38403840
} else {
38413841
Some(register_def(cx, path.def))

src/librustdoc/visit_ast.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -284,10 +284,11 @@ impl<'a, 'tcx, 'rcx> RustdocVisitor<'a, 'tcx, 'rcx> {
284284
debug!("maybe_inline_local def: {:?}", def);
285285

286286
let tcx = self.cx.tcx;
287-
if def == Def::Err {
287+
let def_did = if let Some(did) = def.opt_def_id() {
288+
did
289+
} else {
288290
return false;
289-
}
290-
let def_did = def.def_id();
291+
};
291292

292293
let use_attrs = tcx.hir().attrs(id);
293294
// Don't inline `doc(hidden)` imports so they can be stripped at a later stage.

src/test/rustdoc/use-attr.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// edition:2018
2+
3+
// ICE when rustdoc encountered a use statement of a non-macro attribute (see #58054)
4+
5+
// @has use_attr/index.html
6+
// @has - '//code' 'pub use proc_macro_attribute'
7+
pub use proc_macro_attribute;
8+
use proc_macro_derive;

0 commit comments

Comments
 (0)