Skip to content

Commit 6f087ae

Browse files
committed
Use the correct edition when syntax highlighting doctests
Previously it would unconditionally use edition 2015, which was incorrect.
1 parent f4aa3b5 commit 6f087ae

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

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

+12-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_middle::lint::LintDiagnosticBuilder;
44
use rustc_parse::parse_stream_from_source_str;
55
use rustc_session::parse::ParseSess;
66
use rustc_span::source_map::{FilePathMapping, SourceMap};
7-
use rustc_span::{FileName, InnerSpan};
7+
use rustc_span::{hygiene::AstPass, ExpnData, ExpnKind, FileName, InnerSpan, DUMMY_SP};
88

99
use crate::clean;
1010
use crate::core::DocContext;
@@ -36,12 +36,22 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
3636
let source = dox[code_block.code].to_owned();
3737
let sess = ParseSess::with_span_handler(handler, sm);
3838

39+
let edition = code_block.lang_string.edition.unwrap_or(self.cx.tcx.sess.edition());
40+
let expn_data = ExpnData::default(
41+
ExpnKind::AstPass(AstPass::TestHarness),
42+
DUMMY_SP,
43+
edition,
44+
None,
45+
None,
46+
);
47+
let span = DUMMY_SP.fresh_expansion(expn_data, self.cx.tcx.create_stable_hashing_context());
48+
3949
let is_empty = rustc_driver::catch_fatal_errors(|| {
4050
parse_stream_from_source_str(
4151
FileName::Custom(String::from("doctest")),
4252
source,
4353
&sess,
44-
None,
54+
Some(span),
4555
)
4656
.is_empty()
4757
})

Diff for: src/test/rustdoc-ui/doctest-edition.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// edition:2021
2+
3+
#![deny(rustdoc::invalid_rust_codeblocks)]
4+
//~^ NOTE lint level is defined here
5+
6+
// By default, rustdoc should use the edition of the crate.
7+
//! ```
8+
//! foo'b'
9+
//! ```
10+
//~^^^ ERROR could not parse
11+
//~| NOTE prefix `foo` is unknown
12+
13+
// Rustdoc should respect `edition2018` when highlighting syntax.
14+
//! ```edition2018
15+
//! foo'b'
16+
//! ```

Diff for: src/test/rustdoc-ui/doctest-edition.stderr

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error: could not parse code block as Rust code
2+
--> $DIR/doctest-edition.rs:7:5
3+
|
4+
LL | //! ```
5+
| _____^
6+
LL | | //! foo'b'
7+
LL | | //! ```
8+
| |_______^
9+
|
10+
note: the lint level is defined here
11+
--> $DIR/doctest-edition.rs:3:9
12+
|
13+
LL | #![deny(rustdoc::invalid_rust_codeblocks)]
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15+
= note: error from rustc: prefix `foo` is unknown
16+
help: mark blocks that do not contain Rust code as text
17+
|
18+
LL | //! ```text
19+
| ++++
20+
21+
error: aborting due to previous error
22+

0 commit comments

Comments
 (0)