Skip to content

Commit 0e0a84a

Browse files
Do not consider nested functions as main function even if named main in doctests
1 parent 8aca4ba commit 0e0a84a

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

Diff for: src/librustdoc/doctest/make.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -289,21 +289,28 @@ fn parse_source(
289289
// Recurse through functions body. It is necessary because the doctest source code is
290290
// wrapped in a function to limit the number of AST errors. If we don't recurse into
291291
// functions, we would thing all top-level items (so basically nothing).
292-
fn check_item(item: &ast::Item, info: &mut ParseSourceInfo, crate_name: &Option<&str>) {
292+
fn check_item(
293+
item: &ast::Item,
294+
info: &mut ParseSourceInfo,
295+
crate_name: &Option<&str>,
296+
is_top_level: bool,
297+
) {
293298
if !info.has_global_allocator
294299
&& item.attrs.iter().any(|attr| attr.name_or_empty() == sym::global_allocator)
295300
{
296301
info.has_global_allocator = true;
297302
}
298303
match item.kind {
299304
ast::ItemKind::Fn(ref fn_item) if !info.has_main_fn => {
300-
if item.ident.name == sym::main {
305+
if item.ident.name == sym::main && is_top_level {
301306
info.has_main_fn = true;
302307
}
303308
if let Some(ref body) = fn_item.body {
304309
for stmt in &body.stmts {
305310
match stmt.kind {
306-
ast::StmtKind::Item(ref item) => check_item(item, info, crate_name),
311+
ast::StmtKind::Item(ref item) => {
312+
check_item(item, info, crate_name, false)
313+
}
307314
ast::StmtKind::MacCall(..) => info.found_macro = true,
308315
_ => {}
309316
}
@@ -329,7 +336,7 @@ fn parse_source(
329336
loop {
330337
match parser.parse_item(ForceCollect::No) {
331338
Ok(Some(item)) => {
332-
check_item(&item, info, crate_name);
339+
check_item(&item, info, crate_name, true);
333340

334341
if info.has_main_fn && info.found_extern_crate {
335342
break;

0 commit comments

Comments
 (0)