diff options
| author | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2025-04-23 22:09:11 +0200 |
|---|---|---|
| committer | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2025-04-23 22:09:11 +0200 |
| commit | 35363245657ee53a3735f1cef0df9a45e9ed44b9 (patch) | |
| tree | 9dd1eb262e037ed089f5a8c6cfa1801cdfc034e2 | |
| parent | 553600e0f5f5a7d492de6d95ccb2f057005f5651 (diff) | |
| download | rust-35363245657ee53a3735f1cef0df9a45e9ed44b9.tar.gz rust-35363245657ee53a3735f1cef0df9a45e9ed44b9.zip | |
Fix detection of `main` function if there are expressions around it
| -rw-r--r-- | src/librustdoc/doctest/make.rs | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/src/librustdoc/doctest/make.rs b/src/librustdoc/doctest/make.rs index d5c965f7053..94e227b70e0 100644 --- a/src/librustdoc/doctest/make.rs +++ b/src/librustdoc/doctest/make.rs @@ -407,15 +407,19 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result<ParseSourceIn push_to_s(&mut info.crate_attrs, source, attr.span, &mut prev_span_hi); } } + let mut has_non_module_items = false; for stmt in &body.stmts { let mut is_extern_crate = false; match stmt.kind { StmtKind::Item(ref item) => { 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(); @@ -437,7 +441,11 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result<ParseSourceIn } } } - _ => {} + // 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 @@ -462,6 +470,11 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result<ParseSourceIn push_to_s(&mut info.crates, source, span, &mut prev_span_hi); } } + if has_non_module_items { + // FIXME: if `info.has_main_fn` is `true`, emit a warning here to mention that + // this code will not be called. + info.has_main_fn = false; + } Ok(info) } Err(e) => { |
