diff --git a/src/libcore/path.rs b/src/libcore/path.rs index 1cfafd25740..5658e57f68a 100644 --- a/src/libcore/path.rs +++ b/src/libcore/path.rs @@ -170,7 +170,7 @@ fn splitext(p: path) -> (str, str) { if vec::len(parts) > 1u { let base = str::connect(vec::init(parts), "."); // We just checked that parts is non-empty, so this is safe - let ext = "." + vec::last_unsafe(parts); + let ext = "." + vec::last(parts); fn is_dotfile(base: str) -> bool { str::is_empty(base) diff --git a/src/libcore/vec.rs b/src/libcore/vec.rs index 3737f9bac1d..f1aac380886 100644 --- a/src/libcore/vec.rs +++ b/src/libcore/vec.rs @@ -187,29 +187,26 @@ fn init(v: [const T]) -> [T] { /* Function: last -Returns the last element of a vector - -Returns: - -An option containing the last element of `v` if `v` is not empty, or -none if `v` is empty. -*/ -pure fn last(v: [const T]) -> option { - if len(v) == 0u { ret none; } - ret some(v[len(v) - 1u]); -} - -/* -Function: last_unsafe - Returns the last element of a `v`, failing if the vector is empty. */ -pure fn last_unsafe(v: [const T]) -> T { +pure fn last(v: [const T]) -> T { if len(v) == 0u { fail "last_unsafe: empty vector" } v[len(v) - 1u] } +/* +Function: last_opt + +Returns some(x) where `x` is the last element of a vector `v`, +or none if the vector is empty. + +*/ +pure fn last_opt(v: [const T]) -> option { + if len(v) == 0u { ret none; } + some(v[len(v) - 1u]) +} + /* Function: slice @@ -1270,11 +1267,11 @@ mod tests { #[test] fn test_last() { - let n = last([]); + let n = last_opt([]); assert (n == none); - n = last([1, 2, 3]); + n = last_opt([1, 2, 3]); assert (n == some(3)); - n = last([1, 2, 3, 4, 5]); + n = last_opt([1, 2, 3, 4, 5]); assert (n == some(5)); } diff --git a/src/libstd/fs.rs b/src/libstd/fs.rs index 6627b0fec21..8a5b88f7480 100644 --- a/src/libstd/fs.rs +++ b/src/libstd/fs.rs @@ -271,7 +271,7 @@ fn splitext(p: path) -> (str, str) { if vec::len(parts) > 1u { let base = str::connect(vec::init(parts), "."); // We just checked that parts is non-empty - let ext = "." + vec::last_unsafe(parts); + let ext = "." + vec::last(parts); fn is_dotfile(base: str) -> bool { str::is_empty(base) diff --git a/src/rustc/front/attr.rs b/src/rustc/front/attr.rs index 834d0957ca5..fc9c50a1e00 100644 --- a/src/rustc/front/attr.rs +++ b/src/rustc/front/attr.rs @@ -256,7 +256,7 @@ fn meta_item_from_list( name: str ) -> option<@ast::meta_item> { let items = attr::find_meta_items_by_name(items, name); - vec::last(items) + vec::last_opt(items) } fn meta_item_value_from_list( diff --git a/src/rustc/metadata/creader.rs b/src/rustc/metadata/creader.rs index cfa16a38b4f..986c42ca7c2 100644 --- a/src/rustc/metadata/creader.rs +++ b/src/rustc/metadata/creader.rs @@ -144,7 +144,7 @@ fn find_library_crate(sess: session::session, ident: ast::ident, let crate_name = { let name_items = attr::find_meta_items_by_name(metas, "name"); - alt vec::last(name_items) { + alt vec::last_opt(name_items) { some(i) { alt attr::get_meta_item_value_str(i) { some(n) { n } diff --git a/src/rustc/middle/ast_map.rs b/src/rustc/middle/ast_map.rs index 72a89feedd9..e64ee524b4d 100644 --- a/src/rustc/middle/ast_map.rs +++ b/src/rustc/middle/ast_map.rs @@ -189,7 +189,7 @@ fn map_view_item(vi: @view_item, cx: ctx, _v: vt) { view_path_glob(pth, id) | view_path_list(pth, _, id) { // should be a constraint on the type assert (vec::is_not_empty(*pth)); - (id, vec::last_unsafe(*pth)) + (id, vec::last(*pth)) } }; cx.map.insert(id, node_export(vp, extend(cx, name))); diff --git a/src/rustc/middle/pat_util.rs b/src/rustc/middle/pat_util.rs index bc98c621a34..4327acdb058 100644 --- a/src/rustc/middle/pat_util.rs +++ b/src/rustc/middle/pat_util.rs @@ -70,5 +70,5 @@ fn pat_binding_ids(dm: resolve::def_map, pat: @pat) -> [node_id] { fn path_to_ident(p: @path) -> ident { assert (vec::is_not_empty(p.node.idents)); // should be a constraint on path - vec::last_unsafe(p.node.idents) + vec::last(p.node.idents) } diff --git a/src/rustc/middle/tstate/bitvectors.rs b/src/rustc/middle/tstate/bitvectors.rs index 7433da333ac..a64b9491824 100644 --- a/src/rustc/middle/tstate/bitvectors.rs +++ b/src/rustc/middle/tstate/bitvectors.rs @@ -183,7 +183,7 @@ fn kill_poststate(fcx: fn_ctxt, id: node_id, c: tsconstr) -> bool { fn clear_in_poststate_expr(fcx: fn_ctxt, e: @expr, t: poststate) { alt e.node { expr_path(p) { - alt vec::last(p.node.idents) { + alt vec::last_opt(p.node.idents) { some(i) { alt local_node_id_to_def(fcx, e.id) { some(def_local(nid, _)) { diff --git a/src/rustc/middle/tstate/states.rs b/src/rustc/middle/tstate/states.rs index c7eebf317af..1c997ca84e8 100644 --- a/src/rustc/middle/tstate/states.rs +++ b/src/rustc/middle/tstate/states.rs @@ -406,7 +406,7 @@ fn find_pre_post_state_expr(fcx: fn_ctxt, pres: prestate, e: @expr) -> bool { init_assign), exs, return_val); - let base_pres = alt vec::last(exs) { none { pres } + let base_pres = alt vec::last_opt(exs) { none { pres } some(f) { expr_poststate(fcx.ccx, f) }}; option::may(maybe_base, {|base| changed |= find_pre_post_state_expr(fcx, base_pres, base) | diff --git a/src/rustc/middle/typeck.rs b/src/rustc/middle/typeck.rs index 85dbedaefcc..d690bcb4d9d 100644 --- a/src/rustc/middle/typeck.rs +++ b/src/rustc/middle/typeck.rs @@ -2642,7 +2642,7 @@ fn bind_params(fcx: @fn_ctxt, tp: ty::t, count: uint) } fn get_self_info(ccx: @crate_ctxt) -> option { - ret vec::last(ccx.self_infos); + ret vec::last_opt(ccx.self_infos); } fn check_decl_initializer(fcx: @fn_ctxt, nid: ast::node_id, diff --git a/src/rustc/util/common.rs b/src/rustc/util/common.rs index 87a1f6145d6..cd45deb9423 100644 --- a/src/rustc/util/common.rs +++ b/src/rustc/util/common.rs @@ -89,7 +89,9 @@ fn local_rhs_span(l: @ast::local, def: span) -> span { } fn is_main_name(path: middle::ast_map::path) -> bool { - option::get(vec::last(path)) == middle::ast_map::path_name("main") + // FIXME: path should be a constrained type, so we know + // the call to last doesn't fail + vec::last(path) == middle::ast_map::path_name("main") } // diff --git a/src/rustdoc/reexport_pass.rs b/src/rustdoc/reexport_pass.rs index 02ffa3337b4..978c6fc9014 100644 --- a/src/rustdoc/reexport_pass.rs +++ b/src/rustdoc/reexport_pass.rs @@ -163,7 +163,7 @@ fn build_reexport_path_map(srv: astsrv::srv, -def_map: def_map) -> path_map { }; // should be a constraint on the node_export constructor // that guarantees path is non-empty - let name = alt check vec::last_unsafe(*path) { + let name = alt check vec::last(*path) { ast_map::path_name(nm) { nm } }; let modpath = ast_map::path_to_str(vec::init(*path)); diff --git a/src/test/run-pass/zip-same-length.rs b/src/test/run-pass/zip-same-length.rs index 787fd3e1c1e..813548dc35e 100644 --- a/src/test/run-pass/zip-same-length.rs +++ b/src/test/run-pass/zip-same-length.rs @@ -19,5 +19,5 @@ fn main() { check (is_not_empty(ps)); assert (head(ps) == ('a', 1u)); - assert (last_unsafe(ps) == (j as char, 10u)); + assert (last(ps) == (j as char, 10u)); }