Skip to content

Commit a3b6e57

Browse files
committed
Auto merge of #65324 - Centril:organize-syntax, r=petrochenkov
Split libsyntax apart In this PR the general idea is to separate the AST, parser, and friends by a more data / logic structure (tho not fully realized!) by separating out the parser and macro expansion code from libsyntax. Specifically have now three crates instead of one (libsyntax): - libsyntax: - concrete syntax tree (`syntax::ast`) - definition of tokens and token-streams (`syntax::{token, tokenstream}`) -- used by `syntax::ast` - visitors (`syntax::visit`, `syntax::mut_visit`) - shared definitions between `libsyntax_expand` - feature gating (`syntax::feature_gate`) -- we could possibly move this out to its own crater later. - attribute and meta item utilities, including used-marking (`syntax::attr`) - pretty printer (`syntax::print`) -- this should possibly be moved out later. For now I've reduced down the dependencies to a single essential one which could be broken via `ParseSess`. This entails that e.g. `Debug` impls for `Path` cannot reference the pretty printer. - definition of `ParseSess` (`syntax::sess`) -- this is used by `syntax::{attr, print, feature_gate}` and is a common definition used by the parser and other things like librustc. - the `syntax::source_map` -- this includes definitions used by `syntax::ast` and other things but could ostensibly be moved `syntax_pos` since that is more related to this module. - a smattering of misc utilities not sufficiently important to itemize -- some of these could be moved to where they are used (often a single place) but I wanted to limit the scope of this PR. - librustc_parse: - parser (`rustc_parse::parser`) -- reading a file and such are defined in the crate root tho. - lexer (`rustc_parse::lexer`) - validation of meta grammar (post-expansion) in (`rustc_parse::validate_attr`) - libsyntax_expand -- this defines the infra for macro expansion and conditional compilation but this is not libsyntax_ext; we might want to merge them later but currently libsyntax_expand is depended on by librustc_metadata which libsyntax_ext is not. - conditional compilation (`syntax_expand::config`) -- moved from `syntax::config` to here - the bulk of this crate is made up of the old `syntax::ext` r? @estebank
2 parents 86c2832 + 4ae2728 commit a3b6e57

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+859
-701
lines changed

Cargo.lock

