Skip to content

Commit 6250d77

Browse files
committed
factor out enums from compiletest into build_helper::compiletest
1 parent bcd0683 commit 6250d77

File tree

5 files changed

+160
-152
lines changed

5 files changed

+160
-152
lines changed

src/build_helper/src/compiletest.rs

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
//! Types representing arguments to compiletest.
2+
3+
use std::str::FromStr;
4+
use std::fmt;
5+
pub use self::Mode::*;
6+
7+
macro_rules! string_enum {
8+
($(#[$meta:meta])* $vis:vis enum $name:ident { $($variant:ident => $repr:expr,)* }) => {
9+
$(#[$meta])*
10+
$vis enum $name {
11+
$($variant,)*
12+
}
13+
14+
impl $name {
15+
$vis const VARIANTS: &'static [Self] = &[$(Self::$variant,)*];
16+
$vis const STR_VARIANTS: &'static [&'static str] = &[$(Self::$variant.to_str(),)*];
17+
18+
$vis const fn to_str(&self) -> &'static str {
19+
match self {
20+
$(Self::$variant => $repr,)*
21+
}
22+
}
23+
}
24+
25+
impl fmt::Display for $name {
26+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27+
fmt::Display::fmt(self.to_str(), f)
28+
}
29+
}
30+
31+
impl FromStr for $name {
32+
type Err = String;
33+
34+
fn from_str(s: &str) -> Result<Self, Self::Err> {
35+
match s {
36+
$($repr => Ok(Self::$variant),)*
37+
_ => Err(format!(concat!("unknown `", stringify!($name), "` variant: `{}`"), s)),
38+
}
39+
}
40+
}
41+
}
42+
}
43+
44+
// Make the macro visible outside of this module, for tests.
45+
#[cfg(test)]
46+
pub(crate) use string_enum;
47+
48+
string_enum! {
49+
#[derive(Clone, Copy, PartialEq, Debug)]
50+
pub enum Mode {
51+
Pretty => "pretty",
52+
DebugInfo => "debuginfo",
53+
Codegen => "codegen",
54+
Rustdoc => "rustdoc",
55+
RustdocJson => "rustdoc-json",
56+
CodegenUnits => "codegen-units",
57+
Incremental => "incremental",
58+
RunMake => "run-make",
59+
Ui => "ui",
60+
RustdocJs => "rustdoc-js",
61+
MirOpt => "mir-opt",
62+
Assembly => "assembly",
63+
CoverageMap => "coverage-map",
64+
CoverageRun => "coverage-run",
65+
Crashes => "crashes",
66+
}
67+
}
68+
69+
impl Default for Mode {
70+
fn default() -> Self {
71+
Mode::Ui
72+
}
73+
}
74+
75+
impl Mode {
76+
pub fn aux_dir_disambiguator(self) -> &'static str {
77+
// Pretty-printing tests could run concurrently, and if they do,
78+
// they need to keep their output segregated.
79+
match self {
80+
Pretty => ".pretty",
81+
_ => "",
82+
}
83+
}
84+
85+
pub fn output_dir_disambiguator(self) -> &'static str {
86+
// Coverage tests use the same test files for multiple test modes,
87+
// so each mode should have a separate output directory.
88+
match self {
89+
CoverageMap | CoverageRun => self.to_str(),
90+
_ => "",
91+
}
92+
}
93+
}
94+
95+
string_enum! {
96+
#[derive(Clone, Copy, PartialEq, Debug, Hash)]
97+
pub enum PassMode {
98+
Check => "check",
99+
Build => "build",
100+
Run => "run",
101+
}
102+
}
103+
104+
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd)]
105+
pub enum FailMode {
106+
Check,
107+
Build,
108+
Run,
109+
}
110+
111+
string_enum! {
112+
#[derive(Clone, Debug, PartialEq)]
113+
pub enum CompareMode {
114+
Polonius => "polonius",
115+
NextSolver => "next-solver",
116+
NextSolverCoherence => "next-solver-coherence",
117+
SplitDwarf => "split-dwarf",
118+
SplitDwarfSingle => "split-dwarf-single",
119+
}
120+
}
121+
122+
string_enum! {
123+
#[derive(Clone, Copy, Debug, PartialEq)]
124+
pub enum Debugger {
125+
Cdb => "cdb",
126+
Gdb => "gdb",
127+
Lldb => "lldb",
128+
}
129+
}

