rustc_session/
parse.rs

1//! Contains `ParseSess` which holds state living beyond what one `Parser` might.
2//! It also serves as an input to the parser itself.
3
4use 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::{FatalOnlyEmitter, HumanEmitter, stderr_destination};
12use rustc_errors::translation::Translator;
13use rustc_errors::{
14    BufferedEarlyLint, ColorConfig, DecorateDiagCompat, Diag, DiagCtxt, DiagCtxtHandle,
15    DiagMessage, EmissionGuarantee, MultiSpan, StashKey,
16};
17use rustc_feature::{GateIssue, UnstableFeatures, find_feature_issue};
18use rustc_span::edition::Edition;
19use rustc_span::hygiene::ExpnId;
20use rustc_span::source_map::{FilePathMapping, SourceMap};
21use rustc_span::{Span, Symbol, sym};
22
23use crate::Session;
24use crate::config::{Cfg, CheckCfg};
25use crate::errors::{
26    CliFeatureDiagnosticHelp, FeatureDiagnosticForIssue, FeatureDiagnosticHelp,
27    FeatureDiagnosticSuggestion, FeatureGateError, SuggestUpgradeCompiler,
28};
29use crate::lint::builtin::UNSTABLE_SYNTAX_PRE_EXPANSION;
30use crate::lint::{Lint, LintId};
31
32/// Collected spans during parsing for places where a certain feature was
33/// used and should be feature gated accordingly in `check_crate`.
34#[derive(Default)]
35pub struct GatedSpans {
36    pub spans: Lock<FxHashMap<Symbol, Vec<Span>>>,
37}
38
39impl GatedSpans {
40    /// Feature gate the given `span` under the given `feature`
41    /// which is same `Symbol` used in `unstable.rs`.
42    pub fn gate(&self, feature: Symbol, span: Span) {
43        self.spans.borrow_mut().entry(feature).or_default().push(span);
44    }
45
46    /// Ungate the last span under the given `feature`.
47    /// Panics if the given `span` wasn't the last one.
48    ///
49    /// Using this is discouraged unless you have a really good reason to.
50    pub fn ungate_last(&self, feature: Symbol, span: Span) {
51        let removed_span = self.spans.borrow_mut().entry(feature).or_default().pop().unwrap();
52        debug_assert_eq!(span, removed_span);
53    }
54
55    /// Prepend the given set of `spans` onto the set in `self`.
56    pub fn merge(&self, mut spans: FxHashMap<Symbol, Vec<Span>>) {
57        let mut inner = self.spans.borrow_mut();
58        // The entries will be moved to another map so the drain order does not
59        // matter.
60        #[allow(rustc::potential_query_instability)]
61        for (gate, mut gate_spans) in inner.drain() {
62            spans.entry(gate).or_default().append(&mut gate_spans);
63        }
64        *inner = spans;
65    }
66}
67
68#[derive(Default)]
69pub struct SymbolGallery {
70    /// All symbols occurred and their first occurrence span.
71    pub symbols: Lock<FxIndexMap<Symbol, Span>>,
72}
73
74impl SymbolGallery {
75    /// Insert a symbol and its span into symbol gallery.
76    /// If the symbol has occurred before, ignore the new occurrence.
77    pub fn insert(&self, symbol: Symbol, span: Span) {
78        self.symbols.lock().entry(symbol).or_insert(span);
79    }
80}
81
82// todo: this function now accepts `Session` instead of `ParseSess` and should be relocated
83/// Construct a diagnostic for a language feature error due to the given `span`.
84/// The `feature`'s `Symbol` is the one you used in `unstable.rs` and `rustc_span::symbol`.
85#[track_caller]
86pub fn feature_err(
87    sess: &Session,
88    feature: Symbol,
89    span: impl Into<MultiSpan>,
90    explain: impl Into<DiagMessage>,
91) -> Diag<'_> {
92    feature_err_issue(sess, feature, span, GateIssue::Language, explain)
93}
94
95/// Construct a diagnostic for a feature gate error.
96///
97/// This variant allows you to control whether it is a library or language feature.
98/// Almost always, you want to use this for a language feature. If so, prefer `feature_err`.
99#[track_caller]
100pub fn feature_err_issue(
101    sess: &Session,
102    feature: Symbol,
103    span: impl Into<MultiSpan>,
104    issue: GateIssue,
105    explain: impl Into<DiagMessage>,
106) -> Diag<'_> {
107    let span = span.into();
108
109    // Cancel an earlier warning for this same error, if it exists.
110    if let Some(span) = span.primary_span()
111        && let Some(err) = sess.dcx().steal_non_err(span, StashKey::EarlySyntaxWarning)
112    {
113        err.cancel()
114    }
115
116    let mut err = sess.dcx().create_err(FeatureGateError { span, explain: explain.into() });
117    add_feature_diagnostics_for_issue(&mut err, sess, feature, issue, false, None);
118    err
119}
120
121/// Construct a future incompatibility diagnostic for a feature gate.
122///
123/// This diagnostic is only a warning and *does not cause compilation to fail*.
124#[track_caller]
125pub fn feature_warn(sess: &Session, feature: Symbol, span: Span, explain: &'static str) {
126    feature_warn_issue(sess, feature, span, GateIssue::Language, explain);
127}
128
129/// Construct a future incompatibility diagnostic for a feature gate.
130///
131/// This diagnostic is only a warning and *does not cause compilation to fail*.
132///
133/// This variant allows you to control whether it is a library or language feature.
134/// Almost always, you want to use this for a language feature. If so, prefer `feature_warn`.
135#[allow(rustc::diagnostic_outside_of_impl)]
136#[allow(rustc::untranslatable_diagnostic)]
137#[track_caller]
138pub fn feature_warn_issue(
139    sess: &Session,
140    feature: Symbol,
141    span: Span,
142    issue: GateIssue,
143    explain: &'static str,
144) {
145    let mut err = sess.dcx().struct_span_warn(span, explain);
146    add_feature_diagnostics_for_issue(&mut err, sess, feature, issue, false, None);
147
148    // Decorate this as a future-incompatibility lint as in rustc_middle::lint::lint_level
149    let lint = UNSTABLE_SYNTAX_PRE_EXPANSION;
150    let future_incompatible = lint.future_incompatible.as_ref().unwrap();
151    err.is_lint(lint.name_lower(), /* has_future_breakage */ false);
152    err.warn(lint.desc);
153    err.note(format!("for more information, see {}", future_incompatible.reference));
154
155    // A later feature_err call can steal and cancel this warning.
156    err.stash(span, StashKey::EarlySyntaxWarning);
157}
158
159/// Adds the diagnostics for a feature to an existing error.
160/// Must be a language feature!
161pub fn add_feature_diagnostics<G: EmissionGuarantee>(
162    err: &mut Diag<'_, G>,
163    sess: &Session,
164    feature: Symbol,
165) {
166    add_feature_diagnostics_for_issue(err, sess, feature, GateIssue::Language, false, None);
167}
168
169/// Adds the diagnostics for a feature to an existing error.
170///
171/// This variant allows you to control whether it is a library or language feature.
172/// Almost always, you want to use this for a language feature. If so, prefer
173/// `add_feature_diagnostics`.
174#[allow(rustc::diagnostic_outside_of_impl)] // FIXME
175pub fn add_feature_diagnostics_for_issue<G: EmissionGuarantee>(
176    err: &mut Diag<'_, G>,
177    sess: &Session,
178    feature: Symbol,
179    issue: GateIssue,
180    feature_from_cli: bool,
181    inject_span: Option<Span>,
182) {
183    if let Some(n) = find_feature_issue(feature, issue) {
184        err.subdiagnostic(FeatureDiagnosticForIssue { n });
185    }
186
187    // #23973: do not suggest `#![feature(...)]` if we are in beta/stable
188    if sess.psess.unstable_features.is_nightly_build() {
189        if feature_from_cli {
190            err.subdiagnostic(CliFeatureDiagnosticHelp { feature });
191        } else if let Some(span) = inject_span {
192            err.subdiagnostic(FeatureDiagnosticSuggestion { feature, span });
193        } else {
194            err.subdiagnostic(FeatureDiagnosticHelp { feature });
195        }
196        if feature == sym::rustc_attrs {
197            // We're unlikely to stabilize something out of `rustc_attrs`
198            // without at least renaming it, so pointing out how old
199            // the compiler is will do little good.
200        } else if sess.opts.unstable_opts.ui_testing {
201            err.subdiagnostic(SuggestUpgradeCompiler::ui_testing());
202        } else if let Some(suggestion) = SuggestUpgradeCompiler::new() {
203            err.subdiagnostic(suggestion);
204        }
205    }
206}
207
208/// This is only used by unstable_feature_bound as it does not have issue number information for now.
209/// This is basically the same as `feature_err_issue`
210/// but without the feature issue note. If we can do a lookup for issue number from feature name,
211/// then we should directly use `feature_err_issue` for ambiguity error of
212/// `#[unstable_feature_bound]`.
213#[track_caller]
214pub fn feature_err_unstable_feature_bound(
215    sess: &Session,
216    feature: Symbol,
217    span: impl Into<MultiSpan>,
218    explain: impl Into<DiagMessage>,
219) -> Diag<'_> {
220    let span = span.into();
221
222    // Cancel an earlier warning for this same error, if it exists.
223    if let Some(span) = span.primary_span() {
224        if let Some(err) = sess.dcx().steal_non_err(span, StashKey::EarlySyntaxWarning) {
225            err.cancel()
226        }
227    }
228
229    let mut err = sess.dcx().create_err(FeatureGateError { span, explain: explain.into() });
230
231    // #23973: do not suggest `#![feature(...)]` if we are in beta/stable
232    if sess.psess.unstable_features.is_nightly_build() {
233        err.subdiagnostic(FeatureDiagnosticHelp { feature });
234
235        if feature == sym::rustc_attrs {
236            // We're unlikely to stabilize something out of `rustc_attrs`
237            // without at least renaming it, so pointing out how old
238            // the compiler is will do little good.
239        } else if sess.opts.unstable_opts.ui_testing {
240            err.subdiagnostic(SuggestUpgradeCompiler::ui_testing());
241        } else if let Some(suggestion) = SuggestUpgradeCompiler::new() {
242            err.subdiagnostic(suggestion);
243        }
244    }
245    err
246}
247
248/// Info about a parsing session.
249pub struct ParseSess {
250    dcx: DiagCtxt,
251    pub unstable_features: UnstableFeatures,
252    pub config: Cfg,
253    pub check_config: CheckCfg,
254    pub edition: Edition,
255    /// Places where raw identifiers were used. This is used to avoid complaining about idents
256    /// clashing with keywords in new editions.
257    pub raw_identifier_spans: AppendOnlyVec<Span>,
258    /// Places where identifiers that contain invalid Unicode codepoints but that look like they
259    /// should be. Useful to avoid bad tokenization when encountering emoji. We group them to
260    /// provide a single error per unique incorrect identifier.
261    pub bad_unicode_identifiers: Lock<FxIndexMap<Symbol, Vec<Span>>>,
262    source_map: Arc<SourceMap>,
263    pub buffered_lints: Lock<Vec<BufferedEarlyLint>>,
264    /// Contains the spans of block expressions that could have been incomplete based on the
265    /// operation token that followed it, but that the parser cannot identify without further
266    /// analysis.
267    pub ambiguous_block_expr_parse: Lock<FxIndexMap<Span, Span>>,
268    pub gated_spans: GatedSpans,
269    pub symbol_gallery: SymbolGallery,
270    /// Environment variables accessed during the build and their values when they exist.
271    pub env_depinfo: Lock<FxIndexSet<(Symbol, Option<Symbol>)>>,
272    /// File paths accessed during the build.
273    pub file_depinfo: Lock<FxIndexSet<Symbol>>,
274    /// Whether cfg(version) should treat the current release as incomplete
275    pub assume_incomplete_release: bool,
276    /// Spans passed to `proc_macro::quote_span`. Each span has a numerical
277    /// identifier represented by its position in the vector.
278    proc_macro_quoted_spans: AppendOnlyVec<Span>,
279    /// Used to generate new `AttrId`s. Every `AttrId` is unique.
280    pub attr_id_generator: AttrIdGenerator,
281}
282
283impl ParseSess {
284    /// Used for testing.
285    pub fn new(locale_resources: Vec<&'static str>) -> Self {
286        let translator = Translator::with_fallback_bundle(locale_resources, false);
287        let sm = Arc::new(SourceMap::new(FilePathMapping::empty()));
288        let emitter = Box::new(
289            HumanEmitter::new(stderr_destination(ColorConfig::Auto), translator)
290                .sm(Some(Arc::clone(&sm))),
291        );
292        let dcx = DiagCtxt::new(emitter);
293        ParseSess::with_dcx(dcx, sm)
294    }
295
296    pub fn with_dcx(dcx: DiagCtxt, source_map: Arc<SourceMap>) -> Self {
297        Self {
298            dcx,
299            unstable_features: UnstableFeatures::from_environment(None),
300            config: Cfg::default(),
301            check_config: CheckCfg::default(),
302            edition: ExpnId::root().expn_data().edition,
303            raw_identifier_spans: Default::default(),
304            bad_unicode_identifiers: Lock::new(Default::default()),
305            source_map,
306            buffered_lints: Lock::new(vec![]),
307            ambiguous_block_expr_parse: Lock::new(Default::default()),
308            gated_spans: GatedSpans::default(),
309            symbol_gallery: SymbolGallery::default(),
310            env_depinfo: Default::default(),
311            file_depinfo: Default::default(),
312            assume_incomplete_release: false,
313            proc_macro_quoted_spans: Default::default(),
314            attr_id_generator: AttrIdGenerator::new(),
315        }
316    }
317
318    pub fn with_fatal_emitter(locale_resources: Vec<&'static str>, fatal_note: String) -> Self {
319        let translator = Translator::with_fallback_bundle(locale_resources, false);
320        let sm = Arc::new(SourceMap::new(FilePathMapping::empty()));
321        let fatal_emitter =
322            Box::new(HumanEmitter::new(stderr_destination(ColorConfig::Auto), translator));
323        let dcx = DiagCtxt::new(Box::new(FatalOnlyEmitter {
324            fatal_emitter,
325            fatal_note: Some(fatal_note),
326        }))
327        .disable_warnings();
328        ParseSess::with_dcx(dcx, sm)
329    }
330
331    #[inline]
332    pub fn source_map(&self) -> &SourceMap {
333        &self.source_map
334    }
335
336    pub fn clone_source_map(&self) -> Arc<SourceMap> {
337        Arc::clone(&self.source_map)
338    }
339
340    pub fn buffer_lint(
341        &self,
342        lint: &'static Lint,
343        span: impl Into<MultiSpan>,
344        node_id: NodeId,
345        diagnostic: impl Into<DecorateDiagCompat>,
346    ) {
347        self.opt_span_buffer_lint(lint, Some(span.into()), node_id, diagnostic.into())
348    }
349
350    pub(crate) fn opt_span_buffer_lint(
351        &self,
352        lint: &'static Lint,
353        span: Option<MultiSpan>,
354        node_id: NodeId,
355        diagnostic: DecorateDiagCompat,
356    ) {
357        self.buffered_lints.with_lock(|buffered_lints| {
358            buffered_lints.push(BufferedEarlyLint {
359                span,
360                node_id,
361                lint_id: LintId::of(lint),
362                diagnostic,
363            });
364        });
365    }
366
367    pub fn save_proc_macro_span(&self, span: Span) -> usize {
368        self.proc_macro_quoted_spans.push(span)
369    }
370
371    pub fn proc_macro_quoted_spans(&self) -> impl Iterator<Item = (usize, Span)> {
372        // This is equivalent to `.iter().copied().enumerate()`, but that isn't possible for
373        // AppendOnlyVec, so we resort to this scheme.
374        self.proc_macro_quoted_spans.iter_enumerated()
375    }
376
377    pub fn dcx(&self) -> DiagCtxtHandle<'_> {
378        self.dcx.handle()
379    }
380}