Skip to content

Commit

Permalink
Rollup merge of rust-lang#48415 - QuietMisdreavus:traits-on-traits-on…
Browse files Browse the repository at this point in the history
…-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.
  • Loading branch information
Manishearth authored Feb 24, 2018
2 parents 9901bef + 8872e7b commit 43d1d6e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/librustdoc/clean/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,16 @@ fn separate_supertrait_bounds(mut g: clean::Generics)
}

pub fn record_extern_trait(cx: &DocContext, did: DefId) {
cx.external_traits.borrow_mut().entry(did).or_insert_with(|| {
build_external_trait(cx, did)
});
if cx.external_traits.borrow().contains_key(&did) ||
cx.active_extern_traits.borrow().contains(&did)
{
return;
}

cx.active_extern_traits.borrow_mut().push(did);

let trait_ = build_external_trait(cx, did);

cx.external_traits.borrow_mut().insert(did, trait_);
cx.active_extern_traits.borrow_mut().remove_item(&did);
}
4 changes: 4 additions & 0 deletions src/librustdoc/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ pub struct DocContext<'a, 'tcx: 'a, 'rcx: 'a> {
pub renderinfo: RefCell<RenderInfo>,
/// Later on moved through `clean::Crate` into `html::render::CACHE_KEY`
pub external_traits: RefCell<FxHashMap<DefId, clean::Trait>>,
/// Used while populating `external_traits` to ensure we don't process the same trait twice at
/// the same time.
pub active_extern_traits: RefCell<Vec<DefId>>,
// The current set of type and lifetime substitutions,
// for expanding type aliases at the HIR level:

Expand Down Expand Up @@ -253,6 +256,7 @@ pub fn run_core(search_paths: SearchPaths,
populated_all_crate_impls: Cell::new(false),
access_levels: RefCell::new(access_levels),
external_traits: Default::default(),
active_extern_traits: Default::default(),
renderinfo: Default::default(),
ty_substs: Default::default(),
lt_substs: Default::default(),
Expand Down
15 changes: 15 additions & 0 deletions src/test/rustdoc/auxiliary/issue-48414.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

/// Woah, this trait links to [OtherTrait](OtherTrait)!
pub trait SomeTrait {}

/// Woah, this trait links to [SomeTrait](SomeTrait)!
pub trait OtherTrait {}
21 changes: 21 additions & 0 deletions src/test/rustdoc/issue-48414.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// aux-build:issue-48414.rs

// ICE when resolving paths for a trait that linked to another trait, when both were in an external
// crate

#![crate_name = "base"]

extern crate issue_48414;

#[doc(inline)]
pub use issue_48414::{SomeTrait, OtherTrait};

0 comments on commit 43d1d6e

Please sign in to comment.