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::{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/// Collected spans during parsing for places where a certain feature was
32/// used and should be feature gated accordingly in `check_crate`.
33#[derive(Default)]
34pub struct GatedSpans {
35    pub spans: Lock<FxHashMap<Symbol, Vec<Span>>>,
36}
37
38impl GatedSpans {
39    /// Feature gate the given `span` under the given `feature`
40    /// which is same `Symbol` used in `unstable.rs`.
41    pub fn gate(&self, feature: Symbol, span: Span) {
42        self.spans.borrow_mut().entry(feature).or_default().push(span);
43    }
44
45    /// Ungate the last span under the given `feature`.
46    /// Panics if the given `span` wasn't the last one.
47    ///
48    /// Using this is discouraged unless you have a really good reason to.
49    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    /// Prepend the given set of `spans` onto the set in `self`.
55    pub fn merge(&self, mut spans: FxHashMap<Symbol, Vec<Span>>) {
56        let mut inner = self.spans.borrow_mut();
57        // The entries will be moved to another map so the drain order does not
58        // matter.
59        #[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    /// All symbols occurred and their first occurrence span.
70    pub symbols: Lock<FxIndexMap<Symbol, Span>>,
71}
72
73impl SymbolGallery {
74    /// Insert a symbol and its span into symbol gallery.
75    /// If the symbol has occurred before, ignore the new occurrence.
76    pub fn insert(&self, symbol: Symbol, span: Span) {
77        self.symbols.lock().entry(symbol).or_insert(span);
78    }
79}
80
81// todo: this function now accepts `Session` instead of `ParseSess` and should be relocated
82/// Construct a diagnostic for a language feature error due to the given `span`.
83/// The `feature`'s `Symbol` is the one you used in `unstable.rs` and `rustc_span::symbol`.
84#[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/// Construct a diagnostic for a feature gate error.
95///
96/// This variant allows you to control whether it is a library or language feature.
97/// Almost always, you want to use this for a language feature. If so, prefer `feature_err`.
98#[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    // Cancel an earlier warning for this same error, if it exists.
109    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/// Construct a future incompatibility diagnostic for a feature gate.
121///
122/// This diagnostic is only a warning and *does not cause compilation to fail*.
123#[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/// Construct a future incompatibility diagnostic for a feature gate.
129///
130/// This diagnostic is only a warning and *does not cause compilation to fail*.
131///
132/// This variant allows you to control whether it is a library or language feature.
133/// Almost always, you want to use this for a language feature. If so, prefer `feature_warn`.
134#[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    // Decorate this as a future-incompatibility lint as in rustc_middle::lint::lint_level
148    let lint = UNSTABLE_SYNTAX_PRE_EXPANSION;
149    let future_incompatible = lint.future_incompatible.as_ref().unwrap();
150    err.is_lint(lint.name_lower(), /* has_future_breakage */ false);
151    err.warn(lint.desc);
152    err.note(format!("for more information, see {}", future_incompatible.reference));
153
154    // A later feature_err call can steal and cancel this warning.
155    err.stash(span, StashKey::EarlySyntaxWarning);
156}
157
158/// Adds the diagnostics for a feature to an existing error.
159/// Must be a language feature!
160pub 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/// Adds the diagnostics for a feature to an existing error.
169///
170/// This variant allows you to control whether it is a library or language feature.
171/// Almost always, you want to use this for a language feature. If so, prefer
172/// `add_feature_diagnostics`.
173#[allow(rustc::diagnostic_outside_of_impl)] // FIXME
174pub 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    // #23973: do not suggest `#![feature(...)]` if we are in beta/stable
187    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
204/// Info about a parsing session.
205pub struct ParseSess {
206    dcx: DiagCtxt,
207    pub unstable_features: UnstableFeatures,
208    pub config: Cfg,
209    pub check_config: CheckCfg,
210    pub edition: Edition,
211    /// Places where raw identifiers were used. This is used to avoid complaining about idents
212    /// clashing with keywords in new editions.
213    pub raw_identifier_spans: AppendOnlyVec<Span>,
214    /// Places where identifiers that contain invalid Unicode codepoints but that look like they
215    /// should be. Useful to avoid bad tokenization when encountering emoji. We group them to
216    /// provide a single error per unique incorrect identifier.
217    pub bad_unicode_identifiers: Lock<FxIndexMap<Symbol, Vec<Span>>>,
218    source_map: Arc<SourceMap>,
219    pub buffered_lints: Lock<Vec<BufferedEarlyLint>>,
220    /// Contains the spans of block expressions that could have been incomplete based on the
221    /// operation token that followed it, but that the parser cannot identify without further
222    /// analysis.
223    pub ambiguous_block_expr_parse: Lock<FxIndexMap<Span, Span>>,
224    pub gated_spans: GatedSpans,
225    pub symbol_gallery: SymbolGallery,
226    /// Environment variables accessed during the build and their values when they exist.
227    pub env_depinfo: Lock<FxIndexSet<(Symbol, Option<Symbol>)>>,
228    /// File paths accessed during the build.
229    pub file_depinfo: Lock<FxIndexSet<Symbol>>,
230    /// Whether cfg(version) should treat the current release as incomplete
231    pub assume_incomplete_release: bool,
232    /// Spans passed to `proc_macro::quote_span`. Each span has a numerical
233    /// identifier represented by its position in the vector.
234    proc_macro_quoted_spans: AppendOnlyVec<Span>,
235    /// Used to generate new `AttrId`s. Every `AttrId` is unique.
236    pub attr_id_generator: AttrIdGenerator,
237}
238
239impl ParseSess {
240    /// Used for testing.
241    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        // This is equivalent to `.iter().copied().enumerate()`, but that isn't possible for
334        // AppendOnlyVec, so we resort to this scheme.
335        self.proc_macro_quoted_spans.iter_enumerated()
336    }
337
338    pub fn dcx(&self) -> DiagCtxtHandle<'_> {
339        self.dcx.handle()
340    }
341}