Skip to content

Commit

Permalink
Use bug! instead of unreachable!
Browse files Browse the repository at this point in the history
  • Loading branch information
jhpratt committed Feb 22, 2023
1 parent 0e82474 commit 4edf73a
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 36 deletions.
17 changes: 6 additions & 11 deletions time-macros/src/format_description/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ fn parse_inner<

Some(match next {
lexer::Token::Literal(Spanned { value: _, span: _ }) if NESTED => {
unreachable!("internal error: literal should not be present in nested description")
bug!("literal should not be present in nested description")
}
lexer::Token::Literal(value) => Ok(Item::Literal(value)),
lexer::Token::Bracket {
Expand All @@ -105,23 +105,18 @@ fn parse_inner<
kind: lexer::BracketKind::Closing,
location: _,
} if NESTED => {
unreachable!(
"internal error: closing bracket should be caught by the `if` statement"
)
bug!("closing bracket should be caught by the `if` statement")
}
lexer::Token::Bracket {
kind: lexer::BracketKind::Closing,
location: _,
} => {
unreachable!(
"internal error: closing bracket should have been consumed by \
`parse_component`"
)
bug!("closing bracket should have been consumed by `parse_component`")
}
lexer::Token::ComponentPart { kind: _, value } if NESTED => Ok(Item::Literal(value)),
lexer::Token::ComponentPart { kind: _, value: _ } => unreachable!(
"internal error: component part should have been consumed by `parse_component`"
),
lexer::Token::ComponentPart { kind: _, value: _ } => {
bug!("component part should have been consumed by `parse_component`")
}
})
})
}
Expand Down
6 changes: 2 additions & 4 deletions time-macros/src/format_description/format_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl<'a> From<Box<[Item<'a>]>> for crate::format_description::public::OwnedForma
if let Ok([item]) = <[_; 1]>::try_from(items) {
item.into()
} else {
unreachable!("the length was just checked to be 1")
bug!("the length was just checked to be 1")
}
} else {
Self::Compound(items.into_iter().map(Self::from).collect())
Expand Down Expand Up @@ -181,9 +181,7 @@ macro_rules! component_definition {
then {
match $field {
Some(value) => value.into(),
None => unreachable!(
"internal error: required modifier was not set"
),
None => bug!("required modifier was not set"),
}
} else {
$field.unwrap_or_default().into()
Expand Down
8 changes: 4 additions & 4 deletions time-macros/src/helpers/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ fn parse_lit_str_cooked(mut s: &str) -> Vec<u8> {
continue 'outer;
}
},
_ => unreachable!("invalid escape"),
_ => bug!("invalid escape"),
}
}
b'\r' => {
Expand Down Expand Up @@ -120,7 +120,7 @@ fn parse_lit_byte_str_cooked(mut v: &[u8]) -> Vec<u8> {
continue 'outer;
}
},
_ => unreachable!("invalid escape"),
_ => bug!("invalid escape"),
}
}
b'\r' => {
Expand Down Expand Up @@ -151,7 +151,7 @@ where
b'0'..=b'9' => b1 - b'0',
b'a'..=b'f' => 10 + (b1 - b'a'),
b'A'..=b'F' => 10 + (b1 - b'A'),
_ => unreachable!("invalid hex escape"),
_ => bug!("invalid hex escape"),
};
(ch, &s[2..])
}
Expand All @@ -172,7 +172,7 @@ fn backslash_u(mut s: &str) -> (char, &str) {
continue;
}
b'}' if digits != 0 => break,
_ => unreachable!("invalid unicode escape"),
_ => bug!("invalid unicode escape"),
};
ch *= 0x10;
ch += u32::from(digit);
Expand Down
9 changes: 8 additions & 1 deletion time-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@
clippy::option_if_let_else, // suggests terrible code
)]

