Skip to content

Commit 0457f2e

Browse files
GuillaumeGomezcuviper
authored andcommitted
Fix detection of main function if there are expressions around it
(cherry picked from commit 3536324)
1 parent 8ec60ca commit 0457f2e

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/librustdoc/doctest/make.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -411,15 +411,19 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result<ParseSourceIn
411411
push_to_s(&mut info.crate_attrs, source, attr.span, &mut prev_span_hi);
412412
}
413413
}
414+
let mut has_non_module_items = false;
414415
for stmt in &body.stmts {
415416
let mut is_extern_crate = false;
416417
match stmt.kind {
417418
StmtKind::Item(ref item) => {
418419
is_extern_crate = check_item(&item, &mut info, crate_name);
419420
}
420-
StmtKind::Expr(ref expr) if matches!(expr.kind, ast::ExprKind::Err(_)) => {
421-
reset_error_count(&psess);
422-
return Err(());
421+
StmtKind::Expr(ref expr) => {
422+
if matches!(expr.kind, ast::ExprKind::Err(_)) {
423+
reset_error_count(&psess);
424+
return Err(());
425+
}
426+
has_non_module_items = true;
423427
}
424428
StmtKind::MacCall(ref mac_call) if !info.has_main_fn => {
425429
let mut iter = mac_call.mac.args.tokens.iter();
@@ -441,7 +445,11 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result<ParseSourceIn
441445
}
442446
}
443447
}
444-
_ => {}
448+
// We do nothing in this case. Not marking it as `non_module_items` either.
449+
StmtKind::Empty => {}
450+
_ => {
451+
has_non_module_items = true;
452+
}
445453
}
446454

447455
// 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<ParseSourceIn
466474
push_to_s(&mut info.crates, source, span, &mut prev_span_hi);
467475
}
468476
}
477+
if has_non_module_items {
478+
// FIXME: if `info.has_main_fn` is `true`, emit a warning here to mention that
479+
// this code will not be called.
480+
info.has_main_fn = false;
481+
}
469482
Ok(info)
470483
}
471484
Err(e) => {

0 commit comments

Comments
 (0)