Skip to content

Commit 99be87a

Browse files
committed
unify error handling to single method
1 parent 7451cd8 commit 99be87a

File tree

5 files changed

+32
-42
lines changed

5 files changed

+32
-42
lines changed

src/librustc_metadata/cstore_impl.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use syntax::attr;
2929
use syntax::source_map;
3030
use syntax::edition::Edition;
3131
use syntax::parse::source_file_to_stream;
32+
use syntax::parse::parser::emit_unclosed_delims;
3233
use syntax::symbol::Symbol;
3334
use syntax_pos::{Span, NO_EXPANSION, FileName};
3435
use rustc_data_structures::bit_set::BitSet;
@@ -437,12 +438,7 @@ impl cstore::CStore {
437438
let source_file = sess.parse_sess.source_map().new_source_file(source_name, def.body);
438439
let local_span = Span::new(source_file.start_pos, source_file.end_pos, NO_EXPANSION);
439440
let (body, errors) = source_file_to_stream(&sess.parse_sess, source_file, None);
440-
for err in errors {
441-
sess.struct_span_err(
442-
err.found_span,
443-
"unclosed delimiter cstore",
444-
).emit();
445-
}
441+
emit_unclosed_delims(&errors, &sess.diagnostic());
446442

447443
// Mark the attrs as used
448444
let attrs = data.get_item_attrs(id.index, sess);

src/libsyntax/parse/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ pub fn maybe_file_to_stream(
275275
Err(err) => {
276276
let mut buffer = Vec::with_capacity(1);
277277
err.buffer(&mut buffer);
278+
// Not using `emit_unclosed_delims` to use `db.buffer`
278279
for unmatched in srdr.unmatched_braces {
279280
let mut db = sess.span_diagnostic.struct_span_err(unmatched.found_span, &format!(
280281
"incorrect close delimiter: `{}`",

src/libsyntax/parse/parser.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,7 @@ impl<'a> Parser<'a> {
724724
if let Some(sp) = unmatched.unclosed_span {
725725
err.span_label(sp, "in order to close this...");
726726
}
727-
err.span_suggestion_short_with_applicability(
727+
err.span_suggestion_short(
728728
self.sess.source_map().next_point(self.prev_span),
729729
&format!("{} may belong here", delim.to_string()),
730730
delim.to_string(),
@@ -1180,7 +1180,7 @@ impl<'a> Parser<'a> {
11801180
// self.struct_span_err(
11811181
// self.span,
11821182
// &format!("expected `>`, found `{}`", self.this_token_to_string()),
1183-
// // ).span_suggestion_short_with_applicability(
1183+
// // ).span_suggestion_short(
11841184
// ).emit();
11851185
// Ok(())
11861186
// }
@@ -8503,20 +8503,7 @@ impl<'a> Parser<'a> {
85038503
module: self.parse_mod_items(&token::Eof, lo)?,
85048504
span: lo.to(self.span),
85058505
});
8506-
for unmatched in &self.unclosed_delims {
8507-
let mut err = self.struct_span_err(unmatched.found_span, &format!(
8508-
"incorrect close delimiter: `{}`",
8509-
pprust::token_to_string(&token::Token::CloseDelim(unmatched.found_delim)),
8510-
));
8511-
err.span_label(unmatched.found_span, "incorrect close delimiter");
8512-
if let Some(sp) = unmatched.candidate_span {
8513-
err.span_label(sp, "close delimiter possibly meant for this");
8514-
}
8515-
if let Some(sp) = unmatched.unclosed_span {
8516-
err.span_label(sp, "un-closed delimiter");
8517-
}
8518-
err.emit();
8519-
}
8506+
emit_unclosed_delims(&self.unclosed_delims, self.diagnostic());
85208507
self.unclosed_delims.clear();
85218508
krate
85228509
}
@@ -8547,3 +8534,20 @@ impl<'a> Parser<'a> {
85478534
}
85488535
}
85498536
}
8537+
8538+
pub fn emit_unclosed_delims(unclosed_delims: &[UnmatchedBrace], handler: &errors::Handler) {
8539+
for unmatched in unclosed_delims {
8540+
let mut err = handler.struct_span_err(unmatched.found_span, &format!(
8541+
"incorrect close delimiter: `{}`",
8542+
pprust::token_to_string(&token::Token::CloseDelim(unmatched.found_delim)),
8543+
));
8544+
err.span_label(unmatched.found_span, "incorrect close delimiter");
8545+
if let Some(sp) = unmatched.candidate_span {
8546+
err.span_label(sp, "close delimiter possibly meant for this");
8547+
}
8548+
if let Some(sp) = unmatched.unclosed_span {
8549+
err.span_label(sp, "un-closed delimiter");
8550+
}
8551+
err.emit();
8552+
}
8553+
}

src/libsyntax/parse/token.rs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::print::pprust;
1010
use crate::ptr::P;
1111
use crate::symbol::keywords;
1212
use crate::syntax::parse::parse_stream_from_source_str;
13+
use crate::syntax::parse::parser::emit_unclosed_delims;
1314
use crate::tokenstream::{self, DelimSpan, TokenStream, TokenTree};
1415

1516
use serialize::{Decodable, Decoder, Encodable, Encoder};
@@ -547,12 +548,7 @@ impl Token {
547548
let filename = FileName::macro_expansion_source_code(&source);
548549
let (tokens, errors) = parse_stream_from_source_str(
549550
filename, source, sess, Some(span));
550-
for err in errors {
551-
sess.span_diagnostic.struct_span_err(
552-
err.found_span,
553-
"unclosed delimiter for_real",
554-
).emit();
555-
}
551+
emit_unclosed_delims(&errors, &sess.span_diagnostic);
556552
tokens
557553
});
558554

@@ -800,12 +796,7 @@ fn prepend_attrs(sess: &ParseSess,
800796
sess,
801797
Some(span),
802798
);
803-
for err in errors {
804-
sess.span_diagnostic.struct_span_err(
805-
err.found_span,
806-
"unclosed delimiter attrs",
807-
).emit();
808-
}
799+
emit_unclosed_delims(&errors, &sess.span_diagnostic);
809800
builder.push(stream);
810801
continue
811802
}
@@ -828,12 +819,7 @@ fn prepend_attrs(sess: &ParseSess,
828819
sess,
829820
Some(span),
830821
);
831-
for err in errors {
832-
sess.span_diagnostic.struct_span_err(
833-
err.found_span,
834-
"unclosed delimiter attrs 2",
835-
).emit();
836-
}
822+
emit_unclosed_delims(&errors, &sess.span_diagnostic);
837823
brackets.push(stream);
838824
}
839825

src/libsyntax_ext/proc_macro_server.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use syntax::ast;
1111
use syntax::ext::base::ExtCtxt;
1212
use syntax::parse::lexer::comments;
1313
use syntax::parse::{self, token, ParseSess};
14+
use syntax::parse::parser::emit_unclosed_delims;
1415
use syntax::tokenstream::{self, DelimSpan, IsJoint::*, TokenStream, TreeAndJoint};
1516
use syntax_pos::hygiene::{SyntaxContext, Transparency};
1617
use syntax_pos::symbol::{keywords, Symbol};
@@ -408,12 +409,14 @@ impl server::TokenStream for Rustc<'_> {
408409
stream.is_empty()
409410
}
410411
fn from_str(&mut self, src: &str) -> Self::TokenStream {
411-
parse::parse_stream_from_source_str(
412+
let (tokens, errors) = parse::parse_stream_from_source_str(
412413
FileName::proc_macro_source_code(src.clone()),
413414
src.to_string(),
414415
self.sess,
415416
Some(self.call_site),
416-
).0
417+
);
418+
emit_unclosed_delims(&errors, &self.sess.span_diagnostic);
419+
tokens
417420
}
418421
fn to_string(&mut self, stream: &Self::TokenStream) -> String {
419422
stream.to_string()

0 commit comments

Comments
 (0)