diff options
Diffstat (limited to 'src/libsyntax')
| -rw-r--r-- | src/libsyntax/Cargo.toml | 1 | ||||
| -rw-r--r-- | src/libsyntax/ast.rs | 48 | ||||
| -rw-r--r-- | src/libsyntax/early_buffered_lints.rs | 40 | ||||
| -rw-r--r-- | src/libsyntax/lib.rs | 2 | ||||
| -rw-r--r-- | src/libsyntax/sess.rs | 172 |
5 files changed, 25 insertions, 238 deletions
diff --git a/src/libsyntax/Cargo.toml b/src/libsyntax/Cargo.toml index 085c1760c80..8a00bcbfe17 100644 --- a/src/libsyntax/Cargo.toml +++ b/src/libsyntax/Cargo.toml @@ -24,3 +24,4 @@ rustc_lexer = { path = "../librustc_lexer" } rustc_macros = { path = "../librustc_macros" } smallvec = { version = "1.0", features = ["union", "may_dangle"] } rustc_error_codes = { path = "../librustc_error_codes" } +rustc_session = { path = "../librustc_session" } diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 8018e005b12..75ddf10514d 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -30,9 +30,8 @@ use crate::token::{self, DelimToken}; use crate::tokenstream::{TokenStream, TokenTree, DelimSpan}; use syntax_pos::symbol::{kw, sym, Symbol}; -use syntax_pos::{Span, DUMMY_SP, ExpnId}; +use syntax_pos::{Span, DUMMY_SP}; -use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::sync::Lrc; use rustc_data_structures::thin_vec::ThinVec; @@ -268,46 +267,7 @@ impl ParenthesizedArgs { } } -// hack to ensure that we don't try to access the private parts of `NodeId` in this module -mod node_id_inner { - use rustc_index::vec::Idx; - rustc_index::newtype_index! { - pub struct NodeId { - ENCODABLE = custom - DEBUG_FORMAT = "NodeId({})" - } - } -} - -pub use node_id_inner::NodeId; - -impl NodeId { - pub fn placeholder_from_expn_id(expn_id: ExpnId) -> Self { - NodeId::from_u32(expn_id.as_u32()) - } - - pub fn placeholder_to_expn_id(self) -> ExpnId { - ExpnId::from_u32(self.as_u32()) - } -} - -impl fmt::Display for NodeId { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt::Display::fmt(&self.as_u32(), f) - } -} - -impl rustc_serialize::UseSpecializedEncodable for NodeId { - fn default_encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { - s.emit_u32(self.as_u32()) - } -} - -impl rustc_serialize::UseSpecializedDecodable for NodeId { - fn default_decode<D: Decoder>(d: &mut D) -> Result<NodeId, D::Error> { - d.read_u32().map(NodeId::from_u32) - } -} +pub use rustc_session::node_id::NodeId; /// `NodeId` used to represent the root of the crate. pub const CRATE_NODE_ID: NodeId = NodeId::from_u32_const(0); @@ -470,9 +430,7 @@ pub struct WhereEqPredicate { pub rhs_ty: P<Ty>, } -/// The set of `MetaItem`s that define the compilation environment of the crate, -/// used to drive conditional compilation. -pub type CrateConfig = FxHashSet<(Name, Option<Symbol>)>; +pub use rustc_session::parse::CrateConfig; #[derive(Clone, RustcEncodable, RustcDecodable, Debug)] pub struct Crate { diff --git a/src/libsyntax/early_buffered_lints.rs b/src/libsyntax/early_buffered_lints.rs index 5cc953b9066..2c32894a23b 100644 --- a/src/libsyntax/early_buffered_lints.rs +++ b/src/libsyntax/early_buffered_lints.rs @@ -3,28 +3,28 @@ //! Since we cannot have a dependency on `librustc`, we implement some types here that are somewhat //! redundant. Later, these types can be converted to types for use by the rest of the compiler. -use crate::ast::NodeId; -use syntax_pos::MultiSpan; +use rustc_session::lint::FutureIncompatibleInfo; +use rustc_session::declare_lint; +pub use rustc_session::lint::BufferedEarlyLint; -/// Since we cannot import `LintId`s from `rustc::lint`, we define some Ids here which can later be -/// passed to `rustc::lint::Lint::from_parser_lint_id` to get a `rustc::lint::Lint`. -pub enum BufferedEarlyLintId { - IllFormedAttributeInput, - MetaVariableMisuse, - IncompleteInclude, +declare_lint! { + pub ILL_FORMED_ATTRIBUTE_INPUT, + Deny, + "ill-formed attribute inputs that were previously accepted and used in practice", + @future_incompatible = FutureIncompatibleInfo { + reference: "issue #57571 <https://github.com/rust-lang/rust/issues/57571>", + edition: None, + }; } -/// Stores buffered lint info which can later be passed to `librustc`. -pub struct BufferedEarlyLint { - /// The span of code that we are linting on. - pub span: MultiSpan, - - /// The lint message. - pub msg: String, - - /// The `NodeId` of the AST node that generated the lint. - pub id: NodeId, +declare_lint! { + pub META_VARIABLE_MISUSE, + Allow, + "possible meta-variable misuse at macro definition" +} - /// A lint Id that can be passed to `rustc::lint::Lint::from_parser_lint_id`. - pub lint_id: BufferedEarlyLintId, +declare_lint! { + pub INCOMPLETE_INCLUDE, + Deny, + "trailing content in included file" } diff --git a/src/libsyntax/lib.rs b/src/libsyntax/lib.rs index 3dcdd4db637..a94742634cf 100644 --- a/src/libsyntax/lib.rs +++ b/src/libsyntax/lib.rs @@ -102,7 +102,7 @@ pub mod ptr; pub mod show_span; pub use syntax_pos::edition; pub use syntax_pos::symbol; -pub mod sess; +pub use rustc_session::parse as sess; pub mod token; pub mod tokenstream; pub mod visit; diff --git a/src/libsyntax/sess.rs b/src/libsyntax/sess.rs deleted file mode 100644 index aa9217c1b69..00000000000 --- a/src/libsyntax/sess.rs +++ /dev/null @@ -1,172 +0,0 @@ -//! Contains `ParseSess` which holds state living beyond what one `Parser` might. -//! It also serves as an input to the parser itself. - -use crate::ast::{CrateConfig, NodeId}; -use crate::early_buffered_lints::{BufferedEarlyLint, BufferedEarlyLintId}; - -use errors::{Applicability, emitter::SilentEmitter, Handler, ColorConfig, DiagnosticBuilder}; -use rustc_data_structures::fx::{FxHashSet, FxHashMap}; -use rustc_data_structures::sync::{Lrc, Lock, Once}; -use rustc_feature::UnstableFeatures; -use syntax_pos::{Symbol, Span, MultiSpan}; -use syntax_pos::edition::Edition; -use syntax_pos::hygiene::ExpnId; -use syntax_pos::source_map::{SourceMap, FilePathMapping}; - -use std::path::PathBuf; -use std::str; - -/// Collected spans during parsing for places where a certain feature was -/// used and should be feature gated accordingly in `check_crate`. -#[derive(Default)] -pub struct GatedSpans { - pub spans: Lock<FxHashMap<Symbol, Vec<Span>>>, -} - -impl GatedSpans { - /// Feature gate the given `span` under the given `feature` - /// which is same `Symbol` used in `active.rs`. - pub fn gate(&self, feature: Symbol, span: Span) { - self.spans - .borrow_mut() - .entry(feature) - .or_default() - .push(span); - } - - /// Ungate the last span under the given `feature`. - /// Panics if the given `span` wasn't the last one. - /// - /// Using this is discouraged unless you have a really good reason to. - pub fn ungate_last(&self, feature: Symbol, span: Span) { - let removed_span = self.spans - .borrow_mut() - .entry(feature) - .or_default() - .pop() - .unwrap(); - debug_assert_eq!(span, removed_span); - } - - /// Is the provided `feature` gate ungated currently? - /// - /// Using this is discouraged unless you have a really good reason to. - pub fn is_ungated(&self, feature: Symbol) -> bool { - self.spans - .borrow() - .get(&feature) - .map_or(true, |spans| spans.is_empty()) - } - - /// Prepend the given set of `spans` onto the set in `self`. - pub fn merge(&self, mut spans: FxHashMap<Symbol, Vec<Span>>) { - let mut inner = self.spans.borrow_mut(); - for (gate, mut gate_spans) in inner.drain() { - spans.entry(gate).or_default().append(&mut gate_spans); - } - *inner = spans; - } -} - -/// Info about a parsing session. -pub struct ParseSess { - pub span_diagnostic: Handler, - pub unstable_features: UnstableFeatures, - pub config: CrateConfig, - pub edition: Edition, - pub missing_fragment_specifiers: Lock<FxHashSet<Span>>, - /// Places where raw identifiers were used. This is used for feature-gating raw identifiers. - pub raw_identifier_spans: Lock<Vec<Span>>, - /// Used to determine and report recursive module inclusions. - pub included_mod_stack: Lock<Vec<PathBuf>>, - source_map: Lrc<SourceMap>, - pub buffered_lints: Lock<Vec<BufferedEarlyLint>>, - /// Contains the spans of block expressions that could have been incomplete based on the - /// operation token that followed it, but that the parser cannot identify without further - /// analysis. - pub ambiguous_block_expr_parse: Lock<FxHashMap<Span, Span>>, - pub injected_crate_name: Once<Symbol>, - pub gated_spans: GatedSpans, - /// The parser has reached `Eof` due to an unclosed brace. Used to silence unnecessary errors. - pub reached_eof: Lock<bool>, -} - -impl ParseSess { - pub fn new(file_path_mapping: FilePathMapping) -> Self { - let cm = Lrc::new(SourceMap::new(file_path_mapping)); - let handler = Handler::with_tty_emitter( - ColorConfig::Auto, - true, - None, - Some(cm.clone()), - ); - ParseSess::with_span_handler(handler, cm) - } - - pub fn with_span_handler( - handler: Handler, - source_map: Lrc<SourceMap>, - ) -> Self { - Self { - span_diagnostic: handler, - unstable_features: UnstableFeatures::from_environment(), - config: FxHashSet::default(), - edition: ExpnId::root().expn_data().edition, - missing_fragment_specifiers: Lock::new(FxHashSet::default()), - raw_identifier_spans: Lock::new(Vec::new()), - included_mod_stack: Lock::new(vec![]), - source_map, - buffered_lints: Lock::new(vec![]), - ambiguous_block_expr_parse: Lock::new(FxHashMap::default()), - injected_crate_name: Once::new(), - gated_spans: GatedSpans::default(), - reached_eof: Lock::new(false), - } - } - - pub fn with_silent_emitter() -> Self { - let cm = Lrc::new(SourceMap::new(FilePathMapping::empty())); - let handler = Handler::with_emitter(false, None, Box::new(SilentEmitter)); - ParseSess::with_span_handler(handler, cm) - } - - #[inline] - pub fn source_map(&self) -> &SourceMap { - &self.source_map - } - - pub fn buffer_lint( - &self, - lint_id: BufferedEarlyLintId, - span: impl Into<MultiSpan>, - id: NodeId, - msg: &str, - ) { - self.buffered_lints.with_lock(|buffered_lints| { - buffered_lints.push(BufferedEarlyLint{ - span: span.into(), - id, - msg: msg.into(), - lint_id, - }); - }); - } - - /// Extend an error with a suggestion to wrap an expression with parentheses to allow the - /// parser to continue parsing the following operation as part of the same expression. - pub fn expr_parentheses_needed( - &self, - err: &mut DiagnosticBuilder<'_>, - span: Span, - alt_snippet: Option<String>, - ) { - if let Some(snippet) = self.source_map().span_to_snippet(span).ok().or(alt_snippet) { - err.span_suggestion( - span, - "parentheses are required to parse this as an expression", - format!("({})", snippet), - Applicability::MachineApplicable, - ); - } - } -} |
