Skip to content

Commit 7abf557

Browse files
committed
core: Move 'unreachable' to util. Improve docs
1 parent 77b0845 commit 7abf557

File tree

5 files changed

+32
-14
lines changed

5 files changed

+32
-14
lines changed

src/libcore/core.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use ptr::Ptr;
2222
use to_str::ToStr;
2323

2424
export Path, WindowsPath, PosixPath, GenericPath;
25-
export Option, Some, None, unreachable;
25+
export Option, Some, None;
2626
export Result, Ok, Err;
2727
export extensions;
2828
// The following exports are the extension impls for numeric types
@@ -96,13 +96,3 @@ mod std {
9696
extern mod std(vers = "0.4");
9797
use std::test;
9898
}
99-
100-
/**
101-
* A standard function to use to indicate unreachable code. Because the
102-
* function is guaranteed to fail typestate will correctly identify
103-
* any code paths following the appearance of this function as unreachable.
104-
*/
105-
fn unreachable() -> ! {
106-
fail ~"Internal error: entered unreachable code";
107-
}
108-

src/libcore/rand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ impl Rng {
206206
return Some(item.item);
207207
}
208208
}
209-
unreachable();
209+
util::unreachable();
210210
}
211211

212212
/**

src/libcore/util.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,34 @@ struct NonCopyable {
6464

6565
fn NonCopyable() -> NonCopyable { NonCopyable { i: () } }
6666

67+
/**
68+
A utility function for indicating unreachable code. It will fail if
69+
executed. This is occasionally useful to put after loops that never
70+
terminate normally, but instead directly return from a function.
71+
72+
# Example
73+
74+
~~~
75+
fn choose_weighted_item(v: &[Item]) -> Item {
76+
assert v.is_not_empty();
77+
let mut so_far = 0u;
78+
for v.each |item| {
79+
so_far += item.weight;
80+
if so_far > 100 {
81+
return item;
82+
}
83+
}
84+
// The above loop always returns, so we must hint to the
85+
// type checker that it isn't possible to get down here
86+
util::unreachable();
87+
}
88+
~~~
89+
90+
*/
91+
fn unreachable() -> ! {
92+
fail ~"internal error: entered unreachable code";
93+
}
94+
6795
mod tests {
6896
#[test]
6997
fn identity_crisis() {

src/rustc/back/rpath.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ fn get_rpath_relative_to_output(os: session::os,
106106
let prefix = match os {
107107
session::os_linux | session::os_freebsd => "$ORIGIN",
108108
session::os_macos => "@executable_path",
109-
session::os_win32 => core::unreachable()
109+
session::os_win32 => core::util::unreachable()
110110
};
111111

112112
Path(prefix).push_rel(&get_relative_to(&os::make_absolute(output),

src/rustc/middle/trans/alt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ fn variant_opt(tcx: ty::ctxt, pat_id: ast::node_id) -> Opt {
170170
for vec::each(*variants) |v| {
171171
if vdef.var == v.id { return var(v.disr_val, vdef); }
172172
}
173-
core::unreachable();
173+
core::util::unreachable();
174174
}
175175

176176
enum TransBindingMode {

0 commit comments

Comments
 (0)