Skip to content

Commit f4aa3b5

Browse files
committed
Preserve the whole LangSyntax when parsing doctests
Previously, only the raw string and the `is_ignore` field were preserved, which made it hard to recover anything else.
1 parent dda2a0e commit f4aa3b5

File tree

2 files changed

+10
-17
lines changed

2 files changed

+10
-17
lines changed

Diff for: src/librustdoc/html/markdown.rs

+8-15
Original file line numberDiff line numberDiff line change
@@ -1316,8 +1316,7 @@ crate struct RustCodeBlock {
13161316
/// The range in the markdown that the code within the code block occupies.
13171317
crate code: Range<usize>,
13181318
crate is_fenced: bool,
1319-
crate syntax: Option<String>,
1320-
crate is_ignore: bool,
1319+
crate lang_string: LangString,
13211320
}
13221321

13231322
/// Returns a range of bytes for each code block in the markdown that is tagged as `rust` or
@@ -1333,7 +1332,7 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_>) -> Vec<RustCodeB
13331332

13341333
while let Some((event, offset)) = p.next() {
13351334
if let Event::Start(Tag::CodeBlock(syntax)) = event {
1336-
let (syntax, code_start, code_end, range, is_fenced, is_ignore) = match syntax {
1335+
let (lang_string, code_start, code_end, range, is_fenced) = match syntax {
13371336
CodeBlockKind::Fenced(syntax) => {
13381337
let syntax = syntax.as_ref();
13391338
let lang_string = if syntax.is_empty() {
@@ -1344,8 +1343,6 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_>) -> Vec<RustCodeB
13441343
if !lang_string.rust {
13451344
continue;
13461345
}
1347-
let is_ignore = lang_string.ignore != Ignore::None;
1348-
let syntax = if syntax.is_empty() { None } else { Some(syntax.to_owned()) };
13491346
let (code_start, mut code_end) = match p.next() {
13501347
Some((Event::Text(_), offset)) => (offset.start, offset.end),
13511348
Some((_, sub_offset)) => {
@@ -1354,8 +1351,7 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_>) -> Vec<RustCodeB
13541351
is_fenced: true,
13551352
range: offset,
13561353
code,
1357-
syntax,
1358-
is_ignore,
1354+
lang_string,
13591355
});
13601356
continue;
13611357
}
@@ -1365,31 +1361,29 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_>) -> Vec<RustCodeB
13651361
is_fenced: true,
13661362
range: offset,
13671363
code,
1368-
syntax,
1369-
is_ignore,
1364+
lang_string,
13701365
});
13711366
continue;
13721367
}
13731368
};
13741369
while let Some((Event::Text(_), offset)) = p.next() {
13751370
code_end = offset.end;
13761371
}
1377-
(syntax, code_start, code_end, offset, true, is_ignore)
1372+
(lang_string, code_start, code_end, offset, true)
13781373
}
13791374
CodeBlockKind::Indented => {
13801375
// The ending of the offset goes too far sometime so we reduce it by one in
13811376
// these cases.
13821377
if offset.end > offset.start && md.get(offset.end..=offset.end) == Some(&"\n") {
13831378
(
1384-
None,
1379+
LangString::default(),
13851380
offset.start,
13861381
offset.end,
13871382
Range { start: offset.start, end: offset.end - 1 },
13881383
false,
1389-
false,
13901384
)
13911385
} else {
1392-
(None, offset.start, offset.end, offset, false, false)
1386+
(LangString::default(), offset.start, offset.end, offset, false)
13931387
}
13941388
}
13951389
};
@@ -1398,8 +1392,7 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_>) -> Vec<RustCodeB
13981392
is_fenced,
13991393
range,
14001394
code: Range { start: code_start, end: code_end },
1401-
syntax,
1402-
is_ignore,
1395+
lang_string,
14031396
});
14041397
}
14051398
}

Diff for: src/librustdoc/passes/check_code_block_syntax.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
6161
};
6262

6363
let hir_id = self.cx.tcx.hir().local_def_id_to_hir_id(local_id);
64-
let empty_block = code_block.syntax.is_none() && code_block.is_fenced;
65-
let is_ignore = code_block.is_ignore;
64+
let empty_block = code_block.lang_string == Default::default() && code_block.is_fenced;
65+
let is_ignore = code_block.lang_string.ignore != markdown::Ignore::None;
6666

6767
// The span and whether it is precise or not.
6868
let (sp, precise_span) = match super::source_span_for_markdown_range(

0 commit comments

Comments
 (0)