| 1 |
#![allow(clippy::iter_cloned_collect, clippy::uninlined_format_args)] |
| 2 |
|
| 3 |
use core::fmt::Display; |
| 4 |
#[cfg(feature = "std")] |
| 5 |
use std::path::PathBuf; |
| 6 |
use thiserror::Error; |
| 7 |
|
| 8 |
// Some of the elaborate cases from the rcc codebase, which is a C compiler in |
| 9 |
// Rust. https://github.com/jyn514/rcc/blob/0.8.0/src/data/error.rs |
| 10 |
#[derive(Error, Debug)] |
| 11 |
pub enum CompilerError { |
| 12 |
#[error("cannot shift {} by {maximum} or more bits (got {current})", if *.is_left { "left" } else { "right" })] |
| 13 |
TooManyShiftBits { |
| 14 |
is_left: bool, |
| 15 |
maximum: u64, |
| 16 |
current: u64, |
| 17 |
}, |
| 18 |
|
| 19 |
#[error("#error {}", (.0).iter().copied().collect::<Vec<_>>().join(" "))] |
| 20 |
User(Vec<&'static str>), |
| 21 |
|
| 22 |
#[error("overflow while parsing {}integer literal", |
| 23 |
if let Some(signed) = .is_signed { |
| 24 |
if *signed { "signed "} else { "unsigned "} |
| 25 |
} else { |
| 26 |
"" |
| 27 |
} |
| 28 |
)] |
| 29 |
IntegerOverflow { is_signed: Option<bool> }, |
| 30 |
|
| 31 |
#[error("overflow while parsing {}integer literal", match .is_signed { |
| 32 |
Some(true) => "signed ", |
| 33 |
Some(false) => "unsigned ", |
| 34 |
None => "", |
| 35 |
})] |
| 36 |
IntegerOverflow2 { is_signed: Option<bool> }, |
| 37 |
} |
| 38 |
|
| 39 |
// Examples drawn from Rustup. |
| 40 |
#[derive(Error, Debug)] |
| 41 |
pub enum RustupError { |
| 42 |
#[error( |
| 43 |
"toolchain '{name}' does not contain component {component}{}", |
| 44 |
.suggestion |
| 45 |
.as_ref() |
| 46 |
.map_or_else(String::new, |s| format!("; did you mean '{}'?", s)), |
| 47 |
)] |
| 48 |
UnknownComponent { |
| 49 |
name: String, |
| 50 |
component: String, |
| 51 |
suggestion: Option<String>, |
| 52 |
}, |
| 53 |
} |
| 54 |
|
| 55 |
#[track_caller] |
| 56 |
fn assert<T: Display>(expected: &str, value: T) { |
| 57 |
assert_eq!(expected, value.to_string()); |
| 58 |
} |
| 59 |
|
| 60 |
#[test] |
| 61 |
fn test_rcc() { |
| 62 |
assert( |
| 63 |
"cannot shift left by 32 or more bits (got 50)", |
| 64 |
CompilerError::TooManyShiftBits { |
| 65 |
is_left: true, |
| 66 |
maximum: 32, |
| 67 |
current: 50, |
| 68 |
}, |
| 69 |
); |
| 70 |
|
| 71 |
assert("#error A B C", CompilerError::User(vec!["A", "B", "C"])); |
| 72 |
|
| 73 |
assert( |
| 74 |
"overflow while parsing signed integer literal", |
| 75 |
CompilerError::IntegerOverflow { |
| 76 |
is_signed: Some(true), |
| 77 |
}, |
| 78 |
); |
| 79 |
} |
| 80 |
|
| 81 |
#[test] |
| 82 |
fn test_rustup() { |
| 83 |
assert( |
| 84 |
"toolchain 'nightly' does not contain component clipy; did you mean 'clippy'?", |
| 85 |
RustupError::UnknownComponent { |
| 86 |
name: "nightly".to_owned(), |
| 87 |
component: "clipy".to_owned(), |
| 88 |
suggestion: Some("clippy".to_owned()), |
| 89 |
}, |
| 90 |
); |
| 91 |
} |
| 92 |
|
| 93 |
// Regression test for https://github.com/dtolnay/thiserror/issues/335 |
| 94 |
#[cfg(feature = "std")] |
| 95 |
#[test] |
| 96 |
#[allow(non_snake_case)] |
| 97 |
fn test_assoc_type_equality_constraint() { |
| 98 |
pub trait Trait<T>: Display { |
| 99 |
type A; |
| 100 |
} |
| 101 |
|
| 102 |
impl<T> Trait<T> for i32 { |
| 103 |
type A = i32; |
| 104 |
} |
| 105 |
|
| 106 |
#[derive(Error, Debug)] |
| 107 |
#[error("{A} {b}", b = &0 as &dyn Trait<i32, A = i32>)] |
| 108 |
pub struct Error { |
| 109 |
pub A: PathBuf, |
| 110 |
} |
| 111 |
|
| 112 |
assert( |
| 113 |
"... 0", |
| 114 |
Error { |
| 115 |
A: PathBuf::from("..."), |
| 116 |
}, |
| 117 |
); |
| 118 |
} |