From 0457f2e9273b69c8e00a0a113e62e460a96c2fcf Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 23 Apr 2025 22:09:11 +0200 Subject: [PATCH] Fix detection of `main` function if there are expressions around it (cherry picked from commit 35363245657ee53a3735f1cef0df9a45e9ed44b9) --- src/librustdoc/doctest/make.rs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/librustdoc/doctest/make.rs b/src/librustdoc/doctest/make.rs index 56b1e76ae8cf..3896a3c6761f 100644 --- a/src/librustdoc/doctest/make.rs +++ b/src/librustdoc/doctest/make.rs @@ -411,15 +411,19 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result { is_extern_crate = check_item(&item, &mut info, crate_name); } - StmtKind::Expr(ref expr) if matches!(expr.kind, ast::ExprKind::Err(_)) => { - reset_error_count(&psess); - return Err(()); + StmtKind::Expr(ref expr) => { + if matches!(expr.kind, ast::ExprKind::Err(_)) { + reset_error_count(&psess); + return Err(()); + } + has_non_module_items = true; } StmtKind::MacCall(ref mac_call) if !info.has_main_fn => { let mut iter = mac_call.mac.args.tokens.iter(); @@ -441,7 +445,11 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result {} + // We do nothing in this case. Not marking it as `non_module_items` either. + StmtKind::Empty => {} + _ => { + has_non_module_items = true; + } } // Weirdly enough, the `Stmt` span doesn't include its attributes, so we need to @@ -466,6 +474,11 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result {