Skip to content

Commit 65eb024

Browse files
Remove the 'cfg' field from session::config::Options.
The 'cfg' in the Options struct is only the commandline-specified subset of the crate configuration and it's almost always wrong to read that instead of the CrateConfig in HIR crate node.
1 parent d3578ab commit 65eb024

File tree

10 files changed

+74
-240
lines changed

10 files changed

+74
-240
lines changed

src/librustc/session/config.rs

Lines changed: 16 additions & 210 deletions
Large diffs are not rendered by default.

src/librustc_driver/driver.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,10 @@ pub fn phase_2_configure_and_expand<'a, F>(sess: &Session,
667667
trace_mac: sess.opts.debugging_opts.trace_macros,
668668
should_test: sess.opts.test,
669669
};
670-
let mut loader = macro_import::MacroLoader::new(sess, &cstore, crate_name);
670+
let mut loader = macro_import::MacroLoader::new(sess,
671+
&cstore,
672+
crate_name,
673+
krate.config.clone());
671674
let mut ecx = syntax::ext::base::ExtCtxt::new(&sess.parse_sess,
672675
krate.config.clone(),
673676
cfg,

src/librustc_driver/lib.rs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ pub fn run_compiler_with_file_loader<'a, L>(args: &[String],
181181
None => return (Ok(()), None),
182182
};
183183

184-
let sopts = config::build_session_options(&matches);
184+
let (sopts, cfg) = config::build_session_options_and_crate_config(&matches);
185185

