@@ -289,21 +289,28 @@ fn parse_source(
289
289
// Recurse through functions body. It is necessary because the doctest source code is
290
290
// wrapped in a function to limit the number of AST errors. If we don't recurse into
291
291
// functions, we would thing all top-level items (so basically nothing).
292
- fn check_item ( item : & ast:: Item , info : & mut ParseSourceInfo , crate_name : & Option < & str > ) {
292
+ fn check_item (
293
+ item : & ast:: Item ,
294
+ info : & mut ParseSourceInfo ,
295
+ crate_name : & Option < & str > ,
296
+ is_top_level : bool ,
297
+ ) {
293
298
if !info. has_global_allocator
294
299
&& item. attrs . iter ( ) . any ( |attr| attr. name_or_empty ( ) == sym:: global_allocator)
295
300
{
296
301
info. has_global_allocator = true ;
297
302
}
298
303
match item. kind {
299
304
ast:: ItemKind :: Fn ( ref fn_item) if !info. has_main_fn => {
300
- if item. ident . name == sym:: main {
305
+ if item. ident . name == sym:: main && is_top_level {
301
306
info. has_main_fn = true ;
302
307
}
303
308
if let Some ( ref body) = fn_item. body {
304
309
for stmt in & body. stmts {
305
310
match stmt. kind {
306
- ast:: StmtKind :: Item ( ref item) => check_item ( item, info, crate_name) ,
311
+ ast:: StmtKind :: Item ( ref item) => {
312
+ check_item ( item, info, crate_name, false )
313
+ }
307
314
ast:: StmtKind :: MacCall ( ..) => info. found_macro = true ,
308
315
_ => { }
309
316
}
@@ -329,7 +336,7 @@ fn parse_source(
329
336
loop {
330
337
match parser. parse_item ( ForceCollect :: No ) {
331
338
Ok ( Some ( item) ) => {
332
- check_item ( & item, info, crate_name) ;
339
+ check_item ( & item, info, crate_name, true ) ;
333
340
334
341
if info. has_main_fn && info. found_extern_crate {
335
342
break ;
0 commit comments