Skip to main content

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};
10use rustc_data_structures::sync::{AppendOnlyVec, DynSend, DynSync, Lock};
11use rustc_errors::annotate_snippet_emitter_writer::AnnotateSnippetEmitter;
12use rustc_errors::emitter::{EmitterWithNote, stderr_destination};
13use rustc_errors::{
14    BufferedEarlyLint, ColorConfig, DecorateDiagCompat, Diag, DiagCtxt, DiagCtxtHandle,
15    DiagMessage, EmissionGuarantee, Level, 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(#[automatically_derived]
impl ::core::default::Default for GatedSpans {
    #[inline]
    fn default() -> GatedSpans {
        GatedSpans { spans: ::core::default::Default::default() }
    }
}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        if true {
    match (&span, &removed_span) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val, ::core::option::Option::None);
            }
        }
    };
};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(#[automatically_derived]
impl ::core::default::Default for SymbolGallery {
    #[inline]
    fn default() -> SymbolGallery {
        SymbolGallery { symbols: ::core::default::Default::default() }
    }
}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// FIXME: 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#[track_caller]
136pub fn feature_warn_issue(
137    sess: &Session,
138    feature: Symbol,
139    span: Span,
140    issue: GateIssue,
141    explain: &'static str,
142) {
143    let mut err = sess.dcx().struct_span_warn(span, explain);
144    add_feature_diagnostics_for_issue(&mut err, sess, feature, issue, false, None);
145
146    // Decorate this as a future-incompatibility lint as in rustc_middle::lint::lint_level
147    let lint = UNSTABLE_SYNTAX_PRE_EXPANSION;
148    let future_incompatible = lint.future_incompatible.as_ref().unwrap();
149    err.is_lint(lint.name_lower(), /* has_future_breakage */ false);
150    err.warn(lint.desc);
151    err.note(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("for more information, see {0}",
                future_incompatible.reason.reference()))
    })format!("for more information, see {}", future_incompatible.reason.reference()));