src/build_helper/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ pub mod git;
77
pub mod metrics;
88
pub mod stage0_parser;
99
pub mod util;
10+
pub mod compiletest;
11+
#[cfg(test)]
12+
mod tests;
1013

1114
/// The default set of crates for opt-dist to collect LLVM profiles.
1215
pub const LLVM_PGO_CRATES: &[&str] = &[

src/build_helper/src/tests.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#[test]
2+
fn string_enums() {
3+
// These imports are needed for the macro-generated code
4+
use std::fmt;
5+
use std::str::FromStr;
6+
7+
crate::compiletest::string_enum! {
8+
#[derive(Clone, Copy, Debug, PartialEq)]
9+
enum Animal {
10+
Cat => "meow",
11+
Dog => "woof",
12+
}
13+
}
14+
15+
// General assertions, mostly to silence the dead code warnings
16+
assert_eq!(Animal::VARIANTS.len(), 2);
17+
assert_eq!(Animal::STR_VARIANTS.len(), 2);
18+
19+
// Correct string conversions
20+
assert_eq!(Animal::Cat, "meow".parse().unwrap());
21+
assert_eq!(Animal::Dog, "woof".parse().unwrap());
22+
23+
// Invalid conversions
24+
let animal = "nya".parse::<Animal>();
25+
assert_eq!("unknown `Animal` variant: `nya`", animal.unwrap_err());
26+
}

src/tools/compiletest/src/common.rs

Lines changed: 2 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -2,141 +2,17 @@ use std::collections::{BTreeSet, HashMap, HashSet};
22
use std::ffi::OsString;
33
use std::path::{Path, PathBuf};
44
use std::process::Command;
5-
use std::str::FromStr;
65
use std::sync::OnceLock;
7-
use std::{fmt, iter};
6+
use std::iter;
87

98
use build_helper::git::GitConfig;
109
use semver::Version;
1110
use serde::de::{Deserialize, Deserializer, Error as _};
1211
use test::{ColorConfig, OutputFormat};
1312

14-
pub use self::Mode::*;
1513
use crate::util::{PathBufExt, add_dylib_path};
14+
pub use build_helper::compiletest::*;
1615

17-
macro_rules! string_enum {
18-
($(#[$meta:meta])* $vis:vis enum $name:ident { $($variant:ident => $repr:expr,)* }) => {
19-
$(#[$meta])*
20-
$vis enum $name {
21-
$($variant,)*
22-
}
23-
24-
impl $name {
25-
$vis const VARIANTS: &'static [Self] = &[$(Self::$variant,)*];
26-
$vis const STR_VARIANTS: &'static [&'static str] = &[$(Self::$variant.to_str(),)*];
27-
28-
$vis const fn to_str(&self) -> &'static str {
29-
match self {
30-
$(Self::$variant => $repr,)*
31-
}
32-
}
33-
}
34-
35-
impl fmt::Display for $name {
36-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37-
fmt::Display::fmt(self.to_str(), f)
38-
}
39-
}
40-
41-
impl FromStr for $name {
42-
type Err = String;
43-
44-
fn from_str(s: &str) -> Result<Self, Self::Err> {
45-
match s {
46-
$($repr => Ok(Self::$variant),)*
47-
_ => Err(format!(concat!("unknown `", stringify!($name), "` variant: `{}`"), s)),
48-
}
49-
}
50-
}
51-
}
52-
}
53-
54-
// Make the macro visible outside of this module, for tests.
55-
#[cfg(test)]
56-
pub(crate) use string_enum;
57-
58-
string_enum! {
59-
#[derive(Clone, Copy, PartialEq, Debug)]
60-
pub enum Mode {
61-
Pretty => "pretty",
62-
DebugInfo => "debuginfo",
63-
Codegen => "codegen",
64-
Rustdoc => "rustdoc",
65-
RustdocJson => "rustdoc-json",
66-
CodegenUnits => "codegen-units",
67-
Incremental => "incremental",
68-
RunMake => "run-make",
69-
Ui => "ui",
70-
RustdocJs => "rustdoc-js",
71-
MirOpt => "mir-opt",
72-
Assembly => "assembly",
73-
CoverageMap => "coverage-map",
74-
CoverageRun => "coverage-run",
75-
Crashes => "crashes",
76-
}
77-
}
78-
79-
impl Default for Mode {
80-
fn default() -> Self {
81-
Mode::Ui
82-
}
83-
}
84-
85-
impl Mode {
86-
pub fn aux_dir_disambiguator(self) -> &'static str {
87-
// Pretty-printing tests could run concurrently, and if they do,
88-
// they need to keep their output segregated.
89-
match self {
90-
Pretty => ".pretty",
91-
_ => "",
92-
}
93-
}
94-
95-
pub fn output_dir_disambiguator(self) -> &'static str {
96-
// Coverage tests use the same test files for multiple test modes,
97-
// so each mode should have a separate output directory.
98-
match self {
99-
CoverageMap | CoverageRun => self.to_str(),
100-
_ => "",
101-
}
102-
}
103-
}
104-
105-
string_enum! {
106-
#[derive(Clone, Copy, PartialEq, Debug, Hash)]
107-
pub enum PassMode {
108-
Check => "check",
109-
Build => "build",
110-
Run => "run",
111-
}
112-
}
113-
114-
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd)]
115-
pub enum FailMode {
116-
Check,
117-
Build,
118-
Run,
119-
}
120-
121-
string_enum! {
122-
#[derive(Clone, Debug, PartialEq)]
123-
pub enum CompareMode {
124-
Polonius => "polonius",
125-
NextSolver => "next-solver",
126-
NextSolverCoherence => "next-solver-coherence",
127-
SplitDwarf => "split-dwarf",
128-
SplitDwarfSingle => "split-dwarf-single",
129-
}
130-
}
131-
132-
string_enum! {
133-
#[derive(Clone, Copy, Debug, PartialEq)]
134-
pub enum Debugger {
135-
Cdb => "cdb",
136-
Gdb => "gdb",
137-
Lldb => "lldb",
138-
}
139-
}
14016

