diff options
| author | Mazdak Farrokhzad <twingoow@gmail.com> | 2019-10-10 10:26:10 +0200 |
|---|---|---|
| committer | Mazdak Farrokhzad <twingoow@gmail.com> | 2019-11-10 02:46:17 +0100 |
| commit | be023ebe850261c6bb202a02a686827d821c3697 (patch) | |
| tree | 4595cee969fbdb83ffc536ab940e15be2503e454 | |
| parent | 5011ec7fedffe34d943654ffb4308875fc5ec8f3 (diff) | |
| download | rust-be023ebe850261c6bb202a02a686827d821c3697.tar.gz rust-be023ebe850261c6bb202a02a686827d821c3697.zip | |
move config.rs to libsyntax_expand
31 files changed, 259 insertions, 175 deletions
diff --git a/src/librustc/session/mod.rs b/src/librustc/session/mod.rs index 403b32df20e..9d702e7d6bf 100644 --- a/src/librustc/session/mod.rs +++ b/src/librustc/session/mod.rs @@ -27,7 +27,7 @@ use syntax::expand::allocator::AllocatorKind; use syntax::feature_gate::{self, AttributeType}; use syntax::json::JsonEmitter; use syntax::source_map; -use syntax::sess::ParseSess; +use syntax::sess::{ParseSess, ProcessCfgMod}; use syntax::symbol::Symbol; use syntax_pos::{MultiSpan, Span}; use crate::util::profiling::{SelfProfiler, SelfProfilerRef}; @@ -952,6 +952,7 @@ pub fn build_session( sopts: config::Options, local_crate_source_file: Option<PathBuf>, registry: errors::registry::Registry, + process_cfg_mod: ProcessCfgMod, ) -> Session { let file_path_mapping = sopts.file_path_mapping(); @@ -962,6 +963,7 @@ pub fn build_session( Lrc::new(source_map::SourceMap::new(file_path_mapping)), DiagnosticOutput::Default, Default::default(), + process_cfg_mod, ) } @@ -1040,6 +1042,7 @@ pub fn build_session_with_source_map( source_map: Lrc<source_map::SourceMap>, diagnostics_output: DiagnosticOutput, lint_caps: FxHashMap<lint::LintId, lint::Level>, + process_cfg_mod: ProcessCfgMod, ) -> Session { // FIXME: This is not general enough to make the warning lint completely override // normal diagnostic warnings, since the warning lint can also be denied and changed @@ -1080,7 +1083,14 @@ pub fn build_session_with_source_map( }, ); - build_session_(sopts, local_crate_source_file, diagnostic_handler, source_map, lint_caps) + build_session_( + sopts, + local_crate_source_file, + diagnostic_handler, + source_map, + lint_caps, + process_cfg_mod, + ) } fn build_session_( @@ -1089,6 +1099,7 @@ fn build_session_( span_diagnostic: errors::Handler, source_map: Lrc<source_map::SourceMap>, driver_lint_caps: FxHashMap<lint::LintId, lint::Level>, + process_cfg_mod: ProcessCfgMod, ) -> Session { let self_profiler = if let SwitchWithOptPath::Enabled(ref d) = sopts.debugging_opts.self_profile { @@ -1127,6 +1138,7 @@ fn build_session_( let parse_sess = ParseSess::with_span_handler( span_diagnostic, source_map, + process_cfg_mod, ); let sysroot = match &sopts.maybe_sysroot { Some(sysroot) => sysroot.clone(), diff --git a/src/librustc_interface/interface.rs b/src/librustc_interface/interface.rs index 034e861b212..61f30392e06 100644 --- a/src/librustc_interface/interface.rs +++ b/src/librustc_interface/interface.rs @@ -14,11 +14,12 @@ use rustc_data_structures::fx::{FxHashSet, FxHashMap}; use std::path::PathBuf; use std::result; use std::sync::{Arc, Mutex}; -use syntax::{self, parse}; use syntax::ast::{self, MetaItemKind}; +use syntax::parse::new_parser_from_source_str; use syntax::token; use syntax::source_map::{FileName, FileLoader, SourceMap}; use syntax::sess::ParseSess; +use syntax_expand::config::process_configure_mod; use syntax_pos::edition; pub type Result<T> = result::Result<T, ErrorReported>; @@ -64,9 +65,9 @@ impl Compiler { pub fn parse_cfgspecs(cfgspecs: Vec<String>) -> FxHashSet<(String, Option<String>)> { syntax::with_default_globals(move || { let cfg = cfgspecs.into_iter().map(|s| { - let sess = ParseSess::with_silent_emitter(); + let sess = ParseSess::with_silent_emitter(process_configure_mod); let filename = FileName::cfg_spec_source_code(&s); - let mut parser = parse::new_parser_from_source_str(&sess, filename, s.to_string()); + let mut parser = new_parser_from_source_str(&sess, filename, s.to_string()); macro_rules! error {($reason: expr) => { early_error(ErrorOutputType::default(), diff --git a/src/librustc_interface/passes.rs b/src/librustc_interface/passes.rs index ce34caee6fa..c874e94124d 100644 --- a/src/librustc_interface/passes.rs +++ b/src/librustc_interface/passes.rs @@ -181,7 +181,7 @@ pub fn register_plugins<'a>( ) }); - let (krate, features) = syntax::config::features( + let (krate, features) = syntax_expand::config::features( krate, &sess.parse_sess, sess.edition(), diff --git a/src/librustc_interface/tests.rs b/src/librustc_interface/tests.rs index 7a57605da58..8c1dac21576 100644 --- a/src/librustc_interface/tests.rs +++ b/src/librustc_interface/tests.rs @@ -8,7 +8,7 @@ use rustc::session::config::{build_configuration, build_session_options, to_crat use rustc::session::config::{LtoCli, LinkerPluginLto, SwitchWithOptPath, ExternEntry}; use rustc::session::config::{Externs, OutputType, OutputTypes, SymbolManglingVersion}; use rustc::session::config::{rustc_optgroups, Options, ErrorOutputType, Passes}; -use rustc::session::build_session; +use rustc::session::{build_session, Session}; use rustc::session::search_paths::SearchPath; use std::collections::{BTreeMap, BTreeSet}; use std::iter::FromIterator; @@ -17,16 +17,23 @@ use rustc_target::spec::{MergeFunctions, PanicStrategy, RelroLevel}; use syntax::symbol::sym; use syntax::edition::{Edition, DEFAULT_EDITION}; use syntax; +use syntax_expand::config::process_configure_mod; use rustc_data_structures::fx::FxHashSet; use rustc_errors::{ColorConfig, emitter::HumanReadableErrorType, registry}; -pub fn build_session_options_and_crate_config( - matches: &getopts::Matches, -) -> (Options, FxHashSet<(String, Option<String>)>) { - ( - build_session_options(matches), - parse_cfgspecs(matches.opt_strs("cfg")), - ) +type CfgSpecs = FxHashSet<(String, Option<String>)>; + +fn build_session_options_and_crate_config(matches: getopts::Matches) -> (Options, CfgSpecs) { + let sessopts = build_session_options(&matches); + let cfg = parse_cfgspecs(matches.opt_strs("cfg")); + (sessopts, cfg) +} + +fn mk_session(matches: getopts::Matches) -> (Session, CfgSpecs) { + let registry = registry::Registry::new(&[]); + let (sessopts, cfg) = build_session_options_and_crate_config(matches); + let sess = build_session(sessopts, None, registry, process_configure_mod); + (sess, cfg) } fn new_public_extern_entry<S, I>(locations: I) -> ExternEntry @@ -59,31 +66,19 @@ fn mk_map<K: Ord, V>(entries: Vec<(K, V)>) -> BTreeMap<K, V> { #[test] fn test_switch_implies_cfg_test() { syntax::with_default_globals(|| { - let matches = &match optgroups().parse(&["--test".to_string()]) { - Ok(m) => m, - Err(f) => panic!("test_switch_implies_cfg_test: {}", f), - }; - let registry = registry::Registry::new(&[]); - let (sessopts, cfg) = build_session_options_and_crate_config(matches); - let sess = build_session(sessopts, None, registry); + let matches = optgroups().parse(&["--test".to_string()]).unwrap(); + let (sess, cfg) = mk_session(matches); let cfg = build_configuration(&sess, to_crate_config(cfg)); assert!(cfg.contains(&(sym::test, None))); }); } -// When the user supplies --test and --cfg test, don't implicitly add -// another --cfg test +// When the user supplies --test and --cfg test, don't implicitly add another --cfg test #[test] fn test_switch_implies_cfg_test_unless_cfg_test() { syntax::with_default_globals(|| { - let matches = &match optgroups().parse(&["--test".to_string(), - "--cfg=test".to_string()]) { - Ok(m) => m, - Err(f) => panic!("test_switch_implies_cfg_test_unless_cfg_test: {}", f), - }; - let registry = registry::Registry::new(&[]); - let (sessopts, cfg) = build_session_options_and_crate_config(matches); - let sess = build_session(sessopts, None, registry); + let matches = optgroups().parse(&["--test".to_string(), "--cfg=test".to_string()]).unwrap(); + let (sess, cfg) = mk_session(matches); let cfg = build_configuration(&sess, to_crate_config(cfg)); let mut test_items = cfg.iter().filter(|&&(name, _)| name == sym::test); assert!(test_items.next().is_some()); @@ -95,9 +90,7 @@ fn test_switch_implies_cfg_test_unless_cfg_test() { fn test_can_print_warnings() { syntax::with_default_globals(|| { let matches = optgroups().parse(&["-Awarnings".to_string()]).unwrap(); - let registry = registry::Registry::new(&[]); - let (sessopts, _) = build_session_options_and_crate_config(&matches); - let sess = build_session(sessopts, None, registry); + let (sess, _) = mk_session(matches); assert!(!sess.diagnostic().can_emit_warnings()); }); @@ -105,17 +98,13 @@ fn test_can_print_warnings() { let matches = optgroups() .parse(&["-Awarnings".to_string(), "-Dwarnings".to_string()]) .unwrap(); - let registry = registry::Registry::new(&[]); - let (sessopts, _) = build_session_options_and_crate_config(&matches); - let sess = build_session(sessopts, None, registry); + let (sess, _) = mk_session(matches); assert!(sess.diagnostic().can_emit_warnings()); }); syntax::with_default_globals(|| { let matches = optgroups().parse(&["-Adead_code".to_string()]).unwrap(); - let registry = registry::Registry::new(&[]); - let (sessopts, _) = build_session_options_and_crate_config(&matches); - let sess = build_session(sessopts, None, registry); + let (sess, _) = mk_session(matches); assert!(sess.diagnostic().can_emit_warnings()); }); } @@ -704,6 +693,6 @@ fn test_edition_parsing() { let matches = optgroups() .parse(&["--edition=2018".to_string()]) .unwrap(); - let (sessopts, _) = build_session_options_and_crate_config(&matches); + let (sessopts, _) = build_session_options_and_crate_config(matches); assert!(sessopts.edition == Edition::Edition2018) } diff --git a/src/librustc_interface/util.rs b/src/librustc_interface/util.rs index a74ea4bca39..c02e5b9ae28 100644 --- a/src/librustc_interface/util.rs +++ b/src/librustc_interface/util.rs @@ -36,6 +36,7 @@ use syntax::util::lev_distance::find_best_match_for_name; use syntax::source_map::{FileLoader, RealFileLoader, SourceMap}; use syntax::symbol::{Symbol, sym}; use syntax::{self, ast, attr}; +use syntax_expand::config::process_configure_mod; use syntax_pos::edition::Edition; #[cfg(not(parallel_compiler))] use std::{thread, panic}; @@ -103,6 +104,7 @@ pub fn create_session( source_map.clone(), diagnostic_output, lint_caps, + process_configure_mod, ); let codegen_backend = get_codegen_backend(&sess); diff --git a/src/librustdoc/html/highlight.rs b/src/librustdoc/html/highlight.rs index 4bd72f7e61c..71a44c0ebdb 100644 --- a/src/librustdoc/html/highlight.rs +++ b/src/librustdoc/html/highlight.rs @@ -16,6 +16,7 @@ use syntax::parse::lexer; use syntax::token::{self, Token}; use syntax::sess::ParseSess; use syntax::symbol::{kw, sym}; +use syntax_expand::config::process_configure_mod; use syntax_pos::{Span, FileName}; /// Highlights `src`, returning the HTML output. @@ -33,7 +34,7 @@ pub fn render_with_highlighting( class, tooltip).unwrap(); } - let sess = ParseSess::with_silent_emitter(); + let sess = ParseSess::with_silent_emitter(process_configure_mod); let fm = sess.source_map().new_source_file( FileName::Custom(String::from("rustdoc-highlighting")), src.to_owned(), diff --git a/src/librustdoc/passes/check_code_block_syntax.rs b/src/librustdoc/passes/check_code_block_syntax.rs index 4603e77b0fd..405f3a70e0e 100644 --- a/src/librustdoc/passes/check_code_block_syntax.rs +++ b/src/librustdoc/passes/check_code_block_syntax.rs @@ -3,6 +3,7 @@ use syntax::parse::lexer::{StringReader as Lexer}; use syntax::token; use syntax::sess::ParseSess; use syntax::source_map::FilePathMapping; +use syntax_expand::config::process_configure_mod; use syntax_pos::{InnerSpan, FileName}; use crate::clean; @@ -27,7 +28,7 @@ struct SyntaxChecker<'a, 'tcx> { impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> { fn check_rust_syntax(&self, item: &clean::Item, dox: &str, code_block: RustCodeBlock) { - let sess = ParseSess::new(FilePathMapping::empty()); + let sess = ParseSess::new(FilePathMapping::empty(), process_configure_mod); let source_file = sess.source_map().new_source_file( FileName::Custom(String::from("doctest")), dox[code_block.code].to_owned(), diff --git a/src/librustdoc/test.rs b/src/librustdoc/test.rs index 07dc1e4e915..e4cf1bb0bb9 100644 --- a/src/librustdoc/test.rs +++ b/src/librustdoc/test.rs @@ -17,6 +17,7 @@ use std::path::PathBuf; use std::process::{self, Command, Stdio}; use std::str; use syntax::symbol::sym; +use syntax_expand::config::process_configure_mod; use syntax_pos::{BytePos, DUMMY_SP, Pos, Span, FileName}; use tempfile::Builder as TempFileBuilder; use testing; @@ -411,7 +412,7 @@ pub fn make_test(s: &str, let emitter = EmitterWriter::new(box io::sink(), None, false, false, false, None, false); // FIXME(misdreavus): pass `-Z treat-err-as-bug` to the doctest parser let handler = Handler::with_emitter(false, None, box emitter); - let sess = ParseSess::with_span_handler(handler, cm); + let sess = ParseSess::with_span_handler(handler, cm, process_configure_mod); let mut found_main = false; let mut found_extern_crate = cratename.is_none(); diff --git a/src/libsyntax/attr/mod.rs b/src/libsyntax/attr/mod.rs index 2c011b06470..d458549e298 100644 --- a/src/libsyntax/attr/mod.rs +++ b/src/libsyntax/attr/mod.rs @@ -363,8 +363,12 @@ crate fn mk_attr_id() -> AttrId { } pub fn mk_attr(style: AttrStyle, path: Path, tokens: TokenStream, span: Span) -> Attribute { + mk_attr_from_item(style, AttrItem { path, tokens }, span) +} + +pub fn mk_attr_from_item(style: AttrStyle, item: AttrItem, span: Span) -> Attribute { Attribute { - kind: AttrKind::Normal(AttrItem { path, tokens }), + kind: AttrKind::Normal(item), id: mk_attr_id(), style, span, diff --git a/src/libsyntax/json/tests.rs b/src/libsyntax/json/tests.rs index eb0d9ef3947..1edefd5bc4b 100644 --- a/src/libsyntax/json/tests.rs +++ b/src/libsyntax/json/tests.rs @@ -2,7 +2,6 @@ use super::*; use crate::json::JsonEmitter; use crate::source_map::{FilePathMapping, SourceMap}; -use crate::tests::Shared; use crate::with_default_globals; use errors::emitter::{ColorConfig, HumanReadableErrorType}; @@ -27,6 +26,20 @@ struct SpanTestData { pub column_end: u32, } +struct Shared<T> { + data: Arc<Mutex<T>>, +} + +impl<T: Write> Write for Shared<T> { + fn write(&mut self, buf: &[u8]) -> io::Result<usize> { + self.data.lock().unwrap().write(buf) + } + + fn flush(&mut self) -> io::Result<()> { + self.data.lock().unwrap().flush() + } +} + /// Test the span yields correct positions in JSON. fn test_positions(code: &str, span: (u32, u32), expected_output: SpanTestData) { let expected_output = TestData { spans: vec![expected_output] }; diff --git a/src/libsyntax/lib.rs b/src/libsyntax/lib.rs index 1b17de529c4..7fecc87a18f 100644 --- a/src/libsyntax/lib.rs +++ b/src/libsyntax/lib.rs @@ -26,9 +26,6 @@ pub use rustc_data_structures::thin_vec::ThinVec; use ast::AttrId; use syntax_pos::edition::Edition; -#[cfg(test)] -mod tests; - pub const MACRO_ARGUMENTS: Option<&'static str> = Some("macro arguments"); #[macro_export] @@ -100,7 +97,6 @@ pub mod ast; pub mod attr; pub mod expand; pub mod source_map; -#[macro_use] pub mod config; pub mod entry; pub mod feature_gate; pub mod mut_visit; diff --git a/src/libsyntax/mut_visit.rs b/src/libsyntax/mut_visit.rs index 7696ea48f93..376323a83ea 100644 --- a/src/libsyntax/mut_visit.rs +++ b/src/libsyntax/mut_visit.rs @@ -22,9 +22,6 @@ use rustc_data_structures::sync::Lrc; use std::ops::DerefMut; use std::{panic, process, ptr}; -#[cfg(test)] -mod tests; - pub trait ExpectOne<A: Array> { fn expect_one(self, err: &'static str) -> A::Item; } diff --git a/src/libsyntax/parse/lexer/mod.rs b/src/libsyntax/parse/lexer/mod.rs index b1b7b08c78a..f2d5ff3440e 100644 --- a/src/libsyntax/parse/lexer/mod.rs +++ b/src/libsyntax/parse/lexer/mod.rs @@ -13,9 +13,6 @@ use std::convert::TryInto; use rustc_data_structures::sync::Lrc; use log::debug; -#[cfg(test)] -mod tests; - mod tokentrees; mod unicode_chars; mod unescape_error_reporting; @@ -35,7 +32,8 @@ pub struct StringReader<'a> { /// Initial position, read-only. start_pos: BytePos, /// The absolute offset within the source_map of the current character. - pos: BytePos, + // FIXME(#64197): `pub` is needed by tests for now. + pub pos: BytePos, /// Stop reading src at this index. end_src_index: usize, /// Source text to tokenize. diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs index 9155cbe5dd8..af3f8ecb45a 100644 --- a/src/libsyntax/parse/mod.rs +++ b/src/libsyntax/parse/mod.rs @@ -17,8 +17,7 @@ use std::str; use log::info; -#[cfg(test)] -mod tests; +pub const MACRO_ARGUMENTS: Option<&'static str> = Some("macro arguments"); #[macro_use] pub mod parser; diff --git a/src/libsyntax/parse/parser/attr.rs b/src/libsyntax/parse/parser/attr.rs index 31f0a02a483..0f9e573af82 100644 --- a/src/libsyntax/parse/parser/attr.rs +++ b/src/libsyntax/parse/parser/attr.rs @@ -268,7 +268,7 @@ impl<'a> Parser<'a> { } /// Parses `cfg_attr(pred, attr_item_list)` where `attr_item_list` is comma-delimited. - crate fn parse_cfg_attr(&mut self) -> PResult<'a, (ast::MetaItem, Vec<(ast::AttrItem, Span)>)> { + pub fn parse_cfg_attr(&mut self) -> PResult<'a, (ast::MetaItem, Vec<(ast::AttrItem, Span)>)> { self.expect(&token::OpenDelim(token::Paren))?; let cfg_predicate = self.parse_meta_item()?; diff --git a/src/libsyntax/parse/parser/module.rs b/src/libsyntax/parse/parser/module.rs index 3e5974c2eee..ad72b3a1dea 100644 --- a/src/libsyntax/parse/parser/module.rs +++ b/src/libsyntax/parse/parser/module.rs @@ -7,8 +7,8 @@ use crate::ast::{self, Ident, Attribute, ItemKind, Mod, Crate}; use crate::parse::{new_sub_parser_from_file, DirectoryOwnership}; use crate::token::{self, TokenKind}; use crate::source_map::{SourceMap, Span, DUMMY_SP, FileName}; -use crate::symbol::sym; +use syntax_pos::symbol::sym; use errors::PResult; use std::path::{self, Path, PathBuf}; @@ -39,17 +39,12 @@ impl<'a> Parser<'a> { /// Parses a `mod <foo> { ... }` or `mod <foo>;` item. pub(super) fn parse_item_mod(&mut self, outer_attrs: &[Attribute]) -> PResult<'a, ItemInfo> { - let (in_cfg, outer_attrs) = { - // FIXME(Centril): This results in a cycle between config and parsing. - // Consider using dynamic dispatch via `self.sess` to disentangle the knot. - let mut strip_unconfigured = crate::config::StripUnconfigured { - sess: self.sess, - features: None, // Don't perform gated feature checking. - }; - let mut outer_attrs = outer_attrs.to_owned(); - strip_unconfigured.process_cfg_attrs(&mut outer_attrs); - (!self.cfg_mods || strip_unconfigured.in_cfg(&outer_attrs), outer_attrs) - }; + // HACK(Centril): See documentation on `ParseSess::process_cfg_mod`. + let (in_cfg, outer_attrs) = (self.sess.process_cfg_mod)( + self.sess, + self.cfg_mods, + outer_attrs, + ); let id_span = self.token.span; let id = self.parse_ident()?; diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 2203e8d9d06..04f096200b8 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -939,8 +939,11 @@ impl<'a> State<'a> { self.commasep_cmnt(b, exprs, |s, e| s.print_expr(e), |e| e.span) } - crate fn print_mod(&mut self, _mod: &ast::Mod, - attrs: &[ast::Attribute]) { + pub fn print_mod( + &mut self, + _mod: &ast::Mod, + attrs: &[ast::Attribute], + ) { self.print_inner_attributes(attrs); for item in &_mod.items { self.print_item(item); diff --git a/src/libsyntax/sess.rs b/src/libsyntax/sess.rs index faad3e4af1e..5e96b27c970 100644 --- a/src/libsyntax/sess.rs +++ b/src/libsyntax/sess.rs @@ -1,7 +1,7 @@ //! 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::ast::{CrateConfig, NodeId, Attribute}; use crate::early_buffered_lints::{BufferedEarlyLint, BufferedEarlyLintId}; use crate::source_map::{SourceMap, FilePathMapping}; use crate::feature_gate::UnstableFeatures; @@ -89,10 +89,22 @@ pub struct ParseSess { pub gated_spans: GatedSpans, /// The parser has reached `Eof` due to an unclosed brace. Used to silence unnecessary errors. pub reached_eof: Lock<bool>, + /// Process the potential `cfg` attributes on a module. + /// Also determine if the module should be included in this configuration. + /// + /// HACK(Centril): This is used to break a cyclic dependency between + /// the parser and cfg-stripping as defined in `syntax_expand::config`. + /// The dependency edge from the parser comes from `parse_item_mod`. + /// A principled solution to this hack would be to implement [#64197]. + /// + /// [#64197]: https://github.com/rust-lang/rust/issues/64197 + pub process_cfg_mod: ProcessCfgMod, } +pub type ProcessCfgMod = fn(&ParseSess, bool, &[Attribute]) -> (bool, Vec<Attribute>); + impl ParseSess { - pub fn new(file_path_mapping: FilePathMapping) -> Self { + pub fn new(file_path_mapping: FilePathMapping, process_cfg_mod: ProcessCfgMod) -> Self { let cm = Lrc::new(SourceMap::new(file_path_mapping)); let handler = Handler::with_tty_emitter( ColorConfig::Auto, @@ -100,12 +112,17 @@ impl ParseSess { None, Some(cm.clone()), ); - ParseSess::with_span_handler(handler, cm) + ParseSess::with_span_handler(handler, cm, process_cfg_mod) } - pub fn with_span_handler(handler: Handler, source_map: Lrc<SourceMap>) -> Self { + pub fn with_span_handler( + handler: Handler, + source_map: Lrc<SourceMap>, + process_cfg_mod: ProcessCfgMod, + ) -> Self { Self { span_diagnostic: handler, + process_cfg_mod, unstable_features: UnstableFeatures::from_environment(), config: FxHashSet::default(), edition: ExpnId::root().expn_data().edition, @@ -121,10 +138,10 @@ impl ParseSess { } } - pub fn with_silent_emitter() -> Self { + pub fn with_silent_emitter(process_cfg_mod: ProcessCfgMod) -> 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) + ParseSess::with_span_handler(handler, cm, process_cfg_mod) } #[inline] diff --git a/src/libsyntax/tokenstream.rs b/src/libsyntax/tokenstream.rs index 6e1bb85ce1a..4d08d0974c1 100644 --- a/src/libsyntax/tokenstream.rs +++ b/src/libsyntax/tokenstream.rs @@ -23,9 +23,6 @@ use smallvec::{SmallVec, smallvec}; use std::{iter, mem}; -#[cfg(test)] -mod tests; - /// When the main rust parser encounters a syntax-extension invocation, it /// parses the arguments to the invocation as a token-tree. This is a very /// loose structure, such that all sorts of different AST-fragments can @@ -218,7 +215,7 @@ impl TokenStream { self.0.len() } - pub(crate) fn from_streams(mut streams: SmallVec<[TokenStream; 2]>) -> TokenStream { + pub fn from_streams(mut streams: SmallVec<[TokenStream; 2]>) -> TokenStream { match streams.len() { 0 => TokenStream::default(), 1 => streams.pop().unwrap(), diff --git a/src/libsyntax/util/comments.rs b/src/libsyntax/util/comments.rs index 448b4f3b825..ef9226a8879 100644 --- a/src/libsyntax/util/comments.rs +++ b/src/libsyntax/util/comments.rs @@ -47,7 +47,8 @@ crate fn is_block_doc_comment(s: &str) -> bool { res } -crate fn is_doc_comment(s: &str) -> bool { +// FIXME(#64197): Try to privatize this again. +pub fn is_doc_comment(s: &str) -> bool { (s.starts_with("///") && is_line_doc_comment(s)) || s.starts_with("//!") || (s.starts_with("/**") && is_block_doc_comment(s)) || s.starts_with("/*!") } diff --git a/src/libsyntax/config.rs b/src/libsyntax_expand/config.rs index a460fc2b99a..9acb9948926 100644 --- a/src/libsyntax/config.rs +++ b/src/libsyntax_expand/config.rs @@ -1,20 +1,20 @@ -use crate::attr::HasAttrs; -use crate::feature_gate::{ +use syntax::attr::HasAttrs; +use syntax::feature_gate::{ feature_err, EXPLAIN_STMT_ATTR_SYNTAX, Features, get_features, GateIssue, }; -use crate::attr; -use crate::ast; -use crate::edition::Edition; -use crate::mut_visit::*; -use crate::parse::{self, validate_attr}; -use crate::ptr::P; -use crate::sess::ParseSess; -use crate::symbol::sym; -use crate::util::map_in_place::MapInPlace; +use syntax::attr; +use syntax::ast; +use syntax::edition::Edition; +use syntax::mut_visit::*; +use syntax::parse::{self, validate_attr}; +use syntax::ptr::P; +use syntax::sess::ParseSess; +use syntax::symbol::sym; +use syntax::util::map_in_place::MapInPlace; use errors::Applicability; use smallvec::SmallVec; @@ -135,12 +135,11 @@ impl<'a> StripUnconfigured<'a> { // `cfg_attr` inside of another `cfg_attr`. E.g. // `#[cfg_attr(false, cfg_attr(true, some_attr))]`. expanded_attrs.into_iter() - .flat_map(|(item, span)| self.process_cfg_attr(ast::Attribute { - kind: ast::AttrKind::Normal(item), - id: attr::mk_attr_id(), - style: attr.style, + .flat_map(|(item, span)| self.process_cfg_attr(attr::mk_attr_from_item( + attr.style, + item, span, - })) + ))) .collect() } else { vec![] @@ -148,7 +147,7 @@ impl<'a> StripUnconfigured<'a> { } /// Determines if a node with the given attributes should be included in this configuration. - pub fn in_cfg(&mut self, attrs: &[ast::Attribute]) -> bool { + pub fn in_cfg(&self, attrs: &[ast::Attribute]) -> bool { attrs.iter().all(|attr| { if !is_cfg(attr) { return true; @@ -350,3 +349,17 @@ impl<'a> MutVisitor for StripUnconfigured<'a> { fn is_cfg(attr: &ast::Attribute) -> bool { attr.check_name(sym::cfg) } + +/// Process the potential `cfg` attributes on a module. +/// Also determine if the module should be included in this configuration. +pub fn process_configure_mod( + sess: &ParseSess, + cfg_mods: bool, + attrs: &[ast::Attribute], +) -> (bool, Vec<ast::Attribute>) { + // Don't perform gated feature checking. + let mut strip_unconfigured = StripUnconfigured { sess, features: None }; + let mut attrs = attrs.to_owned(); + strip_unconfigured.process_cfg_attrs(&mut attrs); + (!cfg_mods || strip_unconfigured.in_cfg(&attrs), attrs) +} diff --git a/src/libsyntax_expand/expand.rs b/src/libsyntax_expand/expand.rs index d0b60fa308d..1e6c23087d8 100644 --- a/src/libsyntax_expand/expand.rs +++ b/src/libsyntax_expand/expand.rs @@ -3,13 +3,13 @@ use crate::proc_macro::{collect_derives, MarkAttrs}; use crate::hygiene::{ExpnId, SyntaxContext, ExpnData, ExpnKind}; use crate::mbe::macro_rules::annotate_err_with_kind; use crate::placeholders::{placeholder, PlaceholderExpander}; +use crate::config::StripUnconfigured; +use crate::configure; use syntax::ast::{self, AttrItem, Block, Ident, LitKind, NodeId, PatKind, Path}; use syntax::ast::{MacStmtStyle, StmtKind, ItemKind}; use syntax::attr::{self, HasAttrs}; use syntax::source_map::respan; -use syntax::configure; -use syntax::config::StripUnconfigured; use syntax::feature_gate::{self, Features, GateIssue, is_builtin_attr, emit_feature_err}; use syntax::mut_visit::*; use syntax::parse::DirectoryOwnership; diff --git a/src/libsyntax_expand/lib.rs b/src/libsyntax_expand/lib.rs index 10eb3ecb20b..46d59dd249c 100644 --- a/src/libsyntax_expand/lib.rs +++ b/src/libsyntax_expand/lib.rs @@ -33,6 +33,35 @@ pub use mbe::macro_rules::compile_declarative_macro; pub mod base; pub mod build; pub mod expand; +#[macro_use] pub mod config; pub mod proc_macro; crate mod mbe; + +// HACK(Centril, #64197): These shouldn't really be here. +// Rather, they should be with their respective modules which are defined in other crates. +// However, since for now constructing a `ParseSess` sorta requires `config` from this crate, +// these tests will need to live here in the iterim. + +#[cfg(test)] +mod tests; +#[cfg(test)] +mod parse { + #[cfg(test)] + mod tests; + #[cfg(test)] + mod lexer { + #[cfg(test)] + mod tests; + } +} +#[cfg(test)] +mod tokenstream { + #[cfg(test)] + mod tests; +} +#[cfg(test)] +mod mut_visit { + #[cfg(test)] + mod tests; +} diff --git a/src/libsyntax/mut_visit/tests.rs b/src/libsyntax_expand/mut_visit/tests.rs index f779e0d0a60..30e812a1179 100644 --- a/src/libsyntax/mut_visit/tests.rs +++ b/src/libsyntax_expand/mut_visit/tests.rs @@ -1,10 +1,9 @@ -use super::*; - -use crate::ast::{self, Ident}; use crate::tests::{string_to_crate, matches_codepattern}; -use crate::print::pprust; -use crate::mut_visit; -use crate::with_default_globals; + +use syntax::ast::{self, Ident}; +use syntax::print::pprust; +use syntax::mut_visit::{self, MutVisitor}; +use syntax::with_default_globals; // This version doesn't care about getting comments or doc-strings in. fn fake_print_crate(s: &mut pprust::State<'_>, diff --git a/src/libsyntax/parse/lexer/tests.rs b/src/libsyntax_expand/parse/lexer/tests.rs index baa6fb59537..3d5726fa601 100644 --- a/src/libsyntax/parse/lexer/tests.rs +++ b/src/libsyntax_expand/parse/lexer/tests.rs @@ -1,10 +1,13 @@ -use super::*; +use crate::config::process_configure_mod; -use crate::symbol::Symbol; -use crate::source_map::{SourceMap, FilePathMapping}; -use crate::token; -use crate::util::comments::is_doc_comment; -use crate::with_default_globals; +use rustc_data_structures::sync::Lrc; +use syntax::token::{self, Token, TokenKind}; +use syntax::sess::ParseSess; +use syntax::source_map::{SourceMap, FilePathMapping}; +use syntax::util::comments::is_doc_comment; +use syntax::with_default_globals; +use syntax::parse::lexer::StringReader; +use syntax_pos::symbol::Symbol; use errors::{Handler, emitter::EmitterWriter}; use std::io; @@ -21,7 +24,11 @@ fn mk_sess(sm: Lrc<SourceMap>) -> ParseSess { None, false, ); - ParseSess::with_span_handler(Handler::with_emitter(true, None, Box::new(emitter)), sm) + ParseSess::with_span_handler( + Handler::with_emitter(true, None, Box::new(emitter)), + sm, + process_configure_mod, + ) } // Creates a string reader for the given string. diff --git a/src/libsyntax/parse/tests.rs b/src/libsyntax_expand/parse/tests.rs index 27ca2b6472f..01739809f7f 100644 --- a/src/libsyntax/parse/tests.rs +++ b/src/libsyntax_expand/parse/tests.rs @@ -1,21 +1,27 @@ -use super::*; - -use crate::ast::{self, Name, PatKind}; -use crate::attr::first_attr_value_str_by_name; -use crate::sess::ParseSess; -use crate::parse::{PResult, new_parser_from_source_str}; -use crate::token::Token; -use crate::print::pprust::item_to_string; -use crate::ptr::P; -use crate::source_map::FilePathMapping; -use crate::symbol::{kw, sym}; +use crate::config::process_configure_mod; use crate::tests::{matches_codepattern, string_to_stream, with_error_checking_parse}; -use crate::tokenstream::{DelimSpan, TokenTree, TokenStream}; -use crate::with_default_globals; -use syntax_pos::{Span, BytePos, Pos}; + +use syntax::ast::{self, Name, PatKind}; +use syntax::attr::first_attr_value_str_by_name; +use syntax::sess::ParseSess; +use syntax::token::{self, Token}; +use syntax::print::pprust::item_to_string; +use syntax::ptr::P; +use syntax::source_map::FilePathMapping; +use syntax::symbol::{kw, sym}; +use syntax::tokenstream::{DelimSpan, TokenTree, TokenStream}; +use syntax::visit; +use syntax::with_default_globals; +use syntax::parse::new_parser_from_source_str; +use syntax_pos::{Span, BytePos, Pos, FileName}; +use errors::PResult; use std::path::PathBuf; +fn sess() -> ParseSess { + ParseSess::new(FilePathMapping::empty(), process_configure_mod) +} + /// Parses an item. /// /// Returns `Ok(Some(item))` when successful, `Ok(None)` when no item was found, and `Err` @@ -32,18 +38,12 @@ fn sp(a: u32, b: u32) -> Span { /// Parses a string, return an expression. fn string_to_expr(source_str : String) -> P<ast::Expr> { - let ps = ParseSess::new(FilePathMapping::empty()); - with_error_checking_parse(source_str, &ps, |p| { - p.parse_expr() - }) + with_error_checking_parse(source_str, &sess(), |p| p.parse_expr()) } /// Parses a string, returns an item. fn string_to_item(source_str : String) -> Option<P<ast::Item>> { - let ps = ParseSess::new(FilePathMapping::empty()); - with_error_checking_parse(source_str, &ps, |p| { - p.parse_item() - }) + with_error_checking_parse(source_str, &sess(), |p| p.parse_item()) } #[should_panic] @@ -169,20 +169,20 @@ fn get_spans_of_pat_idents(src: &str) -> Vec<Span> { struct PatIdentVisitor { spans: Vec<Span> } - impl<'a> crate::visit::Visitor<'a> for PatIdentVisitor { + impl<'a> visit::Visitor<'a> for PatIdentVisitor { fn visit_pat(&mut self, p: &'a ast::Pat) { match p.kind { PatKind::Ident(_ , ref ident, _) => { self.spans.push(ident.span.clone()); } _ => { - crate::visit::walk_pat(self, p); + visit::walk_pat(self, p); } } } } let mut v = PatIdentVisitor { spans: Vec::new() }; - crate::visit::walk_item(&mut v, &item); + visit::walk_item(&mut v, &item); return v.spans; } @@ -233,7 +233,7 @@ let mut fflags: c_int = wb(); #[test] fn crlf_doc_comments() { with_default_globals(|| { - let sess = ParseSess::new(FilePathMapping::empty()); + let sess = sess(); let name_1 = FileName::Custom("crlf_source_1".to_string()); let source = "/// doc comment\r\nfn foo() {}".to_string(); @@ -268,7 +268,7 @@ fn ttdelim_span() { } with_default_globals(|| { - let sess = ParseSess::new(FilePathMapping::empty()); + let sess = sess(); let expr = parse_expr_from_source_str(PathBuf::from("foo").into(), "foo!( fn main() { body } )".to_string(), &sess).unwrap(); @@ -292,11 +292,10 @@ fn ttdelim_span() { #[test] fn out_of_line_mod() { with_default_globals(|| { - let sess = ParseSess::new(FilePathMapping::empty()); let item = parse_item_from_source_str( PathBuf::from("foo").into(), "mod foo { struct S; mod this_does_not_exist; }".to_owned(), - &sess, + &sess(), ).unwrap().unwrap(); if let ast::ItemKind::Mod(ref m) = item.kind { diff --git a/src/libsyntax/tests.rs b/src/libsyntax_expand/tests.rs index ed457c3627f..461d9a43954 100644 --- a/src/libsyntax/tests.rs +++ b/src/libsyntax_expand/tests.rs @@ -1,16 +1,15 @@ -use crate::ast; -use crate::parse::source_file_to_stream; -use crate::parse::new_parser_from_source_str; -use crate::parse::parser::Parser; -use crate::sess::ParseSess; -use crate::source_map::{SourceMap, FilePathMapping}; -use crate::tokenstream::TokenStream; -use crate::with_default_globals; +use crate::config::process_configure_mod; +use syntax::ast; +use syntax::tokenstream::TokenStream; +use syntax::sess::ParseSess; +use syntax::source_map::{SourceMap, FilePathMapping}; +use syntax::with_default_globals; +use syntax::parse::{source_file_to_stream, new_parser_from_source_str, parser::Parser}; +use syntax_pos::{BytePos, Span, MultiSpan}; use errors::emitter::EmitterWriter; use errors::{PResult, Handler}; use rustc_data_structures::sync::Lrc; -use syntax_pos::{BytePos, Span, MultiSpan}; use std::io; use std::io::prelude::*; @@ -35,7 +34,7 @@ crate fn with_error_checking_parse<'a, T, F>(s: String, ps: &'a ParseSess, f: F) /// Maps a string to tts, using a made-up filename. crate fn string_to_stream(source_str: String) -> TokenStream { - let ps = ParseSess::new(FilePathMapping::empty()); + let ps = ParseSess::new(FilePathMapping::empty(), process_configure_mod); source_file_to_stream( &ps, ps.source_map().new_source_file(PathBuf::from("bogofile").into(), @@ -45,7 +44,7 @@ crate fn string_to_stream(source_str: String) -> TokenStream { /// Parses a string, returns a crate. crate fn string_to_crate(source_str : String) -> ast::Crate { - let ps = ParseSess::new(FilePathMapping::empty()); + let ps = ParseSess::new(FilePathMapping::empty(), process_configure_mod); with_error_checking_parse(source_str, &ps, |p| { p.parse_crate_mod() }) diff --git a/src/libsyntax/tokenstream/tests.rs b/src/libsyntax_expand/tokenstream/tests.rs index 5017e5f5424..cf9fead638e 100644 --- a/src/libsyntax/tokenstream/tests.rs +++ b/src/libsyntax_expand/tokenstream/tests.rs @@ -1,9 +1,11 @@ -use super::*; - -use crate::ast::Name; -use crate::with_default_globals; use crate::tests::string_to_stream; + +use syntax::ast::Name; +use syntax::token; +use syntax::tokenstream::{TokenStream, TokenStreamBuilder, TokenTree}; +use syntax::with_default_globals; use syntax_pos::{Span, BytePos}; +use smallvec::smallvec; fn string_to_ts(string: &str) -> TokenStream { string_to_stream(string.to_owned()) diff --git a/src/test/ui-fulldeps/ast_stmt_expr_attr.rs b/src/test/ui-fulldeps/ast_stmt_expr_attr.rs index ac864e76784..3d2181d4e37 100644 --- a/src/test/ui-fulldeps/ast_stmt_expr_attr.rs +++ b/src/test/ui-fulldeps/ast_stmt_expr_attr.rs @@ -6,6 +6,7 @@ #![feature(rustc_private)] extern crate syntax; +extern crate syntax_expand; extern crate rustc_errors; use rustc_errors::PResult; @@ -14,13 +15,13 @@ use syntax::attr::*; use syntax::ast; use syntax::sess::ParseSess; use syntax::source_map::{FilePathMapping, FileName}; -use syntax::parse; +use syntax::ptr::P; +use syntax::print::pprust; +use syntax::parse::parser::attr::*; use syntax::parse::new_parser_from_source_str; use syntax::parse::parser::Parser; use syntax::token; -use syntax::ptr::P; -use syntax::parse::parser::attr::*; -use syntax::print::pprust; +use syntax_expand::config::process_configure_mod; use std::fmt; // Copied out of syntax::util::parser_testing @@ -72,8 +73,12 @@ fn str_compare<T, F: Fn(&T) -> String>(e: &str, expected: &[T], actual: &[T], f: } } +fn sess() -> ParseSess { + ParseSess::new(FilePathMapping::empty(), process_configure_mod) +} + fn check_expr_attrs(es: &str, expected: &[&str]) { - let ps = ParseSess::new(FilePathMapping::empty()); + let ps = sess(); let e = expr(es, &ps).expect("parse error"); let actual = &e.attrs; str_compare(es, @@ -83,7 +88,7 @@ fn check_expr_attrs(es: &str, expected: &[&str]) { } fn check_stmt_attrs(es: &str, expected: &[&str]) { - let ps = ParseSess::new(FilePathMapping::empty()); + let ps = sess(); let e = stmt(es, &ps).expect("parse error"); let actual = e.kind.attrs(); str_compare(es, @@ -93,7 +98,7 @@ fn check_stmt_attrs(es: &str, expected: &[&str]) { } fn reject_expr_parse(es: &str) { - let ps = ParseSess::new(FilePathMapping::empty()); + let ps = sess(); match expr(es, &ps) { Ok(_) => panic!("parser did not reject `{}`", es), Err(mut e) => e.cancel(), @@ -101,7 +106,7 @@ fn reject_expr_parse(es: &str) { } fn reject_stmt_parse(es: &str) { - let ps = ParseSess::new(FilePathMapping::empty()); + let ps = sess(); match stmt(es, &ps) { Ok(_) => panic!("parser did not reject `{}`", es), Err(mut e) => e.cancel(), diff --git a/src/test/ui-fulldeps/mod_dir_path_canonicalized.rs b/src/test/ui-fulldeps/mod_dir_path_canonicalized.rs index ac97ec70be2..472440e2e14 100644 --- a/src/test/ui-fulldeps/mod_dir_path_canonicalized.rs +++ b/src/test/ui-fulldeps/mod_dir_path_canonicalized.rs @@ -5,11 +5,13 @@ #![feature(rustc_private)] extern crate syntax; +extern crate syntax_expand; use std::path::Path; use syntax::sess::ParseSess; use syntax::source_map::FilePathMapping; -use syntax::parse; +use syntax::parse::new_parser_from_file; +use syntax_expand::config::process_configure_mod; #[path = "mod_dir_simple/test.rs"] mod gravy; @@ -21,10 +23,10 @@ pub fn main() { } fn parse() { - let parse_session = ParseSess::new(FilePathMapping::empty()); + let parse_session = ParseSess::new(FilePathMapping::empty(), process_configure_mod); let path = Path::new(file!()); let path = path.canonicalize().unwrap(); - let mut parser = parse::new_parser_from_file(&parse_session, &path); + let mut parser = new_parser_from_file(&parse_session, &path); let _ = parser.parse_crate_mod(); } diff --git a/src/test/ui-fulldeps/pprust-expr-roundtrip.rs b/src/test/ui-fulldeps/pprust-expr-roundtrip.rs index 932a173bc67..99da605f357 100644 --- a/src/test/ui-fulldeps/pprust-expr-roundtrip.rs +++ b/src/test/ui-fulldeps/pprust-expr-roundtrip.rs @@ -21,6 +21,7 @@ extern crate rustc_data_structures; extern crate syntax; +extern crate syntax_expand; use rustc_data_structures::thin_vec::ThinVec; use syntax::ast::*; @@ -28,14 +29,15 @@ use syntax::sess::ParseSess; use syntax::source_map::{Spanned, DUMMY_SP, FileName}; use syntax::source_map::FilePathMapping; use syntax::mut_visit::{self, MutVisitor, visit_clobber}; -use syntax::parse; +use syntax::parse::new_parser_from_source_str; use syntax::print::pprust; use syntax::ptr::P; +use syntax_expand::config::process_configure_mod; fn parse_expr(ps: &ParseSess, src: &str) -> Option<P<Expr>> { let src_as_string = src.to_string(); - let mut p = parse::new_parser_from_source_str( + let mut p = new_parser_from_source_str( ps, FileName::Custom(src_as_string.clone()), src_as_string, @@ -202,7 +204,7 @@ fn main() { } fn run() { - let ps = ParseSess::new(FilePathMapping::empty()); + let ps = ParseSess::new(FilePathMapping::empty(), process_configure_mod); iter_exprs(2, &mut |mut e| { // If the pretty printer is correct, then `parse(print(e))` should be identical to `e`, |