152
153    // A later feature_err call can steal and cancel this warning.
154    err.stash(span, StashKey::EarlySyntaxWarning);
155}
156
157/// Adds the diagnostics for a feature to an existing error.
158/// Must be a language feature!
159pub fn add_feature_diagnostics<G: EmissionGuarantee>(
160    err: &mut Diag<'_, G>,
161    sess: &Session,
162    feature: Symbol,
163) {
164    add_feature_diagnostics_for_issue(err, sess, feature, GateIssue::Language, false, None);
165}
166
167/// Adds the diagnostics for a feature to an existing error.
168///
169/// This variant allows you to control whether it is a library or language feature.
170/// Almost always, you want to use this for a language feature. If so, prefer
171/// `add_feature_diagnostics`.
172pub fn add_feature_diagnostics_for_issue<G: EmissionGuarantee>(
173    err: &mut Diag<'_, G>,
174    sess: &Session,
175    feature: Symbol,
176    issue: GateIssue,
177    feature_from_cli: bool,
178    inject_span: Option<Span>,
179) {
180    if let Some(n) = find_feature_issue(feature, issue) {
181        err.subdiagnostic(FeatureDiagnosticForIssue { n });
182    }
183
184    // #23973: do not suggest `#![feature(...)]` if we are in beta/stable
185    if sess.psess.unstable_features.is_nightly_build() {
186        if feature_from_cli {
187            err.subdiagnostic(CliFeatureDiagnosticHelp { feature });
188        } else if let Some(span) = inject_span {
189            err.subdiagnostic(FeatureDiagnosticSuggestion { feature, span });
190        } else {
191            err.subdiagnostic(FeatureDiagnosticHelp { feature });
192        }
193        if feature == sym::rustc_attrs {
194            // We're unlikely to stabilize something out of `rustc_attrs`
195            // without at least renaming it, so pointing out how old
196            // the compiler is will do little good.
197        } else if sess.opts.unstable_opts.ui_testing {
198            err.subdiagnostic(SuggestUpgradeCompiler::ui_testing());
199        } else if let Some(suggestion) = SuggestUpgradeCompiler::new() {
200            err.subdiagnostic(suggestion);
201        }
202    }
203}
204
205/// This is only used by unstable_feature_bound as it does not have issue number information for now.
206/// This is basically the same as `feature_err_issue`
207/// but without the feature issue note. If we can do a lookup for issue number from feature name,
208/// then we should directly use `feature_err_issue` for ambiguity error of
209/// `#[unstable_feature_bound]`.
210#[track_caller]
211pub fn feature_err_unstable_feature_bound(
212    sess: &Session,
213    feature: Symbol,
214    span: impl Into<MultiSpan>,
215    explain: impl Into<DiagMessage>,
216) -> Diag<'_> {
217    let span = span.into();
218
219    // Cancel an earlier warning for this same error, if it exists.
220    if let Some(span) = span.primary_span() {
221        if let Some(err) = sess.dcx().steal_non_err(span, StashKey::EarlySyntaxWarning) {
222            err.cancel()
223        }
224    }
225
226    let mut err = sess.dcx().create_err(FeatureGateError { span, explain: explain.into() });
227
228    // #23973: do not suggest `#![feature(...)]` if we are in beta/stable
229    if sess.psess.unstable_features.is_nightly_build() {
230        err.subdiagnostic(FeatureDiagnosticHelp { feature });
231
232        if feature == sym::rustc_attrs {
233            // We're unlikely to stabilize something out of `rustc_attrs`
234            // without at least renaming it, so pointing out how old
235            // the compiler is will do little good.
236        } else if sess.opts.unstable_opts.ui_testing {
237            err.subdiagnostic(SuggestUpgradeCompiler::ui_testing());
238        } else if let Some(suggestion) = SuggestUpgradeCompiler::new() {
239            err.subdiagnostic(suggestion);
240        }
241    }
242    err
243}
244
245/// Info about a parsing session.
246pub struct ParseSess {
247    dcx: DiagCtxt,
248    pub unstable_features: UnstableFeatures,
249    pub config: Cfg,
250    pub check_config: CheckCfg,
251    pub edition: Edition,
252    /// Places where raw identifiers were used. This is used to avoid complaining about idents
253    /// clashing with keywords in new editions.
254    pub raw_identifier_spans: AppendOnlyVec<Span>,
255    /// Places where identifiers that contain invalid Unicode codepoints but that look like they
256    /// should be. Useful to avoid bad tokenization when encountering emoji. We group them to
257    /// provide a single error per unique incorrect identifier.
258    pub bad_unicode_identifiers: Lock<FxIndexMap<Symbol, Vec<Span>>>,
259    source_map: Arc<SourceMap>,
260    pub buffered_lints: Lock<Vec<BufferedEarlyLint>>,
261    /// Contains the spans of block expressions that could have been incomplete based on the
262    /// operation token that followed it, but that the parser cannot identify without further
263    /// analysis.
264    pub ambiguous_block_expr_parse: Lock<FxIndexMap<Span, Span>>,
265    pub gated_spans: GatedSpans,
266    pub symbol_gallery: SymbolGallery,
267    /// Whether cfg(version) should treat the current release as incomplete
268    pub assume_incomplete_release: bool,
269    /// Spans passed to `proc_macro::quote_span`. Each span has a numerical
270    /// identifier represented by its position in the vector.
271    proc_macro_quoted_spans: AppendOnlyVec<Span>,
272    /// Used to generate new `AttrId`s. Every `AttrId` is unique.
273    pub attr_id_generator: AttrIdGenerator,
274}
275
276impl ParseSess {
277    /// Used for testing.
278    pub fn new() -> Self {
279        let sm = Arc::new(SourceMap::new(FilePathMapping::empty()));
280        let emitter = Box::new(
281            AnnotateSnippetEmitter::new(stderr_destination(ColorConfig::Auto))
282                .sm(Some(Arc::clone(&sm))),
283        );
284        let dcx = DiagCtxt::new(emitter);
285        ParseSess::with_dcx(dcx, sm)
286    }
287
288    pub fn with_dcx(dcx: DiagCtxt, source_map: Arc<SourceMap>) -> Self {
289        Self {
290            dcx,
291            unstable_features: UnstableFeatures::from_environment(None),
292            config: Cfg::default(),
293            check_config: CheckCfg::default(),
294            edition: ExpnId::root().expn_data().edition,
295            raw_identifier_spans: Default::default(),
296            bad_unicode_identifiers: Lock::new(Default::default()),
297            source_map,
298            buffered_lints: Lock::new(::alloc::vec::Vec::new()vec![]),
299            ambiguous_block_expr_parse: Lock::new(Default::default()),
300            gated_spans: GatedSpans::default(),
301            symbol_gallery: SymbolGallery::default(),
302            assume_incomplete_release: false,
303            proc_macro_quoted_spans: Default::default(),
304            attr_id_generator: AttrIdGenerator::new(),
305        }
306    }
307
308    pub fn emitter_with_note(note: String) -> Self {
309        let sm = Arc::new(SourceMap::new(FilePathMapping::empty()));
310        let emitter = Box::new(AnnotateSnippetEmitter::new(stderr_destination(ColorConfig::Auto)));
311        let dcx = DiagCtxt::new(Box::new(EmitterWithNote { emitter, note }));
312        ParseSess::with_dcx(dcx, sm)
313    }
314
315    #[inline]
316    pub fn source_map(&self) -> &SourceMap {
317        &self.source_map
318    }
319
320    pub fn clone_source_map(&self) -> Arc<SourceMap> {
321        Arc::clone(&self.source_map)
322    }
323
324    pub fn buffer_lint(
325        &self,
326        lint: &'static Lint,
327        span: impl Into<MultiSpan>,
328        node_id: NodeId,
329        diagnostic: impl Into<DecorateDiagCompat>,
330    ) {
331        self.opt_span_buffer_lint(lint, Some(span.into()), node_id, diagnostic.into())
332    }
333
334    pub fn dyn_buffer_lint<
335        F: for<'a> FnOnce(DiagCtxtHandle<'a>, Level) -> Diag<'a, ()> + DynSync + DynSend + 'static,
336    >(
337        &self,
338        lint: &'static Lint,
339        span: impl Into<MultiSpan>,
340        node_id: NodeId,
341        callback: F,
342    ) {
343        self.opt_span_buffer_lint(
344            lint,
345            Some(span.into()),
346            node_id,
347            DecorateDiagCompat::Dynamic(Box::new(callback)),
348        )
349    }
350
351    pub(crate) fn opt_span_buffer_lint(
352        &self,
353        lint: &'static Lint,
354        span: Option<MultiSpan>,
355        node_id: NodeId,
356        diagnostic: DecorateDiagCompat,
357    ) {
358        self.buffered_lints.with_lock(|buffered_lints| {
359            buffered_lints.push(BufferedEarlyLint {
360                span,
361                node_id,
362                lint_id: LintId::of(lint),
363                diagnostic,
364            });
365        });
366    }
367
368    pub fn save_proc_macro_span(&self, span: Span) -> usize {
369        self.proc_macro_quoted_spans.push(span)
370    }
371
372    pub fn proc_macro_quoted_spans(&self) -> impl Iterator<Item = (usize, Span)> {
373        // This is equivalent to `.iter().copied().enumerate()`, but that isn't possible for
374        // AppendOnlyVec, so we resort to this scheme.
375        self.proc_macro_quoted_spans.iter_enumerated()
376    }
377
378    pub fn dcx(&self) -> DiagCtxtHandle<'_> {
379        self.dcx.handle()
380    }
381}