Skip to content

Commit

Permalink
Do not consider nested functions as main function even if named `ma…
Browse files Browse the repository at this point in the history
…in` in doctests
  • Loading branch information
GuillaumeGomez committed Oct 24, 2024
1 parent 8aca4ba commit 0e0a84a
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/librustdoc/doctest/make.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,21 +289,28 @@ fn parse_source(
// Recurse through functions body. It is necessary because the doctest source code is
// wrapped in a function to limit the number of AST errors. If we don't recurse into
// functions, we would thing all top-level items (so basically nothing).
fn check_item(item: &ast::Item, info: &mut ParseSourceInfo, crate_name: &Option<&str>) {
fn check_item(
item: &ast::Item,
info: &mut ParseSourceInfo,
crate_name: &Option<&str>,
is_top_level: bool,
) {
if !info.has_global_allocator
&& item.attrs.iter().any(|attr| attr.name_or_empty() == sym::global_allocator)
{
info.has_global_allocator = true;
}
match item.kind {
ast::ItemKind::Fn(ref fn_item) if !info.has_main_fn => {
if item.ident.name == sym::main {
if item.ident.name == sym::main && is_top_level {
info.has_main_fn = true;
}
if let Some(ref body) = fn_item.body {
for stmt in &body.stmts {
match stmt.kind {
ast::StmtKind::Item(ref item) => check_item(item, info, crate_name),
ast::StmtKind::Item(ref item) => {
check_item(item, info, crate_name, false)
}
ast::StmtKind::MacCall(..) => info.found_macro = true,
_ => {}
}
Expand All @@ -329,7 +336,7 @@ fn parse_source(
loop {
match parser.parse_item(ForceCollect::No) {
Ok(Some(item)) => {
check_item(&item, info, crate_name);
check_item(&item, info, crate_name, true);

if info.has_main_fn && info.found_extern_crate {
break;
Expand Down

0 comments on commit 0e0a84a

Please sign in to comment.