Skip to content

Commit d1dde99

Browse files
committed
auto merge of #8992 : chris-morgan/rust/unreachable-macro, r=brson
Rationale: having a function which fails means that the location of failure which is output is that of the unreachable() function, rather than the caller. This is part of #8991 but is not all of it; current usage of ``std::util::unreachable()`` must remain so for the moment, until a new snapshot is made; then I will remove that function entirely in favour of using this macro.
2 parents d84a7b5 + 6b7b8f2 commit d1dde99

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/libsyntax/ext/expand.rs

+30
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,36 @@ pub fn std_macros() -> @str {
884884
)
885885
)
886886

887+
// FIXME(#6266): change the /* to /** when attributes are supported on macros
888+
// (Though even then—is it going to work according to the clear intent here?)
889+
/*
890+
A utility macro for indicating unreachable code. It will fail if
891+
executed. This is occasionally useful to put after loops that never
892+
terminate normally, but instead directly return from a function.
893+
894+
# Example
895+
896+
~~~ {.rust}
897+
fn choose_weighted_item(v: &[Item]) -> Item {
898+
assert!(!v.is_empty());
899+
let mut so_far = 0u;
900+
for v.each |item| {
901+
so_far += item.weight;
902+
if so_far > 100 {
903+
return item;
904+
}
905+
}
906+
// The above loop always returns, so we must hint to the
907+
// type checker that it isn't possible to get down here
908+
unreachable!();
909+
}
910+
~~~
911+
912+
*/
913+
macro_rules! unreachable (() => (
914+
fail!(\"internal error: entered unreachable code\");
915+
))
916+
887917
macro_rules! condition (
888918

889919
{ pub $c:ident: $input:ty -> $out:ty; } => {

0 commit comments

Comments
 (0)