Skip to content

Commit 2383985

Browse files
committedDec 16, 2024·
Also handle cases where attributes are unclosed
1 parent 9c4a61f commit 2383985

File tree

3 files changed

+60
-18
lines changed

3 files changed

+60
-18
lines changed
 

Diff for: ‎src/librustdoc/doctest/make.rs

+40-16
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,17 @@ impl DocTestBuilder {
5151
!lang_str.compile_fail && !lang_str.test_harness && !lang_str.standalone_crate
5252
});
5353

54-
let SourceInfo { crate_attrs, maybe_crate_attrs, crates, everything_else } =
55-
partition_source(source, edition);
54+
let Some(SourceInfo { crate_attrs, maybe_crate_attrs, crates, everything_else }) =
55+
partition_source(source, edition)
56+
else {
57+
return Self::invalid(
58+
String::new(),
59+
String::new(),
60+
String::new(),
61+
source.to_string(),
62+
test_id,
63+
);
64+
};
5665

5766
// Uses librustc_ast to parse the doctest and find if there's a main fn and the extern
5867
// crate already is included.
@@ -77,18 +86,7 @@ impl DocTestBuilder {
7786
else {
7887
// If the parser panicked due to a fatal error, pass the test code through unchanged.
7988
// The error will be reported during compilation.
80-
return Self {
81-
supports_color: false,
82-
has_main_fn: false,
83-
crate_attrs,
84-
maybe_crate_attrs,
85-
crates,
86-
everything_else,
87-
already_has_extern_crate: false,
88-
test_id,
89-
failed_ast: true,
90-
can_be_merged: false,
91-
};
89+
return Self::invalid(crate_attrs, maybe_crate_attrs, crates, everything_else, test_id);
9290
};
9391
// If the AST returned an error, we don't want this doctest to be merged with the
9492
// others. Same if it contains `#[feature]` or `#[no_std]`.
@@ -113,6 +111,27 @@ impl DocTestBuilder {
113111
}
114112
}
115113

114+
fn invalid(
115+
crate_attrs: String,
116+
maybe_crate_attrs: String,
117+
crates: String,
118+
everything_else: String,
119+
test_id: Option<String>,
120+
) -> Self {
121+
Self {
122+
supports_color: false,
123+
has_main_fn: false,
124+
crate_attrs,
125+
maybe_crate_attrs,
126+
crates,
127+
everything_else,
128+
already_has_extern_crate: false,
129+
test_id,
130+
failed_ast: true,
131+
can_be_merged: false,
132+
}
133+
}
134+
116135
/// Transforms a test into code that can be compiled into a Rust binary, and returns the number of
117136
/// lines before the test code begins.
118137
pub(crate) fn generate_unique_doctest(
@@ -533,7 +552,7 @@ struct SourceInfo {
533552
everything_else: String,
534553
}
535554

536-
fn partition_source(s: &str, edition: Edition) -> SourceInfo {
555+
fn partition_source(s: &str, edition: Edition) -> Option<SourceInfo> {
537556
#[derive(Copy, Clone, PartialEq)]
538557
enum PartitionState {
539558
Attrs,
@@ -608,11 +627,16 @@ fn partition_source(s: &str, edition: Edition) -> SourceInfo {
608627
}
609628
}
610629

630+
if !mod_attr_pending.is_empty() {
631+
debug!("invalid doctest code: {s:?}");
632+
return None;
633+
}
634+
611635
source_info.everything_else = source_info.everything_else.trim().to_string();
612636

613637
debug!("crate_attrs:\n{}{}", source_info.crate_attrs, source_info.maybe_crate_attrs);
614638
debug!("crates:\n{}", source_info.crates);
615639
debug!("after:\n{}", source_info.everything_else);
616640

617-
source_info
641+
Some(source_info)
618642
}

Diff for: ‎tests/rustdoc-ui/doctest/comment-in-attr-134221.rs

+4
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,8 @@
2020
foo,
2121
)]
2222
```
23+
24+
```rust
25+
#![
26+
```
2327
*/

Diff for: ‎tests/rustdoc-ui/doctest/comment-in-attr-134221.stdout

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11

2-
running 2 tests
2+
running 3 tests
33
test $DIR/comment-in-attr-134221.rs - (line 11) ... FAILED
44
test $DIR/comment-in-attr-134221.rs - (line 17) ... FAILED
5+
test $DIR/comment-in-attr-134221.rs - (line 23) ... FAILED
56

67
failures:
78

@@ -26,11 +27,24 @@ LL | foo,
2627
error: aborting due to 1 previous error
2728

2829
For more information about this error, try `rustc --explain E0635`.
30+
Couldn't compile the test.
31+
---- $DIR/comment-in-attr-134221.rs - (line 23) stdout ----
32+
error: this file contains an unclosed delimiter
33+
--> $DIR/comment-in-attr-134221.rs:$LINE:$COL
34+
|
35+
LL | #![
36+
| -^
37+
| |
38+
| unclosed delimiter
39+
40+
error: aborting due to 1 previous error
41+
2942
Couldn't compile the test.
3043

3144
failures:
3245
$DIR/comment-in-attr-134221.rs - (line 11)
3346
$DIR/comment-in-attr-134221.rs - (line 17)
47+
$DIR/comment-in-attr-134221.rs - (line 23)
3448

35-
test result: FAILED. 0 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
49+
test result: FAILED. 0 passed; 3 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
3650

0 commit comments

Comments
 (0)
Please sign in to comment.