Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the correct edition for syntax highlighting doctests #89277

Merged
merged 2 commits into from
Sep 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 8 additions & 15 deletions src/librustdoc/html/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1316,8 +1316,7 @@ crate struct RustCodeBlock {
/// The range in the markdown that the code within the code block occupies.
crate code: Range<usize>,
crate is_fenced: bool,
crate syntax: Option<String>,
crate is_ignore: bool,
crate lang_string: LangString,
}

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

while let Some((event, offset)) = p.next() {
if let Event::Start(Tag::CodeBlock(syntax)) = event {
let (syntax, code_start, code_end, range, is_fenced, is_ignore) = match syntax {
let (lang_string, code_start, code_end, range, is_fenced) = match syntax {
CodeBlockKind::Fenced(syntax) => {
let syntax = syntax.as_ref();
let lang_string = if syntax.is_empty() {
Expand All @@ -1344,8 +1343,6 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_>) -> Vec<RustCodeB
if !lang_string.rust {
continue;
}
let is_ignore = lang_string.ignore != Ignore::None;
let syntax = if syntax.is_empty() { None } else { Some(syntax.to_owned()) };
let (code_start, mut code_end) = match p.next() {
Some((Event::Text(_), offset)) => (offset.start, offset.end),
Some((_, sub_offset)) => {
Expand All @@ -1354,8 +1351,7 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_>) -> Vec<RustCodeB
is_fenced: true,
range: offset,
code,
syntax,
is_ignore,
lang_string,
});
continue;
}
Expand All @@ -1365,31 +1361,29 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_>) -> Vec<RustCodeB
is_fenced: true,
range: offset,
code,
syntax,
is_ignore,
lang_string,
});
continue;
}
};
while let Some((Event::Text(_), offset)) = p.next() {
code_end = offset.end;
}
(syntax, code_start, code_end, offset, true, is_ignore)
(lang_string, code_start, code_end, offset, true)
}
CodeBlockKind::Indented => {
// The ending of the offset goes too far sometime so we reduce it by one in
// these cases.
if offset.end > offset.start && md.get(offset.end..=offset.end) == Some(&"\n") {
(
None,
LangString::default(),
offset.start,
offset.end,
Range { start: offset.start, end: offset.end - 1 },
false,
false,
)
} else {
(None, offset.start, offset.end, offset, false, false)
(LangString::default(), offset.start, offset.end, offset, false)
}
}
};
Expand All @@ -1398,8 +1392,7 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_>) -> Vec<RustCodeB
is_fenced,
range,
code: Range { start: code_start, end: code_end },
syntax,
is_ignore,
lang_string,
});
}
}
Expand Down
18 changes: 14 additions & 4 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 All @@ -61,8 +71,8 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
};

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

// The span and whether it is precise or not.
let (sp, precise_span) = match super::source_span_for_markdown_range(
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