1
Fork 0

Deduplicate needed parentheses suggestion code

This commit is contained in:
Esteban Küber 2019-05-02 16:13:28 -07:00
parent e0cef5cf40
commit f6a4b5270a
3 changed files with 33 additions and 35 deletions

View file

@ -11,7 +11,7 @@ use crate::tokenstream::{TokenStream, TokenTree};
use crate::diagnostics::plugin::ErrorMap;
use crate::print::pprust::token_to_string;
use errors::{FatalError, Level, Handler, ColorConfig, Diagnostic, DiagnosticBuilder};
use errors::{Applicability, FatalError, Level, Handler, ColorConfig, Diagnostic, DiagnosticBuilder};
use rustc_data_structures::sync::{Lrc, Lock};
use syntax_pos::{Span, SourceFile, FileName, MultiSpan};
use log::debug;
@ -47,6 +47,9 @@ pub struct ParseSess {
included_mod_stack: Lock<Vec<PathBuf>>,
source_map: Lrc<SourceMap>,
pub buffered_lints: Lock<Vec<BufferedEarlyLint>>,
/// Contains the spans of block expressions that could have been incomplete based on the
/// operation token that followed it, but that the parser cannot identify without further
/// analysis.
pub abiguous_block_expr_parse: Lock<FxHashMap<Span, Span>>,
}
@ -95,6 +98,24 @@ impl ParseSess {
});
});
}
/// Extend an error with a suggestion to wrap an expression with parentheses to allow the
/// parser to continue parsing the following operation as part of the same expression.
pub fn expr_parentheses_needed(
&self,
err: &mut DiagnosticBuilder<'_>,
span: Span,
alt_snippet: Option<String>,
) {
if let Some(snippet) = self.source_map().span_to_snippet(span).ok().or(alt_snippet) {
err.span_suggestion(
span,
"parentheses are required to parse this as an expression",
format!("({})", snippet),
Applicability::MachineApplicable,
);
}
}
}
#[derive(Clone)]