1
Fork 0

Emit missing unclosed delimiter errors

This commit is contained in:
Esteban Küber 2019-03-03 12:14:25 -08:00
parent c70a516c23
commit 51d0e86c22
7 changed files with 56 additions and 55 deletions

View file

@ -439,8 +439,8 @@ 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, mut errors) = source_file_to_stream(&sess.parse_sess, source_file, None);
emit_unclosed_delims(&errors, &sess.diagnostic()); emit_unclosed_delims(&mut errors, &sess.diagnostic());
// 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

@ -7798,7 +7798,10 @@ impl<'a> Parser<'a> {
attributes_allowed: bool, attributes_allowed: bool,
) -> PResult<'a, Option<P<Item>>> { ) -> PResult<'a, Option<P<Item>>> {
let (ret, tokens) = self.collect_tokens(|this| { let (ret, tokens) = self.collect_tokens(|this| {
this.parse_item_implementation(attrs, macros_allowed, attributes_allowed) let item = this.parse_item_implementation(attrs, macros_allowed, attributes_allowed);
let diag = this.diagnostic();
emit_unclosed_delims(&mut this.unclosed_delims, diag);
item
})?; })?;
// Once we've parsed an item and recorded the tokens we got while // Once we've parsed an item and recorded the tokens we got while
@ -8555,8 +8558,8 @@ 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),
}); });
emit_unclosed_delims(&self.unclosed_delims, self.diagnostic()); let diag = self.diagnostic();
self.unclosed_delims.clear(); emit_unclosed_delims(&mut self.unclosed_delims, diag);
krate krate
} }
@ -8587,8 +8590,8 @@ impl<'a> Parser<'a> {
} }
} }
pub fn emit_unclosed_delims(unclosed_delims: &[UnmatchedBrace], handler: &errors::Handler) { pub fn emit_unclosed_delims(unclosed_delims: &mut Vec<UnmatchedBrace>, handler: &errors::Handler) {
for unmatched in unclosed_delims { for unmatched in unclosed_delims.iter() {
let mut err = handler.struct_span_err(unmatched.found_span, &format!( let mut err = handler.struct_span_err(unmatched.found_span, &format!(
"incorrect close delimiter: `{}`", "incorrect close delimiter: `{}`",
pprust::token_to_string(&token::Token::CloseDelim(unmatched.found_delim)), pprust::token_to_string(&token::Token::CloseDelim(unmatched.found_delim)),
@ -8602,4 +8605,5 @@ pub fn emit_unclosed_delims(unclosed_delims: &[UnmatchedBrace], handler: &errors
} }
err.emit(); err.emit();
} }
unclosed_delims.clear();
} }

View file

@ -675,9 +675,9 @@ impl Nonterminal {
// FIXME(#43081): Avoid this pretty-print + reparse hack // FIXME(#43081): Avoid this pretty-print + reparse hack
let source = pprust::nonterminal_to_string(self); let source = pprust::nonterminal_to_string(self);
let filename = FileName::macro_expansion_source_code(&source); let filename = FileName::macro_expansion_source_code(&source);
let (tokens_for_real, errors) = let (tokens_for_real, mut errors) =
parse_stream_from_source_str(filename, source, sess, Some(span)); parse_stream_from_source_str(filename, source, sess, Some(span));
emit_unclosed_delims(&errors, &sess.span_diagnostic); emit_unclosed_delims(&mut errors, &sess.span_diagnostic);
// During early phases of the compiler the AST could get modified // During early phases of the compiler the AST could get modified
// directly (e.g., attributes added or removed) and the internal cache // directly (e.g., attributes added or removed) and the internal cache
@ -740,13 +740,13 @@ fn prepend_attrs(sess: &ParseSess,
let source = pprust::attr_to_string(attr); let source = pprust::attr_to_string(attr);
let macro_filename = FileName::macro_expansion_source_code(&source); let macro_filename = FileName::macro_expansion_source_code(&source);
if attr.is_sugared_doc { if attr.is_sugared_doc {
let (stream, errors) = parse_stream_from_source_str( let (stream, mut errors) = parse_stream_from_source_str(
macro_filename, macro_filename,
source, source,
sess, sess,
Some(span), Some(span),
); );
emit_unclosed_delims(&errors, &sess.span_diagnostic); emit_unclosed_delims(&mut errors, &sess.span_diagnostic);
builder.push(stream); builder.push(stream);
continue continue
} }
@ -763,13 +763,13 @@ fn prepend_attrs(sess: &ParseSess,
// ... and for more complicated paths, fall back to a reparse hack that // ... and for more complicated paths, fall back to a reparse hack that
// should eventually be removed. // should eventually be removed.
} else { } else {
let (stream, errors) = parse_stream_from_source_str( let (stream, mut errors) = parse_stream_from_source_str(
macro_filename, macro_filename,
source, source,
sess, sess,
Some(span), Some(span),
); );
emit_unclosed_delims(&errors, &sess.span_diagnostic); emit_unclosed_delims(&mut errors, &sess.span_diagnostic);
brackets.push(stream); brackets.push(stream);
} }

View file

@ -410,13 +410,13 @@ 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 {
let (tokens, errors) = parse::parse_stream_from_source_str( let (tokens, mut 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),
); );
emit_unclosed_delims(&errors, &self.sess.span_diagnostic); emit_unclosed_delims(&mut errors, &self.sess.span_diagnostic);
tokens tokens
} }
fn to_string(&mut self, stream: &Self::TokenStream) -> String { fn to_string(&mut self, stream: &Self::TokenStream) -> String {

View file

@ -1,9 +1,3 @@
error: unexpected token: `;`
--> $DIR/parser-recovery-2.rs:12:15
|
LL | let x = y.; //~ ERROR unexpected token
| ^
error: incorrect close delimiter: `)` error: incorrect close delimiter: `)`
--> $DIR/parser-recovery-2.rs:8:5 --> $DIR/parser-recovery-2.rs:8:5
| |
@ -13,6 +7,12 @@ LL | let x = foo(); //~ ERROR cannot find function `foo` in this scope
LL | ) //~ ERROR incorrect close delimiter: `)` LL | ) //~ ERROR incorrect close delimiter: `)`
| ^ incorrect close delimiter | ^ incorrect close delimiter
error: unexpected token: `;`
--> $DIR/parser-recovery-2.rs:12:15
|
LL | let x = y.; //~ ERROR unexpected token
| ^
error[E0425]: cannot find function `foo` in this scope error[E0425]: cannot find function `foo` in this scope
--> $DIR/parser-recovery-2.rs:7:17 --> $DIR/parser-recovery-2.rs:7:17
| |

View file

@ -10,16 +10,14 @@ pub mod raw {
pub fn ensure_dir_exists<P: AsRef<Path>, F: FnOnce(&Path)>(path: P, pub fn ensure_dir_exists<P: AsRef<Path>, F: FnOnce(&Path)>(path: P,
callback: F) callback: F)
-> io::Result<bool> { -> io::Result<bool> {
if !is_directory(path.as_ref()) { //~ ERROR: cannot find function `is_directory` if !is_directory(path.as_ref()) {
callback(path.as_ref(); //~ ERROR expected one of //~^ ERROR cannot find function `is_directory`
fs::create_dir_all(path.as_ref()).map(|()| true) //~ ERROR: mismatched types callback(path.as_ref();
//~^ expected (), found enum `std::result::Result` //~^ ERROR expected one of
//~| expected type `()` //~| ERROR this function takes 1 parameter but 2 parameters were supplied
//~| found type `std::result::Result<bool, std::io::Error>` fs::create_dir_all(path.as_ref()).map(|()| true)
//~| expected one of
} else { } else {
//~^ ERROR: expected one of //~^ ERROR incorrect close delimiter: `}`
//~| unexpected token
Ok(false); Ok(false);
} }

View file

@ -1,39 +1,38 @@
error: expected one of `)`, `,`, `.`, `?`, or an operator, found `;` error: incorrect close delimiter: `}`
--> $DIR/token-error-correct-3.rs:14:35 --> $DIR/token-error-correct-3.rs:19:9
| |
LL | callback(path.as_ref(); //~ ERROR expected one of LL | if !is_directory(path.as_ref()) {
| - ^ | - close delimiter possibly meant for this
| | | LL | //~^ ERROR cannot find function `is_directory`
| | help: `)` may belong here LL | callback(path.as_ref();
| unclosed delimiter | - un-closed delimiter
error: expected one of `.`, `;`, `?`, `}`, or an operator, found `)`
--> $DIR/token-error-correct-3.rs:20:9
|
LL | fs::create_dir_all(path.as_ref()).map(|()| true) //~ ERROR: mismatched types
| - expected one of `.`, `;`, `?`, `}`, or an operator here
... ...
LL | } else { LL | } else {
| ^ unexpected token | ^ incorrect close delimiter
error: expected one of `)`, `,`, `.`, `?`, or an operator, found `;`
--> $DIR/token-error-correct-3.rs:15:35
|
LL | callback(path.as_ref();
| ^ expected one of `)`, `,`, `.`, `?`, or an operator here
error[E0425]: cannot find function `is_directory` in this scope error[E0425]: cannot find function `is_directory` in this scope
--> $DIR/token-error-correct-3.rs:13:13 --> $DIR/token-error-correct-3.rs:13:13
| |
LL | if !is_directory(path.as_ref()) { //~ ERROR: cannot find function `is_directory` LL | if !is_directory(path.as_ref()) {
| ^^^^^^^^^^^^ not found in this scope | ^^^^^^^^^^^^ not found in this scope
error[E0308]: mismatched types error[E0057]: this function takes 1 parameter but 2 parameters were supplied
--> $DIR/token-error-correct-3.rs:15:13 --> $DIR/token-error-correct-3.rs:15:13
| |
LL | fs::create_dir_all(path.as_ref()).map(|()| true) //~ ERROR: mismatched types LL | / callback(path.as_ref();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- help: try adding a semicolon: `;` LL | | //~^ ERROR expected one of
| | LL | | //~| ERROR this function takes 1 parameter but 2 parameters were supplied
| expected (), found enum `std::result::Result` LL | | fs::create_dir_all(path.as_ref()).map(|()| true)
| LL | | } else {
= note: expected type `()` | |_________^ expected 1 parameter
found type `std::result::Result<bool, std::io::Error>`
error: aborting due to 4 previous errors error: aborting due to 4 previous errors
Some errors occurred: E0308, E0425. Some errors occurred: E0057, E0425.
For more information about an error, try `rustc --explain E0308`. For more information about an error, try `rustc --explain E0057`.