unify error handling to single method
This commit is contained in:
parent
7451cd8dc0
commit
99be87aac3
5 changed files with 32 additions and 42 deletions
|
@ -29,6 +29,7 @@ use syntax::attr;
|
|||
use syntax::source_map;
|
||||
use syntax::edition::Edition;
|
||||
use syntax::parse::source_file_to_stream;
|
||||
use syntax::parse::parser::emit_unclosed_delims;
|
||||
use syntax::symbol::Symbol;
|
||||
use syntax_pos::{Span, NO_EXPANSION, FileName};
|
||||
use rustc_data_structures::bit_set::BitSet;
|
||||
|
@ -437,12 +438,7 @@ impl cstore::CStore {
|
|||
let source_file = sess.parse_sess.source_map().new_source_file(source_name, def.body);
|
||||
let local_span = Span::new(source_file.start_pos, source_file.end_pos, NO_EXPANSION);
|
||||
let (body, errors) = source_file_to_stream(&sess.parse_sess, source_file, None);
|
||||
for err in errors {
|
||||
sess.struct_span_err(
|
||||
err.found_span,
|
||||
"unclosed delimiter cstore",
|
||||
).emit();
|
||||
}
|
||||
emit_unclosed_delims(&errors, &sess.diagnostic());
|
||||
|
||||
// Mark the attrs as used
|
||||
let attrs = data.get_item_attrs(id.index, sess);
|
||||
|
|
|
@ -275,6 +275,7 @@ pub fn maybe_file_to_stream(
|
|||
Err(err) => {
|
||||
let mut buffer = Vec::with_capacity(1);
|
||||
err.buffer(&mut buffer);
|
||||
// Not using `emit_unclosed_delims` to use `db.buffer`
|
||||
for unmatched in srdr.unmatched_braces {
|
||||
let mut db = sess.span_diagnostic.struct_span_err(unmatched.found_span, &format!(
|
||||
"incorrect close delimiter: `{}`",
|
||||
|
|
|
@ -724,7 +724,7 @@ impl<'a> Parser<'a> {
|
|||
if let Some(sp) = unmatched.unclosed_span {
|
||||
err.span_label(sp, "in order to close this...");
|
||||
}
|
||||
err.span_suggestion_short_with_applicability(
|
||||
err.span_suggestion_short(
|
||||
self.sess.source_map().next_point(self.prev_span),
|
||||
&format!("{} may belong here", delim.to_string()),
|
||||
delim.to_string(),
|
||||
|
@ -1180,7 +1180,7 @@ impl<'a> Parser<'a> {
|
|||
// self.struct_span_err(
|
||||
// self.span,
|
||||
// &format!("expected `>`, found `{}`", self.this_token_to_string()),
|
||||
// // ).span_suggestion_short_with_applicability(
|
||||
// // ).span_suggestion_short(
|
||||
// ).emit();
|
||||
// Ok(())
|
||||
// }
|
||||
|
@ -8503,20 +8503,7 @@ impl<'a> Parser<'a> {
|
|||
module: self.parse_mod_items(&token::Eof, lo)?,
|
||||
span: lo.to(self.span),
|
||||
});
|
||||
for unmatched in &self.unclosed_delims {
|
||||
let mut err = self.struct_span_err(unmatched.found_span, &format!(
|
||||
"incorrect close delimiter: `{}`",
|
||||
pprust::token_to_string(&token::Token::CloseDelim(unmatched.found_delim)),
|
||||
));
|
||||
err.span_label(unmatched.found_span, "incorrect close delimiter");
|
||||
if let Some(sp) = unmatched.candidate_span {
|
||||
err.span_label(sp, "close delimiter possibly meant for this");
|
||||
}
|
||||
if let Some(sp) = unmatched.unclosed_span {
|
||||
err.span_label(sp, "un-closed delimiter");
|
||||
}
|
||||
err.emit();
|
||||
}
|
||||
emit_unclosed_delims(&self.unclosed_delims, self.diagnostic());
|
||||
self.unclosed_delims.clear();
|
||||
krate
|
||||
}
|
||||
|
@ -8547,3 +8534,20 @@ impl<'a> Parser<'a> {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn emit_unclosed_delims(unclosed_delims: &[UnmatchedBrace], handler: &errors::Handler) {
|
||||
for unmatched in unclosed_delims {
|
||||
let mut err = handler.struct_span_err(unmatched.found_span, &format!(
|
||||
"incorrect close delimiter: `{}`",
|
||||
pprust::token_to_string(&token::Token::CloseDelim(unmatched.found_delim)),
|
||||
));
|
||||
err.span_label(unmatched.found_span, "incorrect close delimiter");
|
||||
if let Some(sp) = unmatched.candidate_span {
|
||||
err.span_label(sp, "close delimiter possibly meant for this");
|
||||
}
|
||||
if let Some(sp) = unmatched.unclosed_span {
|
||||
err.span_label(sp, "un-closed delimiter");
|
||||
}
|
||||
err.emit();
|
||||
}
|
||||
}
|
|
@ -10,6 +10,7 @@ use crate::print::pprust;
|
|||
use crate::ptr::P;
|
||||
use crate::symbol::keywords;
|
||||
use crate::syntax::parse::parse_stream_from_source_str;
|
||||
use crate::syntax::parse::parser::emit_unclosed_delims;
|
||||
use crate::tokenstream::{self, DelimSpan, TokenStream, TokenTree};
|
||||
|
||||
use serialize::{Decodable, Decoder, Encodable, Encoder};
|
||||
|
@ -547,12 +548,7 @@ impl Token {
|
|||
let filename = FileName::macro_expansion_source_code(&source);
|
||||
let (tokens, errors) = parse_stream_from_source_str(
|
||||
filename, source, sess, Some(span));
|
||||
for err in errors {
|
||||
sess.span_diagnostic.struct_span_err(
|
||||
err.found_span,
|
||||
"unclosed delimiter for_real",
|
||||
).emit();
|
||||
}
|
||||
emit_unclosed_delims(&errors, &sess.span_diagnostic);
|
||||
tokens
|
||||
});
|
||||
|
||||
|
@ -800,12 +796,7 @@ fn prepend_attrs(sess: &ParseSess,
|
|||
sess,
|
||||
Some(span),
|
||||
);
|
||||
for err in errors {
|
||||
sess.span_diagnostic.struct_span_err(
|
||||
err.found_span,
|
||||
"unclosed delimiter attrs",
|
||||
).emit();
|
||||
}
|
||||
emit_unclosed_delims(&errors, &sess.span_diagnostic);
|
||||
builder.push(stream);
|
||||
continue
|
||||
}
|
||||
|
@ -828,12 +819,7 @@ fn prepend_attrs(sess: &ParseSess,
|
|||
sess,
|
||||
Some(span),
|
||||
);
|
||||
for err in errors {
|
||||
sess.span_diagnostic.struct_span_err(
|
||||
err.found_span,
|
||||
"unclosed delimiter attrs 2",
|
||||
).emit();
|
||||
}
|
||||
emit_unclosed_delims(&errors, &sess.span_diagnostic);
|
||||
brackets.push(stream);
|
||||
}
|
||||
|
||||
|
|
|
@ -11,6 +11,7 @@ use syntax::ast;
|
|||
use syntax::ext::base::ExtCtxt;
|
||||
use syntax::parse::lexer::comments;
|
||||
use syntax::parse::{self, token, ParseSess};
|
||||
use syntax::parse::parser::emit_unclosed_delims;
|
||||
use syntax::tokenstream::{self, DelimSpan, IsJoint::*, TokenStream, TreeAndJoint};
|
||||
use syntax_pos::hygiene::{SyntaxContext, Transparency};
|
||||
use syntax_pos::symbol::{keywords, Symbol};
|
||||
|
@ -408,12 +409,14 @@ impl server::TokenStream for Rustc<'_> {
|
|||
stream.is_empty()
|
||||
}
|
||||
fn from_str(&mut self, src: &str) -> Self::TokenStream {
|
||||
parse::parse_stream_from_source_str(
|
||||
let (tokens, errors) = parse::parse_stream_from_source_str(
|
||||
FileName::proc_macro_source_code(src.clone()),
|
||||
src.to_string(),
|
||||
self.sess,
|
||||
Some(self.call_site),
|
||||
).0
|
||||
);
|
||||
emit_unclosed_delims(&errors, &self.sess.span_diagnostic);
|
||||
tokens
|
||||
}
|
||||
fn to_string(&mut self, stream: &Self::TokenStream) -> String {
|
||||
stream.to_string()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue