Rollup merge of #76166 - matklad:privatereader, r=petrochenkov
Make `StringReader` private r? @ghost closes #75619
This commit is contained in:
commit
90e4bfa952
2 changed files with 35 additions and 46 deletions
|
@ -27,7 +27,7 @@ pub struct UnmatchedBrace {
|
||||||
pub candidate_span: Option<Span>,
|
pub candidate_span: Option<Span>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct StringReader<'a> {
|
crate struct StringReader<'a> {
|
||||||
sess: &'a ParseSess,
|
sess: &'a ParseSess,
|
||||||
/// Initial position, read-only.
|
/// Initial position, read-only.
|
||||||
start_pos: BytePos,
|
start_pos: BytePos,
|
||||||
|
@ -41,7 +41,7 @@ pub struct StringReader<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> StringReader<'a> {
|
impl<'a> StringReader<'a> {
|
||||||
pub fn new(
|
crate fn new(
|
||||||
sess: &'a ParseSess,
|
sess: &'a ParseSess,
|
||||||
source_file: Lrc<rustc_span::SourceFile>,
|
source_file: Lrc<rustc_span::SourceFile>,
|
||||||
override_span: Option<Span>,
|
override_span: Option<Span>,
|
||||||
|
@ -66,7 +66,7 @@ impl<'a> StringReader<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the next token, including trivia like whitespace or comments.
|
/// Returns the next token, including trivia like whitespace or comments.
|
||||||
pub fn next_token(&mut self) -> Token {
|
fn next_token(&mut self) -> Token {
|
||||||
let start_src_index = self.src_index(self.pos);
|
let start_src_index = self.src_index(self.pos);
|
||||||
let text: &str = &self.src[start_src_index..self.end_src_index];
|
let text: &str = &self.src[start_src_index..self.end_src_index];
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
use rustc_ast::token;
|
|
||||||
use rustc_data_structures::sync::{Lock, Lrc};
|
use rustc_data_structures::sync::{Lock, Lrc};
|
||||||
use rustc_errors::{emitter::Emitter, Applicability, Diagnostic, Handler};
|
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_session::parse::ParseSess;
|
||||||
use rustc_span::source_map::{FilePathMapping, SourceMap};
|
use rustc_span::source_map::{FilePathMapping, SourceMap};
|
||||||
use rustc_span::{FileName, InnerSpan};
|
use rustc_span::{FileName, InnerSpan};
|
||||||
|
@ -28,49 +27,34 @@ struct SyntaxChecker<'a, 'tcx> {
|
||||||
|
|
||||||
impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
|
impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
|
||||||
fn check_rust_syntax(&self, item: &clean::Item, dox: &str, code_block: RustCodeBlock) {
|
fn check_rust_syntax(&self, item: &clean::Item, dox: &str, code_block: RustCodeBlock) {
|
||||||
let buffered_messages = Lrc::new(Lock::new(vec![]));
|
let buffer = Lrc::new(Lock::new(Buffer::default()));
|
||||||
|
let emitter = BufferEmitter { buffer: Lrc::clone(&buffer) };
|
||||||
let emitter = BufferEmitter { messages: Lrc::clone(&buffered_messages) };
|
|
||||||
|
|
||||||
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
|
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
|
||||||
let handler = Handler::with_emitter(false, None, Box::new(emitter));
|
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 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 is_empty = rustc_driver::catch_fatal_errors(|| {
|
||||||
let mut has_syntax_errors = false;
|
parse_stream_from_source_str(
|
||||||
let mut only_whitespace = true;
|
FileName::Custom(String::from("doctest")),
|
||||||
// even if there is a syntax error, we need to run the lexer over the whole file
|
source,
|
||||||
let mut lexer = Lexer::new(&sess, source_file, None);
|
&sess,
|
||||||
loop {
|
None,
|
||||||
match lexer.next_token().kind {
|
)
|
||||||
token::Eof => break,
|
.is_empty()
|
||||||
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
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
.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) =
|
let mut diag = if let Some(sp) =
|
||||||
super::source_span_for_markdown_range(self.cx, &dox, &code_block.range, &item.attrs)
|
super::source_span_for_markdown_range(self.cx, &dox, &code_block.range, &item.attrs)
|
||||||
{
|
{
|
||||||
let warning_message = match code_block_invalid {
|
let warning_message = if buffer.has_errors {
|
||||||
CodeBlockInvalid::SyntaxError => "could not parse code block as Rust code",
|
"could not parse code block as Rust code"
|
||||||
CodeBlockInvalid::Empty => "Rust code block is empty",
|
} else {
|
||||||
|
"Rust code block is empty"
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut diag = self.cx.sess().struct_span_warn(sp, warning_message);
|
let mut diag = self.cx.sess().struct_span_warn(sp, warning_message);
|
||||||
|
@ -102,7 +86,7 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
|
||||||
};
|
};
|
||||||
|
|
||||||
// FIXME(#67563): Provide more context for these errors by displaying the spans inline.
|
// 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);
|
diag.note(&message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -125,21 +109,26 @@ impl<'a, 'tcx> DocFolder for SyntaxChecker<'a, 'tcx> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
struct Buffer {
|
||||||
|
messages: Vec<String>,
|
||||||
|
has_errors: bool,
|
||||||
|
}
|
||||||
|
|
||||||
struct BufferEmitter {
|
struct BufferEmitter {
|
||||||
messages: Lrc<Lock<Vec<String>>>,
|
buffer: Lrc<Lock<Buffer>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Emitter for BufferEmitter {
|
impl Emitter for BufferEmitter {
|
||||||
fn emit_diagnostic(&mut self, diag: &Diagnostic) {
|
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>> {
|
fn source_map(&self) -> Option<&Lrc<SourceMap>> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
enum CodeBlockInvalid {
|
|
||||||
SyntaxError,
|
|
||||||
Empty,
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue