1 //! Contains `ParseSess` which holds state living beyond what one `Parser` might.
2 //! It also serves as an input to the parser itself.
4 use crate::ast::{CrateConfig, NodeId};
5 use crate::early_buffered_lints::{BufferedEarlyLint, BufferedEarlyLintId};
6 use crate::source_map::{SourceMap, FilePathMapping};
7 use crate::feature_gate::UnstableFeatures;
9 use errors::{Applicability, Handler, ColorConfig, DiagnosticBuilder};
10 use rustc_data_structures::fx::{FxHashSet, FxHashMap};
11 use rustc_data_structures::sync::{Lrc, Lock, Once};
12 use syntax_pos::{Symbol, Span, MultiSpan};
13 use syntax_pos::edition::Edition;
14 use syntax_pos::hygiene::ExpnId;
16 use std::path::PathBuf;
19 /// Collected spans during parsing for places where a certain feature was
20 /// used and should be feature gated accordingly in `check_crate`.
22 crate struct GatedSpans {
23 /// Spans collected for gating `let_chains`, e.g. `if a && let b = c {}`.
24 crate let_chains: Lock<Vec<Span>>,
25 /// Spans collected for gating `async_closure`, e.g. `async || ..`.
26 crate async_closure: Lock<Vec<Span>>,
27 /// Spans collected for gating `yield e?` expressions (`generators` gate).
28 crate yields: Lock<Vec<Span>>,
29 /// Spans collected for gating `or_patterns`, e.g. `Some(Foo | Bar)`.
30 crate or_patterns: Lock<Vec<Span>>,
31 /// Spans collected for gating `const_extern_fn`, e.g. `const extern fn foo`.
32 crate const_extern_fn: Lock<Vec<Span>>,
35 /// Info about a parsing session.
36 pub struct ParseSess {
37 pub span_diagnostic: Handler,
38 crate unstable_features: UnstableFeatures,
39 pub config: CrateConfig,
41 pub missing_fragment_specifiers: Lock<FxHashSet<Span>>,
42 /// Places where raw identifiers were used. This is used for feature-gating raw identifiers.
43 pub raw_identifier_spans: Lock<Vec<Span>>,
44 /// Used to determine and report recursive module inclusions.
45 pub(super) included_mod_stack: Lock<Vec<PathBuf>>,
46 source_map: Lrc<SourceMap>,
47 pub buffered_lints: Lock<Vec<BufferedEarlyLint>>,
48 /// Contains the spans of block expressions that could have been incomplete based on the
49 /// operation token that followed it, but that the parser cannot identify without further
51 pub ambiguous_block_expr_parse: Lock<FxHashMap<Span, Span>>,
52 pub injected_crate_name: Once<Symbol>,
53 crate gated_spans: GatedSpans,
57 pub fn new(file_path_mapping: FilePathMapping) -> Self {
58 let cm = Lrc::new(SourceMap::new(file_path_mapping));
59 let handler = Handler::with_tty_emitter(
65 ParseSess::with_span_handler(handler, cm)
68 pub fn with_span_handler(handler: Handler, source_map: Lrc<SourceMap>) -> Self {
70 span_diagnostic: handler,
71 unstable_features: UnstableFeatures::from_environment(),
72 config: FxHashSet::default(),
73 edition: ExpnId::root().expn_data().edition,
74 missing_fragment_specifiers: Lock::new(FxHashSet::default()),
75 raw_identifier_spans: Lock::new(Vec::new()),
76 included_mod_stack: Lock::new(vec![]),
78 buffered_lints: Lock::new(vec![]),
79 ambiguous_block_expr_parse: Lock::new(FxHashMap::default()),
80 injected_crate_name: Once::new(),
81 gated_spans: GatedSpans::default(),
86 pub fn source_map(&self) -> &SourceMap {
92 lint_id: BufferedEarlyLintId,
93 span: impl Into<MultiSpan>,
97 self.buffered_lints.with_lock(|buffered_lints| {
98 buffered_lints.push(BufferedEarlyLint{
107 /// Extend an error with a suggestion to wrap an expression with parentheses to allow the
108 /// parser to continue parsing the following operation as part of the same expression.
109 pub fn expr_parentheses_needed(
111 err: &mut DiagnosticBuilder<'_>,
113 alt_snippet: Option<String>,
115 if let Some(snippet) = self.source_map().span_to_snippet(span).ok().or(alt_snippet) {
118 "parentheses are required to parse this as an expression",
119 format!("({})", snippet),
120 Applicability::MachineApplicable,