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::node_id::NodeId;
5 use crate::lint::BufferedEarlyLint;
7 use rustc_errors::{Applicability, emitter::SilentEmitter, Handler, ColorConfig, DiagnosticBuilder};
8 use rustc_data_structures::fx::{FxHashSet, FxHashMap};
9 use rustc_data_structures::sync::{Lrc, Lock, Once};
10 use rustc_feature::UnstableFeatures;
11 use syntax_pos::{Symbol, Span, MultiSpan};
12 use syntax_pos::edition::Edition;
13 use syntax_pos::hygiene::ExpnId;
14 use syntax_pos::source_map::{SourceMap, FilePathMapping};
16 use std::path::PathBuf;
19 // Duplicated from syntax::ast for now
20 type CrateConfig = FxHashSet<(Symbol, Option<Symbol>)>;
22 /// Collected spans during parsing for places where a certain feature was
23 /// used and should be feature gated accordingly in `check_crate`.
25 pub struct GatedSpans {
26 pub spans: Lock<FxHashMap<Symbol, Vec<Span>>>,
30 /// Feature gate the given `span` under the given `feature`
31 /// which is same `Symbol` used in `active.rs`.
32 pub fn gate(&self, feature: Symbol, span: Span) {
40 /// Ungate the last span under the given `feature`.
41 /// Panics if the given `span` wasn't the last one.
43 /// Using this is discouraged unless you have a really good reason to.
44 pub fn ungate_last(&self, feature: Symbol, span: Span) {
45 let removed_span = self.spans
51 debug_assert_eq!(span, removed_span);
54 /// Is the provided `feature` gate ungated currently?
56 /// Using this is discouraged unless you have a really good reason to.
57 pub fn is_ungated(&self, feature: Symbol) -> bool {
61 .map_or(true, |spans| spans.is_empty())
64 /// Prepend the given set of `spans` onto the set in `self`.
65 pub fn merge(&self, mut spans: FxHashMap<Symbol, Vec<Span>>) {
66 let mut inner = self.spans.borrow_mut();
67 for (gate, mut gate_spans) in inner.drain() {
68 spans.entry(gate).or_default().append(&mut gate_spans);
74 /// Info about a parsing session.
75 pub struct ParseSess {
76 pub span_diagnostic: Handler,
77 pub unstable_features: UnstableFeatures,
78 pub config: CrateConfig,
80 pub missing_fragment_specifiers: Lock<FxHashSet<Span>>,
81 /// Places where raw identifiers were used. This is used for feature-gating raw identifiers.
82 pub raw_identifier_spans: Lock<Vec<Span>>,
83 /// Used to determine and report recursive module inclusions.
84 pub included_mod_stack: Lock<Vec<PathBuf>>,
85 source_map: Lrc<SourceMap>,
86 pub buffered_lints: Lock<Vec<BufferedEarlyLint>>,
87 /// Contains the spans of block expressions that could have been incomplete based on the
88 /// operation token that followed it, but that the parser cannot identify without further
90 pub ambiguous_block_expr_parse: Lock<FxHashMap<Span, Span>>,
91 pub injected_crate_name: Once<Symbol>,
92 pub gated_spans: GatedSpans,
93 /// The parser has reached `Eof` due to an unclosed brace. Used to silence unnecessary errors.
94 pub reached_eof: Lock<bool>,
98 pub fn new(file_path_mapping: FilePathMapping) -> Self {
99 let cm = Lrc::new(SourceMap::new(file_path_mapping));
100 let handler = Handler::with_tty_emitter(
106 ParseSess::with_span_handler(handler, cm)
109 pub fn with_span_handler(
111 source_map: Lrc<SourceMap>,
114 span_diagnostic: handler,
115 unstable_features: UnstableFeatures::from_environment(),
116 config: FxHashSet::default(),
117 edition: ExpnId::root().expn_data().edition,
118 missing_fragment_specifiers: Lock::new(FxHashSet::default()),
119 raw_identifier_spans: Lock::new(Vec::new()),
120 included_mod_stack: Lock::new(vec![]),
122 buffered_lints: Lock::new(vec![]),
123 ambiguous_block_expr_parse: Lock::new(FxHashMap::default()),
124 injected_crate_name: Once::new(),
125 gated_spans: GatedSpans::default(),
126 reached_eof: Lock::new(false),
130 pub fn with_silent_emitter() -> Self {
131 let cm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
132 let handler = Handler::with_emitter(false, None, Box::new(SilentEmitter));
133 ParseSess::with_span_handler(handler, cm)
137 pub fn source_map(&self) -> &SourceMap {
143 lint_id: &'static crate::lint::Lint,
144 span: impl Into<MultiSpan>,
148 self.buffered_lints.with_lock(|buffered_lints| {
149 buffered_lints.push(BufferedEarlyLint{
158 /// Extend an error with a suggestion to wrap an expression with parentheses to allow the
159 /// parser to continue parsing the following operation as part of the same expression.
160 pub fn expr_parentheses_needed(
162 err: &mut DiagnosticBuilder<'_>,
164 alt_snippet: Option<String>,
166 if let Some(snippet) = self.source_map().span_to_snippet(span).ok().or(alt_snippet) {
169 "parentheses are required to parse this as an expression",
170 format!("({})", snippet),
171 Applicability::MachineApplicable,