+22-4
Original file line numberDiff line numberDiff line change
@@ -3504,6 +3504,7 @@ dependencies = [
35043504
"rustc_lint",
35053505
"rustc_metadata",
35063506
"rustc_mir",
3507+
"rustc_parse",
35073508
"rustc_plugin",
35083509
"rustc_plugin_impl",
35093510
"rustc_resolve",
@@ -3572,6 +3573,7 @@ dependencies = [
35723573
"rustc_lint",
35733574
"rustc_metadata",
35743575
"rustc_mir",
3576+
"rustc_parse",
35753577
"rustc_passes",
35763578
"rustc_plugin_impl",
35773579
"rustc_privacy",
@@ -3649,6 +3651,7 @@ dependencies = [
36493651
"rustc_data_structures",
36503652
"rustc_errors",
36513653
"rustc_index",
3654+
"rustc_parse",
36523655
"rustc_target",
36533656
"serialize",
36543657
"smallvec 1.0.0",
@@ -3692,6 +3695,21 @@ dependencies = [
36923695
"core",
36933696
]
36943697

3698+
[[package]]
3699+
name = "rustc_parse"
3700+
version = "0.0.0"
3701+
dependencies = [
3702+
"bitflags",
3703+
"log",
3704+
"rustc_data_structures",
3705+
"rustc_errors",
3706+
"rustc_lexer",
3707+
"rustc_target",
3708+
"smallvec 1.0.0",
3709+
"syntax",
3710+
"syntax_pos",
3711+
]
3712+
36953713
[[package]]
36963714
name = "rustc_passes"
36973715
version = "0.0.0"
@@ -3701,6 +3719,7 @@ dependencies = [
37013719
"rustc_data_structures",
37023720
"rustc_errors",
37033721
"rustc_index",
3722+
"rustc_parse",
37043723
"rustc_target",
37053724
"syntax",
37063725
"syntax_pos",
@@ -3763,6 +3782,7 @@ dependencies = [
37633782
"rustc",
37643783
"rustc_codegen_utils",
37653784
"rustc_data_structures",
3785+
"rustc_parse",
37663786
"serde_json",
37673787
"syntax",
37683788
"syntax_pos",
@@ -4372,14 +4392,11 @@ dependencies = [
43724392
name = "syntax_expand"
43734393
version = "0.0.0"
43744394
dependencies = [
4375-
"bitflags",
4376-
"lazy_static 1.3.0",
43774395
"log",
43784396
"rustc_data_structures",
43794397
"rustc_errors",
4380-
"rustc_index",
43814398
"rustc_lexer",
4382-
"scoped-tls",
4399+
"rustc_parse",
43834400
"serialize",
43844401
"smallvec 1.0.0",
43854402
"syntax",
@@ -4394,6 +4411,7 @@ dependencies = [
43944411
"log",
43954412
"rustc_data_structures",
43964413
"rustc_errors",
4414+
"rustc_parse",
43974415
"rustc_target",
43984416
"smallvec 1.0.0",
43994417
"syntax",

src/librustc/session/mod.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use syntax::expand::allocator::AllocatorKind;
2626
use syntax::feature_gate::{self, AttributeType};
2727
use syntax::json::JsonEmitter;
2828
use syntax::source_map;
29-
use syntax::sess::ParseSess;
29+
use syntax::sess::{ParseSess, ProcessCfgMod};
3030
use syntax::symbol::Symbol;
3131
use syntax_pos::{MultiSpan, Span};
3232
use crate::util::profiling::{SelfProfiler, SelfProfilerRef};
@@ -934,6 +934,7 @@ pub fn build_session(
934934
sopts: config::Options,
935935
local_crate_source_file: Option<PathBuf>,
936936
registry: errors::registry::Registry,
937+
process_cfg_mod: ProcessCfgMod,
937938
) -> Session {
938939
let file_path_mapping = sopts.file_path_mapping();
939940

@@ -944,6 +945,7 @@ pub fn build_session(
944945
Lrc::new(source_map::SourceMap::new(file_path_mapping)),
945946
DiagnosticOutput::Default,
946947
Default::default(),
948+
process_cfg_mod,
947949
)
948950
}
949951

@@ -1022,6 +1024,7 @@ pub fn build_session_with_source_map(
10221024
source_map: Lrc<source_map::SourceMap>,
10231025
diagnostics_output: DiagnosticOutput,
10241026
lint_caps: FxHashMap<lint::LintId, lint::Level>,
1027+
process_cfg_mod: ProcessCfgMod,
10251028
) -> Session {
10261029
// FIXME: This is not general enough to make the warning lint completely override
10271030
// normal diagnostic warnings, since the warning lint can also be denied and changed
@@ -1062,7 +1065,14 @@ pub fn build_session_with_source_map(
10621065
},
10631066
);
10641067

1065-
build_session_(sopts, local_crate_source_file, diagnostic_handler, source_map, lint_caps)
1068+
build_session_(
1069+
sopts,
1070+
local_crate_source_file,
1071+
diagnostic_handler,
1072+
source_map,
1073+
lint_caps,
1074+
process_cfg_mod,
1075+
)
10661076
}
10671077

10681078
fn build_session_(
@@ -1071,6 +1081,7 @@ fn build_session_(
10711081
span_diagnostic: errors::Handler,
10721082
source_map: Lrc<source_map::SourceMap>,
10731083
driver_lint_caps: FxHashMap<lint::LintId, lint::Level>,
1084+
process_cfg_mod: ProcessCfgMod,
10741085
) -> Session {
10751086
let self_profiler =
10761087
if let SwitchWithOptPath::Enabled(ref d) = sopts.debugging_opts.self_profile {
@@ -1109,6 +1120,7 @@ fn build_session_(
11091120
let parse_sess = ParseSess::with_span_handler(
11101121
span_diagnostic,
11111122
source_map,
1123+
process_cfg_mod,
11121124
);
11131125
let sysroot = match &sopts.maybe_sysroot {
11141126
Some(sysroot) => sysroot.clone(),

src/librustc_driver/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ rustc_data_structures = { path = "../librustc_data_structures" }
2121
errors = { path = "../librustc_errors", package = "rustc_errors" }
2222
rustc_metadata = { path = "../librustc_metadata" }
2323
rustc_mir = { path = "../librustc_mir" }
24+
rustc_parse = { path = "../librustc_parse" }
2425
rustc_plugin = { path = "../librustc_plugin/deprecated" } # To get this in the sysroot
2526
rustc_plugin_impl = { path = "../librustc_plugin" }
2627
rustc_save_analysis = { path = "../librustc_save_analysis" }

src/librustc_driver/lib.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ use std::time::Instant;
6363
use syntax::ast;
6464
use syntax::source_map::FileLoader;
6565
use syntax::feature_gate::{GatedCfg, UnstableFeatures};
66-
use syntax::parse;
6766
use syntax::symbol::sym;
6867
use syntax_pos::{DUMMY_SP, FileName};
6968

@@ -1062,14 +1061,16 @@ pub fn handle_options(args: &[String]) -> Option<getopts::Matches> {
10621061
}
10631062

10641063
fn parse_crate_attrs<'a>(sess: &'a Session, input: &Input) -> PResult<'a, Vec<ast::Attribute>> {
1065-
match *input {
1066-
Input::File(ref ifile) => {
1067-
parse::parse_crate_attrs_from_file(ifile, &sess.parse_sess)
1064+
match input {
1065+
Input::File(ifile) => {
1066+
rustc_parse::parse_crate_attrs_from_file(ifile, &sess.parse_sess)
10681067
}
1069-
Input::Str { ref name, ref input } => {
1070-
parse::parse_crate_attrs_from_source_str(name.clone(),
1071-
input.clone(),
1072-
&sess.parse_sess)
1068+
Input::Str { name, input } => {
1069+
rustc_parse::parse_crate_attrs_from_source_str(
1070+
name.clone(),
1071+
input.clone(),
1072+
&sess.parse_sess,
1073+
)
10731074
}
10741075
}
10751076
}

src/librustc_interface/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ smallvec = { version = "1.0", features = ["union", "may_dangle"] }
1616
syntax = { path = "../libsyntax" }
1717
syntax_ext = { path = "../libsyntax_ext" }
1818
syntax_expand = { path = "../libsyntax_expand" }
19+
rustc_parse = { path = "../librustc_parse" }
1920
syntax_pos = { path = "../libsyntax_pos" }
2021
rustc_serialize = { path = "../libserialize", package = "serialize" }
2122
rustc = { path = "../librustc" }

src/librustc_interface/interface.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ use rustc_codegen_utils::codegen_backend::CodegenBackend;
1111
use rustc_data_structures::OnDrop;
1212
use rustc_data_structures::sync::Lrc;
1313
use rustc_data_structures::fx::{FxHashSet, FxHashMap};
14+
use rustc_parse::new_parser_from_source_str;
1415
use std::path::PathBuf;
1516
use std::result;
1617
use std::sync::{Arc, Mutex};
17-
use syntax::{self, parse};
1818
use syntax::ast::{self, MetaItemKind};
1919
use syntax::token;
2020
use syntax::source_map::{FileName, FileLoader, SourceMap};
2121
use syntax::sess::ParseSess;
22+
use syntax_expand::config::process_configure_mod;
2223
use syntax_pos::edition;
2324

2425
pub type Result<T> = result::Result<T, ErrorReported>;
@@ -64,9 +65,9 @@ impl Compiler {
6465
pub fn parse_cfgspecs(cfgspecs: Vec<String>) -> FxHashSet<(String, Option<String>)> {
6566
syntax::with_default_globals(move || {
6667
let cfg = cfgspecs.into_iter().map(|s| {
67-
let sess = ParseSess::with_silent_emitter();
68+
let sess = ParseSess::with_silent_emitter(process_configure_mod);
6869
let filename = FileName::cfg_spec_source_code(&s);
69-
let mut parser = parse::new_parser_from_source_str(&sess, filename, s.to_string());
70+
let mut parser = new_parser_from_source_str(&sess, filename, s.to_string());
7071

7172
macro_rules! error {($reason: expr) => {
7273
early_error(ErrorOutputType::default(),

src/librustc_interface/passes.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use rustc_errors::PResult;
2727
use rustc_incremental;
2828
use rustc_metadata::cstore;
2929
use rustc_mir as mir;
30+
use rustc_parse::{parse_crate_from_file, parse_crate_from_source_str};
3031
use rustc_passes::{self, ast_validation, hir_stats, layout_test};
3132
use rustc_plugin as plugin;
3233
use rustc_plugin::registry::Registry;
@@ -38,7 +39,6 @@ use syntax::{self, ast, visit};
3839
use syntax::early_buffered_lints::BufferedEarlyLint;
3940
use syntax_expand::base::{NamedSyntaxExtension, ExtCtxt};
4041
use syntax::mut_visit::MutVisitor;
41-
use syntax::parse;
4242
use syntax::util::node_count::NodeCounter;
4343
use syntax::symbol::Symbol;
4444
use syntax_pos::FileName;
@@ -61,12 +61,11 @@ pub fn parse<'a>(sess: &'a Session, input: &Input) -> PResult<'a, ast::Crate> {
6161
let krate = time(sess, "parsing", || {
6262
let _prof_timer = sess.prof.generic_activity("parse_crate");
6363

64-
match *input {
65-
Input::File(ref file) => parse::parse_crate_from_file(file, &sess.parse_sess),
66-
Input::Str {
67-
ref input,
68-
ref name,
69-
} => parse::parse_crate_from_source_str(name.clone(), input.clone(), &sess.parse_sess),
64+
match input {
65+
Input::File(file) => parse_crate_from_file(file, &sess.parse_sess),
66+
Input::Str { input, name } => {
67+
parse_crate_from_source_str(name.clone(), input.clone(), &sess.parse_sess)
68+
}
7069
}
7170
})?;
7271

@@ -182,7 +181,7 @@ pub fn register_plugins<'a>(
182181
)
183182
});
184183

185-
let (krate, features) = syntax::config::features(
184+
let (krate, features) = syntax_expand::config::features(
186185
krate,
187186
&sess.parse_sess,
188187
sess.edition(),
@@ -489,7 +488,7 @@ pub fn lower_to_hir(
489488
) -> Result<hir::map::Forest> {
490489
// Lower AST to HIR.
491490
let hir_forest = time(sess, "lowering AST -> HIR", || {
492-
let nt_to_tokenstream = syntax::parse::nt_to_tokenstream;
491+
let nt_to_tokenstream = rustc_parse::nt_to_tokenstream;
493492
let hir_crate = lower_crate(sess, &dep_graph, &krate, resolver, nt_to_tokenstream);
494493

495494
if sess.opts.debugging_opts.hir_stats {

src/librustc_interface/tests.rs

+24-35
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc::session::config::{build_configuration, build_session_options, to_crat
88
use rustc::session::config::{LtoCli, LinkerPluginLto, SwitchWithOptPath, ExternEntry};
99
use rustc::session::config::{Externs, OutputType, OutputTypes, SymbolManglingVersion};
1010
use rustc::session::config::{rustc_optgroups, Options, ErrorOutputType, Passes};
11-
use rustc::session::build_session;
11+
use rustc::session::{build_session, Session};
1212
use rustc::session::search_paths::SearchPath;
1313
use std::collections::{BTreeMap, BTreeSet};
1414
use std::iter::FromIterator;
@@ -17,16 +17,23 @@ use rustc_target::spec::{MergeFunctions, PanicStrategy, RelroLevel};
1717
use syntax::symbol::sym;
1818
use syntax::edition::{Edition, DEFAULT_EDITION};
1919
use syntax;
20+
use syntax_expand::config::process_configure_mod;
2021
use rustc_data_structures::fx::FxHashSet;
2122
use rustc_errors::{ColorConfig, emitter::HumanReadableErrorType, registry};
2223

23-
pub fn build_session_options_and_crate_config(
24-
matches: &getopts::Matches,
25-
) -> (Options, FxHashSet<(String, Option<String>)>) {
26-
(
27-
build_session_options(matches),
28-
parse_cfgspecs(matches.opt_strs("cfg")),
29-
)
24+
type CfgSpecs = FxHashSet<(String, Option<String>)>;
25+
26+
fn build_session_options_and_crate_config(matches: getopts::Matches) -> (Options, CfgSpecs) {
27+
let sessopts = build_session_options(&matches);
28+
let cfg = parse_cfgspecs(matches.opt_strs("cfg"));
29+
(sessopts, cfg)
30+
}
31+
32+
fn mk_session(matches: getopts::Matches) -> (Session, CfgSpecs) {
33+
let registry = registry::Registry::new(&[]);
34+
let (sessopts, cfg) = build_session_options_and_crate_config(matches);
35+
let sess = build_session(sessopts, None, registry, process_configure_mod);
36+
(sess, cfg)
3037
}
3138

3239
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> {
5966
#[test]
6067
fn test_switch_implies_cfg_test() {
6168
syntax::with_default_globals(|| {
62-
let matches = &match optgroups().parse(&["--test".to_string()]) {
63-
Ok(m) => m,
64-
Err(f) => panic!("test_switch_implies_cfg_test: {}", f),
65-
};
66-
let registry = registry::Registry::new(&[]);
67-
let (sessopts, cfg) = build_session_options_and_crate_config(matches);
68-
let sess = build_session(sessopts, None, registry);
69+
let matches = optgroups().parse(&["--test".to_string()]).unwrap();
70+
let (sess, cfg) = mk_session(matches);
6971
let cfg = build_configuration(&sess, to_crate_config(cfg));
7072
assert!(cfg.contains(&(sym::test, None)));
7173
});
7274
}
7375

74-
// When the user supplies --test and --cfg test, don't implicitly add
75-
// another --cfg test
76+
// When the user supplies --test and --cfg test, don't implicitly add another --cfg test
7677
#[test]
7778
fn test_switch_implies_cfg_test_unless_cfg_test() {
7879
syntax::with_default_globals(|| {
79-
let matches = &match optgroups().parse(&["--test".to_string(),
80-
"--cfg=test".to_string()]) {
81-
Ok(m) => m,
82-
Err(f) => panic!("test_switch_implies_cfg_test_unless_cfg_test: {}", f),
83-
};
84-
let registry = registry::Registry::new(&[]);
85-
let (sessopts, cfg) = build_session_options_and_crate_config(matches);
86-
let sess = build_session(sessopts, None, registry);
80+
let matches = optgroups().parse(&["--test".to_string(), "--cfg=test".to_string()]).unwrap();
81+
let (sess, cfg) = mk_session(matches);
8782
let cfg = build_configuration(&sess, to_crate_config(cfg));
8883
let mut test_items = cfg.iter().filter(|&&(name, _)| name == sym::test);
8984
assert!(test_items.next().is_some());
@@ -95,27 +90,21 @@ fn test_switch_implies_cfg_test_unless_cfg_test() {
9590
fn test_can_print_warnings() {
9691
syntax::with_default_globals(|| {
9792
let matches = optgroups().parse(&["-Awarnings".to_string()]).unwrap();
98-
let registry = registry::Registry::new(&[]);
99-
let (sessopts, _) = build_session_options_and_crate_config(&matches);
100-
let sess = build_session(sessopts, None, registry);
93+
let (sess, _) = mk_session(matches);
10194
assert!(!sess.diagnostic().can_emit_warnings());
10295
});
10396

10497
syntax::with_default_globals(|| {
10598
let matches = optgroups()
10699
.parse(&["-Awarnings".to_string(), "-Dwarnings".to_string()])
107100
.unwrap();
108-
let registry = registry::Registry::new(&[]);
109-
let (sessopts, _) = build_session_options_and_crate_config(&matches);
110-
let sess = build_session(sessopts, None, registry);
101+
let (sess, _) = mk_session(matches);
111102
assert!(sess.diagnostic().can_emit_warnings());
112103
});
113104

114105
syntax::with_default_globals(|| {
115106
let matches = optgroups().parse(&["-Adead_code".to_string()]).unwrap();
116-
let registry = registry::Registry::new(&[]);
117-
let (sessopts, _) = build_session_options_and_crate_config(&matches);
118-
let sess = build_session(sessopts, None, registry);
107+
let (sess, _) = mk_session(matches);
119108
assert!(sess.diagnostic().can_emit_warnings());
120109
});
121110
}
@@ -704,6 +693,6 @@ fn test_edition_parsing() {
704693
let matches = optgroups()
705694
.parse(&["--edition=2018".to_string()])
706695
.unwrap();
707-
let (sessopts, _) = build_session_options_and_crate_config(&matches);
696+
let (sessopts, _) = build_session_options_and_crate_config(matches);
708697
assert!(sessopts.edition == Edition::Edition2018)
709698
}

0 commit comments

Comments
 (0)