Skip to content

Commit

Permalink
unify error handling to single method
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Feb 7, 2019
1 parent 7451cd8 commit 99be87a
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 42 deletions.
8 changes: 2 additions & 6 deletions src/librustc_metadata/cstore_impl.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/libsyntax/parse/mod.rs
Expand Up @@ -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: `{}`",
Expand Down
36 changes: 20 additions & 16 deletions src/libsyntax/parse/parser.rs
Expand Up @@ -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(),
Expand Down Expand Up @@ -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(())
// }
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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();
}
}
22 changes: 4 additions & 18 deletions src/libsyntax/parse/token.rs
Expand Up @@ -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};
Expand Down Expand Up @@ -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
});

Expand Down Expand Up @@ -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
}
Expand All @@ -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);
}

Expand Down
7 changes: 5 additions & 2 deletions src/libsyntax_ext/proc_macro_server.rs
Expand Up @@ -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};
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 99be87a

Please sign in to comment.