Skip to content

Commit 14f80f9

Browse files
Correctly handle macros using $crate in merged doctests
1 parent 112e447 commit 14f80f9

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/librustdoc/doctest/make.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ impl DocTest {
5858
found_extern_crate,
5959
supports_color,
6060
has_global_allocator,
61+
has_macro_def,
6162
..
6263
},
6364
failed_ast,
@@ -85,6 +86,16 @@ impl DocTest {
8586
can_be_merged: false,
8687
};
8788
};
89+
// If the AST returned an error, we don't want this doctest to be merged with the
90+
// others. Same if it contains `#[feature]` or `#[no_std]`.
91+
let can_be_merged = can_merge_doctests
92+
&& !failed_ast
93+
&& !has_no_std
94+
&& !has_features
95+
&& !has_global_allocator
96+
// If this is a merged doctest and a defined macro uses `$crate`, then the path will
97+
// not work, so better not put it into merged doctests.
98+
&& !(has_macro_def && everything_else.contains("$crate"));
8899
Self {
89100
supports_color,
90101
has_main_fn,
@@ -95,9 +106,7 @@ impl DocTest {
95106
already_has_extern_crate: found_extern_crate,
96107
test_id,
97108
failed_ast: false,
98-
// If the AST returned an error, we don't want this doctest to be merged with the
99-
// others. Same if it contains `#[feature]` or `#[no_std]`.
100-
can_be_merged: !failed_ast && !has_no_std && !has_features && !has_global_allocator,
109+
can_be_merged,
101110
}
102111
}
103112

@@ -309,6 +318,7 @@ fn parse_source(
309318
}
310319
}
311320
ast::ItemKind::MacCall(..) => info.found_macro = true,
321+
ast::ItemKind::MacroDef(..) => info.has_macro_def = true,
312322
_ => {}
313323
}
314324
}
@@ -346,6 +356,7 @@ struct ParseSourceInfo {
346356
found_macro: bool,
347357
supports_color: bool,
348358
has_global_allocator: bool,
359+
has_macro_def: bool,
349360
}
350361

351362
fn check_for_main_and_extern_crate(

0 commit comments

Comments
 (0)