Skip to content

Commit

Permalink
Auto merge of #37431 - jseyfried:refactor_crate_config, r=eddyb
Browse files Browse the repository at this point in the history
Move `CrateConfig` from `Crate` to `ParseSess`

This is a syntax-[breaking-change]. Most breakage can be fixed by removing a `CrateConfig` argument.
r? @eddyb
  • Loading branch information
bors authored Oct 30, 2016
2 parents aef5ca5 + cbd2475 commit 6062e7e
Show file tree
Hide file tree
Showing 39 changed files with 130 additions and 298 deletions.
4 changes: 1 addition & 3 deletions src/libproc_macro/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,8 @@ impl FromStr for TokenStream {
fn from_str(src: &str) -> Result<TokenStream, LexError> {
__internal::with_parse_sess(|sess| {
let src = src.to_string();
let cfg = Vec::new();
let name = "<proc-macro source code>".to_string();
let mut parser = parse::new_parser_from_source_str(sess, cfg, name,
src);
let mut parser = parse::new_parser_from_source_str(sess, name, src);
let mut ret = TokenStream { inner: Vec::new() };
loop {
match parser.parse_item() {
Expand Down
1 change: 0 additions & 1 deletion src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ impl<'a> LoweringContext<'a> {
hir::Crate {
module: self.lower_mod(&c.module),
attrs: self.lower_attrs(&c.attrs),
config: c.config.clone().into(),
span: c.span,
exported_macros: c.exported_macros.iter().map(|m| self.lower_macro_def(m)).collect(),
items: items,
Expand Down
1 change: 0 additions & 1 deletion src/librustc/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,6 @@ pub type CrateConfig = HirVec<P<MetaItem>>;
pub struct Crate {
pub module: Mod,
pub attrs: HirVec<Attribute>,
pub config: CrateConfig,
pub span: Span,
pub exported_macros: HirVec<MacroDef>,

Expand Down
7 changes: 3 additions & 4 deletions src/librustc/session/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1237,10 +1237,9 @@ pub fn rustc_optgroups() -> Vec<RustcOptGroup> {
pub fn parse_cfgspecs(cfgspecs: Vec<String> ) -> ast::CrateConfig {
cfgspecs.into_iter().map(|s| {
let sess = parse::ParseSess::new();
let mut parser = parse::new_parser_from_source_str(&sess,
Vec::new(),
"cfgspec".to_string(),
s.to_string());
let mut parser =
parse::new_parser_from_source_str(&sess, "cfgspec".to_string(), s.to_string());

let meta_item = panictry!(parser.parse_meta_item());

if !parser.reader.is_eof() {
Expand Down
19 changes: 6 additions & 13 deletions src/librustc_driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ pub struct Resolutions {

pub fn compile_input(sess: &Session,
cstore: &CStore,
cfg: ast::CrateConfig,
input: &Input,
outdir: &Option<PathBuf>,
output: &Option<PathBuf>,
Expand All @@ -91,7 +90,7 @@ pub fn compile_input(sess: &Session,
// large chunks of memory alive and we want to free them as soon as
// possible to keep the peak memory usage low
let (outputs, trans) = {
let krate = match phase_1_parse_input(sess, cfg, input) {
let krate = match phase_1_parse_input(sess, input) {
Ok(krate) => krate,
Err(mut parse_error) => {
parse_error.emit();
Expand Down Expand Up @@ -482,23 +481,17 @@ impl<'a, 'b, 'ast, 'tcx> CompileState<'a, 'b, 'ast, 'tcx> {
}
}

pub fn phase_1_parse_input<'a>(sess: &'a Session,
cfg: ast::CrateConfig,
input: &Input)
-> PResult<'a, ast::Crate> {
pub fn phase_1_parse_input<'a>(sess: &'a Session, input: &Input) -> PResult<'a, ast::Crate> {
let continue_after_error = sess.opts.debugging_opts.continue_parse_after_error;
sess.diagnostic().set_continue_after_error(continue_after_error);

let krate = time(sess.time_passes(), "parsing", || {
match *input {
Input::File(ref file) => {
parse::parse_crate_from_file(file, cfg.clone(), &sess.parse_sess)
parse::parse_crate_from_file(file, &sess.parse_sess)
}
Input::Str { ref input, ref name } => {
parse::parse_crate_from_source_str(name.clone(),
input.clone(),
cfg.clone(),
&sess.parse_sess)
parse::parse_crate_from_source_str(name.clone(), input.clone(), &sess.parse_sess)
}
}
})?;
Expand Down Expand Up @@ -636,7 +629,7 @@ pub fn phase_2_configure_and_expand<'a, F>(sess: &Session,
// its contents but the results of name resolution on those contents. Hopefully we'll push
// this back at some point.
let _ignore = sess.dep_graph.in_ignore();
let mut crate_loader = CrateLoader::new(sess, &cstore, crate_name, krate.config.clone());
let mut crate_loader = CrateLoader::new(sess, &cstore, crate_name);
crate_loader.preprocess(&krate);
let resolver_arenas = Resolver::arenas();
let mut resolver =
Expand Down Expand Up @@ -677,7 +670,7 @@ pub fn phase_2_configure_and_expand<'a, F>(sess: &Session,
should_test: sess.opts.test,
..syntax::ext::expand::ExpansionConfig::default(crate_name.to_string())
};
let mut ecx = ExtCtxt::new(&sess.parse_sess, krate.config.clone(), cfg, &mut resolver);
let mut ecx = ExtCtxt::new(&sess.parse_sess, cfg, &mut resolver);
let err_count = ecx.parse_sess.span_diagnostic.err_count();

let krate = ecx.monotonic_expander().expand_crate(krate);
Expand Down
45 changes: 17 additions & 28 deletions src/librustc_driver/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,24 +205,20 @@ pub fn run_compiler<'a>(args: &[String],

let loader = file_loader.unwrap_or(box RealFileLoader);
let codemap = Rc::new(CodeMap::with_file_loader(loader));
let sess = session::build_session_with_codemap(sopts,
&dep_graph,
input_file_path,
descriptions,
cstore.clone(),
codemap,
emitter_dest);
let mut sess = session::build_session_with_codemap(
sopts, &dep_graph, input_file_path, descriptions, cstore.clone(), codemap, emitter_dest,
);
rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));

let mut cfg = config::build_configuration(&sess, cfg);
target_features::add_configuration(&mut cfg, &sess);
sess.parse_sess.config = cfg;

do_or_return!(callbacks.late_callback(&matches, &sess, &cfg, &input, &odir, &ofile),
Some(sess));
do_or_return!(callbacks.late_callback(&matches, &sess, &input, &odir, &ofile), Some(sess));

let plugins = sess.opts.debugging_opts.extra_plugins.clone();
let control = callbacks.build_controller(&sess, &matches);
(driver::compile_input(&sess, &cstore, cfg, &input, &odir, &ofile,
Some(plugins), &control),
(driver::compile_input(&sess, &cstore, &input, &odir, &ofile, Some(plugins), &control),
Some(sess))
}

Expand Down Expand Up @@ -310,7 +306,6 @@ pub trait CompilerCalls<'a> {
fn late_callback(&mut self,
_: &getopts::Matches,
_: &Session,
_: &ast::CrateConfig,
_: &Input,
_: &Option<PathBuf>,
_: &Option<PathBuf>)
Expand Down Expand Up @@ -439,19 +434,18 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
}
let dep_graph = DepGraph::new(sopts.build_dep_graph());
let cstore = Rc::new(CStore::new(&dep_graph));
let sess = build_session(sopts.clone(),
let mut sess = build_session(sopts.clone(),
&dep_graph,
None,
descriptions.clone(),
cstore.clone());
rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
let mut cfg = config::build_configuration(&sess, cfg.clone());
target_features::add_configuration(&mut cfg, &sess);
let should_stop = RustcDefaultCalls::print_crate_info(&sess,
&cfg,
None,
odir,
ofile);
sess.parse_sess.config = cfg;
let should_stop =
RustcDefaultCalls::print_crate_info(&sess, None, odir, ofile);

if should_stop == Compilation::Stop {
return None;
}
Expand All @@ -467,12 +461,11 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
fn late_callback(&mut self,
matches: &getopts::Matches,
sess: &Session,
cfg: &ast::CrateConfig,
input: &Input,
odir: &Option<PathBuf>,
ofile: &Option<PathBuf>)
-> Compilation {
RustcDefaultCalls::print_crate_info(sess, cfg, Some(input), odir, ofile)
RustcDefaultCalls::print_crate_info(sess, Some(input), odir, ofile)
.and_then(|| RustcDefaultCalls::list_metadata(sess, matches, input))
}

Expand Down Expand Up @@ -593,7 +586,6 @@ impl RustcDefaultCalls {


fn print_crate_info(sess: &Session,
cfg: &ast::CrateConfig,
input: Option<&Input>,
odir: &Option<PathBuf>,
ofile: &Option<PathBuf>)
Expand Down Expand Up @@ -649,8 +641,8 @@ impl RustcDefaultCalls {
let allow_unstable_cfg = UnstableFeatures::from_environment()
.is_nightly_build();

for cfg in cfg {
if !allow_unstable_cfg && GatedCfg::gate(&*cfg).is_some() {
for cfg in &sess.parse_sess.config {
if !allow_unstable_cfg && GatedCfg::gate(cfg).is_some() {
continue;
}

Expand Down Expand Up @@ -1036,13 +1028,10 @@ pub fn handle_options(args: &[String]) -> Option<getopts::Matches> {
fn parse_crate_attrs<'a>(sess: &'a Session, input: &Input) -> PResult<'a, Vec<ast::Attribute>> {
match *input {
Input::File(ref ifile) => {
parse::parse_crate_attrs_from_file(ifile, Vec::new(), &sess.parse_sess)
parse::parse_crate_attrs_from_file(ifile, &sess.parse_sess)
}
Input::Str { ref name, ref input } => {
parse::parse_crate_attrs_from_source_str(name.clone(),
input.clone(),
Vec::new(),
&sess.parse_sess)
parse::parse_crate_attrs_from_source_str(name.clone(), input.clone(), &sess.parse_sess)
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_driver/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,11 @@ fn test_env<F>(source_string: &str,
let sess = session::build_session_(options, &dep_graph, None, diagnostic_handler,
Rc::new(CodeMap::new()), cstore.clone());
rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
let krate_config = Vec::new();
let input = config::Input::Str {
name: driver::anon_src(),
input: source_string.to_string(),
};
let krate = driver::phase_1_parse_input(&sess, krate_config, &input).unwrap();
let krate = driver::phase_1_parse_input(&sess, &input).unwrap();
let driver::ExpansionResult { defs, resolutions, mut hir_forest, .. } = {
driver::phase_2_configure_and_expand(
&sess, &cstore, krate, None, "test", None, MakeGlobMap::No, |_| Ok(()),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_incremental/persist/dirty_clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ impl<'a, 'tcx, 'm> DirtyCleanMetadataVisitor<'a, 'tcx, 'm> {
/// flag called `foo`.
fn check_config(tcx: TyCtxt, attr: &ast::Attribute) -> bool {
debug!("check_config(attr={:?})", attr);
let config = &tcx.map.krate().config;
let config = &tcx.sess.parse_sess.config;
debug!("check_config: config={:?}", config);
for item in attr.meta_item_list().unwrap_or(&[]) {
if item.check_name(CFG) {
Expand Down
9 changes: 1 addition & 8 deletions src/librustc_metadata/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ pub struct CrateLoader<'a> {
next_crate_num: CrateNum,
foreign_item_map: FnvHashMap<String, Vec<ast::NodeId>>,
local_crate_name: String,
local_crate_config: ast::CrateConfig,
}

fn dump_crates(cstore: &CStore) {
Expand Down Expand Up @@ -144,18 +143,13 @@ enum LoadResult {
}

impl<'a> CrateLoader<'a> {
pub fn new(sess: &'a Session,
cstore: &'a CStore,
local_crate_name: &str,
local_crate_config: ast::CrateConfig)
-> Self {
pub fn new(sess: &'a Session, cstore: &'a CStore, local_crate_name: &str) -> Self {
CrateLoader {
sess: sess,
cstore: cstore,
next_crate_num: cstore.next_crate_num(),
foreign_item_map: FnvHashMap(),
local_crate_name: local_crate_name.to_owned(),
local_crate_config: local_crate_config,
}
}

Expand Down Expand Up @@ -541,7 +535,6 @@ impl<'a> CrateLoader<'a> {
// NB: Don't use parse::parse_tts_from_source_str because it parses with
// quote_depth > 0.
let mut p = parse::new_parser_from_source_str(&self.sess.parse_sess,
self.local_crate_config.clone(),
source_name.clone(),
def.body);
let lo = p.span.lo;
Expand Down
10 changes: 3 additions & 7 deletions src/librustc_plugin/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub fn load_plugins(sess: &Session,
krate: &ast::Crate,
crate_name: &str,
addl_plugins: Option<Vec<String>>) -> Vec<PluginRegistrar> {
let mut loader = PluginLoader::new(sess, cstore, crate_name, krate.config.clone());
let mut loader = PluginLoader::new(sess, cstore, crate_name);

// do not report any error now. since crate attributes are
// not touched by expansion, every use of plugin without
Expand Down Expand Up @@ -89,14 +89,10 @@ pub fn load_plugins(sess: &Session,
}

impl<'a> PluginLoader<'a> {
fn new(sess: &'a Session,
cstore: &'a CStore,
crate_name: &str,
crate_config: ast::CrateConfig)
-> PluginLoader<'a> {
fn new(sess: &'a Session, cstore: &'a CStore, crate_name: &str) -> Self {
PluginLoader {
sess: sess,
reader: CrateLoader::new(sess, cstore, crate_name, crate_config),
reader: CrateLoader::new(sess, cstore, crate_name),
plugins: vec![],
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_trans/assert_module_sources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ impl<'a, 'tcx> AssertModuleSource<'a, 'tcx> {
/// Scan for a `cfg="foo"` attribute and check whether we have a
/// cfg flag called `foo`.
fn check_config(&self, attr: &ast::Attribute) -> bool {
let config = &self.tcx.map.krate().config;
let config = &self.tcx.sess.parse_sess.config;
let value = self.field(attr, CFG);
debug!("check_config(config={:?}, value={:?})", config, value);
if config.iter().any(|c| c.check_name(&value[..])) {
Expand Down
8 changes: 5 additions & 3 deletions src/librustdoc/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,16 @@ pub fn run_core(search_paths: SearchPaths,
let dep_graph = DepGraph::new(false);
let _ignore = dep_graph.in_ignore();
let cstore = Rc::new(CStore::new(&dep_graph));
let sess = session::build_session_(sessopts, &dep_graph, cpath, diagnostic_handler,
codemap, cstore.clone());
let mut sess = session::build_session_(
sessopts, &dep_graph, cpath, diagnostic_handler, codemap, cstore.clone()
);
rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));

let mut cfg = config::build_configuration(&sess, config::parse_cfgspecs(cfgs));
target_features::add_configuration(&mut cfg, &sess);
sess.parse_sess.config = cfg;

let krate = panictry!(driver::phase_1_parse_input(&sess, cfg, &input));
let krate = panictry!(driver::phase_1_parse_input(&sess, &input));

let name = link::find_crate_name(Some(&sess), &krate.attrs, &input);

Expand Down
36 changes: 14 additions & 22 deletions src/librustdoc/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,20 @@ pub fn run(input: &str,
};

let codemap = Rc::new(CodeMap::new());
let diagnostic_handler = errors::Handler::with_tty_emitter(ColorConfig::Auto,
true,
false,
Some(codemap.clone()));
let handler =
errors::Handler::with_tty_emitter(ColorConfig::Auto, true, false, Some(codemap.clone()));

let dep_graph = DepGraph::new(false);
let _ignore = dep_graph.in_ignore();
let cstore = Rc::new(CStore::new(&dep_graph));
let sess = session::build_session_(sessopts,
&dep_graph,
Some(input_path.clone()),
diagnostic_handler,
codemap,
cstore.clone());
let mut sess = session::build_session_(
sessopts, &dep_graph, Some(input_path.clone()), handler, codemap, cstore.clone(),
);
rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
sess.parse_sess.config =
config::build_configuration(&sess, config::parse_cfgspecs(cfgs.clone()));

let cfg = config::build_configuration(&sess, config::parse_cfgspecs(cfgs.clone()));
let krate = panictry!(driver::phase_1_parse_input(&sess, cfg, &input));
let krate = panictry!(driver::phase_1_parse_input(&sess, &input));
let driver::ExpansionResult { defs, mut hir_forest, .. } = {
phase_2_configure_and_expand(
&sess, &cstore, krate, None, "rustdoc-test", None, MakeGlobMap::No, |_| Ok(())
Expand Down Expand Up @@ -236,28 +232,24 @@ fn runtest(test: &str, cratename: &str, cfgs: Vec<String>, libs: SearchPaths,

let dep_graph = DepGraph::new(false);
let cstore = Rc::new(CStore::new(&dep_graph));
let sess = session::build_session_(sessopts,
&dep_graph,
None,
diagnostic_handler,
codemap,
cstore.clone());
let mut sess = session::build_session_(
sessopts, &dep_graph, None, diagnostic_handler, codemap, cstore.clone(),
);
rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));

let outdir = Mutex::new(TempDir::new("rustdoctest").ok().expect("rustdoc needs a tempdir"));
let libdir = sess.target_filesearch(PathKind::All).get_lib_path();
let mut control = driver::CompileController::basic();
let cfg = config::build_configuration(&sess, config::parse_cfgspecs(cfgs.clone()));
sess.parse_sess.config =
config::build_configuration(&sess, config::parse_cfgspecs(cfgs.clone()));
let out = Some(outdir.lock().unwrap().path().to_path_buf());

if no_run {
control.after_analysis.stop = Compilation::Stop;
}

let res = panic::catch_unwind(AssertUnwindSafe(|| {
driver::compile_input(&sess, &cstore, cfg.clone(),
&input, &out,
&None, None, &control)
driver::compile_input(&sess, &cstore, &input, &out, &None, None, &control)
}));

match res {
Expand Down
Loading

0 comments on commit 6062e7e

Please sign in to comment.