]>
Commit | Line | Data |
---|---|---|
e74abb32 XL |
1 | //! Contains `ParseSess` which holds state living beyond what one `Parser` might. |
2 | //! It also serves as an input to the parser itself. | |
3 | ||
dfeec247 | 4 | use crate::lint::{BufferedEarlyLint, BuiltinLintDiagnostics, Lint, LintId}; |
74b04a01 | 5 | use rustc_ast::node_id::NodeId; |
dfeec247 | 6 | use rustc_data_structures::fx::{FxHashMap, FxHashSet}; |
f9f354fc | 7 | use rustc_data_structures::sync::{Lock, Lrc, OnceCell}; |
dfeec247 XL |
8 | use rustc_errors::{emitter::SilentEmitter, ColorConfig, Handler}; |
9 | use rustc_errors::{error_code, Applicability, DiagnosticBuilder}; | |
10 | use rustc_feature::{find_feature_issue, GateIssue, UnstableFeatures}; | |
11 | use rustc_span::edition::Edition; | |
12 | use rustc_span::hygiene::ExpnId; | |
13 | use rustc_span::source_map::{FilePathMapping, SourceMap}; | |
14 | use rustc_span::{MultiSpan, Span, Symbol}; | |
e74abb32 XL |
15 | |
16 | use std::path::PathBuf; | |
17 | use std::str; | |
18 | ||
60c5eb7d XL |
19 | /// The set of keys (and, optionally, values) that define the compilation |
20 | /// environment of the crate, used to drive conditional compilation. | |
21 | pub type CrateConfig = FxHashSet<(Symbol, Option<Symbol>)>; | |
22 | ||
e74abb32 XL |
23 | /// Collected spans during parsing for places where a certain feature was |
24 | /// used and should be feature gated accordingly in `check_crate`. | |
25 | #[derive(Default)] | |
60c5eb7d XL |
26 | pub struct GatedSpans { |
27 | pub spans: Lock<FxHashMap<Symbol, Vec<Span>>>, | |
28 | } | |
29 | ||
30 | impl GatedSpans { | |
31 | /// Feature gate the given `span` under the given `feature` | |
32 | /// which is same `Symbol` used in `active.rs`. | |
33 | pub fn gate(&self, feature: Symbol, span: Span) { | |
dfeec247 | 34 | self.spans.borrow_mut().entry(feature).or_default().push(span); |
60c5eb7d XL |
35 | } |
36 | ||
37 | /// Ungate the last span under the given `feature`. | |
38 | /// Panics if the given `span` wasn't the last one. | |
39 | /// | |
40 | /// Using this is discouraged unless you have a really good reason to. | |
41 | pub fn ungate_last(&self, feature: Symbol, span: Span) { | |
dfeec247 | 42 | let removed_span = self.spans.borrow_mut().entry(feature).or_default().pop().unwrap(); |
60c5eb7d XL |
43 | debug_assert_eq!(span, removed_span); |
44 | } | |
45 | ||
46 | /// Is the provided `feature` gate ungated currently? | |
47 | /// | |
48 | /// Using this is discouraged unless you have a really good reason to. | |
49 | pub fn is_ungated(&self, feature: Symbol) -> bool { | |
dfeec247 | 50 | self.spans.borrow().get(&feature).map_or(true, |spans| spans.is_empty()) |
60c5eb7d XL |
51 | } |
52 | ||
53 | /// Prepend the given set of `spans` onto the set in `self`. | |
54 | pub fn merge(&self, mut spans: FxHashMap<Symbol, Vec<Span>>) { | |
55 | let mut inner = self.spans.borrow_mut(); | |
56 | for (gate, mut gate_spans) in inner.drain() { | |
57 | spans.entry(gate).or_default().append(&mut gate_spans); | |
58 | } | |
59 | *inner = spans; | |
60 | } | |
e74abb32 XL |
61 | } |
62 | ||
f9f354fc XL |
63 | #[derive(Default)] |
64 | pub struct SymbolGallery { | |
3dfed10e XL |
65 | /// All symbols occurred and their first occurrence span. |
66 | pub symbols: Lock<FxHashMap<Symbol, Span>>, | |
f9f354fc XL |
67 | } |
68 | ||
69 | impl SymbolGallery { | |
70 | /// Insert a symbol and its span into symbol gallery. | |
71 | /// If the symbol has occurred before, ignore the new occurance. | |
72 | pub fn insert(&self, symbol: Symbol, span: Span) { | |
73 | self.symbols.lock().entry(symbol).or_insert(span); | |
74 | } | |
75 | } | |
76 | ||
dfeec247 XL |
77 | /// Construct a diagnostic for a language feature error due to the given `span`. |
78 | /// The `feature`'s `Symbol` is the one you used in `active.rs` and `rustc_span::symbols`. | |
79 | pub fn feature_err<'a>( | |
80 | sess: &'a ParseSess, | |
81 | feature: Symbol, | |
82 | span: impl Into<MultiSpan>, | |
83 | explain: &str, | |
84 | ) -> DiagnosticBuilder<'a> { | |
85 | feature_err_issue(sess, feature, span, GateIssue::Language, explain) | |
86 | } | |
87 | ||
88 | /// Construct a diagnostic for a feature gate error. | |
89 | /// | |
90 | /// This variant allows you to control whether it is a library or language feature. | |
91 | /// Almost always, you want to use this for a language feature. If so, prefer `feature_err`. | |
92 | pub fn feature_err_issue<'a>( | |
93 | sess: &'a ParseSess, | |
94 | feature: Symbol, | |
95 | span: impl Into<MultiSpan>, | |
96 | issue: GateIssue, | |
97 | explain: &str, | |
98 | ) -> DiagnosticBuilder<'a> { | |
99 | let mut err = sess.span_diagnostic.struct_span_err_with_code(span, explain, error_code!(E0658)); | |
100 | ||
101 | if let Some(n) = find_feature_issue(feature, issue) { | |
102 | err.note(&format!( | |
74b04a01 XL |
103 | "see issue #{} <https://github.com/rust-lang/rust/issues/{}> for more information", |
104 | n, n, | |
dfeec247 XL |
105 | )); |
106 | } | |
107 | ||
108 | // #23973: do not suggest `#![feature(...)]` if we are in beta/stable | |
109 | if sess.unstable_features.is_nightly_build() { | |
110 | err.help(&format!("add `#![feature({})]` to the crate attributes to enable", feature)); | |
111 | } | |
112 | ||
113 | err | |
114 | } | |
115 | ||
e74abb32 XL |
116 | /// Info about a parsing session. |
117 | pub struct ParseSess { | |
118 | pub span_diagnostic: Handler, | |
60c5eb7d | 119 | pub unstable_features: UnstableFeatures, |
e74abb32 XL |
120 | pub config: CrateConfig, |
121 | pub edition: Edition, | |
e74abb32 XL |
122 | /// Places where raw identifiers were used. This is used for feature-gating raw identifiers. |
123 | pub raw_identifier_spans: Lock<Vec<Span>>, | |
124 | /// Used to determine and report recursive module inclusions. | |
60c5eb7d | 125 | pub included_mod_stack: Lock<Vec<PathBuf>>, |
e74abb32 XL |
126 | source_map: Lrc<SourceMap>, |
127 | pub buffered_lints: Lock<Vec<BufferedEarlyLint>>, | |
128 | /// Contains the spans of block expressions that could have been incomplete based on the | |
129 | /// operation token that followed it, but that the parser cannot identify without further | |
130 | /// analysis. | |
131 | pub ambiguous_block_expr_parse: Lock<FxHashMap<Span, Span>>, | |
f9f354fc | 132 | pub injected_crate_name: OnceCell<Symbol>, |
60c5eb7d | 133 | pub gated_spans: GatedSpans, |
f9f354fc | 134 | pub symbol_gallery: SymbolGallery, |
e74abb32 XL |
135 | /// The parser has reached `Eof` due to an unclosed brace. Used to silence unnecessary errors. |
136 | pub reached_eof: Lock<bool>, | |
f035d41b XL |
137 | /// Environment variables accessed during the build and their values when they exist. |
138 | pub env_depinfo: Lock<FxHashSet<(Symbol, Option<Symbol>)>>, | |
3dfed10e XL |
139 | /// All the type ascriptions expressions that have had a suggestion for likely path typo. |
140 | pub type_ascription_path_suggestions: Lock<FxHashSet<Span>>, | |
e74abb32 XL |
141 | } |
142 | ||
143 | impl ParseSess { | |
144 | pub fn new(file_path_mapping: FilePathMapping) -> Self { | |
74b04a01 XL |
145 | let sm = Lrc::new(SourceMap::new(file_path_mapping)); |
146 | let handler = Handler::with_tty_emitter(ColorConfig::Auto, true, None, Some(sm.clone())); | |
147 | ParseSess::with_span_handler(handler, sm) | |
e74abb32 XL |
148 | } |
149 | ||
dfeec247 | 150 | pub fn with_span_handler(handler: Handler, source_map: Lrc<SourceMap>) -> Self { |
e74abb32 XL |
151 | Self { |
152 | span_diagnostic: handler, | |
153 | unstable_features: UnstableFeatures::from_environment(), | |
154 | config: FxHashSet::default(), | |
155 | edition: ExpnId::root().expn_data().edition, | |
e74abb32 XL |
156 | raw_identifier_spans: Lock::new(Vec::new()), |
157 | included_mod_stack: Lock::new(vec![]), | |
158 | source_map, | |
159 | buffered_lints: Lock::new(vec![]), | |
160 | ambiguous_block_expr_parse: Lock::new(FxHashMap::default()), | |
f9f354fc | 161 | injected_crate_name: OnceCell::new(), |
e74abb32 | 162 | gated_spans: GatedSpans::default(), |
f9f354fc | 163 | symbol_gallery: SymbolGallery::default(), |
e74abb32 | 164 | reached_eof: Lock::new(false), |
f035d41b | 165 | env_depinfo: Default::default(), |
3dfed10e | 166 | type_ascription_path_suggestions: Default::default(), |
e74abb32 XL |
167 | } |
168 | } | |
169 | ||
60c5eb7d | 170 | pub fn with_silent_emitter() -> Self { |
74b04a01 | 171 | let sm = Lrc::new(SourceMap::new(FilePathMapping::empty())); |
60c5eb7d | 172 | let handler = Handler::with_emitter(false, None, Box::new(SilentEmitter)); |
74b04a01 | 173 | ParseSess::with_span_handler(handler, sm) |
60c5eb7d XL |
174 | } |
175 | ||
e74abb32 XL |
176 | #[inline] |
177 | pub fn source_map(&self) -> &SourceMap { | |
178 | &self.source_map | |
179 | } | |
180 | ||
f9f354fc XL |
181 | pub fn clone_source_map(&self) -> Lrc<SourceMap> { |
182 | self.source_map.clone() | |
183 | } | |
184 | ||
e74abb32 XL |
185 | pub fn buffer_lint( |
186 | &self, | |
dfeec247 | 187 | lint: &'static Lint, |
e74abb32 | 188 | span: impl Into<MultiSpan>, |
dfeec247 | 189 | node_id: NodeId, |
e74abb32 XL |
190 | msg: &str, |
191 | ) { | |
192 | self.buffered_lints.with_lock(|buffered_lints| { | |
dfeec247 | 193 | buffered_lints.push(BufferedEarlyLint { |
e74abb32 | 194 | span: span.into(), |
dfeec247 | 195 | node_id, |
e74abb32 | 196 | msg: msg.into(), |
dfeec247 XL |
197 | lint_id: LintId::of(lint), |
198 | diagnostic: BuiltinLintDiagnostics::Normal, | |
e74abb32 XL |
199 | }); |
200 | }); | |
201 | } | |
202 | ||
74b04a01 XL |
203 | pub fn buffer_lint_with_diagnostic( |
204 | &self, | |
205 | lint: &'static Lint, | |
206 | span: impl Into<MultiSpan>, | |
207 | node_id: NodeId, | |
208 | msg: &str, | |
209 | diagnostic: BuiltinLintDiagnostics, | |
210 | ) { | |
211 | self.buffered_lints.with_lock(|buffered_lints| { | |
212 | buffered_lints.push(BufferedEarlyLint { | |
213 | span: span.into(), | |
214 | node_id, | |
215 | msg: msg.into(), | |
216 | lint_id: LintId::of(lint), | |
217 | diagnostic, | |
218 | }); | |
219 | }); | |
220 | } | |
221 | ||
e74abb32 XL |
222 | /// Extend an error with a suggestion to wrap an expression with parentheses to allow the |
223 | /// parser to continue parsing the following operation as part of the same expression. | |
224 | pub fn expr_parentheses_needed( | |
225 | &self, | |
226 | err: &mut DiagnosticBuilder<'_>, | |
227 | span: Span, | |
228 | alt_snippet: Option<String>, | |
229 | ) { | |
230 | if let Some(snippet) = self.source_map().span_to_snippet(span).ok().or(alt_snippet) { | |
231 | err.span_suggestion( | |
232 | span, | |
233 | "parentheses are required to parse this as an expression", | |
234 | format!("({})", snippet), | |
235 | Applicability::MachineApplicable, | |
236 | ); | |
237 | } | |
238 | } | |
239 | } |