Skip to content

Commit f6a4b52

Browse files
committed
Deduplicate needed parentheses suggestion code
1 parent e0cef5c commit f6a4b52

File tree

3 files changed

+33
-35
lines changed

3 files changed

+33
-35
lines changed

src/librustc_typeck/check/mod.rs

+5-11
Original file line numberDiff line numberDiff line change
@@ -4177,17 +4177,11 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
41774177
if let Some(sp) = tcx.sess.parse_sess.abiguous_block_expr_parse
41784178
.borrow().get(&sp)
41794179
{
4180-
if let Ok(snippet) = tcx.sess.source_map()
4181-
.span_to_snippet(*sp)
4182-
{
4183-
err.span_suggestion(
4184-
*sp,
4185-
"parentheses are required to parse this \
4186-
as an expression",
4187-
format!("({})", snippet),
4188-
Applicability::MachineApplicable,
4189-
);
4190-
}
4180+
tcx.sess.parse_sess.expr_parentheses_needed(
4181+
&mut err,
4182+
*sp,
4183+
None,
4184+
);
41914185
}
41924186
err.emit();
41934187
oprnd_t = tcx.types.err;

src/libsyntax/parse/mod.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::tokenstream::{TokenStream, TokenTree};
1111
use crate::diagnostics::plugin::ErrorMap;
1212
use crate::print::pprust::token_to_string;
1313

14-
use errors::{FatalError, Level, Handler, ColorConfig, Diagnostic, DiagnosticBuilder};
14+
use errors::{Applicability, FatalError, Level, Handler, ColorConfig, Diagnostic, DiagnosticBuilder};
1515
use rustc_data_structures::sync::{Lrc, Lock};
1616
use syntax_pos::{Span, SourceFile, FileName, MultiSpan};
1717
use log::debug;
@@ -47,6 +47,9 @@ pub struct ParseSess {
4747
included_mod_stack: Lock<Vec<PathBuf>>,
4848
source_map: Lrc<SourceMap>,
4949
pub buffered_lints: Lock<Vec<BufferedEarlyLint>>,
50+
/// Contains the spans of block expressions that could have been incomplete based on the
51+
/// operation token that followed it, but that the parser cannot identify without further
52+
/// analysis.
5053
pub abiguous_block_expr_parse: Lock<FxHashMap<Span, Span>>,
5154
}
5255

@@ -95,6 +98,24 @@ impl ParseSess {
9598
});
9699
});
97100
}
101+
102+
/// Extend an error with a suggestion to wrap an expression with parentheses to allow the
103+
/// parser to continue parsing the following operation as part of the same expression.
104+
pub fn expr_parentheses_needed(
105+
&self,
106+
err: &mut DiagnosticBuilder<'_>,
107+
span: Span,
108+
alt_snippet: Option<String>,
109+
) {
110+
if let Some(snippet) = self.source_map().span_to_snippet(span).ok().or(alt_snippet) {
111+
err.span_suggestion(
112+
span,
113+
"parentheses are required to parse this as an expression",
114+
format!("({})", snippet),
115+
Applicability::MachineApplicable,
116+
);
117+
}
118+
}
98119
}
99120

100121
#[derive(Clone)]

src/libsyntax/parse/parser.rs

+6-23
Original file line numberDiff line numberDiff line change
@@ -2931,14 +2931,7 @@ impl<'a> Parser<'a> {
29312931
if let Some(sp) = self.sess.abiguous_block_expr_parse.borrow()
29322932
.get(&sp)
29332933
{
2934-
if let Ok(snippet) = self.sess.source_map().span_to_snippet(*sp) {
2935-
err.span_suggestion(
2936-
*sp,
2937-
"parentheses are required to parse this as an expression",
2938-
format!("({})", snippet),
2939-
Applicability::MachineApplicable,
2940-
);
2941-
}
2934+
self.sess.expr_parentheses_needed(&mut err, *sp, None);
29422935
}
29432936
err.span_label(self.span, "expected expression");
29442937
return Err(err);
@@ -3657,14 +3650,11 @@ impl<'a> Parser<'a> {
36573650
pprust::token_to_string(&self.token),
36583651
));
36593652
err.span_label(self.span, "expected expression");
3660-
let snippet = self.sess.source_map().span_to_snippet(lhs.span)
3661-
.unwrap_or_else(|_| pprust::expr_to_string(&lhs));
3662-
err.span_suggestion(
3653+
self.sess.expr_parentheses_needed(
3654+
&mut err,
36633655
lhs.span,
3664-
"parentheses are required to parse this as an expression",
3665-
format!("({})", snippet),
3666-
Applicability::MachineApplicable,
3667-
);
3656+
Some(pprust::expr_to_string(&lhs),
3657+
));
36683658
err.emit();
36693659
}
36703660
}
@@ -4979,14 +4969,7 @@ impl<'a> Parser<'a> {
49794969
err.span_label(self.span, format!("expected {}", expected));
49804970
let sp = self.sess.source_map().start_point(self.span);
49814971
if let Some(sp) = self.sess.abiguous_block_expr_parse.borrow().get(&sp) {
4982-
if let Ok(snippet) = self.sess.source_map().span_to_snippet(*sp) {
4983-
err.span_suggestion(
4984-
*sp,
4985-
"parentheses are required to parse this as an expression",
4986-
format!("({})", snippet),
4987-
Applicability::MachineApplicable,
4988-
);
4989-
}
4972+
self.sess.expr_parentheses_needed(&mut err, *sp, None);
49904973
}
49914974
return Err(err);
49924975
}

0 commit comments

Comments
 (0)