diff options
| author | bors <bors@rust-lang.org> | 2013-09-05 13:50:46 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2013-09-05 13:50:46 -0700 |
| commit | d1dde99e4b3d43d044cdead7240bbd5f0d7a0ce5 (patch) | |
| tree | d9d1e76108ade6e6ea323f3bd54dbaff36e1c833 /src | |
| parent | d84a7b5ae3b3a820fb0a26292632856ceb959b3e (diff) | |
| parent | 6b7b8f2682780306860a38cdc7138f603e859fde (diff) | |
| download | rust-d1dde99e4b3d43d044cdead7240bbd5f0d7a0ce5.tar.gz rust-d1dde99e4b3d43d044cdead7240bbd5f0d7a0ce5.zip | |
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.
Diffstat (limited to 'src')
| -rw-r--r-- | src/libsyntax/ext/expand.rs | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 7e48fe4d419..ea277a8a625 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -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; } => { |