macro_rules! bug {
() => { compile_error!("provide an error message to help fix a possible bug") };
($descr:literal $($rest:tt)?) => {
unreachable!(concat!("internal error: ", $descr) $($rest)?)
}
}

#[macro_use]
mod quote;
#[cfg(any(feature = "formatting", feature = "parsing"))]
Expand Down Expand Up @@ -165,7 +172,7 @@ pub fn format_description(input: TokenStream) -> TokenStream {
Some(VersionOrModuleName::Version(version)) => Some(version),
None => None,
// This branch should never occur here, as `false` is the provided as a const parameter.
Some(VersionOrModuleName::ModuleName(_)) => unreachable!(),
Some(VersionOrModuleName::ModuleName(_)) => bug!("branch should never occur"),
};
let (span, string) = helpers::get_string_literal(input)?;
let items = format_description::parse_with_version(version, &string, span)?;
Expand Down
17 changes: 6 additions & 11 deletions time/src/format_description/parse/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ fn parse_inner<

Some(match next {
lexer::Token::Literal(Spanned { value: _, span: _ }) if NESTED => {
unreachable!("internal error: literal should not be present in nested description")
bug!("literal should not be present in nested description")
}
lexer::Token::Literal(value) => Ok(Item::Literal(value)),
lexer::Token::Bracket {
Expand All @@ -153,26 +153,21 @@ fn parse_inner<
kind: lexer::BracketKind::Closing,
location: _,
} if NESTED => {
unreachable!(
"internal error: closing bracket should be caught by the `if` statement"
)
bug!("closing bracket should be caught by the `if` statement")
}
lexer::Token::Bracket {
kind: lexer::BracketKind::Closing,
location: _,
} => {
unreachable!(
"internal error: closing bracket should have been consumed by \
`parse_component`"
)
bug!("closing bracket should have been consumed by `parse_component`")
}
lexer::Token::ComponentPart {
kind: _, // whitespace is significant in nested components
value,
} if NESTED => Ok(Item::Literal(value)),
lexer::Token::ComponentPart { kind: _, value: _ } => unreachable!(
"internal error: component part should have been consumed by `parse_component`"
),
lexer::Token::ComponentPart { kind: _, value: _ } => {
bug!("component part should have been consumed by `parse_component`")
}
})
})
}
Expand Down
6 changes: 2 additions & 4 deletions time/src/format_description/parse/format_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ impl<'a> From<Box<[Item<'a>]>> for crate::format_description::OwnedFormatItem {
if let Ok([item]) = <[_; 1]>::try_from(items) {
item.into()
} else {
unreachable!("the length was just checked to be 1")
bug!("the length was just checked to be 1")
}
} else {
Self::Compound(items.into_iter().map(Self::from).collect())
Expand Down Expand Up @@ -241,9 +241,7 @@ macro_rules! component_definition {
then {
match $field {
Some(value) => value.into(),
None => unreachable!(
"internal error: required modifier was not set"
),
None => bug!("required modifier was not set"),
}
} else {
$field.unwrap_or_default().into()
Expand Down
8 changes: 8 additions & 0 deletions time/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,14 @@ macro_rules! expect_opt {
}
};
}

/// `unreachable!()`, but better.
macro_rules! bug {
() => { compile_error!("provide an error message to help fix a possible bug") };
($descr:literal $($rest:tt)?) => {
unreachable!(concat!("internal error: ", $descr) $($rest)?)
}
}
// endregion macros

#[macro_use]
Expand Down
2 changes: 1 addition & 1 deletion time/src/parsing/parsable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ impl<const CONFIG: EncodedConfig> sealed::Sealed for Iso8601<CONFIG> {
if !date_is_present && !time_is_present && !offset_is_present {
match first_error {
Some(err) => return Err(err),
None => unreachable!("an error should be present if no components were parsed"),
None => bug!("an error should be present if no components were parsed"),
}
}

Expand Down

0 comments on commit 4edf73a

Please sign in to comment.