Skip to content

Commit 3536324

Browse files
Fix detection of main function if there are expressions around it
1 parent 553600e commit 3536324

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/librustdoc/doctest/make.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -407,15 +407,19 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result<ParseSourceIn
407407
push_to_s(&mut info.crate_attrs, source, attr.span, &mut prev_span_hi);
408408
}
409409
}
410+
let mut has_non_module_items = false;
410411
for stmt in &body.stmts {
411412
let mut is_extern_crate = false;
412413
match stmt.kind {
413414
StmtKind::Item(ref item) => {
414415
is_extern_crate = check_item(&item, &mut info, crate_name);
415416
}
416-
StmtKind::Expr(ref expr) if matches!(expr.kind, ast::ExprKind::Err(_)) => {
417-
reset_error_count(&psess);
418-
return Err(());
417+
StmtKind::Expr(ref expr) => {
418+
if matches!(expr.kind, ast::ExprKind::Err(_)) {
419+
reset_error_count(&psess);
420+
return Err(());
421+
}
422+
has_non_module_items = true;
419423
}
420424
StmtKind::MacCall(ref mac_call) if !info.has_main_fn => {
421425
let mut iter = mac_call.mac.args.tokens.iter();
@@ -437,7 +441,11 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result<ParseSourceIn
437441
}
438442
}
439443
}
440-
_ => {}
444+
// We do nothing in this case. Not marking it as `non_module_items` either.
445+
StmtKind::Empty => {}
446+
_ => {
447+
has_non_module_items = true;
448+
}
441449
}
442450

443451
// 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
462470
push_to_s(&mut info.crates, source, span, &mut prev_span_hi);
463471
}
464472
}
473+
if has_non_module_items {
474+
// FIXME: if `info.has_main_fn` is `true`, emit a warning here to mention that
475+
// this code will not be called.
476+
info.has_main_fn = false;
477+
}
465478
Ok(info)
466479
}
467480
Err(e) => {

0 commit comments

Comments
 (0)