From 0c5fdc8745cd0bc5fbf9272301d3aafa2eb8f331 Mon Sep 17 00:00:00 2001 From: Tim Chevalier Date: Thu, 8 Mar 2012 15:24:27 -0800 Subject: [PATCH] Rename last to last_opt, last_unsafe to last As per discussion on IRC. I am about to file an RFC for further discussion about the more general issue of whether to enforce invariants through types, typestate, or dynamic checks, but for now, removing the misleading name "last_unsafe". --- src/libcore/path.rs | 2 +- src/libcore/vec.rs | 35 ++++++++++++--------------- src/libstd/fs.rs | 2 +- src/rustc/front/attr.rs | 2 +- src/rustc/metadata/creader.rs | 2 +- src/rustc/middle/ast_map.rs | 2 +- src/rustc/middle/pat_util.rs | 2 +- src/rustc/middle/tstate/bitvectors.rs | 2 +- src/rustc/middle/tstate/states.rs | 2 +- src/rustc/middle/typeck.rs | 2 +- src/rustc/util/common.rs | 4 ++- src/rustdoc/reexport_pass.rs | 2 +- src/test/run-pass/zip-same-length.rs | 2 +- 13 files changed, 30 insertions(+), 31 deletions(-) diff --git a/src/libcore/path.rs b/src/libcore/path.rs index 1cfafd257405..5658e57f68a4 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 3737f9bac1d7..f1aac380886f 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 6627b0fec212..8a5b88f7480f 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 834d0957ca50..fc9c50a1e00f 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 cfa16a38b4fa..986c42ca7c2e 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 72a89feedd92..e64ee524b4de 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 bc98c621a345..4327acdb0580 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 7433da333ac6..a64b94918245 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 c7eebf317af8..1c997ca84e82 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 85dbedaefcc0..d690bcb4d9d5 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 87a1f6145d6c..cd45deb9423b 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 02ffa3337b48..978c6fc90148 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 787fd3e1c1ef..813548dc35ee 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)); }