Skip to content

Commit

Permalink
Use the correct edition when syntax highlighting doctests
Browse files Browse the repository at this point in the history
Previously it would unconditionally use edition 2015, which was
incorrect.
  • Loading branch information
jyn514 committed Sep 26, 2021
1 parent f4aa3b5 commit 6f087ae
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/librustdoc/passes/check_code_block_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rustc_middle::lint::LintDiagnosticBuilder;
use rustc_parse::parse_stream_from_source_str;
use rustc_session::parse::ParseSess;
use rustc_span::source_map::{FilePathMapping, SourceMap};
use rustc_span::{FileName, InnerSpan};
use rustc_span::{hygiene::AstPass, ExpnData, ExpnKind, FileName, InnerSpan, DUMMY_SP};

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

let edition = code_block.lang_string.edition.unwrap_or(self.cx.tcx.sess.edition());
let expn_data = ExpnData::default(
ExpnKind::AstPass(AstPass::TestHarness),
DUMMY_SP,
edition,
None,
None,
);
let span = DUMMY_SP.fresh_expansion(expn_data, self.cx.tcx.create_stable_hashing_context());

let is_empty = rustc_driver::catch_fatal_errors(|| {
parse_stream_from_source_str(
FileName::Custom(String::from("doctest")),
source,
&sess,
None,
Some(span),
)
.is_empty()
})
Expand Down
16 changes: 16 additions & 0 deletions src/test/rustdoc-ui/doctest-edition.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// edition:2021

#![deny(rustdoc::invalid_rust_codeblocks)]
//~^ NOTE lint level is defined here

// By default, rustdoc should use the edition of the crate.
//! ```
//! foo'b'
//! ```
//~^^^ ERROR could not parse
//~| NOTE prefix `foo` is unknown

// Rustdoc should respect `edition2018` when highlighting syntax.
//! ```edition2018
//! foo'b'
//! ```
22 changes: 22 additions & 0 deletions src/test/rustdoc-ui/doctest-edition.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
error: could not parse code block as Rust code
--> $DIR/doctest-edition.rs:7:5
|
LL | //! ```
| _____^
LL | | //! foo'b'
LL | | //! ```
| |_______^
|
note: the lint level is defined here
--> $DIR/doctest-edition.rs:3:9
|
LL | #![deny(rustdoc::invalid_rust_codeblocks)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: error from rustc: prefix `foo` is unknown
help: mark blocks that do not contain Rust code as text
|
LL | //! ```text
| ++++

error: aborting due to previous error

0 comments on commit 6f087ae

Please sign in to comment.