1
Fork 0

unify error handling to single method

This commit is contained in:
Esteban Küber 2019-02-05 01:35:25 -08:00
parent 7451cd8dc0
commit 99be87aac3
5 changed files with 32 additions and 42 deletions

View file

@ -29,6 +29,7 @@ use syntax::attr;
use syntax::source_map; use syntax::source_map;
use syntax::edition::Edition; use syntax::edition::Edition;
use syntax::parse::source_file_to_stream; use syntax::parse::source_file_to_stream;
use syntax::parse::parser::emit_unclosed_delims;
use syntax::symbol::Symbol; use syntax::symbol::Symbol;
use syntax_pos::{Span, NO_EXPANSION, FileName}; use syntax_pos::{Span, NO_EXPANSION, FileName};
use rustc_data_structures::bit_set::BitSet; 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 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 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); let (body, errors) = source_file_to_stream(&sess.parse_sess, source_file, None);
for err in errors { emit_unclosed_delims(&errors, &sess.diagnostic());
sess.struct_span_err(
err.found_span,
"unclosed delimiter cstore",
).emit();
}
// Mark the attrs as used // Mark the attrs as used
let attrs = data.get_item_attrs(id.index, sess); let attrs = data.get_item_attrs(id.index, sess);

View file

@ -275,6 +275,7 @@ pub fn maybe_file_to_stream(
Err(err) => { Err(err) => {
let mut buffer = Vec::with_capacity(1); let mut buffer = Vec::with_capacity(1);
err.buffer(&mut buffer); err.buffer(&mut buffer);
// Not using `emit_unclosed_delims` to use `db.buffer`
for unmatched in srdr.unmatched_braces { for unmatched in srdr.unmatched_braces {
let mut db = sess.span_diagnostic.struct_span_err(unmatched.found_span, &format!( let mut db = sess.span_diagnostic.struct_span_err(unmatched.found_span, &format!(
"incorrect close delimiter: `{}`", "incorrect close delimiter: `{}`",

View file

@ -724,7 +724,7 @@ impl<'a> Parser<'a> {
if let Some(sp) = unmatched.unclosed_span { if let Some(sp) = unmatched.unclosed_span {
err.span_label(sp, "in order to close this..."); 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), self.sess.source_map().next_point(self.prev_span),
&format!("{} may belong here", delim.to_string()), &format!("{} may belong here", delim.to_string()),
delim.to_string(), delim.to_string(),
@ -1180,7 +1180,7 @@ impl<'a> Parser<'a> {
// self.struct_span_err( // self.struct_span_err(
// self.span, // self.span,
// &format!("expected `>`, found `{}`", self.this_token_to_string()), // &format!("expected `>`, found `{}`", self.this_token_to_string()),
// // ).span_suggestion_short_with_applicability( // // ).span_suggestion_short(
// ).emit(); // ).emit();
// Ok(()) // Ok(())
// } // }
@ -8503,20 +8503,7 @@ impl<'a> Parser<'a> {
module: self.parse_mod_items(&token::Eof, lo)?, module: self.parse_mod_items(&token::Eof, lo)?,
span: lo.to(self.span), span: lo.to(self.span),
}); });
for unmatched in &self.unclosed_delims { emit_unclosed_delims(&self.unclosed_delims, self.diagnostic());
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();
}
self.unclosed_delims.clear(); self.unclosed_delims.clear();
krate 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();
}
}

View file

@ -10,6 +10,7 @@ use crate::print::pprust;
use crate::ptr::P; use crate::ptr::P;
use crate::symbol::keywords; use crate::symbol::keywords;
use crate::syntax::parse::parse_stream_from_source_str; 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 crate::tokenstream::{self, DelimSpan, TokenStream, TokenTree};
use serialize::{Decodable, Decoder, Encodable, Encoder}; use serialize::{Decodable, Decoder, Encodable, Encoder};
@ -547,12 +548,7 @@ impl Token {
let filename = FileName::macro_expansion_source_code(&source); let filename = FileName::macro_expansion_source_code(&source);
let (tokens, errors) = parse_stream_from_source_str( let (tokens, errors) = parse_stream_from_source_str(
filename, source, sess, Some(span)); filename, source, sess, Some(span));
for err in errors { emit_unclosed_delims(&errors, &sess.span_diagnostic);
sess.span_diagnostic.struct_span_err(
err.found_span,
"unclosed delimiter for_real",
).emit();
}
tokens tokens
}); });
@ -800,12 +796,7 @@ fn prepend_attrs(sess: &ParseSess,
sess, sess,
Some(span), Some(span),
); );
for err in errors { emit_unclosed_delims(&errors, &sess.span_diagnostic);
sess.span_diagnostic.struct_span_err(
err.found_span,
"unclosed delimiter attrs",
).emit();
}
builder.push(stream); builder.push(stream);
continue continue
} }
@ -828,12 +819,7 @@ fn prepend_attrs(sess: &ParseSess,
sess, sess,
Some(span), Some(span),
); );
for err in errors { emit_unclosed_delims(&errors, &sess.span_diagnostic);
sess.span_diagnostic.struct_span_err(
err.found_span,
"unclosed delimiter attrs 2",
).emit();
}
brackets.push(stream); brackets.push(stream);
} }

View file

@ -11,6 +11,7 @@ use syntax::ast;
use syntax::ext::base::ExtCtxt; use syntax::ext::base::ExtCtxt;
use syntax::parse::lexer::comments; use syntax::parse::lexer::comments;
use syntax::parse::{self, token, ParseSess}; use syntax::parse::{self, token, ParseSess};
use syntax::parse::parser::emit_unclosed_delims;
use syntax::tokenstream::{self, DelimSpan, IsJoint::*, TokenStream, TreeAndJoint}; use syntax::tokenstream::{self, DelimSpan, IsJoint::*, TokenStream, TreeAndJoint};
use syntax_pos::hygiene::{SyntaxContext, Transparency}; use syntax_pos::hygiene::{SyntaxContext, Transparency};
use syntax_pos::symbol::{keywords, Symbol}; use syntax_pos::symbol::{keywords, Symbol};
@ -408,12 +409,14 @@ impl server::TokenStream for Rustc<'_> {
stream.is_empty() stream.is_empty()
} }
fn from_str(&mut self, src: &str) -> Self::TokenStream { 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()), FileName::proc_macro_source_code(src.clone()),
src.to_string(), src.to_string(),
self.sess, self.sess,
Some(self.call_site), Some(self.call_site),
).0 );
emit_unclosed_delims(&errors, &self.sess.span_diagnostic);
tokens
} }
fn to_string(&mut self, stream: &Self::TokenStream) -> String { fn to_string(&mut self, stream: &Self::TokenStream) -> String {
stream.to_string() stream.to_string()