]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_interface/src/tests.rs
New upstream version 1.65.0+dfsg1
[rustc.git] / compiler / rustc_interface / src / tests.rs
CommitLineData
f2b60f7d 1#![allow(rustc::bad_opt_access)]
e74abb32
XL
2use crate::interface::parse_cfgspecs;
3
dfeec247
XL
4use rustc_data_structures::fx::FxHashSet;
5use rustc_errors::{emitter::HumanReadableErrorType, registry, ColorConfig};
cdc7bbd5 6use rustc_session::config::InstrumentCoverage;
f9f354fc 7use rustc_session::config::Strip;
ba9703b0 8use rustc_session::config::{build_configuration, build_session_options, to_crate_config};
3c0e092e
XL
9use rustc_session::config::{
10 rustc_optgroups, ErrorOutputType, ExternLocation, LocationDetail, Options, Passes,
11};
f035d41b 12use rustc_session::config::{
5e7ed085 13 BranchProtection, Externs, OomStrategy, OutputType, OutputTypes, PAuthKey, PacRet,
064997fb 14 ProcMacroExecutionStrategy, SymbolManglingVersion, WasiExecModel,
f035d41b 15};
5099ac24 16use rustc_session::config::{CFGuard, ExternEntry, LinkerPluginLto, LtoCli, SwitchWithOptPath};
ba9703b0
XL
17use rustc_session::lint::Level;
18use rustc_session::search_paths::SearchPath;
17df50a5 19use rustc_session::utils::{CanonicalizedPath, NativeLib, NativeLibKind};
f9f354fc 20use rustc_session::{build_session, getopts, DiagnosticOutput, Session};
dfeec247
XL
21use rustc_span::edition::{Edition, DEFAULT_EDITION};
22use rustc_span::symbol::sym;
f9f354fc 23use rustc_span::SourceFileHashAlgorithm;
f2b60f7d
FG
24use rustc_target::spec::{CodeModel, LinkerFlavorCli, MergeFunctions, PanicStrategy, RelocModel};
25use rustc_target::spec::{RelroLevel, SanitizerSet, SplitDebuginfo, StackProtector, TlsModel};
cdc7bbd5 26
dc9dc135
XL
27use std::collections::{BTreeMap, BTreeSet};
28use std::iter::FromIterator;
6a06907d 29use std::num::NonZeroUsize;
5869c6ff 30use std::path::{Path, PathBuf};
e74abb32 31
60c5eb7d
XL
32type CfgSpecs = FxHashSet<(String, Option<String>)>;
33
34fn build_session_options_and_crate_config(matches: getopts::Matches) -> (Options, CfgSpecs) {
35 let sessopts = build_session_options(&matches);
36 let cfg = parse_cfgspecs(matches.opt_strs("cfg"));
37 (sessopts, cfg)
38}
39
40fn mk_session(matches: getopts::Matches) -> (Session, CfgSpecs) {
41 let registry = registry::Registry::new(&[]);
42 let (sessopts, cfg) = build_session_options_and_crate_config(matches);
f9f354fc
XL
43 let sess = build_session(
44 sessopts,
45 None,
04454e1e 46 None,
f9f354fc
XL
47 registry,
48 DiagnosticOutput::Default,
49 Default::default(),
50 None,
1b1a35ee 51 None,
f9f354fc 52 );
60c5eb7d 53 (sess, cfg)
e74abb32
XL
54}
55
56fn new_public_extern_entry<S, I>(locations: I) -> ExternEntry
57where
58 S: Into<String>,
60c5eb7d 59 I: IntoIterator<Item = S>,
e74abb32 60{
5869c6ff
XL
61 let locations: BTreeSet<CanonicalizedPath> =
62 locations.into_iter().map(|s| CanonicalizedPath::new(Path::new(&s.into()))).collect();
e74abb32
XL
63
64 ExternEntry {
60c5eb7d
XL
65 location: ExternLocation::ExactPaths(locations),
66 is_private_dep: false,
67 add_prelude: true,
04454e1e 68 nounused_dep: false,
dc9dc135
XL
69 }
70}
71
72fn optgroups() -> getopts::Options {
73 let mut opts = getopts::Options::new();
e74abb32 74 for group in rustc_optgroups() {
dc9dc135
XL
75 (group.apply)(&mut opts);
76 }
77 return opts;
78}
79
80fn mk_map<K: Ord, V>(entries: Vec<(K, V)>) -> BTreeMap<K, V> {
81 BTreeMap::from_iter(entries.into_iter())
82}
83
cdc7bbd5
XL
84fn assert_same_clone(x: &Options) {
85 assert_eq!(x.dep_tracking_hash(true), x.clone().dep_tracking_hash(true));
86 assert_eq!(x.dep_tracking_hash(false), x.clone().dep_tracking_hash(false));
87}
88
89fn assert_same_hash(x: &Options, y: &Options) {
90 assert_eq!(x.dep_tracking_hash(true), y.dep_tracking_hash(true));
91 assert_eq!(x.dep_tracking_hash(false), y.dep_tracking_hash(false));
92 // Check clone
93 assert_same_clone(x);
94 assert_same_clone(y);
95}
96
97fn assert_different_hash(x: &Options, y: &Options) {
98 assert_ne!(x.dep_tracking_hash(true), y.dep_tracking_hash(true));
99 assert_ne!(x.dep_tracking_hash(false), y.dep_tracking_hash(false));
100 // Check clone
101 assert_same_clone(x);
102 assert_same_clone(y);
103}
104
136023e0
XL
105fn assert_non_crate_hash_different(x: &Options, y: &Options) {
106 assert_eq!(x.dep_tracking_hash(true), y.dep_tracking_hash(true));
107 assert_ne!(x.dep_tracking_hash(false), y.dep_tracking_hash(false));
108 // Check clone
109 assert_same_clone(x);
110 assert_same_clone(y);
111}
112
dc9dc135
XL
113// When the user supplies --test we should implicitly supply --cfg test
114#[test]
115fn test_switch_implies_cfg_test() {
136023e0 116 rustc_span::create_default_session_globals_then(|| {
60c5eb7d
XL
117 let matches = optgroups().parse(&["--test".to_string()]).unwrap();
118 let (sess, cfg) = mk_session(matches);
dc9dc135
XL
119 let cfg = build_configuration(&sess, to_crate_config(cfg));
120 assert!(cfg.contains(&(sym::test, None)));
121 });
122}
123
60c5eb7d 124// When the user supplies --test and --cfg test, don't implicitly add another --cfg test
dc9dc135
XL
125#[test]
126fn test_switch_implies_cfg_test_unless_cfg_test() {
136023e0 127 rustc_span::create_default_session_globals_then(|| {
60c5eb7d
XL
128 let matches = optgroups().parse(&["--test".to_string(), "--cfg=test".to_string()]).unwrap();
129 let (sess, cfg) = mk_session(matches);
dc9dc135
XL
130 let cfg = build_configuration(&sess, to_crate_config(cfg));
131 let mut test_items = cfg.iter().filter(|&&(name, _)| name == sym::test);
132 assert!(test_items.next().is_some());
133 assert!(test_items.next().is_none());
134 });
135}
136
137#[test]
138fn test_can_print_warnings() {
136023e0 139 rustc_span::create_default_session_globals_then(|| {
dc9dc135 140 let matches = optgroups().parse(&["-Awarnings".to_string()]).unwrap();
60c5eb7d 141 let (sess, _) = mk_session(matches);
e1599b0c 142 assert!(!sess.diagnostic().can_emit_warnings());
dc9dc135
XL
143 });
144
136023e0 145 rustc_span::create_default_session_globals_then(|| {
dfeec247
XL
146 let matches =
147 optgroups().parse(&["-Awarnings".to_string(), "-Dwarnings".to_string()]).unwrap();
60c5eb7d 148 let (sess, _) = mk_session(matches);
e1599b0c 149 assert!(sess.diagnostic().can_emit_warnings());
dc9dc135
XL
150 });
151
136023e0 152 rustc_span::create_default_session_globals_then(|| {
dc9dc135 153 let matches = optgroups().parse(&["-Adead_code".to_string()]).unwrap();
60c5eb7d 154 let (sess, _) = mk_session(matches);
e1599b0c 155 assert!(sess.diagnostic().can_emit_warnings());
dc9dc135
XL
156 });
157}
158
159#[test]
160fn test_output_types_tracking_hash_different_paths() {
161 let mut v1 = Options::default();
162 let mut v2 = Options::default();
163 let mut v3 = Options::default();
164
dfeec247
XL
165 v1.output_types = OutputTypes::new(&[(OutputType::Exe, Some(PathBuf::from("./some/thing")))]);
166 v2.output_types = OutputTypes::new(&[(OutputType::Exe, Some(PathBuf::from("/some/thing")))]);
dc9dc135
XL
167 v3.output_types = OutputTypes::new(&[(OutputType::Exe, None)]);
168
136023e0
XL
169 assert_non_crate_hash_different(&v1, &v2);
170 assert_non_crate_hash_different(&v1, &v3);
171 assert_non_crate_hash_different(&v2, &v3);
dc9dc135
XL
172}
173
174#[test]
175fn test_output_types_tracking_hash_different_construction_order() {
176 let mut v1 = Options::default();
177 let mut v2 = Options::default();
178
179 v1.output_types = OutputTypes::new(&[
180 (OutputType::Exe, Some(PathBuf::from("./some/thing"))),
181 (OutputType::Bitcode, Some(PathBuf::from("./some/thing.bc"))),
182 ]);
183
184 v2.output_types = OutputTypes::new(&[
185 (OutputType::Bitcode, Some(PathBuf::from("./some/thing.bc"))),
186 (OutputType::Exe, Some(PathBuf::from("./some/thing"))),
187 ]);
188
cdc7bbd5 189 assert_same_hash(&v1, &v2);
dc9dc135
XL
190}
191
192#[test]
193fn test_externs_tracking_hash_different_construction_order() {
194 let mut v1 = Options::default();
195 let mut v2 = Options::default();
196 let mut v3 = Options::default();
197
198 v1.externs = Externs::new(mk_map(vec![
dfeec247
XL
199 (String::from("a"), new_public_extern_entry(vec!["b", "c"])),
200 (String::from("d"), new_public_extern_entry(vec!["e", "f"])),
dc9dc135
XL
201 ]));
202
203 v2.externs = Externs::new(mk_map(vec![
dfeec247
XL
204 (String::from("d"), new_public_extern_entry(vec!["e", "f"])),
205 (String::from("a"), new_public_extern_entry(vec!["b", "c"])),
dc9dc135
XL
206 ]));
207
208 v3.externs = Externs::new(mk_map(vec![
dfeec247
XL
209 (String::from("a"), new_public_extern_entry(vec!["b", "c"])),
210 (String::from("d"), new_public_extern_entry(vec!["f", "e"])),
dc9dc135
XL
211 ]));
212
cdc7bbd5
XL
213 assert_same_hash(&v1, &v2);
214 assert_same_hash(&v1, &v3);
215 assert_same_hash(&v2, &v3);
dc9dc135
XL
216}
217
218#[test]
219fn test_lints_tracking_hash_different_values() {
220 let mut v1 = Options::default();
221 let mut v2 = Options::default();
222 let mut v3 = Options::default();
223
224 v1.lint_opts = vec![
dfeec247
XL
225 (String::from("a"), Level::Allow),
226 (String::from("b"), Level::Warn),
227 (String::from("c"), Level::Deny),
228 (String::from("d"), Level::Forbid),
dc9dc135
XL
229 ];
230
231 v2.lint_opts = vec![
dfeec247
XL
232 (String::from("a"), Level::Allow),
233 (String::from("b"), Level::Warn),
234 (String::from("X"), Level::Deny),
235 (String::from("d"), Level::Forbid),
dc9dc135
XL
236 ];
237
238 v3.lint_opts = vec![
dfeec247
XL
239 (String::from("a"), Level::Allow),
240 (String::from("b"), Level::Warn),
241 (String::from("c"), Level::Forbid),
242 (String::from("d"), Level::Deny),
dc9dc135
XL
243 ];
244
136023e0
XL
245 assert_non_crate_hash_different(&v1, &v2);
246 assert_non_crate_hash_different(&v1, &v3);
247 assert_non_crate_hash_different(&v2, &v3);
dc9dc135
XL
248}
249
250#[test]
251fn test_lints_tracking_hash_different_construction_order() {
252 let mut v1 = Options::default();
253 let mut v2 = Options::default();
254
255 v1.lint_opts = vec![
dfeec247
XL
256 (String::from("a"), Level::Allow),
257 (String::from("b"), Level::Warn),
258 (String::from("c"), Level::Deny),
259 (String::from("d"), Level::Forbid),
dc9dc135
XL
260 ];
261
262 v2.lint_opts = vec![
dfeec247
XL
263 (String::from("a"), Level::Allow),
264 (String::from("c"), Level::Deny),
265 (String::from("b"), Level::Warn),
266 (String::from("d"), Level::Forbid),
dc9dc135
XL
267 ];
268
17df50a5 269 // The hash should be order-dependent
136023e0
XL
270 assert_non_crate_hash_different(&v1, &v2);
271}
272
273#[test]
274fn test_lint_cap_hash_different() {
275 let mut v1 = Options::default();
276 let mut v2 = Options::default();
277 let v3 = Options::default();
278
279 v1.lint_cap = Some(Level::Forbid);
280 v2.lint_cap = Some(Level::Allow);
281
282 assert_non_crate_hash_different(&v1, &v2);
283 assert_non_crate_hash_different(&v1, &v3);
284 assert_non_crate_hash_different(&v2, &v3);
dc9dc135
XL
285}
286
287#[test]
288fn test_search_paths_tracking_hash_different_order() {
289 let mut v1 = Options::default();
290 let mut v2 = Options::default();
291 let mut v3 = Options::default();
292 let mut v4 = Options::default();
293
e74abb32 294 const JSON: ErrorOutputType = ErrorOutputType::Json {
dc9dc135 295 pretty: false,
e74abb32 296 json_rendered: HumanReadableErrorType::Default(ColorConfig::Never),
dc9dc135
XL
297 };
298
299 // Reference
dfeec247
XL
300 v1.search_paths.push(SearchPath::from_cli_opt("native=abc", JSON));
301 v1.search_paths.push(SearchPath::from_cli_opt("crate=def", JSON));
302 v1.search_paths.push(SearchPath::from_cli_opt("dependency=ghi", JSON));
303 v1.search_paths.push(SearchPath::from_cli_opt("framework=jkl", JSON));
304 v1.search_paths.push(SearchPath::from_cli_opt("all=mno", JSON));
305
306 v2.search_paths.push(SearchPath::from_cli_opt("native=abc", JSON));
307 v2.search_paths.push(SearchPath::from_cli_opt("dependency=ghi", JSON));
308 v2.search_paths.push(SearchPath::from_cli_opt("crate=def", JSON));
309 v2.search_paths.push(SearchPath::from_cli_opt("framework=jkl", JSON));
310 v2.search_paths.push(SearchPath::from_cli_opt("all=mno", JSON));
311
312 v3.search_paths.push(SearchPath::from_cli_opt("crate=def", JSON));
313 v3.search_paths.push(SearchPath::from_cli_opt("framework=jkl", JSON));
314 v3.search_paths.push(SearchPath::from_cli_opt("native=abc", JSON));
315 v3.search_paths.push(SearchPath::from_cli_opt("dependency=ghi", JSON));
316 v3.search_paths.push(SearchPath::from_cli_opt("all=mno", JSON));
317
318 v4.search_paths.push(SearchPath::from_cli_opt("all=mno", JSON));
319 v4.search_paths.push(SearchPath::from_cli_opt("native=abc", JSON));
320 v4.search_paths.push(SearchPath::from_cli_opt("crate=def", JSON));
321 v4.search_paths.push(SearchPath::from_cli_opt("dependency=ghi", JSON));
322 v4.search_paths.push(SearchPath::from_cli_opt("framework=jkl", JSON));
dc9dc135 323
cdc7bbd5
XL
324 assert_same_hash(&v1, &v2);
325 assert_same_hash(&v1, &v3);
326 assert_same_hash(&v1, &v4);
dc9dc135
XL
327}
328
329#[test]
330fn test_native_libs_tracking_hash_different_values() {
331 let mut v1 = Options::default();
332 let mut v2 = Options::default();
333 let mut v3 = Options::default();
334 let mut v4 = Options::default();
17df50a5 335 let mut v5 = Options::default();
dc9dc135
XL
336
337 // Reference
338 v1.libs = vec![
17df50a5
XL
339 NativeLib {
340 name: String::from("a"),
341 new_name: None,
342 kind: NativeLibKind::Static { bundle: None, whole_archive: None },
343 verbatim: None,
344 },
345 NativeLib {
346 name: String::from("b"),
347 new_name: None,
348 kind: NativeLibKind::Framework { as_needed: None },
349 verbatim: None,
350 },
351 NativeLib {
352 name: String::from("c"),
353 new_name: None,
354 kind: NativeLibKind::Unspecified,
355 verbatim: None,
356 },
dc9dc135
XL
357 ];
358
359 // Change label
360 v2.libs = vec![
17df50a5
XL
361 NativeLib {
362 name: String::from("a"),
363 new_name: None,
364 kind: NativeLibKind::Static { bundle: None, whole_archive: None },
365 verbatim: None,
366 },
367 NativeLib {
368 name: String::from("X"),
369 new_name: None,
370 kind: NativeLibKind::Framework { as_needed: None },
371 verbatim: None,
372 },
373 NativeLib {
374 name: String::from("c"),
375 new_name: None,
376 kind: NativeLibKind::Unspecified,
377 verbatim: None,
378 },
dc9dc135
XL
379 ];
380
381 // Change kind
382 v3.libs = vec![
17df50a5
XL
383 NativeLib {
384 name: String::from("a"),
385 new_name: None,
386 kind: NativeLibKind::Static { bundle: None, whole_archive: None },
387 verbatim: None,
388 },
389 NativeLib {
390 name: String::from("b"),
391 new_name: None,
392 kind: NativeLibKind::Static { bundle: None, whole_archive: None },
393 verbatim: None,
394 },
395 NativeLib {
396 name: String::from("c"),
397 new_name: None,
398 kind: NativeLibKind::Unspecified,
399 verbatim: None,
400 },
dc9dc135
XL
401 ];
402
403 // Change new-name
404 v4.libs = vec![
17df50a5
XL
405 NativeLib {
406 name: String::from("a"),
407 new_name: None,
408 kind: NativeLibKind::Static { bundle: None, whole_archive: None },
409 verbatim: None,
410 },
411 NativeLib {
412 name: String::from("b"),
413 new_name: Some(String::from("X")),
414 kind: NativeLibKind::Framework { as_needed: None },
415 verbatim: None,
416 },
417 NativeLib {
418 name: String::from("c"),
419 new_name: None,
420 kind: NativeLibKind::Unspecified,
421 verbatim: None,
422 },
423 ];
424
425 // Change verbatim
426 v5.libs = vec![
427 NativeLib {
428 name: String::from("a"),
429 new_name: None,
430 kind: NativeLibKind::Static { bundle: None, whole_archive: None },
431 verbatim: None,
432 },
433 NativeLib {
434 name: String::from("b"),
435 new_name: None,
436 kind: NativeLibKind::Framework { as_needed: None },
437 verbatim: Some(true),
438 },
439 NativeLib {
440 name: String::from("c"),
441 new_name: None,
442 kind: NativeLibKind::Unspecified,
443 verbatim: None,
444 },
dc9dc135
XL
445 ];
446
cdc7bbd5
XL
447 assert_different_hash(&v1, &v2);
448 assert_different_hash(&v1, &v3);
449 assert_different_hash(&v1, &v4);
17df50a5 450 assert_different_hash(&v1, &v5);
dc9dc135
XL
451}
452
453#[test]
454fn test_native_libs_tracking_hash_different_order() {
455 let mut v1 = Options::default();
456 let mut v2 = Options::default();
457 let mut v3 = Options::default();
458
459 // Reference
460 v1.libs = vec![
17df50a5
XL
461 NativeLib {
462 name: String::from("a"),
463 new_name: None,
464 kind: NativeLibKind::Static { bundle: None, whole_archive: None },
465 verbatim: None,
466 },
467 NativeLib {
468 name: String::from("b"),
469 new_name: None,
470 kind: NativeLibKind::Framework { as_needed: None },
471 verbatim: None,
472 },
473 NativeLib {
474 name: String::from("c"),
475 new_name: None,
476 kind: NativeLibKind::Unspecified,
477 verbatim: None,
478 },
dc9dc135
XL
479 ];
480
481 v2.libs = vec![
17df50a5
XL
482 NativeLib {
483 name: String::from("b"),
484 new_name: None,
485 kind: NativeLibKind::Framework { as_needed: None },
486 verbatim: None,
487 },
488 NativeLib {
489 name: String::from("a"),
490 new_name: None,
491 kind: NativeLibKind::Static { bundle: None, whole_archive: None },
492 verbatim: None,
493 },
494 NativeLib {
495 name: String::from("c"),
496 new_name: None,
497 kind: NativeLibKind::Unspecified,
498 verbatim: None,
499 },
dc9dc135
XL
500 ];
501
502 v3.libs = vec![
17df50a5
XL
503 NativeLib {
504 name: String::from("c"),
505 new_name: None,
506 kind: NativeLibKind::Unspecified,
507 verbatim: None,
508 },
509 NativeLib {
510 name: String::from("a"),
511 new_name: None,
512 kind: NativeLibKind::Static { bundle: None, whole_archive: None },
513 verbatim: None,
514 },
515 NativeLib {
516 name: String::from("b"),
517 new_name: None,
518 kind: NativeLibKind::Framework { as_needed: None },
519 verbatim: None,
520 },
dc9dc135
XL
521 ];
522
17df50a5
XL
523 // The hash should be order-dependent
524 assert_different_hash(&v1, &v2);
525 assert_different_hash(&v1, &v3);
526 assert_different_hash(&v2, &v3);
dc9dc135
XL
527}
528
529#[test]
530fn test_codegen_options_tracking_hash() {
531 let reference = Options::default();
532 let mut opts = Options::default();
533
f9f354fc
XL
534 macro_rules! untracked {
535 ($name: ident, $non_default_value: expr) => {
cdc7bbd5 536 assert_ne!(opts.cg.$name, $non_default_value);
f9f354fc 537 opts.cg.$name = $non_default_value;
cdc7bbd5 538 assert_same_hash(&reference, &opts);
f9f354fc
XL
539 };
540 }
dc9dc135 541
f9f354fc
XL
542 // Make sure that changing an [UNTRACKED] option leaves the hash unchanged.
543 // This list is in alphabetical order.
544 untracked!(ar, String::from("abc"));
545 untracked!(codegen_units, Some(42));
546 untracked!(default_linker_libraries, true);
547 untracked!(extra_filename, String::from("extra-filename"));
548 untracked!(incremental, Some(String::from("abc")));
549 // `link_arg` is omitted because it just forwards to `link_args`.
550 untracked!(link_args, vec![String::from("abc"), String::from("def")]);
1b1a35ee 551 untracked!(link_self_contained, Some(true));
f9f354fc 552 untracked!(linker, Some(PathBuf::from("linker")));
f2b60f7d 553 untracked!(linker_flavor, Some(LinkerFlavorCli::Gcc));
f9f354fc
XL
554 untracked!(no_stack_check, true);
555 untracked!(remark, Passes::Some(vec![String::from("pass1"), String::from("pass2")]));
556 untracked!(rpath, true);
557 untracked!(save_temps, true);
3c0e092e 558 untracked!(strip, Strip::Debuginfo);
f9f354fc
XL
559
560 macro_rules! tracked {
561 ($name: ident, $non_default_value: expr) => {
562 opts = reference.clone();
cdc7bbd5 563 assert_ne!(opts.cg.$name, $non_default_value);
f9f354fc 564 opts.cg.$name = $non_default_value;
cdc7bbd5 565 assert_different_hash(&reference, &opts);
f9f354fc
XL
566 };
567 }
dc9dc135 568
f9f354fc
XL
569 // Make sure that changing a [TRACKED] option changes the hash.
570 // This list is in alphabetical order.
571 tracked!(code_model, Some(CodeModel::Large));
3dfed10e 572 tracked!(control_flow_guard, CFGuard::Checks);
f9f354fc
XL
573 tracked!(debug_assertions, Some(true));
574 tracked!(debuginfo, 0xdeadbeef);
575 tracked!(embed_bitcode, false);
576 tracked!(force_frame_pointers, Some(false));
577 tracked!(force_unwind_tables, Some(true));
578 tracked!(inline_threshold, Some(0xf007ba11));
5099ac24 579 tracked!(instrument_coverage, Some(InstrumentCoverage::All));
f9f354fc 580 tracked!(linker_plugin_lto, LinkerPluginLto::LinkerPluginAuto);
cdc7bbd5 581 tracked!(link_dead_code, Some(true));
f9f354fc
XL
582 tracked!(llvm_args, vec![String::from("1"), String::from("2")]);
583 tracked!(lto, LtoCli::Fat);
584 tracked!(metadata, vec![String::from("A"), String::from("B")]);
585 tracked!(no_prepopulate_passes, true);
586 tracked!(no_redzone, Some(true));
587 tracked!(no_vectorize_loops, true);
588 tracked!(no_vectorize_slp, true);
589 tracked!(opt_level, "3".to_string());
590 tracked!(overflow_checks, Some(true));
591 tracked!(panic, Some(PanicStrategy::Abort));
592 tracked!(passes, vec![String::from("1"), String::from("2")]);
593 tracked!(prefer_dynamic, true);
594 tracked!(profile_generate, SwitchWithOptPath::Enabled(None));
595 tracked!(profile_use, Some(PathBuf::from("abc")));
596 tracked!(relocation_model, Some(RelocModel::Pic));
597 tracked!(soft_float, true);
5869c6ff 598 tracked!(split_debuginfo, Some(SplitDebuginfo::Packed));
a2a8927a 599 tracked!(symbol_mangling_version, Some(SymbolManglingVersion::V0));
f9f354fc
XL
600 tracked!(target_cpu, Some(String::from("abc")));
601 tracked!(target_feature, String::from("all the features, all of them"));
dc9dc135
XL
602}
603
cdc7bbd5
XL
604#[test]
605fn test_top_level_options_tracked_no_crate() {
606 let reference = Options::default();
607 let mut opts;
608
609 macro_rules! tracked {
610 ($name: ident, $non_default_value: expr) => {
611 opts = reference.clone();
612 assert_ne!(opts.$name, $non_default_value);
613 opts.$name = $non_default_value;
614 // The crate hash should be the same
615 assert_eq!(reference.dep_tracking_hash(true), opts.dep_tracking_hash(true));
616 // The incremental hash should be different
617 assert_ne!(reference.dep_tracking_hash(false), opts.dep_tracking_hash(false));
618 };
619 }
620
621 // Make sure that changing a [TRACKED_NO_CRATE_HASH] option leaves the crate hash unchanged but changes the incremental hash.
622 // This list is in alphabetical order.
623 tracked!(remap_path_prefix, vec![("/home/bors/rust".into(), "src".into())]);
624 tracked!(
625 real_rust_source_base_dir,
626 Some("/home/bors/rust/.rustup/toolchains/nightly/lib/rustlib/src/rust".into())
627 );
628}
629
dc9dc135 630#[test]
064997fb 631fn test_unstable_options_tracking_hash() {
dc9dc135
XL
632 let reference = Options::default();
633 let mut opts = Options::default();
634
f9f354fc
XL
635 macro_rules! untracked {
636 ($name: ident, $non_default_value: expr) => {
064997fb
FG
637 assert_ne!(opts.unstable_opts.$name, $non_default_value);
638 opts.unstable_opts.$name = $non_default_value;
cdc7bbd5 639 assert_same_hash(&reference, &opts);
f9f354fc
XL
640 };
641 }
642
643 // Make sure that changing an [UNTRACKED] option leaves the hash unchanged.
644 // This list is in alphabetical order.
3c0e092e 645 untracked!(assert_incr_state, Some(String::from("loaded")));
cdc7bbd5 646 untracked!(deduplicate_diagnostics, false);
f9f354fc 647 untracked!(dep_tasks, true);
5099ac24 648 untracked!(dlltool, Some(PathBuf::from("custom_dlltool.exe")));
f9f354fc
XL
649 untracked!(dont_buffer_diagnostics, true);
650 untracked!(dump_dep_graph, true);
064997fb 651 untracked!(dump_drop_tracking_cfg, Some("cfg.dot".to_string()));
f9f354fc
XL
652 untracked!(dump_mir, Some(String::from("abc")));
653 untracked!(dump_mir_dataflow, true);
654 untracked!(dump_mir_dir, String::from("abc"));
655 untracked!(dump_mir_exclude_pass_number, true);
656 untracked!(dump_mir_graphviz, true);
657 untracked!(emit_stack_sizes, true);
136023e0 658 untracked!(future_incompat_test, true);
f9f354fc
XL
659 untracked!(hir_stats, true);
660 untracked!(identify_regions, true);
661 untracked!(incremental_ignore_spans, true);
662 untracked!(incremental_info, true);
663 untracked!(incremental_verify_ich, true);
664 untracked!(input_stats, true);
665 untracked!(keep_hygiene_data, true);
666 untracked!(link_native_libraries, false);
667 untracked!(llvm_time_trace, true);
668 untracked!(ls, true);
669 untracked!(macro_backtrace, true);
670 untracked!(meta_stats, true);
064997fb 671 untracked!(mir_pretty_relative_line_numbers, true);
f9f354fc
XL
672 untracked!(nll_facts, true);
673 untracked!(no_analysis, true);
674 untracked!(no_interleave_lints, true);
675 untracked!(no_leak_check, true);
676 untracked!(no_parallel_llvm, true);
677 untracked!(parse_only, true);
678 untracked!(perf_stats, true);
f9f354fc
XL
679 // `pre_link_arg` is omitted because it just forwards to `pre_link_args`.
680 untracked!(pre_link_args, vec![String::from("abc"), String::from("def")]);
136023e0 681 untracked!(profile_closures, true);
f9f354fc
XL
682 untracked!(print_llvm_passes, true);
683 untracked!(print_mono_items, Some(String::from("abc")));
f9f354fc 684 untracked!(print_type_sizes, true);
1b1a35ee 685 untracked!(proc_macro_backtrace, true);
064997fb 686 untracked!(proc_macro_execution_strategy, ProcMacroExecutionStrategy::CrossThread);
f9f354fc 687 untracked!(query_dep_graph, true);
f9f354fc
XL
688 untracked!(save_analysis, true);
689 untracked!(self_profile, SwitchWithOptPath::Enabled(None));
690 untracked!(self_profile_events, Some(vec![String::new()]));
f035d41b 691 untracked!(span_debug, true);
f9f354fc 692 untracked!(span_free_formats, true);
3c0e092e 693 untracked!(temps_dir, Some(String::from("abc")));
f9f354fc
XL
694 untracked!(threads, 99);
695 untracked!(time, true);
696 untracked!(time_llvm_passes, true);
697 untracked!(time_passes, true);
698 untracked!(trace_macros, true);
1b1a35ee 699 untracked!(trim_diagnostic_paths, false);
f9f354fc
XL
700 untracked!(ui_testing, true);
701 untracked!(unpretty, Some("expanded".to_string()));
702 untracked!(unstable_options, true);
703 untracked!(validate_mir, true);
704 untracked!(verbose, true);
705
706 macro_rules! tracked {
707 ($name: ident, $non_default_value: expr) => {
708 opts = reference.clone();
064997fb
FG
709 assert_ne!(opts.unstable_opts.$name, $non_default_value);
710 opts.unstable_opts.$name = $non_default_value;
cdc7bbd5 711 assert_different_hash(&reference, &opts);
f9f354fc
XL
712 };
713 }
714
715 // Make sure that changing a [TRACKED] option changes the hash.
716 // This list is in alphabetical order.
717 tracked!(allow_features, Some(vec![String::from("lang_items")]));
718 tracked!(always_encode_mir, true);
719 tracked!(asm_comments, true);
3c0e092e 720 tracked!(assume_incomplete_release, true);
f9f354fc 721 tracked!(binary_dep_depinfo, true);
064997fb 722 tracked!(box_noalias, Some(false));
5099ac24
FG
723 tracked!(
724 branch_protection,
725 Some(BranchProtection {
726 bti: true,
727 pac_ret: Some(PacRet { leaf: true, key: PAuthKey::B })
728 })
729 );
f9f354fc
XL
730 tracked!(chalk, true);
731 tracked!(codegen_backend, Some("abc".to_string()));
732 tracked!(crate_attr, vec!["abc".to_string()]);
c295e0f8 733 tracked!(debug_info_for_profiling, true);
f9f354fc
XL
734 tracked!(debug_macros, true);
735 tracked!(dep_info_omit_d_target, true);
5099ac24 736 tracked!(drop_tracking, true);
064997fb 737 tracked!(export_executable_symbols, true);
f9f354fc 738 tracked!(dual_proc_macros, true);
064997fb
FG
739 tracked!(dwarf_version, Some(5));
740 tracked!(emit_thin_lto, false);
fc512014 741 tracked!(fewer_names, Some(true));
f9f354fc
XL
742 tracked!(force_unstable_if_unmarked, true);
743 tracked!(fuel, Some(("abc".to_string(), 99)));
29967ef6 744 tracked!(function_sections, Some(false));
f9f354fc
XL
745 tracked!(human_readable_cgu_names, true);
746 tracked!(inline_in_all_cgus, Some(true));
6a06907d 747 tracked!(inline_mir, Some(true));
6a06907d 748 tracked!(inline_mir_hint_threshold, Some(123));
3c0e092e 749 tracked!(inline_mir_threshold, Some(123));
cdc7bbd5 750 tracked!(instrument_coverage, Some(InstrumentCoverage::All));
f9f354fc
XL
751 tracked!(instrument_mcount, true);
752 tracked!(link_only, true);
136023e0 753 tracked!(llvm_plugins, vec![String::from("plugin_name")]);
3c0e092e 754 tracked!(location_detail, LocationDetail { file: true, line: false, column: false });
f9f354fc
XL
755 tracked!(merge_functions, Some(MergeFunctions::Disabled));
756 tracked!(mir_emit_retag, true);
04454e1e 757 tracked!(mir_enable_passes, vec![("DestProp".to_string(), false)]);
6a06907d 758 tracked!(mir_opt_level, Some(4));
94222f64 759 tracked!(move_size_limit, Some(4096));
cdc7bbd5 760 tracked!(mutable_noalias, Some(true));
17df50a5 761 tracked!(new_llvm_pass_manager, Some(true));
f9f354fc
XL
762 tracked!(no_generate_arange_section, true);
763 tracked!(no_link, true);
3c0e092e 764 tracked!(no_unique_section_names, true);
94222f64 765 tracked!(no_profiler_runtime, true);
5e7ed085 766 tracked!(oom, OomStrategy::Panic);
f9f354fc 767 tracked!(osx_rpath_install_name, true);
f2b60f7d 768 tracked!(packed_bundled_libs, true);
f9f354fc 769 tracked!(panic_abort_tests, true);
c295e0f8 770 tracked!(panic_in_drop, PanicStrategy::Abort);
3c0e092e 771 tracked!(pick_stable_methods_before_any_unstable, false);
f9f354fc 772 tracked!(plt, Some(true));
fc512014 773 tracked!(polonius, true);
1b1a35ee 774 tracked!(precise_enum_drop_elaboration, false);
f9f354fc
XL
775 tracked!(print_fuel, Some("abc".to_string()));
776 tracked!(profile, true);
777 tracked!(profile_emit, Some(PathBuf::from("abc")));
94222f64 778 tracked!(profiler_runtime, "abc".to_string());
c295e0f8 779 tracked!(profile_sample_use, Some(PathBuf::from("abc")));
29967ef6 780 tracked!(relax_elf_relocations, Some(true));
f9f354fc 781 tracked!(relro_level, Some(RelroLevel::Full));
c295e0f8 782 tracked!(remap_cwd_prefix, Some(PathBuf::from("abc")));
f9f354fc 783 tracked!(report_delayed_bugs, true);
f035d41b 784 tracked!(sanitizer, SanitizerSet::ADDRESS);
f9f354fc 785 tracked!(sanitizer_memory_track_origins, 2);
f035d41b 786 tracked!(sanitizer_recover, SanitizerSet::ADDRESS);
f9f354fc
XL
787 tracked!(saturating_float_casts, Some(true));
788 tracked!(share_generics, Some(true));
789 tracked!(show_span, Some(String::from("abc")));
3c0e092e 790 tracked!(simulate_remapped_rust_src_base, Some(PathBuf::from("/rustc/abc")));
f9f354fc 791 tracked!(src_hash_algorithm, Some(SourceFileHashAlgorithm::Sha1));
3c0e092e 792 tracked!(stack_protector, StackProtector::All);
fc512014 793 tracked!(symbol_mangling_version, Some(SymbolManglingVersion::V0));
f9f354fc
XL
794 tracked!(teach, true);
795 tracked!(thinlto, Some(true));
17df50a5 796 tracked!(thir_unsafeck, true);
f9f354fc 797 tracked!(tls_model, Some(TlsModel::GeneralDynamic));
923072b8 798 tracked!(translate_remapped_path_to_local_path, false);
fc512014 799 tracked!(trap_unreachable, Some(false));
6a06907d 800 tracked!(treat_err_as_bug, NonZeroUsize::new(1));
3c0e092e 801 tracked!(tune_cpu, Some(String::from("abc")));
5e7ed085 802 tracked!(uninit_const_chunk_threshold, 123);
f9f354fc
XL
803 tracked!(unleash_the_miri_inside_of_you, true);
804 tracked!(use_ctors_section, Some(true));
805 tracked!(verify_llvm_ir, true);
923072b8 806 tracked!(virtual_function_elimination, true);
5869c6ff 807 tracked!(wasi_exec_model, Some(WasiExecModel::Reactor));
136023e0
XL
808
809 macro_rules! tracked_no_crate_hash {
810 ($name: ident, $non_default_value: expr) => {
811 opts = reference.clone();
064997fb
FG
812 assert_ne!(opts.unstable_opts.$name, $non_default_value);
813 opts.unstable_opts.$name = $non_default_value;
136023e0
XL
814 assert_non_crate_hash_different(&reference, &opts);
815 };
816 }
817 tracked_no_crate_hash!(no_codegen, true);
dc9dc135
XL
818}
819
820#[test]
821fn test_edition_parsing() {
822 // test default edition
823 let options = Options::default();
824 assert!(options.edition == DEFAULT_EDITION);
825
dfeec247 826 let matches = optgroups().parse(&["--edition=2018".to_string()]).unwrap();
60c5eb7d 827 let (sessopts, _) = build_session_options_and_crate_config(matches);
dc9dc135
XL
828 assert!(sessopts.edition == Edition::Edition2018)
829}