Skip to content

Commit 43d1d6e

Browse files
authored
Rollup merge of rust-lang#48415 - QuietMisdreavus:traits-on-traits-on-traits, r=Manishearth
rustdoc: don't crash when an external trait's docs needs to import another trait Fixes rust-lang#48414 When resolving intra-paths for an item, rustdoc needs to have information about their items on hand, for proper bookkeeping. When loading a path for an external item, it needs to load these items from their host crate, since their information isn't otherwise available. This includes resolving paths for those docs. which can cause this process to recurse. Rustdoc keeps a map of external traits in a `RefCell<HashMap<DefId, Trait>>`, and it keeps a borrow of this active when importing an external trait. In the linked crash, this led to a RefCell borrow error, panic, and ICE. This PR manually releases the borrow while importing the trait, and also keeps a list of traits being imported at the given moment. The latter keeps rustdoc from infinitely recursing as it tries to import the same trait repeatedly.
2 parents 9901bef + 8872e7b commit 43d1d6e

File tree

4 files changed

+52
-3
lines changed

4 files changed

+52
-3
lines changed

src/librustdoc/clean/inline.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,16 @@ fn separate_supertrait_bounds(mut g: clean::Generics)
512512
}
513513

514514
pub fn record_extern_trait(cx: &DocContext, did: DefId) {
515-
cx.external_traits.borrow_mut().entry(did).or_insert_with(|| {
516-
build_external_trait(cx, did)
517-
});
515+
if cx.external_traits.borrow().contains_key(&did) ||
516+
cx.active_extern_traits.borrow().contains(&did)
517+
{
518+
return;
519+
}
520+
521+
cx.active_extern_traits.borrow_mut().push(did);
522+
523+
let trait_ = build_external_trait(cx, did);
524+
525+
cx.external_traits.borrow_mut().insert(did, trait_);
526+
cx.active_extern_traits.borrow_mut().remove_item(&did);
518527
}

src/librustdoc/core.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ pub struct DocContext<'a, 'tcx: 'a, 'rcx: 'a> {
6161
pub renderinfo: RefCell<RenderInfo>,
6262
/// Later on moved through `clean::Crate` into `html::render::CACHE_KEY`
6363
pub external_traits: RefCell<FxHashMap<DefId, clean::Trait>>,
64+
/// Used while populating `external_traits` to ensure we don't process the same trait twice at
65+
/// the same time.
66+
pub active_extern_traits: RefCell<Vec<DefId>>,
6467
// The current set of type and lifetime substitutions,
6568
// for expanding type aliases at the HIR level:
6669

@@ -253,6 +256,7 @@ pub fn run_core(search_paths: SearchPaths,
253256
populated_all_crate_impls: Cell::new(false),
254257
access_levels: RefCell::new(access_levels),
255258
external_traits: Default::default(),
259+
active_extern_traits: Default::default(),
256260
renderinfo: Default::default(),
257261
ty_substs: Default::default(),
258262
lt_substs: Default::default(),
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
/// Woah, this trait links to [OtherTrait](OtherTrait)!
12+
pub trait SomeTrait {}
13+
14+
/// Woah, this trait links to [SomeTrait](SomeTrait)!
15+
pub trait OtherTrait {}

src/test/rustdoc/issue-48414.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// aux-build:issue-48414.rs
12+
13+
// ICE when resolving paths for a trait that linked to another trait, when both were in an external
14+
// crate
15+
16+
#![crate_name = "base"]
17+
18+
extern crate issue_48414;
19+
20+
#[doc(inline)]
21+
pub use issue_48414::{SomeTrait, OtherTrait};

0 commit comments

Comments
 (0)