Skip to content

Commit

Permalink
Add an unreachable!() macro.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
chris-morgan committed Sep 5, 2013
1 parent 2bd628e commit 6b7b8f2
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/libsyntax/ext/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,36 @@ pub fn std_macros() -> @str {
)
)

// FIXME(#6266): change the /* to /** when attributes are supported on macros
// (Though even then—is it going to work according to the clear intent here?)
/*
A utility macro for indicating unreachable code. It will fail if
executed. This is occasionally useful to put after loops that never
terminate normally, but instead directly return from a function.
# Example
~~~ {.rust}
fn choose_weighted_item(v: &[Item]) -> Item {
assert!(!v.is_empty());
let mut so_far = 0u;
for v.each |item| {
so_far += item.weight;
if so_far > 100 {
return item;
}
}
// The above loop always returns, so we must hint to the
// type checker that it isn't possible to get down here
unreachable!();
}
~~~
*/
macro_rules! unreachable (() => (
fail!(\"internal error: entered unreachable code\");
))

macro_rules! condition (

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

5 comments on commit 6b7b8f2

@bors
Copy link
Contributor

@bors bors commented on 6b7b8f2 Sep 5, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from brson
at chris-morgan@6b7b8f2

@bors
Copy link
Contributor

@bors bors commented on 6b7b8f2 Sep 5, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging chris-morgan/rust/unreachable-macro = 6b7b8f2 into auto

@bors
Copy link
Contributor

@bors bors commented on 6b7b8f2 Sep 5, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

chris-morgan/rust/unreachable-macro = 6b7b8f2 merged ok, testing candidate = d1dde99

@bors
Copy link
Contributor

@bors bors commented on 6b7b8f2 Sep 5, 2013

@bors
Copy link
Contributor

@bors bors commented on 6b7b8f2 Sep 5, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = d1dde99

Please sign in to comment.