diff --git a/src/libcore/core.rs b/src/libcore/core.rs index 2feb75a71fa355453f7ce046124c7a089739499b..a7c3c2555ac78ca8c0181f2e5bcfccfb04050223 100644 --- a/src/libcore/core.rs +++ b/src/libcore/core.rs @@ -22,7 +22,7 @@ use to_str::ToStr; export Path, WindowsPath, PosixPath, GenericPath; -export Option, Some, None, unreachable; +export Option, Some, None; export Result, Ok, Err; export extensions; // The following exports are the extension impls for numeric types @@ -96,13 +96,3 @@ mod std { extern mod std(vers = "0.4"); use std::test; } - -/** - * A standard function to use to indicate unreachable code. Because the - * function is guaranteed to fail typestate will correctly identify - * any code paths following the appearance of this function as unreachable. - */ -fn unreachable() -> ! { - fail ~"Internal error: entered unreachable code"; -} - diff --git a/src/libcore/rand.rs b/src/libcore/rand.rs index 5aaf70d84c6cc390702d165ffc2227fae2776933..52b5bdadc350d13c3ff1593fc7ecbc33b81ea481 100644 --- a/src/libcore/rand.rs +++ b/src/libcore/rand.rs @@ -206,7 +206,7 @@ fn choose_weighted_option(v: &[Weighted]) -> Option { return Some(item.item); } } - unreachable(); + util::unreachable(); } /** diff --git a/src/libcore/util.rs b/src/libcore/util.rs index 679ed13d1db75cc589a9e8d9c7628817b5c79800..e9ca27407ea1e39c89258cf33d1d6cfa0325f5ad 100644 --- a/src/libcore/util.rs +++ b/src/libcore/util.rs @@ -64,6 +64,34 @@ struct NonCopyable { fn NonCopyable() -> NonCopyable { NonCopyable { i: () } } +/** +A utility function 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 + +~~~ +fn choose_weighted_item(v: &[Item]) -> Item { + assert v.is_not_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 + util::unreachable(); +} +~~~ + +*/ +fn unreachable() -> ! { + fail ~"internal error: entered unreachable code"; +} + mod tests { #[test] fn identity_crisis() { diff --git a/src/rustc/back/rpath.rs b/src/rustc/back/rpath.rs index 5af2987cb69982840b29eb3807ec191314ebc03a..f115827b13634ff9b3785bcbe4e39ccb62351997 100644 --- a/src/rustc/back/rpath.rs +++ b/src/rustc/back/rpath.rs @@ -106,7 +106,7 @@ fn get_rpath_relative_to_output(os: session::os, let prefix = match os { session::os_linux | session::os_freebsd => "$ORIGIN", session::os_macos => "@executable_path", - session::os_win32 => core::unreachable() + session::os_win32 => core::util::unreachable() }; Path(prefix).push_rel(&get_relative_to(&os::make_absolute(output), diff --git a/src/rustc/middle/trans/alt.rs b/src/rustc/middle/trans/alt.rs index dc667d3ae50ca32a91bae4012634f92981385fa9..376e70fc6c5e85f60a78d9ee527987fd3de17d97 100644 --- a/src/rustc/middle/trans/alt.rs +++ b/src/rustc/middle/trans/alt.rs @@ -170,7 +170,7 @@ fn variant_opt(tcx: ty::ctxt, pat_id: ast::node_id) -> Opt { for vec::each(*variants) |v| { if vdef.var == v.id { return var(v.disr_val, vdef); } } - core::unreachable(); + core::util::unreachable(); } enum TransBindingMode {