Skip to content

Commit ebc1d3e

Browse files
committed
Rename last_total to last_unsafe
See Issue 1943 for any discussion (reopen it if necessary). Closes #1943
1 parent c9cf73f commit ebc1d3e

File tree

7 files changed

+21
-11
lines changed

7 files changed

+21
-11
lines changed

src/libcore/path.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ fn splitext(p: path) -> (str, str) {
169169
let parts = str::split_char(p, '.');
170170
if vec::len(parts) > 1u {
171171
let base = str::connect(vec::init(parts), ".");
172-
let ext = "." + vec::last_total(parts);
172+
// We just checked that parts is non-empty, so this is safe
173+
let ext = "." + vec::last_unsafe(parts);
173174

174175
fn is_dotfile(base: str) -> bool {
175176
str::is_empty(base)

src/libcore/vec.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -200,14 +200,15 @@ pure fn last<T: copy>(v: [const T]) -> option<T> {
200200
}
201201

202202
/*
203-
Function: last_total
203+
Function: last_unsafe
204204
205-
Returns the last element of a non-empty vector `v`
205+
Returns the last element of a `v`, failing if the vector is empty.
206206
207-
Predicates:
208-
<is_not_empty> (v)
209207
*/
210-
pure fn last_total<T: copy>(v: [const T]) -> T { v[len(v) - 1u] }
208+
pure fn last_unsafe<T: copy>(v: [const T]) -> T {
209+
if len(v) == 0u { fail "last_unsafe: empty vector" }
210+
v[len(v) - 1u]
211+
}
211212

212213
/*
213214
Function: slice

src/libstd/fs.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,8 @@ fn splitext(p: path) -> (str, str) {
270270
let parts = str::split_char(p, '.');
271271
if vec::len(parts) > 1u {
272272
let base = str::connect(vec::init(parts), ".");
273-
let ext = "." + vec::last_total(parts);
273+
// We just checked that parts is non-empty
274+
let ext = "." + vec::last_unsafe(parts);
274275

275276
fn is_dotfile(base: str) -> bool {
276277
str::is_empty(base)

src/rustc/middle/ast_map.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,9 @@ fn map_view_item(vi: @view_item, cx: ctx, _v: vt) {
187187
let (id, name) = alt vp.node {
188188
view_path_simple(nm, _, id) { (id, nm) }
189189
view_path_glob(pth, id) | view_path_list(pth, _, id) {
190-
(id, vec::last_total(*pth))
190+
// should be a constraint on the type
191+
assert (vec::is_not_empty(*pth));
192+
(id, vec::last_unsafe(*pth))
191193
}
192194
};
193195
cx.map.insert(id, node_export(vp, extend(cx, name)));

src/rustc/middle/pat_util.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,7 @@ fn pat_binding_ids(dm: resolve::def_map, pat: @pat) -> [node_id] {
6868
ret found;
6969
}
7070

71-
fn path_to_ident(p: @path) -> ident { vec::last_total(p.node.idents) }
71+
fn path_to_ident(p: @path) -> ident {
72+
assert (vec::is_not_empty(p.node.idents)); // should be a constraint on path
73+
vec::last_unsafe(p.node.idents)
74+
}

src/rustdoc/reexport_pass.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,9 @@ fn build_reexport_path_map(srv: astsrv::srv, -def_map: def_map) -> path_map {
161161
let path = alt check ctxt.ast_map.get(exp_id) {
162162
ast_map::node_export(_, path) { path }
163163
};
164-
let name = alt check vec::last_total(*path) {
164+
// should be a constraint on the node_export constructor
165+
// that guarantees path is non-empty
166+
let name = alt check vec::last_unsafe(*path) {
165167
ast_map::path_name(nm) { nm }
166168
};
167169
let modpath = ast_map::path_to_str(vec::init(*path));

src/test/run-pass/zip-same-length.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ fn main() {
1919

2020
check (is_not_empty(ps));
2121
assert (head(ps) == ('a', 1u));
22-
assert (last_total(ps) == (j as char, 10u));
22+
assert (last_unsafe(ps) == (j as char, 10u));
2323
}

0 commit comments

Comments
 (0)