1use std::str;
5use std::sync::Arc;
6
7use rustc_ast::attr::AttrIdGenerator;
8use rustc_ast::node_id::NodeId;
9use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet};
10use rustc_data_structures::sync::{AppendOnlyVec, Lock};
11use rustc_errors::emitter::{HumanEmitter, SilentEmitter, stderr_destination};
12use rustc_errors::{
13 ColorConfig, Diag, DiagCtxt, DiagCtxtHandle, DiagMessage, EmissionGuarantee, MultiSpan,
14 StashKey, fallback_fluent_bundle,
15};
16use rustc_feature::{GateIssue, UnstableFeatures, find_feature_issue};
17use rustc_span::edition::Edition;
18use rustc_span::hygiene::ExpnId;
19use rustc_span::source_map::{FilePathMapping, SourceMap};
20use rustc_span::{Span, Symbol};
21
22use crate::Session;
23use crate::config::{Cfg, CheckCfg};
24use crate::errors::{
25 CliFeatureDiagnosticHelp, FeatureDiagnosticForIssue, FeatureDiagnosticHelp,
26 FeatureDiagnosticSuggestion, FeatureGateError, SuggestUpgradeCompiler,
27};
28use crate::lint::builtin::UNSTABLE_SYNTAX_PRE_EXPANSION;
29use crate::lint::{BufferedEarlyLint, BuiltinLintDiag, Lint, LintId};
30
31#[derive(Default)]
34pub struct GatedSpans {
35 pub spans: Lock<FxHashMap<Symbol, Vec<Span>>>,
36}
37
38impl GatedSpans {
39 pub fn gate(&self, feature: Symbol, span: Span) {
42 self.spans.borrow_mut().entry(feature).or_default().push(span);
43 }
44
45 pub fn ungate_last(&self, feature: Symbol, span: Span) {
50 let removed_span = self.spans.borrow_mut().entry(feature).or_default().pop().unwrap();
51 debug_assert_eq!(span, removed_span);
52 }
53
54 pub fn merge(&self, mut spans: FxHashMap<Symbol, Vec<Span>>) {
56 let mut inner = self.spans.borrow_mut();
57 #[allow(rustc::potential_query_instability)]
60 for (gate, mut gate_spans) in inner.drain() {
61 spans.entry(gate).or_default().append(&mut gate_spans);
62 }
63 *inner = spans;
64 }
65}
66
67#[derive(Default)]
68pub struct SymbolGallery {
69 pub symbols: Lock<FxIndexMap<Symbol, Span>>,
71}
72
73impl SymbolGallery {
74 pub fn insert(&self, symbol: Symbol, span: Span) {
77 self.symbols.lock().entry(symbol).or_insert(span);
78 }
79}
80
81#[track_caller]
85pub fn feature_err(
86 sess: &Session,
87 feature: Symbol,
88 span: impl Into<MultiSpan>,
89 explain: impl Into<DiagMessage>,
90) -> Diag<'_> {
91 feature_err_issue(sess, feature, span, GateIssue::Language, explain)
92}
93
94#[track_caller]
99pub fn feature_err_issue(
100 sess: &Session,
101 feature: Symbol,
102 span: impl Into<MultiSpan>,
103 issue: GateIssue,
104 explain: impl Into<DiagMessage>,
105) -> Diag<'_> {
106 let span = span.into();
107
108 if let Some(span) = span.primary_span() {
110 if let Some(err) = sess.dcx().steal_non_err(span, StashKey::EarlySyntaxWarning) {
111 err.cancel()
112 }
113 }
114
115 let mut err = sess.dcx().create_err(FeatureGateError { span, explain: explain.into() });
116 add_feature_diagnostics_for_issue(&mut err, sess, feature, issue, false, None);
117 err
118}
119
120#[track_caller]
124pub fn feature_warn(sess: &Session, feature: Symbol, span: Span, explain: &'static str) {
125 feature_warn_issue(sess, feature, span, GateIssue::Language, explain);
126}
127
128#[allow(rustc::diagnostic_outside_of_impl)]
135#[allow(rustc::untranslatable_diagnostic)]
136#[track_caller]
137pub fn feature_warn_issue(
138 sess: &Session,
139 feature: Symbol,
140 span: Span,
141 issue: GateIssue,
142 explain: &'static str,
143) {
144 let mut err = sess.dcx().struct_span_warn(span, explain);
145 add_feature_diagnostics_for_issue(&mut err, sess, feature, issue, false, None);
146
147 let lint = UNSTABLE_SYNTAX_PRE_EXPANSION;
149 let future_incompatible = lint.future_incompatible.as_ref().unwrap();
150 err.is_lint(lint.name_lower(), false);
151 err.warn(lint.desc);
152 err.note(format!("for more information, see {}", future_incompatible.reference));
153
154 err.stash(span, StashKey::EarlySyntaxWarning);
156}
157
158pub fn add_feature_diagnostics<G: EmissionGuarantee>(
161 err: &mut Diag<'_, G>,
162 sess: &Session,
163 feature: Symbol,
164) {
165 add_feature_diagnostics_for_issue(err, sess, feature, GateIssue::Language, false, None);
166}
167
168#[allow(rustc::diagnostic_outside_of_impl)] pub fn add_feature_diagnostics_for_issue<G: EmissionGuarantee>(
175 err: &mut Diag<'_, G>,
176 sess: &Session,
177 feature: Symbol,
178 issue: GateIssue,
179 feature_from_cli: bool,
180 inject_span: Option<Span>,
181) {
182 if let Some(n) = find_feature_issue(feature, issue) {
183 err.subdiagnostic(FeatureDiagnosticForIssue { n });
184 }
185
186 if sess.psess.unstable_features.is_nightly_build() {
188 if feature_from_cli {
189 err.subdiagnostic(CliFeatureDiagnosticHelp { feature });
190 } else if let Some(span) = inject_span {
191 err.subdiagnostic(FeatureDiagnosticSuggestion { feature, span });
192 } else {
193 err.subdiagnostic(FeatureDiagnosticHelp { feature });
194 }
195
196 if sess.opts.unstable_opts.ui_testing {
197 err.subdiagnostic(SuggestUpgradeCompiler::ui_testing());
198 } else if let Some(suggestion) = SuggestUpgradeCompiler::new() {
199 err.subdiagnostic(suggestion);
200 }
201 }
202}
203
204pub struct ParseSess {
206 dcx: DiagCtxt,
207 pub unstable_features: UnstableFeatures,
208 pub config: Cfg,
209 pub check_config: CheckCfg,
210 pub edition: Edition,
211 pub raw_identifier_spans: AppendOnlyVec<Span>,
214 pub bad_unicode_identifiers: Lock<FxIndexMap<Symbol, Vec<Span>>>,
218 source_map: Arc<SourceMap>,
219 pub buffered_lints: Lock<Vec<BufferedEarlyLint>>,
220 pub ambiguous_block_expr_parse: Lock<FxIndexMap<Span, Span>>,
224 pub gated_spans: GatedSpans,
225 pub symbol_gallery: SymbolGallery,
226 pub env_depinfo: Lock<FxIndexSet<(Symbol, Option<Symbol>)>>,
228 pub file_depinfo: Lock<FxIndexSet<Symbol>>,
230 pub assume_incomplete_release: bool,
232 proc_macro_quoted_spans: AppendOnlyVec<Span>,
235 pub attr_id_generator: AttrIdGenerator,
237}
238
239impl ParseSess {
240 pub fn new(locale_resources: Vec<&'static str>) -> Self {
242 let fallback_bundle = fallback_fluent_bundle(locale_resources, false);
243 let sm = Arc::new(SourceMap::new(FilePathMapping::empty()));
244 let emitter = Box::new(
245 HumanEmitter::new(stderr_destination(ColorConfig::Auto), fallback_bundle)
246 .sm(Some(Arc::clone(&sm))),
247 );
248 let dcx = DiagCtxt::new(emitter);
249 ParseSess::with_dcx(dcx, sm)
250 }
251
252 pub fn with_dcx(dcx: DiagCtxt, source_map: Arc<SourceMap>) -> Self {
253 Self {
254 dcx,
255 unstable_features: UnstableFeatures::from_environment(None),
256 config: Cfg::default(),
257 check_config: CheckCfg::default(),
258 edition: ExpnId::root().expn_data().edition,
259 raw_identifier_spans: Default::default(),
260 bad_unicode_identifiers: Lock::new(Default::default()),
261 source_map,
262 buffered_lints: Lock::new(vec![]),
263 ambiguous_block_expr_parse: Lock::new(Default::default()),
264 gated_spans: GatedSpans::default(),
265 symbol_gallery: SymbolGallery::default(),
266 env_depinfo: Default::default(),
267 file_depinfo: Default::default(),
268 assume_incomplete_release: false,
269 proc_macro_quoted_spans: Default::default(),
270 attr_id_generator: AttrIdGenerator::new(),
271 }
272 }
273
274 pub fn with_silent_emitter(
275 locale_resources: Vec<&'static str>,
276 fatal_note: String,
277 emit_fatal_diagnostic: bool,
278 ) -> Self {
279 let fallback_bundle = fallback_fluent_bundle(locale_resources, false);
280 let sm = Arc::new(SourceMap::new(FilePathMapping::empty()));
281 let fatal_emitter =
282 Box::new(HumanEmitter::new(stderr_destination(ColorConfig::Auto), fallback_bundle));
283 let dcx = DiagCtxt::new(Box::new(SilentEmitter {
284 fatal_emitter,
285 fatal_note: Some(fatal_note),
286 emit_fatal_diagnostic,
287 }))
288 .disable_warnings();
289 ParseSess::with_dcx(dcx, sm)
290 }
291
292 #[inline]
293 pub fn source_map(&self) -> &SourceMap {
294 &self.source_map
295 }
296
297 pub fn clone_source_map(&self) -> Arc<SourceMap> {
298 Arc::clone(&self.source_map)
299 }
300
301 pub fn buffer_lint(
302 &self,
303 lint: &'static Lint,
304 span: impl Into<MultiSpan>,
305 node_id: NodeId,
306 diagnostic: BuiltinLintDiag,
307 ) {
308 self.opt_span_buffer_lint(lint, Some(span.into()), node_id, diagnostic)
309 }
310
311 pub fn opt_span_buffer_lint(
312 &self,
313 lint: &'static Lint,
314 span: Option<MultiSpan>,
315 node_id: NodeId,
316 diagnostic: BuiltinLintDiag,
317 ) {
318 self.buffered_lints.with_lock(|buffered_lints| {
319 buffered_lints.push(BufferedEarlyLint {
320 span,
321 node_id,
322 lint_id: LintId::of(lint),
323 diagnostic,
324 });
325 });
326 }
327
328 pub fn save_proc_macro_span(&self, span: Span) -> usize {
329 self.proc_macro_quoted_spans.push(span)
330 }
331
332 pub fn proc_macro_quoted_spans(&self) -> impl Iterator<Item = (usize, Span)> {
333 self.proc_macro_quoted_spans.iter_enumerated()
336 }
337
338 pub fn dcx(&self) -> DiagCtxtHandle<'_> {
339 self.dcx.handle()
340 }
341}