@@ -11,7 +11,7 @@ use crate::tokenstream::{TokenStream, TokenTree};
11
11
use crate :: diagnostics:: plugin:: ErrorMap ;
12
12
use crate :: print:: pprust:: token_to_string;
13
13
14
- use errors:: { FatalError , Level , Handler , ColorConfig , Diagnostic , DiagnosticBuilder } ;
14
+ use errors:: { Applicability , FatalError , Level , Handler , ColorConfig , Diagnostic , DiagnosticBuilder } ;
15
15
use rustc_data_structures:: sync:: { Lrc , Lock } ;
16
16
use syntax_pos:: { Span , SourceFile , FileName , MultiSpan } ;
17
17
use log:: debug;
@@ -47,6 +47,9 @@ pub struct ParseSess {
47
47
included_mod_stack : Lock < Vec < PathBuf > > ,
48
48
source_map : Lrc < SourceMap > ,
49
49
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.
50
53
pub abiguous_block_expr_parse : Lock < FxHashMap < Span , Span > > ,
51
54
}
52
55
@@ -95,6 +98,24 @@ impl ParseSess {
95
98
} ) ;
96
99
} ) ;
97
100
}
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
+ }
98
119
}
99
120
100
121
#[ derive( Clone ) ]
0 commit comments