@@ -11,7 +11,7 @@ use crate::tokenstream::{TokenStream, TokenTree};
1111use crate :: diagnostics:: plugin:: ErrorMap ;
1212use 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 } ;
1515use rustc_data_structures:: sync:: { Lrc , Lock } ;
1616use syntax_pos:: { Span , SourceFile , FileName , MultiSpan } ;
1717use 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 ) ]
0 commit comments