Skip to content

Commit a86fcdb

Browse files
committed
Factor out common code in interface tests.
Replacing `mk_session` with `sess_and_cfgs`, which does a bit more of the shared stuff -- the option parsing and the `build_configuration` call.
1 parent a0569fa commit a86fcdb

File tree

1 file changed

+39
-48
lines changed

1 file changed

+39
-48
lines changed

compiler/rustc_interface/src/tests.rs

+39-48
Original file line numberDiff line numberDiff line change
@@ -25,42 +25,46 @@ use std::num::NonZero;
2525
use std::path::{Path, PathBuf};
2626
use std::sync::Arc;
2727

28-
fn mk_session(matches: getopts::Matches) -> (Session, Cfg) {
28+
fn sess_and_cfg<F>(args: &[&'static str], f: F)
29+
where
30+
F: FnOnce(Session, Cfg),
31+
{
2932
let mut early_dcx = EarlyDiagCtxt::new(ErrorOutputType::default());
3033
early_dcx.initialize_checked_jobserver();
3134

32-
let registry = registry::Registry::new(&[]);
35+
let matches = optgroups().parse(args).unwrap();
3336
let sessopts = build_session_options(&mut early_dcx, &matches);
34-
let temps_dir = sessopts.unstable_opts.temps_dir.as_deref().map(PathBuf::from);
35-
let io = CompilerIO {
36-
input: Input::Str { name: FileName::Custom(String::new()), input: String::new() },
37-
output_dir: None,
38-
output_file: None,
39-
temps_dir,
40-
};
41-
4237
let sysroot = filesearch::materialize_sysroot(sessopts.maybe_sysroot.clone());
43-
4438
let target = rustc_session::config::build_target_config(&early_dcx, &sessopts, &sysroot);
4539

46-
let sess = build_session(
47-
early_dcx,
48-
sessopts,
49-
io,
50-
None,
51-
registry,
52-
vec![],
53-
Default::default(),
54-
None,
55-
target,
56-
sysroot,
57-
"",
58-
None,
59-
Arc::default(),
60-
Default::default(),
61-
);
62-
let cfg = parse_cfg(&sess.dcx(), matches.opt_strs("cfg"));
63-
(sess, cfg)
40+
rustc_span::create_default_session_globals_then(|| {
41+
let temps_dir = sessopts.unstable_opts.temps_dir.as_deref().map(PathBuf::from);
42+
let io = CompilerIO {
43+
input: Input::Str { name: FileName::Custom(String::new()), input: String::new() },
44+
output_dir: None,
45+
output_file: None,
46+
temps_dir,
47+
};
48+
let sess = build_session(
49+
early_dcx,
50+
sessopts,
51+
io,
52+
None,
53+
registry::Registry::new(&[]),
54+
vec![],
55+
Default::default(),
56+
None,
57+
target,
58+
sysroot,
59+
"",
60+
None,
61+
Arc::default(),
62+
Default::default(),
63+
);
64+
let cfg = parse_cfg(&sess.dcx(), matches.opt_strs("cfg"));
65+
let cfg = build_configuration(&sess, cfg);
66+
f(sess, cfg)
67+
});
6468
}
6569

6670
fn new_public_extern_entry<S, I>(locations: I) -> ExternEntry
@@ -125,21 +129,15 @@ fn assert_non_crate_hash_different(x: &Options, y: &Options) {
125129
// When the user supplies --test we should implicitly supply --cfg test
126130
#[test]
127131
fn test_switch_implies_cfg_test() {
128-
rustc_span::create_default_session_globals_then(|| {
129-
let matches = optgroups().parse(&["--test".to_string()]).unwrap();
130-
let (sess, cfg) = mk_session(matches);
131-
let cfg = build_configuration(&sess, cfg);
132+
sess_and_cfg(&["--test"], |_sess, cfg| {
132133
assert!(cfg.contains(&(sym::test, None)));
133-
});
134+
})
134135
}
135136

136137
// When the user supplies --test and --cfg test, don't implicitly add another --cfg test
137138
#[test]
138139
fn test_switch_implies_cfg_test_unless_cfg_test() {
139-
rustc_span::create_default_session_globals_then(|| {
140-
let matches = optgroups().parse(&["--test".to_string(), "--cfg=test".to_string()]).unwrap();
141-
let (sess, cfg) = mk_session(matches);
142-
let cfg = build_configuration(&sess, cfg);
140+
sess_and_cfg(&["--test", "--cfg=test"], |_sess, cfg| {
143141
let mut test_items = cfg.iter().filter(|&&(name, _)| name == sym::test);
144142
assert!(test_items.next().is_some());
145143
assert!(test_items.next().is_none());
@@ -148,22 +146,15 @@ fn test_switch_implies_cfg_test_unless_cfg_test() {
148146

149147
#[test]
150148
fn test_can_print_warnings() {
151-
rustc_span::create_default_session_globals_then(|| {
152-
let matches = optgroups().parse(&["-Awarnings".to_string()]).unwrap();
153-
let (sess, _) = mk_session(matches);
149+
sess_and_cfg(&["-Awarnings"], |sess, _cfg| {
154150
assert!(!sess.dcx().can_emit_warnings());
155151
});
156152

157-
rustc_span::create_default_session_globals_then(|| {
158-
let matches =
159-
optgroups().parse(&["-Awarnings".to_string(), "-Dwarnings".to_string()]).unwrap();
160-
let (sess, _) = mk_session(matches);
153+
sess_and_cfg(&["-Awarnings", "-Dwarnings"], |sess, _cfg| {
161154
assert!(sess.dcx().can_emit_warnings());
162155
});
163156

164-
rustc_span::create_default_session_globals_then(|| {
165-
let matches = optgroups().parse(&["-Adead_code".to_string()]).unwrap();
166-
let (sess, _) = mk_session(matches);
157+
sess_and_cfg(&["-Adead_code"], |sess, _cfg| {
167158
assert!(sess.dcx().can_emit_warnings());
168159
});
169160
}

0 commit comments

Comments
 (0)