Skip to content

Rollup of 12 pull requests #76186

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

Merged
merged 29 commits into from
Sep 1, 2020
Merged
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
00cf550
Env use shorter intra-doc links in path
pickfire Aug 26, 2020
1d017eb
Fix env doc vars_os broken link
pickfire Aug 30, 2020
af24bdb
Make `cow_is_borrowed` methods const
Aug 31, 2020
c9a48d1
Fix strings indent
GuillaumeGomez Aug 31, 2020
85a400d
Remove notrust in rustc_middle
pickfire Aug 31, 2020
029ff37
Adjust Linux supported kernels and glibc
tesuji Aug 31, 2020
b741cc5
Fix string
GuillaumeGomez Aug 31, 2020
1baf3ff
Avoid StringReader when checking code blocks for syntax errors
matklad Aug 29, 2020
30ce15f
Make StringReader private
matklad Aug 31, 2020
4404cc5
Revert #75463
ecstatic-morse Aug 31, 2020
f3ae96e
Clean up E0769 explanation
GuillaumeGomez Aug 31, 2020
153f966
Clean up E0764 explanation
GuillaumeGomez Aug 31, 2020
5716c3e
Update expect-test to 1.0
matklad Aug 31, 2020
d591829
Add a test for const
Aug 31, 2020
116ad98
Remove tick for macOS x86
tesuji Sep 1, 2020
ddb054a
Fix `-Z instrument-coverage` on MSVC
richkadel Aug 27, 2020
7225f66
Adds two source span utility functions used in source-based coverage
richkadel Aug 27, 2020
b675824
Rollup merge of #75945 - pickfire:patch-7, r=jyn514
tmandry Sep 1, 2020
6d834a4
Rollup merge of #76002 - richkadel:llvm-coverage-map-gen-6b.3, r=tmandry
tmandry Sep 1, 2020
934127c
Rollup merge of #76003 - richkadel:llvm-coverage-map-gen-6b.4, r=wesl…
tmandry Sep 1, 2020
5033203
Rollup merge of #76059 - GuillaumeGomez:cleanup-e0764, r=Dylan-DPC,pi…
tmandry Sep 1, 2020
4f27620
Rollup merge of #76103 - GuillaumeGomez:cleanup-e0769, r=Dylan-DPC
tmandry Sep 1, 2020
c307e90
Rollup merge of #76139 - CDirkx:cow-is-borrowed, r=ecstatic-morse
tmandry Sep 1, 2020
5ac47b1
Rollup merge of #76154 - GuillaumeGomez:strings-indent, r=jyn514
tmandry Sep 1, 2020
9df193b
Rollup merge of #76161 - pickfire:patch-3, r=pickfire
tmandry Sep 1, 2020
e7b4cde
Rollup merge of #76163 - lzutao:readme-glibc, r=cuviper
tmandry Sep 1, 2020
90e4bfa
Rollup merge of #76166 - matklad:privatereader, r=petrochenkov
tmandry Sep 1, 2020
9d435d2
Rollup merge of #76172 - ecstatic-morse:revert-75463, r=RalfJung
tmandry Sep 1, 2020
8d328d7
Rollup merge of #76178 - matklad:et, r=Mark-Simulacrum
tmandry Sep 1, 2020
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
Prev Previous commit
Next Next commit
Avoid StringReader when checking code blocks for syntax errors
`parse_stream_from_source_str` is a more stable API to convert a
string into a bunch of tokens, and it also catches errors about
mismatched parenthesis.
  • Loading branch information
matklad committed Aug 31, 2020
commit 1baf3ff7e98ea14e84159e213bd9872efd53e07f
75 changes: 32 additions & 43 deletions src/librustdoc/passes/check_code_block_syntax.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use rustc_ast::token;
use rustc_data_structures::sync::{Lock, Lrc};
use rustc_errors::{emitter::Emitter, Applicability, Diagnostic, Handler};
use rustc_parse::lexer::StringReader as Lexer;
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};
Expand All @@ -28,49 +27,34 @@ struct SyntaxChecker<'a, 'tcx> {

impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
fn check_rust_syntax(&self, item: &clean::Item, dox: &str, code_block: RustCodeBlock) {
let buffered_messages = Lrc::new(Lock::new(vec![]));

let emitter = BufferEmitter { messages: Lrc::clone(&buffered_messages) };
let buffer = Lrc::new(Lock::new(Buffer::default()));
let emitter = BufferEmitter { buffer: Lrc::clone(&buffer) };

let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
let handler = Handler::with_emitter(false, None, Box::new(emitter));
let source = dox[code_block.code].to_owned();
let sess = ParseSess::with_span_handler(handler, sm);
let source_file = sess.source_map().new_source_file(
FileName::Custom(String::from("doctest")),
dox[code_block.code].to_owned(),
);

let validation_status = rustc_driver::catch_fatal_errors(|| {
let mut has_syntax_errors = false;
let mut only_whitespace = true;
// even if there is a syntax error, we need to run the lexer over the whole file
let mut lexer = Lexer::new(&sess, source_file, None);
loop {
match lexer.next_token().kind {
token::Eof => break,
token::Whitespace => (),
token::Unknown(..) => has_syntax_errors = true,
_ => only_whitespace = false,
}
}

if has_syntax_errors {
Some(CodeBlockInvalid::SyntaxError)
} else if only_whitespace {
Some(CodeBlockInvalid::Empty)
} else {
None
}
let is_empty = rustc_driver::catch_fatal_errors(|| {
parse_stream_from_source_str(
FileName::Custom(String::from("doctest")),
source,
&sess,
None,
)
.is_empty()
})
.unwrap_or(Some(CodeBlockInvalid::SyntaxError));
.unwrap_or(false);
let buffer = buffer.borrow();

if let Some(code_block_invalid) = validation_status {
if buffer.has_errors || is_empty {
let mut diag = if let Some(sp) =
super::source_span_for_markdown_range(self.cx, &dox, &code_block.range, &item.attrs)
{
let warning_message = match code_block_invalid {
CodeBlockInvalid::SyntaxError => "could not parse code block as Rust code",
CodeBlockInvalid::Empty => "Rust code block is empty",
let warning_message = if buffer.has_errors {
"could not parse code block as Rust code"
} else {
"Rust code block is empty"
};

let mut diag = self.cx.sess().struct_span_warn(sp, warning_message);
Expand Down Expand Up @@ -102,7 +86,7 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
};

// FIXME(#67563): Provide more context for these errors by displaying the spans inline.
for message in buffered_messages.borrow().iter() {
for message in buffer.messages.iter() {
diag.note(&message);
}

Expand All @@ -125,21 +109,26 @@ impl<'a, 'tcx> DocFolder for SyntaxChecker<'a, 'tcx> {
}
}

#[derive(Default)]
struct Buffer {
messages: Vec<String>,
has_errors: bool,
}

struct BufferEmitter {
messages: Lrc<Lock<Vec<String>>>,
buffer: Lrc<Lock<Buffer>>,
}

impl Emitter for BufferEmitter {
fn emit_diagnostic(&mut self, diag: &Diagnostic) {
self.messages.borrow_mut().push(format!("error from rustc: {}", diag.message[0].0));
let mut buffer = self.buffer.borrow_mut();
buffer.messages.push(format!("error from rustc: {}", diag.message[0].0));
if diag.is_error() {
buffer.has_errors = true;
}
}

fn source_map(&self) -> Option<&Lrc<SourceMap>> {
None
}
}

enum CodeBlockInvalid {
SyntaxError,
Empty,
}