14117
#[derive(Clone, Copy, Debug, PartialEq, Default, serde::Deserialize)]
14218
#[serde(rename_all = "kebab-case")]

src/tools/compiletest/src/tests.rs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -67,29 +67,3 @@ fn is_test_test() {
6767
assert!(!is_test(&OsString::from("~a_temp_file")));
6868
}
6969

70-
#[test]
71-
fn string_enums() {
72-
// These imports are needed for the macro-generated code
73-
use std::fmt;
74-
use std::str::FromStr;
75-
76-
crate::common::string_enum! {
77-
#[derive(Clone, Copy, Debug, PartialEq)]
78-
enum Animal {
79-
Cat => "meow",
80-
Dog => "woof",
81-
}
82-
}
83-
84-
// General assertions, mostly to silence the dead code warnings
85-
assert_eq!(Animal::VARIANTS.len(), 2);
86-
assert_eq!(Animal::STR_VARIANTS.len(), 2);
87-
88-
// Correct string conversions
89-
assert_eq!(Animal::Cat, "meow".parse().unwrap());
90-
assert_eq!(Animal::Dog, "woof".parse().unwrap());
91-
92-
// Invalid conversions
93-
let animal = "nya".parse::<Animal>();
94-
assert_eq!("unknown `Animal` variant: `nya`", animal.unwrap_err());
95-
}

0 commit comments

Comments
 (0)