186186
if sopts.debugging_opts.debug_llvm {
187187
unsafe { llvm::LLVMRustSetDebug(1); }
@@ -191,14 +191,15 @@ pub fn run_compiler_with_file_loader<'a, L>(args: &[String],
191191

192192
do_or_return!(callbacks.early_callback(&matches,
193193
&sopts,
194+
&cfg,
194195
&descriptions,
195196
sopts.error_format),
196197
None);
197198

198199
let (odir, ofile) = make_output(&matches);
199200
let (input, input_file_path) = match make_input(&matches.free) {
200201
Some((input, input_file_path)) => callbacks.some_input(input, input_file_path),
201-
None => match callbacks.no_input(&matches, &sopts, &odir, &ofile, &descriptions) {
202+
None => match callbacks.no_input(&matches, &sopts, &cfg, &odir, &ofile, &descriptions) {
202203
Some((input, input_file_path)) => (input, input_file_path),
203204
None => return (Ok(()), None),
204205
},
@@ -214,10 +215,11 @@ pub fn run_compiler_with_file_loader<'a, L>(args: &[String],
214215
cstore.clone(),
215216
codemap);
216217
rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
217-
let mut cfg = config::build_configuration(&sess);
218+
let mut cfg = config::build_configuration(&sess, cfg);
218219
target_features::add_configuration(&mut cfg, &sess);
219220

220-
do_or_return!(callbacks.late_callback(&matches, &sess, &input, &odir, &ofile), Some(sess));
221+
do_or_return!(callbacks.late_callback(&matches, &sess, &cfg, &input, &odir, &ofile),
222+
Some(sess));
221223

222224
let plugins = sess.opts.debugging_opts.extra_plugins.clone();
223225
let control = callbacks.build_controller(&sess, &matches);
@@ -297,6 +299,7 @@ pub trait CompilerCalls<'a> {
297299
fn early_callback(&mut self,
298300
_: &getopts::Matches,
299301
_: &config::Options,
302+
_: &ast::CrateConfig,
300303
_: &errors::registry::Registry,
301304
_: ErrorOutputType)
302305
-> Compilation {
@@ -309,6 +312,7 @@ pub trait CompilerCalls<'a> {
309312
fn late_callback(&mut self,
310313
_: &getopts::Matches,
311314
_: &Session,
315+
_: &ast::CrateConfig,
312316
_: &Input,
313317
_: &Option<PathBuf>,
314318
_: &Option<PathBuf>)
@@ -334,6 +338,7 @@ pub trait CompilerCalls<'a> {
334338
fn no_input(&mut self,
335339
_: &getopts::Matches,
336340
_: &config::Options,
341+
_: &ast::CrateConfig,
337342
_: &Option<PathBuf>,
338343
_: &Option<PathBuf>,
339344
_: &errors::registry::Registry)
@@ -375,7 +380,7 @@ fn handle_explain(code: &str,
375380
}
376381
}
377382

378-
fn check_cfg(sopts: &config::Options,
383+
fn check_cfg(cfg: &ast::CrateConfig,
379384
output: ErrorOutputType) {
380385
let emitter: Box<Emitter> = match output {
381386
config::ErrorOutputType::HumanReadable(color_config) => {
@@ -386,7 +391,7 @@ fn check_cfg(sopts: &config::Options,
386391
let handler = errors::Handler::with_emitter(true, false, emitter);
387392

388393
let mut saw_invalid_predicate = false;
389-
for item in sopts.cfg.iter() {
394+
for item in cfg.iter() {
390395
if item.is_meta_item_list() {
391396
saw_invalid_predicate = true;
392397
handler.emit(&MultiSpan::new(),
@@ -404,7 +409,8 @@ fn check_cfg(sopts: &config::Options,
404409
impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
405410
fn early_callback(&mut self,
406411
matches: &getopts::Matches,
407-
sopts: &config::Options,
412+
_: &config::Options,
413+
cfg: &ast::CrateConfig,
408414
descriptions: &errors::registry::Registry,
409415
output: ErrorOutputType)
410416
-> Compilation {
@@ -413,13 +419,14 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
413419
return Compilation::Stop;
414420
}
415421

416-
check_cfg(sopts, output);
422+
check_cfg(cfg, output);
417423
Compilation::Continue
418424
}
419425

420426
fn no_input(&mut self,
421427
matches: &getopts::Matches,
422428
sopts: &config::Options,
429+
cfg: &ast::CrateConfig,
423430
odir: &Option<PathBuf>,
424431
ofile: &Option<PathBuf>,
425432
descriptions: &errors::registry::Registry)
@@ -440,7 +447,13 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
440447
descriptions.clone(),
441448
cstore.clone());
442449
rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
443-
let should_stop = RustcDefaultCalls::print_crate_info(&sess, None, odir, ofile);
450+
let mut cfg = config::build_configuration(&sess, cfg.clone());
451+
target_features::add_configuration(&mut cfg, &sess);
452+
let should_stop = RustcDefaultCalls::print_crate_info(&sess,
453+
&cfg,
454+
None,
455+
odir,
456+
ofile);
444457
if should_stop == Compilation::Stop {
445458
return None;
446459
}
@@ -456,11 +469,12 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
456469
fn late_callback(&mut self,
457470
matches: &getopts::Matches,
458471
sess: &Session,
472+
cfg: &ast::CrateConfig,
459473
input: &Input,
460474
odir: &Option<PathBuf>,
461475
ofile: &Option<PathBuf>)
462476
-> Compilation {
463-
RustcDefaultCalls::print_crate_info(sess, Some(input), odir, ofile)
477+
RustcDefaultCalls::print_crate_info(sess, cfg, Some(input), odir, ofile)
464478
.and_then(|| RustcDefaultCalls::list_metadata(sess, matches, input))
465479
}
466480

@@ -579,6 +593,7 @@ impl RustcDefaultCalls {
579593

580594

581595
fn print_crate_info(sess: &Session,
596+
cfg: &ast::CrateConfig,
582597
input: Option<&Input>,
583598
odir: &Option<PathBuf>,
584599
ofile: &Option<PathBuf>)
@@ -631,9 +646,6 @@ impl RustcDefaultCalls {
631646
}
632647
}
633648
PrintRequest::Cfg => {
634-
let mut cfg = config::build_configuration(&sess);
635-
target_features::add_configuration(&mut cfg, &sess);
636-
637649
let allow_unstable_cfg = match get_unstable_features_setting() {
638650
UnstableFeatures::Disallow => false,
639651
_ => true,

src/librustc_metadata/creader.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ pub struct CrateReader<'a> {
5656
next_crate_num: ast::CrateNum,
5757
foreign_item_map: FnvHashMap<String, Vec<ast::NodeId>>,
5858
local_crate_name: String,
59+
local_crate_config: ast::CrateConfig,
5960
}
6061

6162
impl<'a> visit::Visitor for LocalCrateReader<'a> {
@@ -152,13 +153,16 @@ enum LoadResult {
152153
impl<'a> CrateReader<'a> {
153154
pub fn new(sess: &'a Session,
154155
cstore: &'a CStore,
155-
local_crate_name: &str) -> CrateReader<'a> {
156+
local_crate_name: &str,
157+
local_crate_config: ast::CrateConfig)
158+
-> CrateReader<'a> {
156159
CrateReader {
157160
sess: sess,
158161
cstore: cstore,
159162
next_crate_num: cstore.next_crate_num(),
160163
foreign_item_map: FnvHashMap(),
161164
local_crate_name: local_crate_name.to_owned(),
165+
local_crate_config: local_crate_config,
162166
}
163167
}
164168

@@ -561,7 +565,7 @@ impl<'a> CrateReader<'a> {
561565
// NB: Don't use parse::parse_tts_from_source_str because it parses with
562566
// quote_depth > 0.
563567
let mut p = parse::new_parser_from_source_str(&self.sess.parse_sess,
564-
self.sess.opts.cfg.clone(),
568+
self.local_crate_config.clone(),
565569
source_name.clone(),
566570
body);
567571
let lo = p.span.lo;
@@ -863,7 +867,7 @@ impl<'a> LocalCrateReader<'a> {
863867
LocalCrateReader {
864868
sess: sess,
865869
cstore: cstore,
866-
creader: CrateReader::new(sess, cstore, local_crate_name),
870+
creader: CrateReader::new(sess, cstore, local_crate_name, krate.config.clone()),
867871
krate: krate,
868872
definitions: defs,
869873
}

src/librustc_metadata/macro_import.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@ pub struct MacroLoader<'a> {
2929
}
3030

3131
impl<'a> MacroLoader<'a> {
32-
pub fn new(sess: &'a Session, cstore: &'a CStore, crate_name: &str) -> MacroLoader<'a> {
32+
pub fn new(sess: &'a Session,
33+
cstore: &'a CStore,
34+
crate_name: &str,
35+
crate_config: ast::CrateConfig)
36+
-> MacroLoader<'a> {
3337
MacroLoader {
3438
sess: sess,
35-
reader: CrateReader::new(sess, cstore, crate_name),
39+
reader: CrateReader::new(sess, cstore, crate_name, crate_config),
3640
}
3741
}
3842
}

src/librustc_plugin/load.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub fn load_plugins(sess: &Session,
4949
krate: &ast::Crate,
5050
crate_name: &str,
5151
addl_plugins: Option<Vec<String>>) -> Vec<PluginRegistrar> {
52-
let mut loader = PluginLoader::new(sess, cstore, crate_name);
52+
let mut loader = PluginLoader::new(sess, cstore, crate_name, krate.config.clone());
5353

5454
// do not report any error now. since crate attributes are
5555
// not touched by expansion, every use of plugin without
@@ -90,10 +90,14 @@ pub fn load_plugins(sess: &Session,
9090
}
9191

9292
impl<'a> PluginLoader<'a> {
93-
fn new(sess: &'a Session, cstore: &'a CStore, crate_name: &str) -> PluginLoader<'a> {
93+
fn new(sess: &'a Session,
94+
cstore: &'a CStore,
95+
crate_name: &str,
96+
crate_config: ast::CrateConfig)
97+
-> PluginLoader<'a> {
9498
PluginLoader {
9599
sess: sess,
96-
reader: CrateReader::new(sess, cstore, crate_name),
100+
reader: CrateReader::new(sess, cstore, crate_name, crate_config),
97101
plugins: vec![],
98102
}
99103
}

src/librustdoc/core.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ pub fn run_core(search_paths: SearchPaths,
119119
lint_cap: Some(lint::Allow),
120120
externs: externs,
121121
target_triple: triple.unwrap_or(config::host_triple().to_string()),
122-
cfg: config::parse_cfgspecs(cfgs),
123122
// Ensure that rustdoc works even if rustc is feature-staged
124123
unstable_features: UnstableFeatures::Allow,
125124
..config::basic_options().clone()
@@ -138,7 +137,7 @@ pub fn run_core(search_paths: SearchPaths,
138137
codemap, cstore.clone());
139138
rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
140139

141-
let mut cfg = config::build_configuration(&sess);
140+
let mut cfg = config::build_configuration(&sess, config::parse_cfgspecs(cfgs));
142141
target_features::add_configuration(&mut cfg, &sess);
143142

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

src/librustdoc/test.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@ pub fn run(input: &str,
9090
cstore.clone());
9191
rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
9292

93-
let mut cfg = config::build_configuration(&sess);
94-
cfg.extend(config::parse_cfgspecs(cfgs.clone()));
93+
let cfg = config::build_configuration(&sess, config::parse_cfgspecs(cfgs.clone()));
9594
let krate = panictry!(driver::phase_1_parse_input(&sess, cfg, &input));
9695
let driver::ExpansionResult { defs, mut hir_forest, .. } = {
9796
phase_2_configure_and_expand(
@@ -247,8 +246,7 @@ fn runtest(test: &str, cratename: &str, cfgs: Vec<String>, libs: SearchPaths,
247246
let outdir = Mutex::new(TempDir::new("rustdoctest").ok().expect("rustdoc needs a tempdir"));
248247
let libdir = sess.target_filesearch(PathKind::All).get_lib_path();
249248
let mut control = driver::CompileController::basic();
250-
let mut cfg = config::build_configuration(&sess);
251-
cfg.extend(config::parse_cfgspecs(cfgs.clone()));
249+
let cfg = config::build_configuration(&sess, config::parse_cfgspecs(cfgs.clone()));
252250
let out = Some(outdir.lock().unwrap().path().to_path_buf());
253251

254252
if no_run {

src/test/run-make/issue-19371/foo.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ fn main() {
5252

5353
fn basic_sess(sysroot: PathBuf) -> (Session, Rc<CStore>) {
5454
let mut opts = basic_options();
55-
opts.output_types = OutputTypes::new([(OutputType::Exe, None)]);
55+
opts.output_types = OutputTypes::new(&[(OutputType::Exe, None)]);
5656
opts.maybe_sysroot = Some(sysroot);
5757

5858
let descriptions = Registry::new(&rustc::DIAGNOSTICS);
@@ -65,7 +65,7 @@ fn basic_sess(sysroot: PathBuf) -> (Session, Rc<CStore>) {
6565

6666
fn compile(code: String, output: PathBuf, sysroot: PathBuf) {
6767
let (sess, cstore) = basic_sess(sysroot);
68-
let cfg = build_configuration(&sess);
68+
let cfg = build_configuration(&sess, vec![]);
6969
let control = CompileController::basic();
7070

7171
compile_input(&sess, &cstore,

src/test/run-pass-fulldeps/compiler-calls.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ extern crate rustc_errors as errors;
2424
use rustc::session::Session;
2525
use rustc::session::config::{self, Input};
2626
use rustc_driver::{driver, CompilerCalls, Compilation};
27+
use syntax::ast;
2728

2829
use std::path::PathBuf;
2930

@@ -35,6 +36,7 @@ impl<'a> CompilerCalls<'a> for TestCalls {
3536
fn early_callback(&mut self,
3637
_: &getopts::Matches,
3738
_: &config::Options,
39+
_: &ast::CrateConfig,
3840
_: &errors::registry::Registry,
3941
_: config::ErrorOutputType)
4042
-> Compilation {
@@ -45,6 +47,7 @@ impl<'a> CompilerCalls<'a> for TestCalls {
4547
fn late_callback(&mut self,
4648
_: &getopts::Matches,
4749
_: &Session,
50+
_: &ast::CrateConfig,
4851
_: &Input,
4952
_: &Option<PathBuf>,
5053
_: &Option<PathBuf>)
@@ -62,6 +65,7 @@ impl<'a> CompilerCalls<'a> for TestCalls {
6265
fn no_input(&mut self,
6366
_: &getopts::Matches,
6467
_: &config::Options,
68+
_: &ast::CrateConfig,
6569
_: &Option<PathBuf>,
6670
_: &Option<PathBuf>,
6771
_: &errors::registry::Registry)

0 commit comments

Comments
 (0)