Skip to content

Commit 0aaf9d5

Browse files
committed
reduce boilerplate with common enums
1 parent 9bc8bb9 commit 0aaf9d5

File tree

2 files changed

+76
-146
lines changed

2 files changed

+76
-146
lines changed

src/tools/compiletest/src/common.rs

Lines changed: 72 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -12,104 +12,78 @@ use lazycell::LazyCell;
1212
use std::collections::HashSet;
1313
use test::{ColorConfig, OutputFormat};
1414

15-
#[derive(Clone, Copy, PartialEq, Debug)]
16-
pub enum Mode {
17-
RunPassValgrind,
18-
Pretty,
19-
DebugInfo,
20-
Codegen,
21-
Rustdoc,
22-
RustdocJson,
23-
CodegenUnits,
24-
Incremental,
25-
RunMake,
26-
Ui,
27-
JsDocTest,
28-
MirOpt,
29-
Assembly,
30-
}
15+
macro_rules! string_enum {
16+
($(#[$meta:meta])* $vis:vis enum $name:ident { $($variant:ident => $repr:expr,)* }) => {
17+
$(#[$meta])*
18+
$vis enum $name {
19+
$($variant,)*
20+
}
3121

32-
impl Mode {
33-
pub fn disambiguator(self) -> &'static str {
34-
// Pretty-printing tests could run concurrently, and if they do,
35-
// they need to keep their output segregated.
36-
match self {
37-
Pretty => ".pretty",
38-
_ => "",
22+
impl $name {
23+
$vis const VARIANTS: &'static [Self] = &[$(Self::$variant,)*];
24+
25+
$vis fn to_str(&self) -> &'static str {
26+
match self {
27+
$(Self::$variant => $repr,)*
28+
}
29+
}
3930
}
40-
}
41-
}
4231

43-
impl FromStr for Mode {
44-
type Err = ();
45-
fn from_str(s: &str) -> Result<Mode, ()> {
46-
match s {
47-
"run-pass-valgrind" => Ok(RunPassValgrind),
48-
"pretty" => Ok(Pretty),
49-
"debuginfo" => Ok(DebugInfo),
50-
"codegen" => Ok(Codegen),
51-
"rustdoc" => Ok(Rustdoc),
52-
"rustdoc-json" => Ok(RustdocJson),
53-
"codegen-units" => Ok(CodegenUnits),
54-
"incremental" => Ok(Incremental),
55-
"run-make" => Ok(RunMake),
56-
"ui" => Ok(Ui),
57-
"js-doc-test" => Ok(JsDocTest),
58-
"mir-opt" => Ok(MirOpt),
59-
"assembly" => Ok(Assembly),
60-
_ => Err(()),
32+
impl fmt::Display for $name {
33+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34+
fmt::Display::fmt(self.to_str(), f)
35+
}
6136
}
62-
}
63-
}
6437

65-
impl fmt::Display for Mode {
66-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
67-
let s = match *self {
68-
RunPassValgrind => "run-pass-valgrind",
69-
Pretty => "pretty",
70-
DebugInfo => "debuginfo",
71-
Codegen => "codegen",
72-
Rustdoc => "rustdoc",
73-
RustdocJson => "rustdoc-json",
74-
CodegenUnits => "codegen-units",
75-
Incremental => "incremental",
76-
RunMake => "run-make",
77-
Ui => "ui",
78-
JsDocTest => "js-doc-test",
79-
MirOpt => "mir-opt",
80-
Assembly => "assembly",
81-
};
82-
fmt::Display::fmt(s, f)
38+
impl FromStr for $name {
39+
type Err = ();
40+
41+
fn from_str(s: &str) -> Result<Self, ()> {
42+
match s {
43+
$($repr => Ok(Self::$variant),)*
44+
_ => Err(()),
45+
}
46+
}
47+
}
8348
}
8449
}
8550

86-
#[derive(Clone, Copy, PartialEq, Debug, Hash)]
87-
pub enum PassMode {
88-
Check,
89-
Build,
90-
Run,
51+
string_enum! {
52+
#[derive(Clone, Copy, PartialEq, Debug)]
53+
pub enum Mode {
54+
RunPassValgrind => "run-pass-valgrind",
55+
Pretty => "pretty",
56+
DebugInfo => "debuginfo",
57+
Codegen => "codegen",
58+
Rustdoc => "rustdoc",
59+
RustdocJson => "rustdoc-json",
60+
CodegenUnits => "codegen-units",
61+
Incremental => "incremental",
62+
RunMake => "run-make",
63+
Ui => "ui",
64+
JsDocTest => "js-doc-test",
65+
MirOpt => "mir-opt",
66+
Assembly => "assembly",
67+
}
9168
}
9269

93-
impl FromStr for PassMode {
94-
type Err = ();
95-
fn from_str(s: &str) -> Result<Self, ()> {
96-
match s {
97-
"check" => Ok(PassMode::Check),
98-
"build" => Ok(PassMode::Build),
99-
"run" => Ok(PassMode::Run),
100-
_ => Err(()),
70+
impl Mode {
71+
pub fn disambiguator(self) -> &'static str {
72+
// Pretty-printing tests could run concurrently, and if they do,
73+
// they need to keep their output segregated.
74+
match self {
75+
Pretty => ".pretty",
76+
_ => "",
10177
}
10278
}
10379
}
10480

105-
impl fmt::Display for PassMode {
106-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
107-
let s = match *self {
108-
PassMode::Check => "check",
109-
PassMode::Build => "build",
110-
PassMode::Run => "run",
111-
};
112-
fmt::Display::fmt(s, f)
81+
string_enum! {
82+
#[derive(Clone, Copy, PartialEq, Debug, Hash)]
83+
pub enum PassMode {
84+
Check => "check",
85+
Build => "build",
86+
Run => "run",
11387
}
11488
}
11589

@@ -120,69 +94,23 @@ pub enum FailMode {
12094
Run,
12195
}
12296

123-
#[derive(Clone, Debug, PartialEq)]
124-
pub enum CompareMode {
125-
Polonius,
126-
Chalk,
127-
NextSolver,
128-
SplitDwarf,
129-
SplitDwarfSingle,
130-
}
131-
132-
impl CompareMode {
133-
pub(crate) const VARIANTS: &'static [CompareMode] = &[
134-
CompareMode::Polonius,
135-
CompareMode::Chalk,
136-
CompareMode::NextSolver,
137-
CompareMode::SplitDwarf,
138-
CompareMode::SplitDwarfSingle,
139-
];
140-
141-
pub(crate) fn to_str(&self) -> &'static str {
142-
match *self {
143-
CompareMode::Polonius => "polonius",
144-
CompareMode::Chalk => "chalk",
145-
CompareMode::NextSolver => "next-solver",
146-
CompareMode::SplitDwarf => "split-dwarf",
147-
CompareMode::SplitDwarfSingle => "split-dwarf-single",
148-
}
149-
}
150-
151-
pub fn parse(s: String) -> CompareMode {
152-
match s.as_str() {
153-
"polonius" => CompareMode::Polonius,
154-
"chalk" => CompareMode::Chalk,
155-
"next-solver" => CompareMode::NextSolver,
156-
"split-dwarf" => CompareMode::SplitDwarf,
157-
"split-dwarf-single" => CompareMode::SplitDwarfSingle,
158-
x => panic!("unknown --compare-mode option: {}", x),
159-
}
160-
}
161-
}
162-
163-
#[derive(Clone, Copy, Debug, PartialEq)]
164-
pub enum Debugger {
165-
Cdb,
166-
Gdb,
167-
Lldb,
168-
}
169-
170-
impl Debugger {
171-
pub(crate) const VARIANTS: &'static [Debugger] =
172-
&[Debugger::Cdb, Debugger::Gdb, Debugger::Lldb];
173-
174-
pub(crate) fn to_str(&self) -> &'static str {
175-
match self {
176-
Debugger::Cdb => "cdb",
177-
Debugger::Gdb => "gdb",
178-
Debugger::Lldb => "lldb",
179-
}
97+
string_enum! {
98+
#[derive(Clone, Debug, PartialEq)]
99+
pub enum CompareMode {
100+
Polonius => "polonius",
101+
Chalk => "chalk",
102+
NextSolver => "next-solver",
103+
SplitDwarf => "split-dwarf",
104+
SplitDwarfSingle => "split-dwarf-single",
180105
}
181106
}
182107

183-
impl fmt::Display for Debugger {
184-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
185-
fmt::Display::fmt(self.to_str(), f)
108+
string_enum! {
109+
#[derive(Clone, Copy, Debug, PartialEq)]
110+
pub enum Debugger {
111+
Cdb => "cdb",
112+
Gdb => "gdb",
113+
Lldb => "lldb",
186114
}
187115
}
188116

src/tools/compiletest/src/main.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
extern crate test;
77

88
use crate::common::{expected_output_path, output_base_dir, output_relative_path, UI_EXTENSIONS};
9-
use crate::common::{CompareMode, Config, Debugger, Mode, PassMode, TestPaths};
9+
use crate::common::{Config, Debugger, Mode, PassMode, TestPaths};
1010
use crate::util::logv;
1111
use build_helper::git::{get_git_modified_files, get_git_untracked_files};
1212
use core::panic;
@@ -293,7 +293,9 @@ pub fn parse_config(args: Vec<String>) -> Config {
293293
only_modified: matches.opt_present("only-modified"),
294294
color,
295295
remote_test_client: matches.opt_str("remote-test-client").map(PathBuf::from),
296-
compare_mode: matches.opt_str("compare-mode").map(CompareMode::parse),
296+
compare_mode: matches
297+
.opt_str("compare-mode")
298+
.map(|s| s.parse().expect("invalid --compare-mode provided")),
297299
rustfix_coverage: matches.opt_present("rustfix-coverage"),
298300
has_tidy,
299301
channel: matches.opt_str("channel").unwrap(),

0 commit comments

Comments
 (0)