diff options
| author | bors <bors@rust-lang.org> | 2025-01-12 14:43:10 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2025-01-12 14:43:10 +0000 |
| commit | 7bb98889534d597911263d647424557b414770ec (patch) | |
| tree | f07a9619e2f42f2f02986545339f5a4901b9c5e0 /src/tools/compiletest | |
| parent | c0f6a1ce3f89a3240efe5c83839675e20f84e001 (diff) | |
| parent | 2fd11d00422558760dad51406d9edafca5cd4e54 (diff) | |
Auto merge of #135402 - matthiaskrgr:rollup-cz7hs13, r=matthiaskrgr
Rollup of 6 pull requests Successful merges: - #129259 (Add inherent versions of MaybeUninit methods for slices) - #135374 (Suggest typo fix when trait path expression is typo'ed) - #135377 (Make MIR cleanup for functions with impossible predicates into a real MIR pass) - #135378 (Remove a bunch of diagnostic stashing that doesn't do anything) - #135397 (compiletest: add erroneous variant to `string_enum`s conversions error) - #135398 (add more crash tests) r? `@ghost` `@rustbot` modify labels: rollup
Diffstat (limited to 'src/tools/compiletest')
| -rw-r--r-- | src/tools/compiletest/src/common.rs | 10 | ||||
| -rw-r--r-- | src/tools/compiletest/src/tests.rs | 27 |
2 files changed, 34 insertions, 3 deletions
diff --git a/src/tools/compiletest/src/common.rs b/src/tools/compiletest/src/common.rs index 36f876bcdc6..4e2510ed9ab 100644 --- a/src/tools/compiletest/src/common.rs +++ b/src/tools/compiletest/src/common.rs @@ -39,18 +39,22 @@ macro_rules! string_enum { } impl FromStr for $name { - type Err = (); + type Err = String; - fn from_str(s: &str) -> Result<Self, ()> { + fn from_str(s: &str) -> Result<Self, Self::Err> { match s { $($repr => Ok(Self::$variant),)* - _ => Err(()), + _ => Err(format!(concat!("unknown `", stringify!($name), "` variant: `{}`"), s)), } } } } } +// Make the macro visible outside of this module, for tests. +#[cfg(test)] +pub(crate) use string_enum; + string_enum! { #[derive(Clone, Copy, PartialEq, Debug)] pub enum Mode { diff --git a/src/tools/compiletest/src/tests.rs b/src/tools/compiletest/src/tests.rs index fec746904de..43c6dc0a67e 100644 --- a/src/tools/compiletest/src/tests.rs +++ b/src/tools/compiletest/src/tests.rs @@ -66,3 +66,30 @@ fn is_test_test() { assert!(!is_test(&OsString::from("#a_dog_gif"))); assert!(!is_test(&OsString::from("~a_temp_file"))); } + +#[test] +fn string_enums() { + // These imports are needed for the macro-generated code + use std::fmt; + use std::str::FromStr; + + crate::common::string_enum! { + #[derive(Clone, Copy, Debug, PartialEq)] + enum Animal { + Cat => "meow", + Dog => "woof", + } + } + + // General assertions, mostly to silence the dead code warnings + assert_eq!(Animal::VARIANTS.len(), 2); + assert_eq!(Animal::STR_VARIANTS.len(), 2); + + // Correct string conversions + assert_eq!(Animal::Cat, "meow".parse().unwrap()); + assert_eq!(Animal::Dog, "woof".parse().unwrap()); + + // Invalid conversions + let animal = "nya".parse::<Animal>(); + assert_eq!("unknown `Animal` variant: `nya`", animal.unwrap_err()); +} |
