Skip to content

Commit 715d6a9

Browse files
committed
Auto merge of #50305 - GuillaumeGomez:fix-mod-stackoverflow, r=QuietMisdreavus
Prevent infinite recursion of modules Fixes #50196. r? @QuietMisdreavus
2 parents 565235e + 3ba7c00 commit 715d6a9

File tree

4 files changed

+47
-9
lines changed

4 files changed

+47
-9
lines changed

src/librustdoc/clean/inline.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use super::Clean;
3838
///
3939
/// The returned value is `None` if the definition could not be inlined,
4040
/// and `Some` of a vector of items if it was successfully expanded.
41-
pub fn try_inline(cx: &DocContext, def: Def, name: ast::Name)
41+
pub fn try_inline(cx: &DocContext, def: Def, name: ast::Name, visited: &mut FxHashSet<DefId>)
4242
-> Option<Vec<clean::Item>> {
4343
if def == Def::Err { return None }
4444
let did = def.def_id();
@@ -87,7 +87,7 @@ pub fn try_inline(cx: &DocContext, def: Def, name: ast::Name)
8787
Def::StructCtor(..) => return Some(Vec::new()),
8888
Def::Mod(did) => {
8989
record_extern_fqn(cx, did, clean::TypeKind::Module);
90-
clean::ModuleItem(build_module(cx, did))
90+
clean::ModuleItem(build_module(cx, did, visited))
9191
}
9292
Def::Static(did, mtbl) => {
9393
record_extern_fqn(cx, did, clean::TypeKind::Static);
@@ -385,24 +385,24 @@ pub fn build_impl(cx: &DocContext, did: DefId, ret: &mut Vec<clean::Item>) {
385385
});
386386
}
387387

388-
fn build_module(cx: &DocContext, did: DefId) -> clean::Module {
388+
fn build_module(cx: &DocContext, did: DefId, visited: &mut FxHashSet<DefId>) -> clean::Module {
389389
let mut items = Vec::new();
390-
fill_in(cx, did, &mut items);
390+
fill_in(cx, did, &mut items, visited);
391391
return clean::Module {
392392
items,
393393
is_crate: false,
394394
};
395395

396-
fn fill_in(cx: &DocContext, did: DefId, items: &mut Vec<clean::Item>) {
396+
fn fill_in(cx: &DocContext, did: DefId, items: &mut Vec<clean::Item>,
397+
visited: &mut FxHashSet<DefId>) {
397398
// If we're re-exporting a re-export it may actually re-export something in
398399
// two namespaces, so the target may be listed twice. Make sure we only
399400
// visit each node at most once.
400-
let mut visited = FxHashSet();
401401
for &item in cx.tcx.item_children(did).iter() {
402402
let def_id = item.def.def_id();
403403
if item.vis == ty::Visibility::Public {
404-
if !visited.insert(def_id) { continue }
405-
if let Some(i) = try_inline(cx, item.def, item.ident.name) {
404+
if did == def_id || !visited.insert(def_id) { continue }
405+
if let Some(i) = try_inline(cx, item.def, item.ident.name, visited) {
406406
items.extend(i)
407407
}
408408
}

src/librustdoc/clean/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -3686,7 +3686,8 @@ impl Clean<Vec<Item>> for doctree::Import {
36863686
} else {
36873687
let name = self.name;
36883688
if !denied {
3689-
if let Some(items) = inline::try_inline(cx, path.def, name) {
3689+
let mut visited = FxHashSet();
3690+
if let Some(items) = inline::try_inline(cx, path.def, name, &mut visited) {
36903691
return items;
36913692
}
36923693
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2018 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+
// compile-flags: -Cmetadata=aux
12+
13+
pub mod tree {
14+
pub use tree;
15+
}
16+
17+
pub mod tree2 {
18+
pub mod prelude {
19+
pub use tree2;
20+
}
21+
}

src/test/rustdoc/mod-stackoverflow.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2018 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:mod-stackoverflow.rs
12+
// ignore-cross-compile
13+
14+
extern crate mod_stackoverflow;
15+
pub use mod_stackoverflow::tree;
16+
pub use mod_stackoverflow::tree2;

0 commit comments

Comments
 (0)